Class: OpenEHR::AQL::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/openehr/aql/parser.rb

Overview

Recursive-descent parser over the Lexer's token stream. Method names mirror the official AqlParser.g4 rule names (see the rule cited in each method's comment) so the implementation stays traceable against the reference grammar. Each project-plan milestone implements a subset of these rules; a construct a milestone hasn't reached yet is simply never consumed, so parsing it falls through to the nearest "expected X, got Y" ParseError instead of being silently misparsed.

Constant Summary collapse

PRIMITIVE_TOKEN_TYPES =
%i[string integer real sci_integer sci_real boolean null].freeze
DESCENDING_DIRECTIONS =
%i[desc descending].freeze
AGGREGATE_FUNCTION_TYPES =
%i[count min max sum avg].freeze
FUNCTION_NAME_TYPES =
%i[length position substring concat concat_ws abs mod ceil floor round
now current_date current_time current_date_time current_timezone].freeze

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
# File 'lib/openehr/aql/parser.rb', line 14

def initialize(tokens)
  @tokens = tokens
  @pos = 0
end

Instance Method Details

#parse_select_queryObject

selectQuery : selectClause fromClause whereClause? orderByClause? limitClause? SYM_DOUBLE_DASH? EOF ; The trailing SYM_DOUBLE_DASH needs no explicit handling here: the Lexer's COMMENT rule already treats a "--" immediately followed by EOF (or a space, or a newline) as an empty/line comment and skips it, so a trailing "--" is silently absorbed before the parser ever sees a token for it.



25
26
27
28
29
30
31
32
33
34
# File 'lib/openehr/aql/parser.rb', line 25

def parse_select_query
  select_clause = parse_select_clause
  from_clause = parse_from_clause
  where_clause = check(:where) ? parse_where_clause : nil
  order_by_clause = check(:order) ? parse_order_by_clause : nil
  limit_clause = check(:limit) ? parse_limit_clause : nil
  expect(:eof)
  Model::Query.new(select_clause: select_clause, from_clause: from_clause, where_clause: where_clause,
                    order_by_clause: order_by_clause, limit_clause: limit_clause)
end