flexr 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +33 -0
- data/README.md +4 -0
- data/benchmark/dfa_stretch.rb +149 -0
- data/benchmark/lrama_lexer.rb +79 -96
- data/benchmark/regexp_parser_corpus.rb +35 -0
- data/benchmark/regexp_parser_parity.rb +89 -0
- data/benchmark/regexp_parser_scanner.rb +159 -0
- data/docs/README.md +4 -1
- data/docs/adr/0021-ruby-34-minimum.md +23 -0
- data/docs/adr/0022-immutable-tokens-and-locations.md +22 -0
- data/docs/adr/0023-stable-explicit-firstmatch.md +21 -0
- data/docs/adr/0024-opt-in-ractor-sharing.md +24 -0
- data/docs/adr/0025-explicit-whole-token-acceleration.md +26 -0
- data/docs/adr/0026-retain-direct-dispatch.md +22 -0
- data/docs/adr/0033-action-free-recognition.md +33 -0
- data/docs/adr/0034-lexer-checkpoints.md +25 -0
- data/docs/adr/0035-parser-token-expectations.md +27 -0
- data/docs/adr/0038-replay-named-regexp-captures.md +23 -0
- data/docs/adr/0039-input-stack.md +25 -0
- data/docs/adr/0040-runtime-delimiters.md +23 -0
- data/docs/adr/0041-post-lexical-indentation.md +24 -0
- data/docs/adr/0042-capture-delegation.md +23 -0
- data/docs/adr/0043-expand-reusable-rules-explicitly.md +27 -0
- data/docs/adr/0044-import-oedipus-firstmatch.md +26 -0
- data/docs/assets/playground-absence-and-diagnostics.gif +0 -0
- data/docs/explanation/backends.md +5 -4
- data/docs/how-to/integrate-with-lrama.md +9 -8
- data/docs/how-to/integrate-with-racc.md +9 -0
- data/docs/how-to/migrate-from-oedipus-lex.md +43 -0
- data/docs/how-to/parser-driven-lexing.md +48 -0
- data/docs/how-to/try-playground-demos.md +13 -0
- data/docs/internals/regexp-parser-experiment.md +67 -0
- data/docs/perf-log.md +120 -8
- data/docs/reference/README.md +1 -1
- data/docs/reference/actions.md +106 -0
- data/docs/reference/cli.md +75 -4
- data/docs/reference/diagnostics.md +3 -9
- data/docs/reference/dsl.md +66 -3
- data/docs/reference/public-api.md +8 -1
- data/docs/reference/regexp.md +27 -4
- data/docs/reference/ruby-lsp.md +15 -0
- data/docs/reference/runtime.md +163 -2
- data/docs/tutorial/build-a-calculator-parser.md +47 -0
- data/examples/with_lrama/README.md +3 -3
- data/lib/flexr/automaton/accel.rb +1 -0
- data/lib/flexr/automaton/compiler.rb +15 -0
- data/lib/flexr/automaton/dfa.rb +0 -33
- data/lib/flexr/automaton/keyword_tables.rb +14 -8
- data/lib/flexr/automaton/reference_dfa.rb +38 -0
- data/lib/flexr/cli.rb +118 -18
- data/lib/flexr/codegen/table.rb +7 -0
- data/lib/flexr/configuration.rb +18 -1
- data/lib/flexr/dsl.rb +14 -3
- data/lib/flexr/generated.rb +4 -0
- data/lib/flexr/generator.rb +39 -7
- data/lib/flexr/importer/oedipus_lex.rb +229 -0
- data/lib/flexr/importer.rb +7 -2
- data/lib/flexr/ir.rb +1 -1
- data/lib/flexr/options.rb +3 -0
- data/lib/flexr/project_template.rb +46 -0
- data/lib/flexr/project_templates/common/.gitignore +1 -0
- data/lib/flexr/project_templates/common/lexer.flexr.rb +12 -0
- data/lib/flexr/project_templates/common/test_calculator.rb +15 -0
- data/lib/flexr/project_templates/lrama/Gemfile +9 -0
- data/lib/flexr/project_templates/lrama/README.md +17 -0
- data/lib/flexr/project_templates/lrama/Rakefile +18 -0
- data/lib/flexr/project_templates/lrama/calculator.rb +28 -0
- data/lib/flexr/project_templates/lrama/parser.y +72 -0
- data/lib/flexr/project_templates/racc/Gemfile +8 -0
- data/lib/flexr/project_templates/racc/README.md +15 -0
- data/lib/flexr/project_templates/racc/Rakefile +14 -0
- data/lib/flexr/project_templates/racc/calculator.rb +22 -0
- data/lib/flexr/project_templates/racc/parser.y +19 -0
- data/lib/flexr/regexp/ast.rb +2 -0
- data/lib/flexr/regexp/parser.rb +13 -11
- data/lib/flexr/regexp/tokenizer.rb +2 -2
- data/lib/flexr/rules.rb +53 -0
- data/lib/flexr/runtime/buffer.rb +1 -1
- data/lib/flexr/runtime/captures.rb +46 -0
- data/lib/flexr/runtime/channels.rb +2 -1
- data/lib/flexr/runtime/checkpoint.rb +116 -0
- data/lib/flexr/runtime/core.rb +55 -43
- data/lib/flexr/runtime/delegation.rb +123 -0
- data/lib/flexr/runtime/delimiters.rb +89 -0
- data/lib/flexr/runtime/firstmatch.rb +266 -0
- data/lib/flexr/runtime/indentation.rb +235 -0
- data/lib/flexr/runtime/input_stack.rb +48 -0
- data/lib/flexr/runtime/interpreter.rb +36 -314
- data/lib/flexr/runtime/recognizer.rb +106 -0
- data/lib/flexr/runtime.rb +10 -0
- data/lib/flexr/skeleton/verification.rb +103 -0
- data/lib/flexr/skeleton.rb +208 -0
- data/lib/flexr/source/fixes.rb +145 -0
- data/lib/flexr/source/prism_reader.rb +187 -25
- data/lib/flexr/testing.rb +5 -2
- data/lib/flexr/unicode/reference_regexp.rb +29 -11
- data/lib/flexr/version.rb +1 -1
- data/lib/ruby_lsp/flexr/addon.rb +88 -13
- data/lib/ruby_lsp/flexr/navigation.rb +231 -0
- data/lib/ruby_lsp/flexr/ranges.rb +40 -0
- data/sig/flexr.rbs +33 -3
- data/site/README.md +22 -7
- data/site/ruby/presets/heredoc.flexr.rb +15 -0
- data/site/ruby/presets/html_javascript.rb +12 -0
- data/site/ruby/presets/html_js.flexr.rb +14 -0
- data/site/ruby/presets/indentation.flexr.rb +14 -0
- data/site/scripts/prepare-playground.mjs +16 -6
- data/site/scripts/stable-release.mjs +4 -1
- data/site/src/content/docs/learn/parser-integration.md +23 -8
- data/site/src/content/docs/reference/cli.md +3 -0
- data/site/src/content/docs/reference/dsl.md +1 -0
- data/site/src/content/docs/reference/runtime.md +26 -0
- data/site/src/pages/playground.astro +15 -1
- data/site/src/playground/client.js +30 -1
- data/site/src/playground/issue.mjs +19 -0
- data/site/src/playground/runtime.mjs +4 -3
- data/site/src/playground/worker.mjs +1 -1
- data/site/tests/browser/playground.spec.mjs +64 -0
- data/site/tests/playground-issue.test.mjs +15 -0
- data/site/tests/playground-release.test.mjs +62 -11
- data/site/tests/playground-runtime.test.mjs +48 -0
- data/test/ruby_lsp_addon_spec.rb +174 -0
- metadata +63 -1
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
module Firstmatch
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def reference_pattern?(pattern)
|
|
9
|
+
Regexp::Matcher.reference?(pattern, encoding: @config.encoding, unicode: @config.options[:unicode] == true)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def scan_firstmatch(position, expected)
|
|
13
|
+
buffer = @lexer.buffer
|
|
14
|
+
@rules.sort_by(&:index).each do |rule|
|
|
15
|
+
next unless rule_active?(rule)
|
|
16
|
+
next if expected && !expected_rule?(rule, expected, buffer, position)
|
|
17
|
+
|
|
18
|
+
matches = rule.patterns.each_with_index.filter_map do |pattern, pattern_index|
|
|
19
|
+
condition = rule.pattern_conditions.fetch(pattern_index)
|
|
20
|
+
next if condition.bol_only && !@lexer.beginning_of_line?
|
|
21
|
+
|
|
22
|
+
regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(::Regexp.escape(pattern.to_s))
|
|
23
|
+
match = streamed_match(
|
|
24
|
+
regexp, buffer, position, reference: reference_pattern?(regexp),
|
|
25
|
+
limit: :token, rule: rule
|
|
26
|
+
)
|
|
27
|
+
next unless match&.begin(0)&.zero?
|
|
28
|
+
next if expected && !expected_rule?(rule, expected, buffer, position, position + match[0].bytesize)
|
|
29
|
+
|
|
30
|
+
[match, condition]
|
|
31
|
+
end
|
|
32
|
+
match, condition = matches.max_by { |candidate| candidate[0][0].bytesize }
|
|
33
|
+
next unless match
|
|
34
|
+
|
|
35
|
+
end_position = position + match[0].bytesize
|
|
36
|
+
next unless @lexer.utf8_boundary?(end_position)
|
|
37
|
+
next if condition.end_anchor && !end_anchor_match?(buffer, end_position)
|
|
38
|
+
trailing = trailing_length(rule, buffer, end_position)
|
|
39
|
+
next if rule.trailing && trailing.nil?
|
|
40
|
+
|
|
41
|
+
ensure_token_size!(end_position, position, rule: rule)
|
|
42
|
+
result = reusable_match(rule, position, end_position, end_position + (trailing || 0))
|
|
43
|
+
result.capture = rule.capture_patterns&.[](condition.pattern_index) && match
|
|
44
|
+
return result
|
|
45
|
+
end
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def streamed_match(pattern, buffer, position, limit:, rule:, reference: false)
|
|
50
|
+
pattern = regexp_pattern(pattern)
|
|
51
|
+
minimum = minimum_match_bytes(pattern)
|
|
52
|
+
loop do
|
|
53
|
+
subject, tail = stream_subject(buffer, position)
|
|
54
|
+
match = match_stream_pattern(pattern, subject, reference: reference)
|
|
55
|
+
ensure_stream_limit!(limit, position, match[0].bytesize, rule) if match
|
|
56
|
+
return match if match && match[0].bytesize < subject.bytesize
|
|
57
|
+
return match if match && %i[eof invalid].include?(tail)
|
|
58
|
+
if !match && subject.bytesize >= minimum && tail != :incomplete
|
|
59
|
+
first_byte = subject.getbyte(0)
|
|
60
|
+
return nil unless first_byte && possible_first_byte?(pattern, first_byte)
|
|
61
|
+
return nil if %i[eof invalid].include?(tail)
|
|
62
|
+
end
|
|
63
|
+
ensure_stream_limit!(limit, position, subject.bytesize, rule) unless match
|
|
64
|
+
@lexer.consume_step!
|
|
65
|
+
return match unless can_refill_match?(buffer)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def match_stream_pattern(pattern, subject, reference:)
|
|
70
|
+
Regexp::Matcher.match(pattern, subject, encoding: @config.encoding,
|
|
71
|
+
unicode: @config.options[:unicode] == true, reference: reference)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def stream_subject(buffer, position)
|
|
75
|
+
return [buffer.byteslice(position...buffer.bytesize).to_s.b, buffer.eof_loaded? ? :eof : :end] unless @lexer.utf8_input?
|
|
76
|
+
|
|
77
|
+
cursor = position
|
|
78
|
+
while cursor < buffer.bytesize
|
|
79
|
+
status, length = utf8_status(buffer, cursor)
|
|
80
|
+
break unless status == :complete
|
|
81
|
+
|
|
82
|
+
cursor += length
|
|
83
|
+
end
|
|
84
|
+
tail = if cursor < buffer.bytesize
|
|
85
|
+
utf8_status(buffer, cursor).first
|
|
86
|
+
elsif buffer.eof_loaded?
|
|
87
|
+
:eof
|
|
88
|
+
else
|
|
89
|
+
:end
|
|
90
|
+
end
|
|
91
|
+
subject = buffer.byteslice(position, cursor - position).to_s.dup.force_encoding(Encoding::UTF_8)
|
|
92
|
+
[subject, tail]
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def utf8_status(buffer, position)
|
|
96
|
+
return [:eof, 0] if position >= buffer.bytesize
|
|
97
|
+
|
|
98
|
+
first = buffer.getbyte(position)
|
|
99
|
+
return [:complete, 1] if first <= 0x7f
|
|
100
|
+
return [:invalid, 1] unless first.between?(0xc2, 0xf4)
|
|
101
|
+
|
|
102
|
+
length = if first <= 0xdf
|
|
103
|
+
2
|
|
104
|
+
elsif first <= 0xef
|
|
105
|
+
3
|
|
106
|
+
else
|
|
107
|
+
4
|
|
108
|
+
end
|
|
109
|
+
return [:incomplete, length] unless buffer.ensure_available?(position + length)
|
|
110
|
+
return [:invalid, length] unless buffer.valid_utf8_at?(position)
|
|
111
|
+
|
|
112
|
+
[:complete, length]
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def can_refill_match?(buffer)
|
|
116
|
+
buffer.ensure_available?(buffer.bytesize + 1)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def minimum_match_bytes(pattern)
|
|
120
|
+
return 1 if posix_pattern?(pattern)
|
|
121
|
+
|
|
122
|
+
ast = Regexp::Parser.new(pattern.source, options: pattern.options,
|
|
123
|
+
encoding: pattern.encoding, unicode: true).parse
|
|
124
|
+
minimum_ast_bytes(ast)
|
|
125
|
+
rescue CompileError, RegexpError
|
|
126
|
+
1
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def regexp_pattern(pattern)
|
|
130
|
+
return pattern if pattern.is_a?(::Regexp)
|
|
131
|
+
|
|
132
|
+
::Regexp.new(::Regexp.escape(pattern.to_s))
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def minimum_ast_bytes(node)
|
|
136
|
+
case node
|
|
137
|
+
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Fail then 0
|
|
138
|
+
when Regexp::AST::ByteRange then 1
|
|
139
|
+
when Regexp::AST::CodepointRange then utf8_length(node.lo)
|
|
140
|
+
when Regexp::AST::CharClass
|
|
141
|
+
ranges = node.ranges.flat_map do |range|
|
|
142
|
+
range.first == Regexp::AST::Property ? property_ranges(range, ignorecase: range[3] == true) : [range]
|
|
143
|
+
end
|
|
144
|
+
ranges = complement_codepoint_ranges(ranges) if node.negated
|
|
145
|
+
ranges.map { |lo, _hi| utf8_length(lo) }.min || 1
|
|
146
|
+
when Regexp::AST::Seq then node.children.sum { |child| minimum_ast_bytes(child) }
|
|
147
|
+
when Regexp::AST::Alt then node.children.map { |child| minimum_ast_bytes(child) }.min || 0
|
|
148
|
+
when Regexp::AST::Repeat then node.minimum * minimum_ast_bytes(node.child)
|
|
149
|
+
else minimum_unknown_bytes(node)
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def minimum_unknown_bytes(node)
|
|
154
|
+
return 0 if node.is_a?(Regexp::AST::Star)
|
|
155
|
+
|
|
156
|
+
1
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def possible_first_byte?(pattern, byte)
|
|
160
|
+
return true if posix_pattern?(pattern)
|
|
161
|
+
|
|
162
|
+
ast = Regexp::Parser.new(pattern.source, options: pattern.options,
|
|
163
|
+
encoding: pattern.encoding, unicode: true).parse
|
|
164
|
+
binary = !@lexer.utf8_input?
|
|
165
|
+
first_byte_ranges(ast, binary: binary).any? { |lo, hi| byte.between?(lo, hi) }
|
|
166
|
+
rescue CompileError, RegexpError
|
|
167
|
+
true
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def first_byte_ranges(node, binary: false)
|
|
171
|
+
case node
|
|
172
|
+
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Fail then []
|
|
173
|
+
when Regexp::AST::ByteRange then [[node.lo, node.hi]]
|
|
174
|
+
when Regexp::AST::CodepointRange
|
|
175
|
+
ranges = [[node.lo, node.hi]]
|
|
176
|
+
return ranges if binary
|
|
177
|
+
|
|
178
|
+
ranges.flat_map { |lo, hi| Unicode::Utf8Splitter.split(lo, hi).map(&:first).map(&:first) }
|
|
179
|
+
.map { |value| [value, value] }
|
|
180
|
+
when Regexp::AST::CharClass
|
|
181
|
+
ranges = node.ranges.flat_map do |range|
|
|
182
|
+
range.first == Regexp::AST::Property ? property_ranges(range, ignorecase: range[3] == true) : [range]
|
|
183
|
+
end
|
|
184
|
+
ranges = complement_codepoint_ranges(ranges) if node.negated
|
|
185
|
+
return ranges if binary
|
|
186
|
+
|
|
187
|
+
ranges.flat_map { |lo, hi| Unicode::Utf8Splitter.split(lo, hi).map(&:first).map(&:first) }
|
|
188
|
+
.map { |value| [value, value] }
|
|
189
|
+
when Regexp::AST::Seq
|
|
190
|
+
ranges = []
|
|
191
|
+
node.children.each do |child|
|
|
192
|
+
ranges.concat(first_byte_ranges(child, binary: binary))
|
|
193
|
+
break unless nullable?(child)
|
|
194
|
+
end
|
|
195
|
+
ranges
|
|
196
|
+
when Regexp::AST::Alt
|
|
197
|
+
node.children.flat_map { |child| first_byte_ranges(child, binary: binary) }
|
|
198
|
+
when Regexp::AST::Star, Regexp::AST::Repeat
|
|
199
|
+
first_byte_ranges(node.child, binary: binary)
|
|
200
|
+
else
|
|
201
|
+
first_byte_fallback
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def first_byte_fallback
|
|
206
|
+
[]
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def nullable?(node)
|
|
210
|
+
case node
|
|
211
|
+
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Star then true
|
|
212
|
+
when Regexp::AST::Repeat then node.minimum.zero? || nullable?(node.child)
|
|
213
|
+
when Regexp::AST::Seq then node.children.all? { |child| nullable?(child) }
|
|
214
|
+
when Regexp::AST::Alt then node.children.any? { |child| nullable?(child) }
|
|
215
|
+
else false
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def utf8_length(codepoint)
|
|
220
|
+
return 1 if codepoint <= 0x7f
|
|
221
|
+
return 2 if codepoint <= 0x7ff
|
|
222
|
+
return 3 if codepoint <= 0xffff
|
|
223
|
+
|
|
224
|
+
4
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def complement_codepoint_ranges(ranges)
|
|
228
|
+
result = []
|
|
229
|
+
cursor = 0
|
|
230
|
+
ranges.sort_by(&:first).each do |lo, hi|
|
|
231
|
+
result << [cursor, lo - 1] if cursor < lo
|
|
232
|
+
cursor = [cursor, hi + 1].max
|
|
233
|
+
end
|
|
234
|
+
result << [cursor, 0x10ffff] if cursor <= 0x10ffff
|
|
235
|
+
result
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
def property_ranges(range, ignorecase:)
|
|
239
|
+
ranges = Unicode::Property.ranges(range[2])
|
|
240
|
+
ranges = Unicode::CaseFold.merge(ranges.flat_map { |lo, hi| Unicode::CaseFold.ranges(lo, hi) }) if ignorecase
|
|
241
|
+
range[1] ? complement_codepoint_ranges(ranges) : ranges
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
def posix_pattern?(pattern)
|
|
245
|
+
pattern.is_a?(::Regexp) && pattern.source.match?(/\[:(?:\^)?[a-z]+:\]/)
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def ensure_stream_limit!(limit, position, size, rule)
|
|
249
|
+
if limit == :lookahead
|
|
250
|
+
@lexer.ensure_lookahead_size!(size, rule: rule)
|
|
251
|
+
else
|
|
252
|
+
ensure_token_size!(position + size, position, rule: rule)
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def rule_active?(rule)
|
|
257
|
+
return true if rule.states.include?(:initial) && @lexer.state == :initial
|
|
258
|
+
|
|
259
|
+
state = @config.states.fetch(@lexer.state)
|
|
260
|
+
return true if state.inclusive && rule.states.include?(:initial)
|
|
261
|
+
|
|
262
|
+
rule.states.include?(@lexer.state)
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
end
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
module Indentation
|
|
6
|
+
def self.configuration(newline: nil, indent: nil, dedent: nil, brackets: [], tab_width: 8)
|
|
7
|
+
types = [newline, indent, dedent]
|
|
8
|
+
validate!(types.all?(Symbol) && types.uniq.length == 3, "requires distinct newline, indent, and dedent Symbols")
|
|
9
|
+
validate!(tab_width.is_a?(Integer) && tab_width.positive?, "tab_width must be a positive Integer")
|
|
10
|
+
validate!(brackets.is_a?(Array) && brackets.all? { |pair| pair.is_a?(String) && pair.valid_encoding? && pair.length == 2 },
|
|
11
|
+
"brackets must be an Array of two-character strings")
|
|
12
|
+
characters = brackets.flat_map(&:chars).map(&:b)
|
|
13
|
+
validate!(characters.uniq.length == characters.length, "bracket characters must be distinct")
|
|
14
|
+
{ newline: newline, indent: indent, dedent: dedent, brackets: brackets.map { |pair| pair.dup.freeze }.freeze,
|
|
15
|
+
tab_width: tab_width }.freeze
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def self.validate!(condition, message)
|
|
19
|
+
return if condition
|
|
20
|
+
|
|
21
|
+
message = "indentation #{message}"
|
|
22
|
+
diagnostic = Diagnostics.error("FLEXR-E018", message)
|
|
23
|
+
raise CompileError.new(message, diagnostic: diagnostic)
|
|
24
|
+
end
|
|
25
|
+
private_class_method :validate!
|
|
26
|
+
|
|
27
|
+
def initialize(...)
|
|
28
|
+
super
|
|
29
|
+
@__flexr_indentation = self.class.__flexr_config.options[:indentation]
|
|
30
|
+
return unless @__flexr_indentation
|
|
31
|
+
|
|
32
|
+
@__flexr_indent_pairs = @__flexr_indentation.fetch(:brackets).to_h { |pair| pair.chars.map(&:b) }
|
|
33
|
+
@__flexr_indent_bracket_lexemes = @__flexr_indent_pairs.keys + @__flexr_indent_pairs.values
|
|
34
|
+
@__flexr_indent_bracket_lengths = @__flexr_indent_bracket_lexemes.map(&:bytesize).uniq
|
|
35
|
+
reset_indentation
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def next_token(expect: nil)
|
|
39
|
+
return expect.nil? ? super() : super unless @__flexr_indentation
|
|
40
|
+
|
|
41
|
+
expect = Configuration.expected_tokens!(expect) + [@__flexr_indentation.fetch(:newline)] unless expect.nil?
|
|
42
|
+
return @__flexr_indent_queue.shift unless @__flexr_indent_queue.empty?
|
|
43
|
+
|
|
44
|
+
loop do
|
|
45
|
+
@__flexr_indent_bracket = @__flexr_indent_newline_width = @__flexr_delegated_location = nil
|
|
46
|
+
token = expect.nil? ? super() : super(expect: expect)
|
|
47
|
+
unless token
|
|
48
|
+
finish_indentation
|
|
49
|
+
return @__flexr_indent_queue.shift unless @__flexr_indent_queue.empty?
|
|
50
|
+
return unless @__flexr_input_frames && !@halted && eof?
|
|
51
|
+
|
|
52
|
+
resume_suspended_input
|
|
53
|
+
next
|
|
54
|
+
end
|
|
55
|
+
return token if @__flexr_token_channel
|
|
56
|
+
|
|
57
|
+
if !@__flexr_delegated_location && token[0] == @__flexr_indentation.fetch(:newline)
|
|
58
|
+
blank = !@__flexr_indent_candidate.nil?
|
|
59
|
+
@__flexr_indent_candidate = @__flexr_indent_newline_width || 0
|
|
60
|
+
@__flexr_indent_counting = true
|
|
61
|
+
if blank || !@__flexr_indent_brackets.empty?
|
|
62
|
+
trivia = token.leading_trivia if token.respond_to?(:leading_trivia)
|
|
63
|
+
@__flexr_leading_trivia = trivia.dup if trivia && !trivia.empty?
|
|
64
|
+
next
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
return token
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
if @match_start == @position && @match_end == @position && eof?
|
|
71
|
+
finish_indentation
|
|
72
|
+
else
|
|
73
|
+
if @__flexr_indent_candidate && @__flexr_indent_brackets.empty?
|
|
74
|
+
location = @__flexr_delegated_location || (token.location if @token_kind == :struct)
|
|
75
|
+
adjust_indentation(location)
|
|
76
|
+
end
|
|
77
|
+
@__flexr_indent_candidate = nil
|
|
78
|
+
track_indentation_bracket
|
|
79
|
+
end
|
|
80
|
+
return token if @__flexr_indent_queue.empty?
|
|
81
|
+
|
|
82
|
+
@__flexr_indent_queue << token
|
|
83
|
+
return @__flexr_indent_queue.shift
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def __flexr_layout_candidate?(expected, starting, ending)
|
|
88
|
+
return true if @__flexr_indent_candidate && @__flexr_indent_brackets.empty? &&
|
|
89
|
+
(expected.key?(@__flexr_indentation.fetch(:indent)) || expected.key?(@__flexr_indentation.fetch(:dedent)))
|
|
90
|
+
|
|
91
|
+
return false if @__flexr_indent_bracket_lexemes.empty?
|
|
92
|
+
|
|
93
|
+
starting = @more_start || starting
|
|
94
|
+
if ending
|
|
95
|
+
return false unless @__flexr_indent_bracket_lengths.include?(ending - starting)
|
|
96
|
+
|
|
97
|
+
@__flexr_indent_bracket_lexemes.include?(@buffer.byteslice(starting...ending).b)
|
|
98
|
+
else
|
|
99
|
+
byte = @buffer.getbyte(starting)
|
|
100
|
+
@__flexr_indent_bracket_lexemes.any? { |lexeme| lexeme.getbyte(0) == byte }
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def seek(position)
|
|
105
|
+
return super unless @__flexr_indentation
|
|
106
|
+
raise ArgumentError, "indentation lexers can seek only to byte 0; use a checkpoint to restore another offset" if position != 0
|
|
107
|
+
|
|
108
|
+
super
|
|
109
|
+
reset_indentation
|
|
110
|
+
self
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
def defer_input_resume? = !@__flexr_indentation.nil?
|
|
116
|
+
|
|
117
|
+
def reset_input_cursor(...)
|
|
118
|
+
super
|
|
119
|
+
reset_indentation if @__flexr_indentation
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def reset_indentation
|
|
123
|
+
@__flexr_indent_levels = [0]
|
|
124
|
+
@__flexr_indent_brackets = []
|
|
125
|
+
@__flexr_indent_queue = []
|
|
126
|
+
@__flexr_indent_candidate = 0
|
|
127
|
+
@__flexr_indent_counting = true
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def execute(rule)
|
|
131
|
+
super
|
|
132
|
+
return unless @__flexr_indentation
|
|
133
|
+
|
|
134
|
+
@__flexr_indent_candidate, @__flexr_indent_counting = indentation_width(@match_start, @match_end, @__flexr_indent_candidate) if
|
|
135
|
+
@__flexr_indent_candidate && @__flexr_indent_counting
|
|
136
|
+
@__flexr_indent_bracket = nil
|
|
137
|
+
return unless @pending && !@__flexr_token_channel
|
|
138
|
+
|
|
139
|
+
if @pending[0] == @__flexr_indentation.fetch(:newline)
|
|
140
|
+
@__flexr_indent_newline_width = indentation_after_newline
|
|
141
|
+
elsif @__flexr_indent_bracket_lengths.include?(@match_end - @text_start)
|
|
142
|
+
@__flexr_indent_bracket = @buffer.byteslice(@text_start, @match_end - @text_start).b
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def indentation_width(starting, ending, width = 0)
|
|
147
|
+
tab_width = @__flexr_indentation.fetch(:tab_width)
|
|
148
|
+
while starting < ending
|
|
149
|
+
case @buffer.getbyte(starting)
|
|
150
|
+
when 0x20 then width += 1
|
|
151
|
+
when 0x09 then width += tab_width - (width % tab_width)
|
|
152
|
+
else break
|
|
153
|
+
end
|
|
154
|
+
starting += 1
|
|
155
|
+
end
|
|
156
|
+
[width, starting == ending]
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def indentation_after_newline
|
|
160
|
+
starting = @match_end
|
|
161
|
+
while starting > @match_start
|
|
162
|
+
byte = @buffer.getbyte(starting - 1)
|
|
163
|
+
break unless [0x20, 0x09].include?(byte)
|
|
164
|
+
|
|
165
|
+
starting -= 1
|
|
166
|
+
end
|
|
167
|
+
indentation_width(starting, @match_end).first if starting > @match_start && @buffer.getbyte(starting - 1) == 0x0a
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def adjust_indentation(location)
|
|
171
|
+
width = @__flexr_indent_candidate
|
|
172
|
+
return if width == @__flexr_indent_levels.last
|
|
173
|
+
|
|
174
|
+
if width > @__flexr_indent_levels.last
|
|
175
|
+
@__flexr_indent_levels << width
|
|
176
|
+
@__flexr_indent_queue << indentation_token(:indent, location)
|
|
177
|
+
else
|
|
178
|
+
level = @__flexr_indent_levels.rindex(width)
|
|
179
|
+
unless level
|
|
180
|
+
raise LexError.new("indentation width #{width} does not match any earlier indentation level",
|
|
181
|
+
filename: @filename, byte_pos: @text_start, line: @text_line)
|
|
182
|
+
end
|
|
183
|
+
emit_indentation_dedents(level, location)
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def finish_indentation
|
|
188
|
+
emit_indentation_dedents(0)
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def emit_indentation_dedents(level, location = nil)
|
|
192
|
+
while @__flexr_indent_levels.length > level + 1
|
|
193
|
+
@__flexr_indent_levels.pop
|
|
194
|
+
@__flexr_indent_queue << indentation_token(:dedent, location)
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def indentation_token(kind, location)
|
|
199
|
+
type = @__flexr_indentation.fetch(kind)
|
|
200
|
+
return [type, nil] unless @token_kind == :struct
|
|
201
|
+
|
|
202
|
+
filename = location ? location.filename : @filename
|
|
203
|
+
position = location ? location.byte_begin : @position
|
|
204
|
+
line = location ? location.line_begin : @line
|
|
205
|
+
column = location ? location.column_begin : @column
|
|
206
|
+
point = Location.new(filename: filename, byte_begin: position, byte_end: position,
|
|
207
|
+
line_begin: line, line_end: line, column_begin: column, column_end: column)
|
|
208
|
+
Token.new(type: type, value: nil, location: point)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def track_indentation_bracket
|
|
212
|
+
bracket = @__flexr_indent_bracket
|
|
213
|
+
return unless bracket
|
|
214
|
+
|
|
215
|
+
if (closing = @__flexr_indent_pairs[bracket])
|
|
216
|
+
@__flexr_indent_brackets << closing
|
|
217
|
+
elsif @__flexr_indent_brackets.last == bracket
|
|
218
|
+
@__flexr_indent_brackets.pop
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
module DSL
|
|
225
|
+
def indentation(newline:, indent:, dedent:, brackets: [], tab_width: 8)
|
|
226
|
+
__flexr_mutate! do
|
|
227
|
+
@__flexr_config.options[:indentation] = Runtime::Indentation.configuration(
|
|
228
|
+
newline: newline, indent: indent, dedent: dedent, brackets: brackets, tab_width: tab_width
|
|
229
|
+
)
|
|
230
|
+
prepend Runtime::Indentation unless ancestors.include?(Runtime::Indentation)
|
|
231
|
+
end
|
|
232
|
+
nil
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
INPUT_STACK_FIELDS = (CHECKPOINT_FIELDS + %i[
|
|
6
|
+
@buffer @filename @string_input @stable_input_end @valid_utf8_input @simple_location_input
|
|
7
|
+
@token_limit_required @__flexr_input_id
|
|
8
|
+
]).freeze
|
|
9
|
+
private_constant :INPUT_STACK_FIELDS
|
|
10
|
+
|
|
11
|
+
def push_input(input, filename: nil)
|
|
12
|
+
buffer = Buffer.new(input, chunk_size: @buffer.chunk_size, max_buffer_size: @buffer.max_buffer_size,
|
|
13
|
+
retain_input: @retain_input, filename: filename)
|
|
14
|
+
(@__flexr_pending_inputs ||= []) << [buffer, input, filename]
|
|
15
|
+
nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def activate_pending_inputs
|
|
21
|
+
raise ArgumentError, "a delegated lexer scans only the captured input" if @__flexr_delegation_depth
|
|
22
|
+
|
|
23
|
+
pending = @__flexr_pending_inputs
|
|
24
|
+
@__flexr_pending_inputs = nil
|
|
25
|
+
pending.each do |buffer, input, filename|
|
|
26
|
+
(@__flexr_input_frames ||= []) << INPUT_STACK_FIELDS.map { |field| instance_variable_get(field) }
|
|
27
|
+
@buffer = buffer
|
|
28
|
+
@filename = filename
|
|
29
|
+
@__flexr_input_id = Object.new.freeze
|
|
30
|
+
reset_input_cursor(input, self.class.__flexr_config.encoding)
|
|
31
|
+
end
|
|
32
|
+
clear_input_scanners
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def defer_input_resume? = false
|
|
36
|
+
|
|
37
|
+
def resume_suspended_input
|
|
38
|
+
frame = @__flexr_input_frames.pop
|
|
39
|
+
@__flexr_input_frames = nil if @__flexr_input_frames.empty?
|
|
40
|
+
INPUT_STACK_FIELDS.each_with_index { |field, index| instance_variable_set(field, frame.fetch(index)) }
|
|
41
|
+
clear_input_scanners
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def clear_input_scanners
|
|
45
|
+
@interpreter = @__flexr_whole_scanner = @__flexr_generated_accel_scanner = nil
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|