Class: Synvert::Core::NodeQuery::Lexer
- Inherits:
-
Object
- Object
- Synvert::Core::NodeQuery::Lexer
- Defined in:
- lib/synvert/core/node_query/lexer.rex.rb
Overview
The generated lexer Synvert::Core::NodeQuery::Lexer
Defined Under Namespace
Classes: LexerError, ScanError
Constant Summary collapse
- OPEN_ATTRIBUTE =
:stopdoc:
/\[/
- CLOSE_ATTRIBUTE =
/\]/
- OPEN_ARRAY =
/\(/
- CLOSE_ARRAY =
/\)/
- OPEN_SELECTOR =
/\(/
- CLOSE_SELECTOR =
/\)/
- OPEN_DYNAMIC_ATTRIBUTE =
/{{/
- CLOSE_DYNAMIC_ATTRIBUTE =
/}}/
- NODE_TYPE =
/\.[a-z]+/
- IDENTIFIER =
/[\.\w]+/
- IDENTIFIER_VALUE =
/[\.\w!&:\?<>=]+/
- FALSE =
/false/
- FLOAT =
/\d+\.\d+/
- INTEGER =
/\d+/
- NIL =
/nil/
- REGEXP_BODY =
/(?:[^\/]|\\\/)*/
- REGEXP =
/\/(#{REGEXP_BODY})(?<!\\)\/([imxo]*)/
- SYMBOL =
/:[\w!\?<>=]+/
- TRUE =
/true/
- SINGLE_QUOTE_STRING =
/'(.+?)'/
- DOUBLE_QUOTE_STRING =
/"(.+?)"/
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.
- #do_parse ⇒ Object
-
#initialize ⇒ Lexer
constructor
def next_token.
-
#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) ⇒ Object
Parse the given string.
-
#parse_file(path) ⇒ Object
Read in and parse the file at
path
. -
#scanner_class ⇒ Object
The current scanner class.
Constructor Details
#initialize ⇒ Lexer
def next_token
289 290 291 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 289 def initialize @nested_count = 0 end |
Instance Attribute Details
#filename ⇒ Object
The file name / path
47 48 49 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 47 def filename @filename end |
#ss ⇒ Object Also known as: match
The StringScanner for this lexer.
52 53 54 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 52 def ss @ss end |
#state ⇒ Object
The current lexical state.
57 58 59 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 57 def state @state end |
Instance Method Details
#action ⇒ Object
Yields on the current action.
73 74 75 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 73 def action yield end |
#do_parse ⇒ Object
292 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 292 def do_parse; end |
#location ⇒ Object
The current location in the parse.
108 109 110 111 112 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 108 def location [ (filename || "<input>"), ].compact.join(":") end |
#matches ⇒ Object
The match groups for the current scan.
64 65 66 67 68 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 64 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.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 117 def next_token token = nil until ss.eos? or token do token = case state when nil then case when ss.skip(/\s+/) then # do nothing when ss.skip(/:first-child/) then action { [:tINDEX, 0] } when ss.skip(/:last-child/) then action { [:tINDEX, -1] } when text = ss.scan(/:nth-child\(\d+\)/) then action { [:tINDEX, text.sub(':nth-child(', '').to_i - 1] } when text = ss.scan(/:nth-last-child\(\d+\)/) then action { [:tINDEX, -text.sub(':nth-last-child(', '').to_i] } when text = ss.scan(/:has/) then action { [:tHAS, text[1..-1]] } when text = ss.scan(/#{NODE_TYPE}/) then action { [:tNODE_TYPE, text[1..]] } when text = ss.scan(/>/) then action { [:tCHILD, text] } when text = ss.scan(/~/) then action { [:tSUBSEQUENT_SIBLING, text] } when text = ss.scan(/\+/) then action { [:tNEXT_SIBLING, text] } when text = ss.scan(/#{OPEN_SELECTOR}/) then action { [:tOPEN_SELECTOR, text] } when text = ss.scan(/#{CLOSE_SELECTOR}/) then action { [:tCLOSE_SELECTOR, text] } when text = ss.scan(/#{OPEN_ATTRIBUTE}/) then action { @nested_count += 1; @state = :KEY; [:tOPEN_ATTRIBUTE, text] } else text = ss.string[ss.pos .. -1] raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'" end when :KEY then case when ss.skip(/\s+/) then # do nothing when text = ss.scan(/!=/) then action { @state = :VALUE; [:tNOT_EQUAL, text] } when text = ss.scan(/=~/) then action { @state = :VALUE; [:tMATCH, text] } when text = ss.scan(/!~/) then action { @state = :VALUE; [:tNOT_MATCH, text] } when text = ss.scan(/>=/) then action { @state = :VALUE; [:tGREATER_THAN_OR_EQUAL, text] } when text = ss.scan(/<=/) then action { @state = :VALUE; [:tLESS_THAN_OR_EQUAL, text] } when text = ss.scan(/>/) then action { @state = :VALUE; [:tGREATER_THAN, text] } when text = ss.scan(/</) then action { @state = :VALUE; [:tLESS_THAN, text] } when text = ss.scan(/=/) then action { @state = :VALUE; [:tEQUAL, text] } when text = ss.scan(/includes/i) then action { @state = :VALUE; [:tINCLUDES, text] } when text = ss.scan(/not in/i) then action { @state = :VALUE; [:tNOT_IN, text] } when text = ss.scan(/in/i) then action { @state = :VALUE; [:tIN, text] } when text = ss.scan(/#{IDENTIFIER}/) then action { [:tKEY, text] } else text = ss.string[ss.pos .. -1] raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'" end when :VALUE then case when ss.skip(/\s+/) then # do nothing when text = ss.scan(/#{OPEN_DYNAMIC_ATTRIBUTE}/) then action { @state = :DYNAMIC_ATTRIBUTE; [:tOPEN_DYNAMIC_ATTRIBUTE, text] } when text = ss.scan(/#{OPEN_ARRAY}/) then action { @state = :ARRAY_VALUE; [:tOPEN_ARRAY, text] } when text = ss.scan(/#{CLOSE_ATTRIBUTE}/) then action { @nested_count -= 1; @state = @nested_count == 0 ? nil : :VALUE; [:tCLOSE_ATTRIBUTE, text] } when ss.skip(/#{NIL}/) then action { [:tNIL, nil] } when ss.skip(/#{TRUE}/) then action { [:tBOOLEAN, true] } when ss.skip(/#{FALSE}/) then action { [:tBOOLEAN, false] } when text = ss.scan(/#{SYMBOL}/) then action { [:tSYMBOL, text[1..-1].to_sym] } when text = ss.scan(/#{FLOAT}/) then action { [:tFLOAT, text.to_f] } when text = ss.scan(/#{INTEGER}/) then action { [:tINTEGER, text.to_i] } when text = ss.scan(/#{REGEXP}/) then action { [:tREGEXP, eval(text)] } when text = ss.scan(/#{DOUBLE_QUOTE_STRING}/) then action { [:tSTRING, text[1...-1]] } when text = ss.scan(/#{SINGLE_QUOTE_STRING}/) then action { [:tSTRING, text[1...-1]] } when text = ss.scan(/#{NODE_TYPE}/) then action { [:tNODE_TYPE, text[1..]] } when text = ss.scan(/>/) then action { [:tCHILD, text] } when text = ss.scan(/~/) then action { [:tSUBSEQUENT_SIBLING, text] } when text = ss.scan(/\+/) then action { [:tNEXT_SIBLING, text] } when text = ss.scan(/#{OPEN_ATTRIBUTE}/) then action { @nested_count += 1; @state = :KEY; [:tOPEN_ATTRIBUTE, text] } when text = ss.scan(/#{IDENTIFIER_VALUE}/) then action { [:tIDENTIFIER_VALUE, text] } else text = ss.string[ss.pos .. -1] raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'" end when :DYNAMIC_ATTRIBUTE then case when text = ss.scan(/#{CLOSE_DYNAMIC_ATTRIBUTE}/) then action { @state = :VALUE; [:tCLOSE_DYNAMIC_ATTRIBUTE, text] } when text = ss.scan(/#{IDENTIFIER}/) then action { [:tDYNAMIC_ATTRIBUTE, text] } else text = ss.string[ss.pos .. -1] raise ScanError, "can not match (#{state.inspect}) at #{location}: '#{text}'" end when :ARRAY_VALUE then case when ss.skip(/\s+/) then # do nothing when text = ss.scan(/,/) then action { [:tCOMMA, text] } when text = ss.scan(/#{CLOSE_ARRAY}/) then action { @state = :VALUE; [:tCLOSE_ARRAY, text] } when ss.skip(/#{NIL}/) then action { [:tNIL, nil] } when ss.skip(/#{TRUE}/) then action { [:tBOOLEAN, true] } when ss.skip(/#{FALSE}/) then action { [:tBOOLEAN, false] } when text = ss.scan(/#{SYMBOL}/) then action { [:tSYMBOL, text[1..-1].to_sym] } when text = ss.scan(/#{FLOAT}/) then action { [:tFLOAT, text.to_f] } when text = ss.scan(/#{INTEGER}/) then action { [:tINTEGER, text.to_i] } when text = ss.scan(/#{REGEXP}/) then action { [:tREGEXP, eval(text)] } when text = ss.scan(/#{DOUBLE_QUOTE_STRING}/) then action { [:tSTRING, text[1...-1]] } when text = ss.scan(/#{SINGLE_QUOTE_STRING}/) then action { [:tSTRING, text[1...-1]] } when text = ss.scan(/#{IDENTIFIER_VALUE}/) then action { [:tIDENTIFIER_VALUE, text] } 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) ⇒ Object
Parse the given string.
88 89 90 91 92 93 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 88 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
.
98 99 100 101 102 103 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 98 def parse_file path self.filename = path open path do |f| parse f.read end end |
#scanner_class ⇒ Object
The current scanner class. Must be overridden in subclasses.
81 82 83 |
# File 'lib/synvert/core/node_query/lexer.rex.rb', line 81 def scanner_class StringScanner end |