Example #1
0
 public void AnotherMultilineComment()
 {
     var token = new Lexer(@"#!
                                 This is a comment
                                 in multiple lines
                             !#").NextToken();
     Assert.True(token.Type == TokenType.Eof);
 }
Example #2
0
 public void EmptySource()
 {
     var token = new Lexer("  ").NextToken();
     Assert.True(token.Type == TokenType.Eof);
 }
Example #3
0
 public void Comment()
 {
     var token = new Lexer("# This is a comment").NextToken();
     Assert.True(token.Type == TokenType.Eof);
 }
Example #4
0
 private static void Match(string expected, string source, bool removeSpaces = true)
 {
     var actual = new StringBuilder();
     var lexer = new Lexer(source);
     var token = lexer.NextToken();
     while (token.Type != TokenType.Eof)
     {
         switch (token.Type)
         {
             case TokenType.Number:
                 actual.Append("n$").Append(token.Text);
                 break;
             case TokenType.String:
                 actual.Append("'").Append(token.Text).Append("'");
                 break;
             case TokenType.Identifier:
                 actual.Append("i$").Append(token.Text);
                 break;
             case TokenType.Dot:
                 actual.Append("d$").Append(token.Text);
                 break;
             default:
                 if (token.Type.IsKeyword())
                     actual.Append("k$").Append(token.Text);
                 else
                     actual.Append(token.Text);
                 break;
         }
         token = lexer.NextToken();
     }
     actual.Append(token.Text);
     if (removeSpaces)
         expected = expected.Replace(Environment.NewLine, string.Empty).Replace(" ", string.Empty);
     Assert.AreEqual(expected, actual.ToString());
 }
Example #5
0
        public void StringLiterals()
        {
            string source = "'this is a string'";
            var token = new Lexer(source).NextToken();
            Assert.AreEqual(TokenType.String, token.Type);
            Assert.AreEqual("this is a string", token.Text);

            source = "\"this is a string\"";
            token = new Lexer(source).NextToken();
            Assert.AreEqual(TokenType.String, token.Type);
            Assert.AreEqual("this is a string", token.Text);

            source = "\"this \n is \t a \r string \'\"";
            token = new Lexer(source).NextToken();
            Assert.AreEqual(TokenType.String, token.Type);
            Assert.AreEqual("this \n is \t a \r string \'", token.Text);

            source = "'this is a string"; // unfinished quote
            try
            {
                new Lexer(source).NextToken();
                Assert.Fail();
            }
            catch (ParseException){}
        }
Example #6
0
        internal object Execute(string source, string filePath = null)
        {
            var oldCurentExecFolder = Context.CurrentExecFolder;
            var currentExecFolder = filePath == null
                ? Directory.GetCurrentDirectory()
                : Path.GetDirectoryName(filePath);
            Context.CurrentExecFolder = currentExecFolder;
            if (currentExecFolder != null)
                Directory.SetCurrentDirectory(currentExecFolder);

            try
            {
                var lexer = new Lexer(source, filePath);
                var parser = new Parser(lexer);
                var ast = parser.Parse();
                return ast.Accept(this);
            }
            finally
            {
                Context.CurrentExecFolder = oldCurentExecFolder;
                if (oldCurentExecFolder != null)
                    Directory.SetCurrentDirectory(oldCurentExecFolder);
            }
        }
Example #7
0
 public Parser(Lexer lexer)
 {
     myLexer = lexer;
 }