Reads characters from Tokenizer and returns instances of ExpressionEngine.Internal.Token derived types.
Inheritance: IDisposable
Exemple #1
0
        public void Should_lex_expression_with_nested_parentheses()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("((10 + 1 - (3 - .234)) * 300)  /  10.1"));

            // Then
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            ((LiteralToken) scanner.NextToken()).Value.Should().Be(10D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1D);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(3D);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.234D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.Multiply);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(300D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.Divide);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10.1D);
            scanner.NextToken().Should().BeNull();
        }
Exemple #2
0
 /// <summary>
 /// Convenience method for building an AST from a string, used internally.
 /// </summary>
 public static SyntaxTree ParseString(string value)
 {
     using (var scanner = new Lexer(Tokenizer.OfString(value)))
     {
         return new Parser(scanner).Parse();
     }
 }
Exemple #3
0
        public void Should_not_move_forward_when_using_peek()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("1 + 3"));

            // Then
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(1D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1D);

            // Then
            scanner.PeekToken().Type.Should().Be(TokenType.Plus);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);

            // Then
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(3D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(3D);

            // Then
            scanner.PeekToken().Should().BeNull();
            scanner.NextToken().Should().BeNull();
        }
Exemple #4
0
        public void Should_lex_number_with_lower_case_exponent_and_minus_sign()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("10E-11 + 10e-11"));

            // Than
            ((LiteralToken) scanner.NextToken()).Value.Should().Be(0.0000000001D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.0000000001D);
        }
Exemple #5
0
        public void Should_lex_number_with_lower_case_exponent()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("10e2 + 4"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1000D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(4D);
        }
Exemple #6
0
        public void Should_lex_expression_formed_by_a_single_positive_number_with_plus_sign_explicit()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("+123"));

            // Then
            scanner.PeekToken().Type.Should().Be(TokenType.Plus);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(123D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(123D);

        }
Exemple #7
0
        public void Should_lex_expression_formed_by_negative_and_positive_decimal_number_with_zero_omitted_and_explicit_sign()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(".2 / +9.99 * (-.123 + -2)"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.2D);
            scanner.NextToken().Type.Should().Be(TokenType.Divide);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(9.99D);
            scanner.NextToken().Type.Should().Be(TokenType.Multiply);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.123D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(2D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
        }
Exemple #8
0
        public void Should_lex_number_with_exponent_and_plus_sign()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("1 + 10E+2"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1000D);
        }
Exemple #9
0
        public void Should_lex_string_literal_with_escape_characters()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(@"0 + ""\t \n \\ \r"" + 0"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be("\t \n \\ \r");
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0D);
        }
Exemple #10
0
        public void Should_lex_string_literal_with_double_quote_escape_character()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(@" ""a\""bc"" "));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be("a\"bc");
        }
Exemple #11
0
        public void Should_lex_less_than_or_equal_operator()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(".3 <= .9"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.3D);
            scanner.NextToken().Type.Should().Be(TokenType.LessThanOrEqual);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.9D);
        }
Exemple #12
0
        public void Should_lex_greater_than_operator()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("10 > (10 * 2.2)"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10D);
            scanner.NextToken().Type.Should().Be(TokenType.GreaterThan);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10D);
            scanner.NextToken().Type.Should().Be(TokenType.Multiply);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(2.2D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
        }
Exemple #13
0
        public void Should_lex_equality_operator()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("var1 == 10.3"));

            // Than
            ((IdentifierToken)scanner.NextToken()).Text.Should().Be("var1");
            scanner.NextToken().Type.Should().Be(TokenType.Equality);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10.3D);
        }
Exemple #14
0
        public void Should_lex_inequality_operator()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(".3 != 0.003003"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.3D);
            scanner.NextToken().Type.Should().Be(TokenType.Inequality);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.003003D);
        }
Exemple #15
0
        public void Should_lex_identifier_with_underscore()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("var_name"));

            // Than
            ((IdentifierToken)scanner.NextToken()).Text.Should().Be("var_name");
        }
Exemple #16
0
        public void Should_lex_expression_with_function()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("3 + pow(10, 2)"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(3D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((IdentifierToken)scanner.NextToken()).Text.Should().Be("pow");
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10D);
            scanner.NextToken().Type.Should().Be(TokenType.Comma);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(2D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
        }
Exemple #17
0
        public void Should_lex_expression_with_sum_near_unary_operators()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("-.23 + +.99 * ((-.123 + -2.1)--333)"));

            // Than
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.23D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.99D);
            scanner.NextToken().Type.Should().Be(TokenType.Multiply);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.LeftParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.123D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(2.1D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
            scanner.NextToken().Type.Should().Be(TokenType.Minus); ;
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(333D);
            scanner.NextToken().Type.Should().Be(TokenType.RightParenthesis);
        }
Exemple #18
0
        public void Should_lex_string_literal()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("\"hello, csharp\""));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be("hello, csharp");
        }
Exemple #19
0
        public void Should_lex_expression_with_modulo()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("10 % 2"));
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(10D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(10D);

            // Then
            scanner.PeekToken().Type.Should().Be(TokenType.Modulo);
            scanner.NextToken().Type.Should().Be(TokenType.Modulo);
        }
Exemple #20
0
        public void Should_lex_expression_containing_string_literal()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(".3 + \"hello, fsharp\" + 0.999"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.3D);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be("hello, fsharp");
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.999D);
        }
Exemple #21
0
        public void Should_lex_expression_formed_by_a_single_number()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("123"));

            // Then
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(123D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(123D);
        }
Exemple #22
0
        public void Should_lex_string_literal_with_decimal_escape()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString(@" ""\048\048\055"" "));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be("007");
        }
Exemple #23
0
        public void Should_lex_false_literal()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("false != .01"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(false);
            scanner.NextToken().Type.Should().Be(TokenType.Inequality);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.01D);
        }
Exemple #24
0
        public void Should_lex_greater_than_or_equal_operator()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("0.1 >= +5.9"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(0.1D);
            scanner.NextToken().Type.Should().Be(TokenType.GreaterThanOrEqual);
            scanner.NextToken().Type.Should().Be(TokenType.Plus);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(5.9D);
        }
Exemple #25
0
        public void Should_lex_expression_formed_by_a_single_negative__decimal_number_with_zero_omitted()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("-.123"));

            // Than
            scanner.PeekToken().Type.Should().Be(TokenType.Minus);
            scanner.NextToken().Type.Should().Be(TokenType.Minus);
            ((LiteralToken)scanner.PeekToken()).Value.Should().Be(.123D);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(.123D);
        }
Exemple #26
0
        public void Should_lex_true_literal()
        {
            // Given when
            var scanner = new Lexer(Tokenizer.OfString("true == 1"));

            // Than
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(true);
            scanner.NextToken().Type.Should().Be(TokenType.Equality);
            ((LiteralToken)scanner.NextToken()).Value.Should().Be(1D);
        }
Exemple #27
0
 public Parser(Lexer scanner)
 {
     _scanner = scanner;
 }