-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
lexer.rb
482 lines (436 loc) · 13.6 KB
/
lexer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
module EBNF::LL1
require 'ebnf/ll1/scanner' unless defined?(Scanner)
##
# A lexical analyzer
#
# @example Tokenizing a Turtle string
# terminals = [
# [:BLANK_NODE_LABEL, %r(_:(#{PN_LOCAL}))],
# ...
# ]
# ttl = "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> ."
# lexer = EBNF::LL1::Lexer.tokenize(ttl, terminals)
# lexer.each_token do |token|
# puts token.inspect
# end
#
# @example Tokenizing and returning a token stream
# lexer = EBNF::LL1::Lexer.tokenize(...)
# while :some-condition
# token = lexer.first # Get the current token
# token = lexer.shift # Get the current token and shift to the next
# end
#
# @example Handling error conditions
# begin
# EBNF::LL1::Lexer.tokenize(query)
# rescue EBNF::LL1::Lexer::Error => error
# warn error.inspect
# end
#
# @see https://en.wikipedia.org/wiki/Lexical_analysis
class Lexer
include Enumerable
include ::EBNF::Unescape
##
# @return [Regexp] defines whitespace, including comments, otherwise whitespace must be explicit in terminals
attr_reader :whitespace
##
# Tokenizes the given `input` string or stream.
#
# @param [String, #to_s] input
# @param [Array<Array<Symbol, Regexp>>] terminals
# Array of symbol, regexp pairs used to match terminals.
# If the symbol is nil, it defines a Regexp to match string terminals.
# @param [Hash{Symbol => Object}] options
# @yield [lexer]
# @yieldparam [Lexer] lexer
# @return [Lexer]
# @raise [Lexer::Error] on invalid input
def self.tokenize(input, terminals, **options, &block)
lexer = self.new(input, terminals, **options)
block_given? ? block.call(lexer) : lexer
end
##
# Initializes a new lexer instance.
#
# @param [String, #to_s] input
# @param [Array<Array<Symbol, Regexp>, Terminal>] terminals
# Array of symbol, regexp pairs used to match terminals.
# If the symbol is nil, it defines a Regexp to match string terminals.
# @param [Hash{Symbol => Object}] options
# @option options [Regexp] :whitespace
# Whitespace between tokens, including comments
# @option options[Integer] :high_water passed to scanner
# @option options[Integer] :low_water passed to scanner
def initialize(input = nil, terminals = nil, **options)
@options = options.dup
@whitespace = @options[:whitespace]
@terminals = terminals.map do |term|
if term.is_a?(Array) && term.length ==3
# Last element is options
Terminal.new(term[0], term[1], **term[2])
elsif term.is_a?(Array)
Terminal.new(*term)
else
term
end
end
raise Error, "Terminal patterns not defined" unless @terminals && @terminals.length > 0
@scanner = Scanner.new(input, **options)
end
##
# Any additional options for the lexer.
#
# @return [Hash]
attr_reader :options
##
# The current input string being processed.
#
# @return [String]
attr_accessor :input
##
# Returns `true` if the input string is lexically valid.
#
# To be considered valid, the input string must contain more than zero
# terminals, and must not contain any invalid terminals.
#
# @return [Boolean]
def valid?
begin
!count.zero?
rescue Error
false
end
end
##
# Enumerates each token in the input string.
#
# @yield [token]
# @yieldparam [Token] token
# @return [Enumerator]
def each_token(&block)
if block_given?
while token = shift
yield token
end
end
enum_for(:each_token)
end
alias_method :each, :each_token
##
# Returns first token in input stream
#
# @param [Array[Symbol]] types Optional set of types for restricting terminals examined
# @return [Token]
def first(*types)
return nil unless scanner
@first ||= begin
{} while !scanner.eos? && skip_whitespace
return nil if scanner.eos?
token = match_token(*types)
if token.nil?
lexme = (scanner.rest.split(@whitespace || /\s/).first rescue nil) || scanner.rest
raise Error.new("Invalid token #{lexme[0..100].inspect}",
input: scanner.rest[0..100], token: lexme, lineno: lineno)
end
token
end
rescue ArgumentError, Encoding::CompatibilityError => e
raise Error.new(e.message,
input: (scanner.rest[0..100] rescue '??'), token: lexme, lineno: lineno)
rescue Error
raise
rescue
STDERR.puts "Expected ArgumentError, got #{$!.class}"
raise
end
##
# Returns first token and shifts to next
#
# @return [Token]
def shift
cur = first
@first = nil
cur
end
##
# Skip input until a token is matched
#
# @param [Array[Symbol]] types Optional set of types for restricting terminals examined
# @return [Token]
def recover(*types)
until scanner.eos? || tok = match_token(*types)
if scanner.skip_until(@whitespace || /\s+/m).nil? # Skip past current "token"
# No whitespace at the end, must be and end of string
scanner.terminate
else
skip_whitespace
end
end
scanner.unscan if tok
first
end
##
# The current line number (one-based).
#
# @return [Integer]
def lineno
scanner.lineno
end
protected
# @return [StringScanner]
attr_reader :scanner
##
# Skip whitespace, as defined through input options or defaults
def skip_whitespace
# skip all white space, but keep track of the current line number
while @whitespace && !scanner.eos?
unless scanner.scan(@whitespace)
return
end
end
end
##
# Return the matched token.
#
# If the token was matched with a case-insensitive regexp,
# track this with the resulting {Token}, so that comparisons
# with that token are also case insensitive
#
# @param [Array[Symbol]] types Optional set of types for restricting terminals examined
# @return [Token]
def match_token(*types)
@terminals.each do |term|
next unless types.empty? || types.include?(term.type)
#STDERR.puts "match[#{term.type}] #{scanner.rest[0..100].inspect} against #{term.regexp.inspect}" #if term.type == :STRING_LITERAL_SINGLE_QUOTE
if term.partial_regexp && scanner.match?(term.partial_regexp) && !scanner.match?(term.regexp) && scanner.respond_to?(:ensure_buffer_full)
scanner.ensure_buffer_full
end
if matched = scanner.scan(term.regexp)
#STDERR.puts " matched #{term.type.inspect}: #{matched.inspect}"
tok = token(term.type, term.canonicalize(matched))
return tok
end
end
nil
end
# Terminal class, representing the terminal identifier and
# matching regular expression. Optionally, a Terminal may include
# a map to turn case-insensitively matched terminals into their
# canonical form
class Terminal
attr_reader :type
attr_reader :regexp
attr_reader :partial_regexp
# @param [Symbol, nil] type
# @param [Regexp] regexp
# @param [Hash{Symbol => Object}] options
# @option options [Hash{String => String}] :map ({})
# A mapping from terminals, in lower-case form, to
# their canonical value
# @option options [Boolean] :unescape
# Cause strings and codepoints to be unescaped.
# @option options [Regexp] :partial_regexp
# A regular expression matching the beginning of this terminal; useful for terminals that match things longer than the scanner low water mark.
def initialize(type, regexp, **options)
@type, @regexp, @options = type, regexp, options
@partial_regexp = options[:partial_regexp]
@map = options.fetch(:map, {})
end
# Map a terminal to it's canonical form. If there is no
# map, `value` is returned. `value` is unescaped if there
# is no canonical mapping, and the `:unescape` option is set.
#
# @param [String] value
# value to canonicalize
# @return [String]
def canonicalize(value)
@map.fetch(value.downcase, unescape(value))
end
def ==(other)
case other
when Array
@type == other.first && @regexp == other.last
when Terminal
@type == other.type && @regexp == other.regexp
end
end
protected
# Perform string and codepoint unescaping if defined for this terminal
# @param [String] string
# @return [String]
def unescape(string)
if @options[:unescape]
EBNF::Unescape.unescape(string)
else
string
end
end
end
##
# Constructs a new token object annotated with the current line number.
#
# The parser relies on the type being a symbolized URI and the value being
# a string, if there is no type. If there is a type, then the value takes
# on the native representation appropriate for that type.
#
# @param [Symbol] type
# @param [String] value
# Scanner instance with access to matched groups
# @param [Hash{Symbol => Object}] options
# @return [Token]
def token(type, value, **options)
Token.new(type, value, lineno: lineno, **options)
end
##
# Represents a lexer token.
#
# @example Creating a new token
# token = EBNF::LL1::Lexer::Token.new(:LANGTAG, "en")
# token.type #=> :LANGTAG
# token.value #=> "en"
#
# @see https://en.wikipedia.org/wiki/Lexical_analysis#Token
class Token
##
# The token's symbol type.
#
# @return [Symbol]
attr_reader :type
##
# The token's value.
#
# @return [String]
attr_reader :value
##
# The line number where the token was encountered.
#
# @return [Integer]
attr_reader :lineno
##
# Any additional options for the token.
#
# @return [Hash]
attr_reader :options
##
# Initializes a new token instance.
#
# @param [Symbol] type
# @param [String] value
# @param [Hash{Symbol => Object}] options
# @option options [Integer] :lineno (nil)
def initialize(type, value, **options)
@type = type.to_s.to_sym if type
@value = value.to_s
@options = options.dup
@lineno = @options.delete(:lineno)
end
##
# Returns the attribute named by `key`.
#
# @param [Symbol] key
# @return [Object]
def [](key)
key = key.to_s.to_sym unless key.is_a?(Integer) || key.is_a?(Symbol)
case key
when 0, :type then @type
when 1, :value then @value
else nil
end
end
##
# Returns `true` if the given `value` matches either the type or value
# of this token.
#
# @example Matching using the symbolic type
# EBNF::LL1::Lexer::Token.new(:NIL) === :NIL #=> true
#
# @example Matching using the string value
# EBNF::LL1::Lexer::Token.new(nil, "{") === "{" #=> true
#
# @param [Symbol, String] value
# @return [Boolean]
def ===(value)
case value
when Symbol
value == @type
when ::String
@value == (@options[:case_insensitive] ? value.to_s.downcase : value.to_s)
else value == @value
end
end
##
# Returns a hash table representation of this token.
#
# @return [Hash]
def to_hash
{type: @type, value: @value}
end
##
# Readable version of token
def to_s
@type ? @type.inspect : @value
end
##
# Returns type, if not nil, otherwise value
def representation
@type ? @type : @value
end
##
# Returns an array representation of this token.
#
# @return [Array]
def to_a
[@type, @value]
end
##
# Returns a developer-friendly representation of this token.
#
# @return [String]
def inspect
"#{@value.inspect}#{'(' + @type.to_s + ')' if @type}"
end
end # class Token
##
# Raised for errors during lexical analysis.
#
# @example Raising a lexer error
# raise EBNF::LL1::Lexer::Error.new(
# "invalid token '%' on line 10",
# input: query, token: '%', lineno: 9)
#
# @see https://ruby-doc.org/core/classes/StandardError.html
class Error < StandardError
##
# The input string associated with the error.
#
# @return [String]
attr_reader :input
##
# The invalid token which triggered the error.
#
# @return [String]
attr_reader :token
##
# The line number where the error occurred.
#
# @return [Integer]
attr_reader :lineno
##
# Initializes a new lexer error instance.
#
# @param [String, #to_s] message
# @param [Hash{Symbol => Object}] options
# @option options [String] :input (nil)
# @option options [String] :token (nil)
# @option options [Integer] :lineno (nil)
def initialize(message, **options)
@input = options[:input]
@token = options[:token]
@lineno = options[:lineno]
super(message.to_s)
end
end # class Error
end # class Lexer
end # module EBNF