### Home Classes Methods

### In Files

- lib/expression_parser/parser.rb

### Parent

Object

### Methods

- #compare_expr
- #expression
- #factor
- #number
- #parse

### Class/Module Index

Quicksearch

- ExpressionParser
- ExpressionParser::Lexer
- ExpressionParser::Parser
- ExpressionParser::Token

No matching classes.

# ExpressionParser::Parser

### Public Instance Methods

parse(input) click to toggle source

    # File lib/expression_parser/parser.rb, line 5
    def parse(input)
      @lexer = Lexer.new(input)

      expression_value = expression
      token = @lexer.get_next_token
      if token.kind == Token::End
        expression_value
      else
        compare_expr(token,expression_value,expression)
      end
    end

### Protected Instance Methods

compare_expr(token,expression_value,expression) click to toggle source

    # File lib/expression_parser/parser.rb, line 18
    def compare_expr(token,expression_value,expression)
      case token.kind
      when Token::GThan
        expression_value > expression ? 1 : 0
      when Token::LThan
        expression_value < expression ? 1 : 0
      when Token::Equal
        expression_value == expression ? 1 : 0
      when Token::NotEqual
        expression_value != expression ? 1 : 0
      when Token::GThanE
        expression_value >= expression ? 1 : 0
      when Token::LThanE
        expression_value <= expression ? 1 : 0
      else
        raise 'End expected'
      end
    end

expression() click to toggle source

    # File lib/expression_parser/parser.rb, line 37
    def expression
      component1 = factor

      additive_operators = [Token::Plus, Token::Minus]

      token = @lexer.get_next_token
      while additive_operators.include?(token.kind)
        component2 = factor

        if token.kind == Token::Plus
          component1 += component2
        else
          component1 -= component2
        end

        token = @lexer.get_next_token
      end
      @lexer.revert

      component1
    end

factor() click to toggle source

    # File lib/expression_parser/parser.rb, line 59
    def factor
      factor1 = number

      multiplicative_operators = [Token::Multiply, Token::Divide, Token::MOD]

      token = @lexer.get_next_token
      while multiplicative_operators.include?(token.kind)
        factor2 = number

        if token.kind == Token::Multiply
          factor1 *= factor2
        elsif token.kind == Token::MOD
          factor1 = factor2
        else
          factor1 /= factor2
        end

        token = @lexer.get_next_token
      end
      @lexer.revert

      factor1
    end

number() click to toggle source

    # File lib/expression_parser/parser.rb, line 83
    def number
      token = @lexer.get_next_token

      if token.kind == Token::LParen
        value = expression

        expected_rparen = @lexer.get_next_token
        if [Token::GThan,Token::LThan,Token::Equal,Token::NotEqual,Token::GThanE,Token::LThanE].include?(expected_rparen.kind)
          tmp = expression
          value = compare_expr(expected_rparen,value,tmp)
          expected_rparen = @lexer.get_next_token
        end

        expected_rparen
        raise "Unbalanced parenthesis" unless expected_rparen.kind == Token::RParen
      elsif token.kind == Token::Number
        value = token.value
      else
        raise 'Not a number'
      end

      value
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.