Class: ApiQueryLanguage::Sorting::ExpressionLexer
- Inherits:
-
Object
- Object
- ApiQueryLanguage::Sorting::ExpressionLexer
- Defined in:
- lib/api_query_language/sorting/expression_lexer.rex.rb
Overview
The generated lexer ApiQueryLanguage::Sorting::ExpressionLexer
Defined Under Namespace
Classes: LexerError, ScanError
Constant Summary collapse
- FIELD_IDENTIFIER =
:stopdoc:
/[A-Za-z][0-9A-Za-z_-]+/- DIRECTION_OPERATOR =
/asc|desc/
Instance Attribute Summary collapse
-
#filename ⇒ Object
The file name / path.
-
#ss ⇒ Object
(also: #match)
The StringScanner for this lexer.
-
#state ⇒ Object
The current lexical state.
Instance Method Summary collapse
-
#action ⇒ Object
Yields on the current action.
-
#location ⇒ Object
The current location in the parse.
-
#matches ⇒ Object
The match groups for the current scan.
-
#next_token ⇒ Object
Lex the next token.
-
#parse(str, parser) ⇒ Object
def next_token.
-
#parse_file(path) ⇒ Object
Read in and parse the file at
path. - #scanner_class ⇒ Object
Instance Attribute Details
#filename ⇒ Object
The file name / path
29 30 31 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 29 def filename @filename end |
#ss ⇒ Object Also known as: match
The StringScanner for this lexer.
34 35 36 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 34 def ss @ss end |
#state ⇒ Object
The current lexical state.
39 40 41 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 39 def state @state end |
Instance Method Details
#action ⇒ Object
Yields on the current action.
55 56 57 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 55 def action yield end |
#location ⇒ Object
The current location in the parse.
91 92 93 94 95 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 91 def location [ filename || "<input>" ].compact.join(":") end |
#matches ⇒ Object
The match groups for the current scan.
46 47 48 49 50 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 46 def matches m = (1..9).map { |i| ss[i] } m.pop until m[-1] or m.empty? m end |
#next_token ⇒ Object
Lex the next token.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 100 def next_token token = nil until ss.eos? or token token = case state when nil if text = ss.scan(/#{FIELD_IDENTIFIER}/o) action { [:FIELD_IDENTIFIER, text] } elsif ss.skip(/:(#{DIRECTION_OPERATOR})/o) action { [:DIRECTION_OPERATOR, matches[0]] } elsif text = ss.scan(".") action { [:FIELD_IDENTIFIER_SEPARATOR, text] } elsif text = ss.scan(";") action { [:SORT_SEPARATOR, text] } elsif ss.skip(/\s+/) # do nothing else text = ss.string[ss.pos..-1] raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'" end else raise ScanError, "undefined state at #{location}: '#{state}'" end # token = case state next unless token # allow functions to trigger redo w/ nil end # while raise LexerError, "bad lexical result at #{location}: #{token.inspect}" unless token.nil? || (Array === token && token.size >= 2) # auto-switch state self.state = token.last if token && token.first == :state token end |
#parse(str, parser) ⇒ Object
def next_token
71 72 73 74 75 76 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 71 def parse str self.ss = scanner_class.new str self.state ||= nil do_parse end |
#parse_file(path) ⇒ Object
Read in and parse the file at path.
81 82 83 84 85 86 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 81 def parse_file path self.filename = path open path do |f| parse f.read end end |
#scanner_class ⇒ Object
63 64 65 |
# File 'lib/api_query_language/sorting/expression_lexer.rex.rb', line 63 def scanner_class StringScanner end |