Module: ApiQueryLanguage::ExpressionParserHelper

Defined in:
lib/api_query_language/expression_parser_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_stringObject



14
15
16
17
# File 'lib/api_query_language/expression_parser_helper.rb', line 14

def current_string
  return unless lexer.ss
  lexer.ss.string[lexer.ss.pos..]
end

#lexerObject

Raises:

  • (NoMethodError)


5
6
7
# File 'lib/api_query_language/expression_parser_helper.rb', line 5

def lexer
  raise NoMethodError, "You must implement a lexer method that returns an instance of ExpressionLexer"
end

#next_tokenObject

Delegate to lexer for tokens



10
11
12
# File 'lib/api_query_language/expression_parser_helper.rb', line 10

def next_token
  lexer.next_token
end

#parse!(str) ⇒ Object



31
32
33
34
35
36
# File 'lib/api_query_language/expression_parser_helper.rb', line 31

def parse!(str)
  validate_expression!(str)
  lexer.parse(str, self) # This then calls the do_parse method on the parser
rescue Racc::ParseError, Filtering::ExpressionLexer::LexerError, Sorting::ExpressionLexer::LexerError
  raise Errors::InvalidExpressionError.new(str, parse_error_reason)
end

#parse_error_contextObject



19
20
21
# File 'lib/api_query_language/expression_parser_helper.rb', line 19

def parse_error_context
  " at position #{lexer.ss.pos + 1}"
end

#parse_error_reasonObject



23
24
25
26
27
28
29
# File 'lib/api_query_language/expression_parser_helper.rb', line 23

def parse_error_reason
  if current_string
    " the following cannot be parsed '#{current_string}'.#{parse_error_context}"
  else
    " it is not a valid query expression."
  end
end

#validate_expression!(str) ⇒ Object



38
39
40
41
42
# File 'lib/api_query_language/expression_parser_helper.rb', line 38

def validate_expression!(str)
  raise Errors::InvalidExpressionError.new(str, "it is not a string") unless str.is_a?(String)
  raise Errors::InvalidExpressionError.new(str, "it is blank") if str.nil? || str.empty?
  raise Errors::InvalidExpressionError.new(str, "it is too long") if str.length > 1000
end