Class: Fast::ExpressionParser
- Inherits:
-
Object
- Object
- Fast::ExpressionParser
- Defined in:
- lib/fast.rb
Overview
ExpressionParser empowers the AST search in Ruby. All classes inheriting Fast::Find have a grammar shortcut that is processed here.
Instance Method Summary collapse
- #error_message ⇒ Object
-
#initialize(expression) ⇒ ExpressionParser
constructor
A new instance of ExpressionParser.
-
#parse ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength.
- #tokens_left? ⇒ Boolean
Constructor Details
#initialize(expression) ⇒ ExpressionParser
Returns a new instance of ExpressionParser.
471 472 473 474 |
# File 'lib/fast.rb', line 471 def initialize(expression) @tokens = expression.scan TOKENIZER @nesting = [] end |
Instance Method Details
#error_message ⇒ Object
512 513 514 515 516 517 518 |
# File 'lib/fast.rb', line 512 def if @tokens.any? "Unconsumed tokens after parsing: #{@tokens.join(' ')}" elsif @nesting.any? "Unclosed nesting: expected #{@nesting.reverse.join(', ')}" end end |
#parse ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
# File 'lib/fast.rb', line 479 def parse case (token = next_token) when '(' @nesting << ')' parse_until_peek(')') when '{' @nesting << '}' Any.new(parse_until_peek('}')) when '[' @nesting << ']' All.new(parse_until_peek(']')) when ')', '}', ']' raise SyntaxError, "Unexpected token: #{token}" when /^"/ then FindString.new(token[1..-2]) when /^#\w/ then MethodCall.new(token[1..]) when /^\.\w[\w\d_]+\?/ then InstanceMethodCall.new(token[1..]) when '$' then Capture.new(parse) when '!' then (@tokens.any? && !closing_token?(@tokens.first) ? Not.new(parse) : Find.new(token)) when '?' then Maybe.new(parse) when '^' then Parent.new(parse) when '\\' then FindWithCapture.new(parse) when /^%\d/ then FindFromArgument.new(token[1..]) when ';' raise SyntaxError, "Semicolons are not allowed in patterns: #{token}" when nil then nil else Find.new(token) end end |
#tokens_left? ⇒ Boolean
508 509 510 |
# File 'lib/fast.rb', line 508 def tokens_left? @tokens.any? || @nesting.any? end |