class Cucumber::TagExpressions::Parser
Ruby tag expression parser
Public Class Methods
Source
# File lib/cucumber/tag_expressions/parser.rb, line 7 def initialize @expressions = [] @operators = [] @operator_types = { 'or' => { type: :binary_operator, precedence: 0, assoc: :left }, 'and' => { type: :binary_operator, precedence: 1, assoc: :left }, 'not' => { type: :unary_operator, precedence: 2, assoc: :right }, ')' => { type: :close_paren, precedence: -1 }, '(' => { type: :open_paren, precedence: 1 } } end
Public Instance Methods
Source
# File lib/cucumber/tag_expressions/parser.rb, line 20 def parse(infix_expression) process_tokens!(infix_expression) while @operators.any? raise 'Syntax error. Unmatched (' if @operators.last == '(' push_expression(pop(@operators)) end expression = pop(@expressions) @expressions.empty? ? expression : raise('Not empty') end
Private Instance Methods
Source
# File lib/cucumber/tag_expressions/parser.rb, line 35 def assoc_of(token, value) @operator_types[token][:assoc] == value end
Helpers
Source
# File lib/cucumber/tag_expressions/parser.rb, line 124 def check(expected_token_type, token_type) if expected_token_type != token_type raise "Syntax error. Expected #{expected_token_type}" end end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 92 def handle_binary_operator(token, expected_token_type) check(expected_token_type, :operator) while @operators.any? && operator?(@operators.last) && lower_precedence?(token) push_expression(pop(@operators)) end @operators.push(token) :operand end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 108 def handle_close_paren(_token, expected_token_type) check(expected_token_type, :operator) while @operators.any? && @operators.last != '(' push_expression(pop(@operators)) end raise 'Syntax error. Unmatched )' if @operators.empty? pop(@operators) if @operators.last == '(' :operator end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 118 def handle_literal(token, expected_token_type) check(expected_token_type, :operand) push_expression(token) :operator end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 102 def handle_open_paren(token, expected_token_type) check(expected_token_type, :operand) @operators.push(token) :operand end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 86 def handle_unary_operator(token, expected_token_type) check(expected_token_type, :operand) @operators.push(token) :operand end
Handlers
Source
# File lib/cucumber/tag_expressions/parser.rb, line 39 def lower_precedence?(operation) (assoc_of(operation, :left) && precedence(operation) <= precedence(@operators.last)) || (assoc_of(operation, :right) && precedence(operation) < precedence(@operators.last)) end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 46 def operator?(token) @operator_types[token][:type] == :unary_operator || @operator_types[token][:type] == :binary_operator end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 130 def pop(array, n = 1) result = array.pop(n) raise('Empty stack') if result.size != n n == 1 ? result.first : result end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 51 def precedence(token) @operator_types[token][:precedence] end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 59 def process_tokens!(infix_expression) expected_token_type = :operand tokens(infix_expression).each do |token| if @operator_types[token] expected_token_type = send("handle_#{@operator_types[token][:type]}", token, expected_token_type) else expected_token_type = handle_literal(token, expected_token_type) end end end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 70 def push_expression(token) case token when 'and' @expressions.push(And.new(*pop(@expressions, 2))) when 'or' @expressions.push(Or.new(*pop(@expressions, 2))) when 'not' @expressions.push(Not.new(pop(@expressions))) else @expressions.push(Literal.new(token)) end end
Source
# File lib/cucumber/tag_expressions/parser.rb, line 55 def tokens(infix_expression) infix_expression.gsub(/(?<!\\)\(/, ' ( ').gsub(/(?<!\\)\)/, ' ) ').strip.split(/\s+/) end