Class: OpenEHR::AQL::Lexer

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

Constant Summary collapse

KEYWORDS =
{
  'select' => :select, 'as' => :as, 'from' => :from, 'where' => :where,
  'order' => :order, 'by' => :by, 'desc' => :desc, 'descending' => :descending,
  'asc' => :asc, 'ascending' => :ascending, 'limit' => :limit, 'offset' => :offset,
  'distinct' => :distinct, 'version' => :version, 'latest_version' => :latest_version,
  'all_versions' => :all_versions, 'null' => :null,
  'top' => :top, 'forward' => :forward, 'backward' => :backward,
  'contains' => :contains, 'and' => :and, 'or' => :or, 'not' => :not, 'exists' => :exists,
  'like' => :like, 'matches' => :matches,
  'count' => :count, 'min' => :min, 'max' => :max, 'sum' => :sum, 'avg' => :avg,
  'terminology' => :terminology,
  'length' => :length, 'position' => :position, 'substring' => :substring,
  'concat' => :concat, 'concat_ws' => :concat_ws,
  'abs' => :abs, 'mod' => :mod, 'ceil' => :ceil, 'floor' => :floor, 'round' => :round,
  'now' => :now, 'current_date' => :current_date, 'current_time' => :current_time,
  'current_date_time' => :current_date_time, 'current_timezone' => :current_timezone
}.freeze
ARCHETYPE_HRID_RE =
/\A[A-Za-z][A-Za-z0-9_]*-[A-Za-z][A-Za-z0-9_]*-[A-Za-z][A-Za-z0-9_]*
\.[A-Za-z][A-Za-z0-9_-]*
\.v\d+(?:\.\d+)*(?:-(?:rc|alpha)(?:\.\d+)?)?/x
AT_ID_CODE_RE =
/\A(?:at|id)\d+(?:\.\d+)*/
IDENTIFIER_RE =
/\A[A-Za-z][A-Za-z0-9_]*/
URI_CHAR =
%r{[A-Za-z0-9\-._~%!$&'()*+,;=:@/]}.source
URI_RE =
%r{\A[A-Za-z][A-Za-z0-9+.-]*:(?://)?#{URI_CHAR}*(?:\?#{URI_CHAR}*)?(?:\##{URI_CHAR}*)?}o
NUMBER_RE =
/\A(?:\d*\.\d+|\d+)(?:[eE][+-]?\d+)?/
ESCAPES =
{
  '\\' => "\\", "'" => "'", '"' => '"', '?' => '?',
  'a' => "\a", 'b' => "\b", 'f' => "\f", 'n' => "\n",
  'r' => "\r", 't' => "\t", 'v' => "\v"
}.freeze
SYMBOLS =
{
  ',' => :comma, '/' => :slash, '*' => :asterisk, '+' => :plus,
  '(' => :left_paren, ')' => :right_paren,
  '[' => :left_bracket, ']' => :right_bracket,
  '{' => :left_curly, '}' => :right_curly
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Lexer

Returns a new instance of Lexer.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
# File 'lib/openehr/aql/lexer.rb', line 87

def initialize(source)
  raise ArgumentError, 'source must be a String' unless source.is_a?(String)

  @chars = source.chars
  @pos = 0
  @line = 1
  @column = 1
end

Instance Method Details

#tokenizeObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/openehr/aql/lexer.rb', line 96

def tokenize
  tokens = []
  loop do
    skip_whitespace_and_comments
    start_line, start_column = @line, @column
    if at_end?
      tokens << Token.new(:eof, nil, line: start_line, column: start_column)
      break
    end
    tokens << next_token(start_line, start_column)
  end
  tokens.freeze
end