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
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
# Generated by flexr. DO NOT EDIT.
|
|
3
3
|
# source: lib/flexr/regexp/tokenizer.flexr.rb
|
|
4
|
-
# spec-digest: sha256:
|
|
4
|
+
# spec-digest: sha256:f26f85aa5f1fe785662db7dd96a0e3412440e9c36775587a3c3f4d681d01309d
|
|
5
5
|
# artifact-schema: 2
|
|
6
6
|
# runtime-abi: 2
|
|
7
7
|
# unicode: 15.1.0
|
|
@@ -17,7 +17,7 @@ module Flexr
|
|
|
17
17
|
# This specification is intentionally small: it is a dogfood target for
|
|
18
18
|
# the source lexer path, not the parser's implementation.
|
|
19
19
|
class SourceLexer < Flexr::Lexer
|
|
20
|
-
Flexr::Generated.install_compiled!(self, { artifact: {schema_version: 2, compiler_version: "2.
|
|
20
|
+
Flexr::Generated.install_compiled!(self, { artifact: {schema_version: 2, compiler_version: "2.2.0", runtime_abi_version: 2, unicode_version: "15.1.0"}, rules: [{ index: 0, patterns: [/[ \t\r\n]+/], pattern_conditions: [[0, 0, false, false]], trailing: nil, action: :skip, states: [:initial], bol_only: false, end_anchor: nil }, { index: 1, patterns: [/\\p\{[A-Za-z_][A-Za-z0-9_]*\}/], pattern_conditions: [[1, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 2, patterns: [/\\./], pattern_conditions: [[2, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 3, patterns: [/\[[^\]\n]*\]/], pattern_conditions: [[3, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 4, patterns: [/[?*+]|\{[0-9]+(?:,[0-9]*)?\}/], pattern_conditions: [[4, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 5, patterns: [/\|/], pattern_conditions: [[5, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 6, patterns: [/\(/], pattern_conditions: [[6, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 7, patterns: [/\)/], pattern_conditions: [[7, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 8, patterns: [/\^|\$/], pattern_conditions: [[8, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 9, patterns: [/\./], pattern_conditions: [[9, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 10, patterns: [/[^\\\[\]().|?*+{}^$ \t\r\n]/], pattern_conditions: [[10, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }], backend: :table, token_kind: :array, encoding: Encoding::BINARY, declared_tokens: [:PROPERTY, :ESCAPE, :CHAR_CLASS, :QUANTIFIER, :ALTERNATION, :GROUP_OPEN, :GROUP_CLOSE, :ANCHOR, :DOT, :LITERAL], options: {}, eof_rules: { }, states: [], inclusive_states: {initial: true}, compiled: { machines: { :initial => { state_name: :initial, dfa: {accepts: [[], [[10, 0, false, false]], [[0, 0, false, false]], [[8, 0, false, false]], [[6, 0, false, false]], [[7, 0, false, false]], [[4, 0, false, false]], [[9, 0, false, false]], [], [], [], [[5, 0, false, false]], [[3, 0, false, false]], [[2, 0, false, false]], [[2, 0, false, false]], [], [], [], [], [[1, 0, false, false]]], ec: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 32, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34], class_count: 35, state_count: 20, start: 0, rule_ids: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], packed: {base: [0, 0, 12, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 3, 22, 15, 4, 21, 0], default: [1, nil, nil, nil, nil, nil, nil, nil, 8, 13, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], next: [nil, 2, 2, nil, 2, nil, 2, nil, 3, nil, 4, 5, 6, 2, 2, 7, 2, 15, 2, 6, nil, 17, 8, 9, nil, 3, nil, 12, nil, nil, 14, 10, 11, nil, 16, 17, 18, 6, 18, 15, nil, 18, 18, 18, 18, 18, nil, 18, nil, 18, 18, 18, nil, nil, 19, 6], check: [nil, 0, 0, 9, 0, 8, 0, nil, 0, nil, 0, 0, 0, 2, 2, 0, 2, 10, 2, 0, nil, 17, 0, 0, 0, 0, nil, 8, nil, nil, 9, 0, 0, 0, 14, 15, 16, 17, 18, 15, nil, 16, 18, 16, 16, 16, nil, 18, nil, 18, 18, 18, nil, nil, 18, 15]}} } }, states: [:initial], stats: {initial: {states: 20, classes: 35, accepting_states: 12}}, diagnostics: [] } })
|
|
21
21
|
def scan_one
|
|
22
22
|
if @__flexr_generated_scan_state != @state
|
|
23
23
|
machine = self.class.__flexr_compiled.machines.fetch(@state)
|
data/lib/flexr/rules.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Rules
|
|
5
|
+
def self.extended(rule_set)
|
|
6
|
+
raise ArgumentError, "extend Flexr::Rules on a module" if rule_set.is_a?(Class)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def rule(...) = __flexr_rule_template.rule(...)
|
|
10
|
+
def pattern(...) = __flexr_rule_template.pattern(...)
|
|
11
|
+
def upto(...) = __flexr_rule_template.upto(...)
|
|
12
|
+
|
|
13
|
+
def __flexr_rule_entries
|
|
14
|
+
__flexr_rule_template.__flexr_rules.dup
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def __flexr_rule_template
|
|
20
|
+
@__flexr_rule_template ||= Class.new(Lexer)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
module Use
|
|
24
|
+
def use(rule_set)
|
|
25
|
+
raise ArgumentError, "use expects a module extended with Flexr::Rules" unless rule_set.is_a?(Module) && rule_set.is_a?(Rules)
|
|
26
|
+
|
|
27
|
+
__flexr_mutate! do
|
|
28
|
+
states = @__flexr_state_stack.empty? ? [:initial] : @__flexr_state_stack
|
|
29
|
+
first_index = @__flexr_rules.length
|
|
30
|
+
additions = rule_set.__flexr_rule_entries.map.with_index do |entry, index|
|
|
31
|
+
IR::Rule.new(**entry.to_h, index: first_index + index, patterns: entry.patterns.dup, states: states.dup)
|
|
32
|
+
end
|
|
33
|
+
@__flexr_rules.concat(additions)
|
|
34
|
+
include Runtime::Channels if additions.any?(&:channel)
|
|
35
|
+
end
|
|
36
|
+
nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def __flexr_bind_rule_set(rule_set, count, bindings)
|
|
42
|
+
entries = rule_set.__flexr_rule_entries if rule_set.is_a?(Rules)
|
|
43
|
+
raise CompileError, "rule set declarations changed after generation" unless entries&.length == count
|
|
44
|
+
|
|
45
|
+
bindings.each do |rule_index, entry_index|
|
|
46
|
+
__flexr_bind_generated_action(rule_index, entries.fetch(entry_index).action)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Lexer.extend(Rules::Use)
|
|
53
|
+
end
|
data/lib/flexr/runtime/buffer.rb
CHANGED
|
@@ -5,7 +5,7 @@ module Flexr
|
|
|
5
5
|
class Buffer
|
|
6
6
|
DEFAULT_CHUNK_SIZE = 64 * 1024
|
|
7
7
|
|
|
8
|
-
attr_reader :base_offset, :max_buffer_size
|
|
8
|
+
attr_reader :base_offset, :chunk_size, :max_buffer_size
|
|
9
9
|
|
|
10
10
|
def initialize(input, chunk_size: DEFAULT_CHUNK_SIZE, max_buffer_size: 64 * 1024 * 1024,
|
|
11
11
|
retain_input: true, filename: nil)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
module Captures
|
|
6
|
+
def captures
|
|
7
|
+
return EMPTY_CAPTURES unless @capture_match
|
|
8
|
+
|
|
9
|
+
@captures ||= capture_data.named_captures(symbolize_names: true).transform_values(&:freeze).freeze
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def execute_with_captures(match)
|
|
15
|
+
@capture_match = match
|
|
16
|
+
execute(match.rule)
|
|
17
|
+
finish_delegation if @__flexr_delegation_end
|
|
18
|
+
ensure
|
|
19
|
+
@capture_match = @captures = @capture_data = nil
|
|
20
|
+
@__flexr_delegation_tokens = @__flexr_delegation_end = nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def capture_data
|
|
24
|
+
@capture_data ||= replay_capture_data
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def replay_capture_data
|
|
28
|
+
match = @capture_match
|
|
29
|
+
return match.capture if match.capture.is_a?(MatchData)
|
|
30
|
+
|
|
31
|
+
regexp = match.capture
|
|
32
|
+
subject = @buffer.byteslice(match.start_pos, match.end_pos - match.start_pos)
|
|
33
|
+
encoding = if regexp.options.anybits?(::Regexp::NOENCODING)
|
|
34
|
+
Encoding::BINARY
|
|
35
|
+
else
|
|
36
|
+
self.class.__flexr_config.encoding
|
|
37
|
+
end
|
|
38
|
+
data = regexp.match(subject.force_encoding(encoding))
|
|
39
|
+
return data if data
|
|
40
|
+
|
|
41
|
+
message = "Ruby could not replay named captures for #{Diagnostics.rule_label(match.rule)}"
|
|
42
|
+
raise LexError.new(message, filename: @filename, byte_pos: match.start_pos, line: @line, text: subject)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -27,7 +27,7 @@ module Flexr
|
|
|
27
27
|
@__flexr_all_channels = previous
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
def next_token
|
|
30
|
+
def next_token(...)
|
|
31
31
|
loop do
|
|
32
32
|
@__flexr_token_channel = nil
|
|
33
33
|
@__flexr_recovery_token = nil
|
|
@@ -48,6 +48,7 @@ module Flexr
|
|
|
48
48
|
|
|
49
49
|
trivia = @__flexr_leading_trivia
|
|
50
50
|
@__flexr_leading_trivia = nil
|
|
51
|
+
trivia.concat(token.leading_trivia)
|
|
51
52
|
return ChannelToken.new(**token.to_h, leading_trivia: trivia)
|
|
52
53
|
end
|
|
53
54
|
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
class Checkpoint
|
|
6
|
+
def initialize(owner, lexer_id, input, state)
|
|
7
|
+
@owner = owner
|
|
8
|
+
@lexer_id = lexer_id
|
|
9
|
+
@input = input
|
|
10
|
+
@state = state.freeze
|
|
11
|
+
freeze
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private_class_method :new
|
|
15
|
+
|
|
16
|
+
def inspect = "#<#{self.class}>"
|
|
17
|
+
|
|
18
|
+
def __flexr_state(owner, lexer_id, input)
|
|
19
|
+
raise ArgumentError, "checkpoint belongs to a different lexer" unless @owner.equal?(owner) && @lexer_id == lexer_id
|
|
20
|
+
raise ArgumentError, "checkpoint belongs to a different input" unless @input.equal?(input)
|
|
21
|
+
|
|
22
|
+
@state
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
CHECKPOINT_FIELDS = %i[
|
|
27
|
+
@position @line @column @bol @state @state_stack @pending @matched @match_start @match_end
|
|
28
|
+
@text_start @text_line @text_column @more_start @more_line @more_column @more_requested
|
|
29
|
+
@non_progress_signatures @eof_fired_states @halted @__flexr_leading_trivia
|
|
30
|
+
@__flexr_delegated_queue
|
|
31
|
+
@__flexr_indent_levels @__flexr_indent_brackets @__flexr_indent_queue @__flexr_indent_candidate @__flexr_indent_counting
|
|
32
|
+
].freeze
|
|
33
|
+
private_constant :CHECKPOINT_FIELDS
|
|
34
|
+
|
|
35
|
+
def checkpoint
|
|
36
|
+
@__flexr_checkpoint_owner ||= Object.new.freeze
|
|
37
|
+
state = CHECKPOINT_FIELDS.to_h do |field|
|
|
38
|
+
[field, copy_cursor_value(instance_variable_get(field))]
|
|
39
|
+
end
|
|
40
|
+
Checkpoint.send(:new, @__flexr_checkpoint_owner, object_id, @__flexr_input_id, state)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def restore(checkpoint)
|
|
44
|
+
raise ArgumentError, "expected a lexer checkpoint" unless checkpoint.instance_of?(Checkpoint)
|
|
45
|
+
|
|
46
|
+
state = checkpoint.__flexr_state(@__flexr_checkpoint_owner, object_id, @__flexr_input_id)
|
|
47
|
+
check_cursor_range!(state[:@more_start] || state.fetch(:@position))
|
|
48
|
+
check_cursor_range!(state.fetch(:@position))
|
|
49
|
+
state.each { |field, value| instance_variable_set(field, copy_cursor_value(value, freeze_copy: false)) }
|
|
50
|
+
self
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def seek(position)
|
|
54
|
+
raise ArgumentError, "byte position must be an Integer" unless position.is_a?(Integer)
|
|
55
|
+
|
|
56
|
+
check_cursor_range!(position)
|
|
57
|
+
raise ArgumentError, "seek must end at a UTF-8 codepoint boundary" unless seek_boundary?(position)
|
|
58
|
+
|
|
59
|
+
starting, line, column = seek_origin(position)
|
|
60
|
+
bytes = @buffer.byteslice(starting, position - starting).force_encoding(Encoding::BINARY)
|
|
61
|
+
if (newline = bytes.rindex("\n"))
|
|
62
|
+
line += bytes.count("\n")
|
|
63
|
+
column = 1
|
|
64
|
+
bytes = bytes.byteslice((newline + 1)..)
|
|
65
|
+
end
|
|
66
|
+
bytes.force_encoding(Encoding::UTF_8) if utf8_input?
|
|
67
|
+
column += bytes.length
|
|
68
|
+
|
|
69
|
+
@position = @match_start = @match_end = @text_start = position
|
|
70
|
+
@line = @text_line = line
|
|
71
|
+
@column = @text_column = column
|
|
72
|
+
@bol = column == 1
|
|
73
|
+
@matched = @pending = @active_rule = nil
|
|
74
|
+
@__flexr_delegated_queue = nil
|
|
75
|
+
@__flexr_leading_trivia = nil if defined?(@__flexr_leading_trivia)
|
|
76
|
+
@more_requested = @halted = false
|
|
77
|
+
finalize_more
|
|
78
|
+
@eof_fired_states.clear
|
|
79
|
+
@non_progress_signatures.clear
|
|
80
|
+
discard_consumed_input! unless @retain_input
|
|
81
|
+
self
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def copy_cursor_value(value, freeze_copy: true)
|
|
87
|
+
return value unless value.is_a?(Array) || value.is_a?(Hash) || value.is_a?(String)
|
|
88
|
+
|
|
89
|
+
copy = value.dup
|
|
90
|
+
freeze_copy ? copy.freeze : copy
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def check_cursor_range!(position)
|
|
94
|
+
raise RangeError, "byte #{position} is outside the buffered input" unless position.between?(0, @buffer.bytesize)
|
|
95
|
+
raise RangeError, "byte #{position} has been discarded from the streaming buffer" if position < @buffer.base_offset
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def seek_origin(position)
|
|
99
|
+
return [@position, @line, @column] if position >= @position
|
|
100
|
+
return [0, 1, 1] if @buffer.base_offset.zero?
|
|
101
|
+
return [@more_start, @more_line, @more_column] if @more_start && position >= @more_start
|
|
102
|
+
|
|
103
|
+
raise RangeError, "the coordinates of byte #{position} are no longer retained"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def seek_boundary?(position)
|
|
107
|
+
return true unless utf8_input? && position.positive?
|
|
108
|
+
return (@buffer.getbyte(position) & 0xc0) != 0x80 if position < @buffer.bytesize
|
|
109
|
+
return true if @buffer.eof_loaded? || position == @buffer.base_offset
|
|
110
|
+
|
|
111
|
+
start = position - 1
|
|
112
|
+
start -= 1 while start > @buffer.base_offset && position - start < 4 && (@buffer.getbyte(start) & 0xc0) == 0x80
|
|
113
|
+
@buffer.byteslice(start, position - start).force_encoding(Encoding::UTF_8).valid_encoding?
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
data/lib/flexr/runtime/core.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Flexr
|
|
4
4
|
module Runtime
|
|
5
|
+
EMPTY_CAPTURES = {}.freeze
|
|
6
|
+
|
|
5
7
|
def initialize(input, filename: nil, error_mode: :raise, max_token_size: 16 * 1024 * 1024,
|
|
6
8
|
max_lookahead_size: nil, max_buffer_size: 64 * 1024 * 1024,
|
|
7
9
|
max_state_stack: 1024, max_steps: nil, cancellation: nil, retain_input: true,
|
|
@@ -20,48 +22,19 @@ module Flexr
|
|
|
20
22
|
input, chunk_size: chunk_size, max_buffer_size: max_buffer_size,
|
|
21
23
|
retain_input: retain_input, filename: filename
|
|
22
24
|
)
|
|
23
|
-
@string_input = input.is_a?(String)
|
|
24
|
-
@stable_input_end = input.bytesize if @string_input && input.frozen?
|
|
25
|
-
@valid_utf8_input = @string_input && input.frozen? && (config.encoding != Encoding::UTF_8 || begin
|
|
26
|
-
bytes = input.encoding == Encoding::UTF_8 ? input : input.dup.force_encoding(Encoding::UTF_8)
|
|
27
|
-
bytes.valid_encoding?
|
|
28
|
-
end)
|
|
29
|
-
@simple_location_input = @string_input && input.frozen? && retain_input &&
|
|
30
|
-
input.ascii_only? && !input.include?("\n")
|
|
31
25
|
@retain_input = retain_input
|
|
32
26
|
@filename = filename
|
|
33
27
|
@error_mode = error_mode
|
|
34
28
|
@token_kind = config.token_kind
|
|
35
29
|
@accel_mode = config.options.fetch(:accel, :auto)
|
|
36
30
|
@max_token_size = max_token_size.to_i
|
|
37
|
-
@token_limit_required = !@string_input || !input.frozen? || input.bytesize > @max_token_size
|
|
38
31
|
@max_lookahead_size = (max_lookahead_size || max_token_size).to_i
|
|
39
32
|
@max_state_stack = max_state_stack.to_i
|
|
40
33
|
@max_steps = max_steps&.to_i
|
|
41
34
|
@cancellation = cancellation.respond_to?(:arity) ? cancellation : cancellation&.method(:call)
|
|
42
35
|
@steps = 0
|
|
43
|
-
|
|
44
|
-
@line = 1
|
|
45
|
-
@column = 1
|
|
46
|
-
@state = :initial
|
|
47
|
-
@state_stack = []
|
|
48
|
-
@pending = nil
|
|
49
|
-
@matched = nil
|
|
50
|
-
@match_start = 0
|
|
51
|
-
@match_end = 0
|
|
52
|
-
@text_start = 0
|
|
53
|
-
@text_line = 1
|
|
54
|
-
@text_column = 1
|
|
55
|
-
@bol = true
|
|
56
|
-
@more_start = nil
|
|
57
|
-
@more_line = nil
|
|
58
|
-
@more_column = nil
|
|
59
|
-
@more_requested = false
|
|
60
|
-
@non_progress_signatures = {}
|
|
61
|
-
@active_rule = nil
|
|
62
|
-
@eof_fired_states = {}
|
|
36
|
+
reset_input_cursor(input, config.encoding)
|
|
63
37
|
@on_error = nil
|
|
64
|
-
@halted = false
|
|
65
38
|
@interpreter = nil
|
|
66
39
|
@generated_scanner = generated_runtime? && respond_to?(:scan_one, true)
|
|
67
40
|
end
|
|
@@ -74,8 +47,11 @@ module Flexr
|
|
|
74
47
|
@buffer.source
|
|
75
48
|
end
|
|
76
49
|
|
|
77
|
-
def next_token
|
|
50
|
+
def next_token(expect: nil)
|
|
51
|
+
expected = Configuration.expected_tokens!(expect).to_h { |type| [type, true] } unless expect.nil?
|
|
52
|
+
|
|
78
53
|
until @halted
|
|
54
|
+
activate_pending_inputs if @__flexr_pending_inputs
|
|
79
55
|
consume_step! if scan_steps_guarded?
|
|
80
56
|
|
|
81
57
|
if eof? && @pending.nil?
|
|
@@ -94,14 +70,19 @@ module Flexr
|
|
|
94
70
|
return token if token
|
|
95
71
|
next
|
|
96
72
|
end
|
|
73
|
+
if @__flexr_input_frames
|
|
74
|
+
return nil if defer_input_resume?
|
|
75
|
+
|
|
76
|
+
resume_suspended_input
|
|
77
|
+
next
|
|
78
|
+
end
|
|
97
79
|
return nil
|
|
98
80
|
end
|
|
99
81
|
|
|
100
|
-
|
|
101
|
-
match = if @generated_scanner
|
|
82
|
+
match = if @generated_scanner && !expected
|
|
102
83
|
scan_one
|
|
103
84
|
else
|
|
104
|
-
(@interpreter ||= Runtime::Interpreter.new(self)).scan
|
|
85
|
+
(@interpreter ||= Runtime::Interpreter.new(self)).scan(expected)
|
|
105
86
|
end
|
|
106
87
|
unless match
|
|
107
88
|
unless eof?
|
|
@@ -129,13 +110,21 @@ module Flexr
|
|
|
129
110
|
@position = match.end_pos
|
|
130
111
|
empty_match = match.end_pos == match.start_pos
|
|
131
112
|
@active_rule = match.rule
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
113
|
+
begin
|
|
114
|
+
if match.capture
|
|
115
|
+
execute_with_captures(match)
|
|
116
|
+
else
|
|
117
|
+
execute(match.rule)
|
|
118
|
+
end
|
|
119
|
+
finalize_more
|
|
120
|
+
ensure_progress!(match, scan_state, empty_match: empty_match) unless
|
|
121
|
+
@position > match.start_pos && @non_progress_signatures.empty?
|
|
122
|
+
ensure_token_size! if @token_limit_required
|
|
123
|
+
update_position
|
|
124
|
+
discard_consumed_input! unless @retain_input
|
|
125
|
+
ensure
|
|
126
|
+
@active_rule = nil
|
|
127
|
+
end
|
|
139
128
|
token = @pending
|
|
140
129
|
@pending = nil
|
|
141
130
|
return token if token
|
|
@@ -170,8 +159,8 @@ module Flexr
|
|
|
170
159
|
result
|
|
171
160
|
end
|
|
172
161
|
|
|
173
|
-
def racc_next_token
|
|
174
|
-
token = next_token
|
|
162
|
+
def racc_next_token(expect: nil)
|
|
163
|
+
token = expect.nil? ? next_token : next_token(expect: expect)
|
|
175
164
|
token ? [token[0], token[1]] : [false, "$end"]
|
|
176
165
|
end
|
|
177
166
|
|
|
@@ -181,6 +170,8 @@ module Flexr
|
|
|
181
170
|
@matched = @buffer.byteslice(@text_start, @match_end - @text_start)
|
|
182
171
|
end
|
|
183
172
|
|
|
173
|
+
def captures = EMPTY_CAPTURES
|
|
174
|
+
|
|
184
175
|
def text_bytesize
|
|
185
176
|
@match_end - @text_start
|
|
186
177
|
end
|
|
@@ -410,6 +401,27 @@ module Flexr
|
|
|
410
401
|
|
|
411
402
|
private
|
|
412
403
|
|
|
404
|
+
def reset_input_cursor(input, encoding)
|
|
405
|
+
@string_input = input.is_a?(String)
|
|
406
|
+
@stable_input_end = @string_input && input.frozen? ? input.bytesize : nil
|
|
407
|
+
@valid_utf8_input = @stable_input_end && (encoding != Encoding::UTF_8 || begin
|
|
408
|
+
bytes = input.encoding == Encoding::UTF_8 ? input : input.dup.force_encoding(Encoding::UTF_8)
|
|
409
|
+
bytes.valid_encoding?
|
|
410
|
+
end)
|
|
411
|
+
@simple_location_input = @stable_input_end && @retain_input && input.ascii_only? && !input.include?("\n")
|
|
412
|
+
@token_limit_required = !@stable_input_end || @stable_input_end > @max_token_size
|
|
413
|
+
@position = @match_start = @match_end = @text_start = 0
|
|
414
|
+
@line = @column = @text_line = @text_column = 1
|
|
415
|
+
@state = :initial
|
|
416
|
+
@state_stack = []
|
|
417
|
+
@pending = @matched = @active_rule = @__flexr_leading_trivia = nil
|
|
418
|
+
@bol = true
|
|
419
|
+
@more_start = @more_line = @more_column = nil
|
|
420
|
+
@more_requested = @halted = false
|
|
421
|
+
@non_progress_signatures = {}
|
|
422
|
+
@eof_fired_states = {}
|
|
423
|
+
end
|
|
424
|
+
|
|
413
425
|
def execute(rule)
|
|
414
426
|
return __flexr_generated_execute(rule) if generated_runtime? && respond_to?(:__flexr_generated_execute, true)
|
|
415
427
|
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
module DelegatedTokens
|
|
6
|
+
def next_token(...)
|
|
7
|
+
return super if @halted || !@__flexr_delegated_queue || (@__flexr_indent_queue && !@__flexr_indent_queue.empty?)
|
|
8
|
+
|
|
9
|
+
token = @__flexr_delegated_queue.shift
|
|
10
|
+
@__flexr_delegated_queue = nil if @__flexr_delegated_queue.empty?
|
|
11
|
+
token
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
MAX_DELEGATION_DEPTH = 32
|
|
16
|
+
private_constant :MAX_DELEGATION_DEPTH
|
|
17
|
+
|
|
18
|
+
def delegate(lexer_class, capture:)
|
|
19
|
+
raise ArgumentError, "delegation requires a named capture action" unless @capture_match
|
|
20
|
+
raise ArgumentError, "delegation requires a default-channel parent rule" if @capture_match.rule.channel
|
|
21
|
+
raise ArgumentError, "delegate requires a Flexr::Lexer subclass" unless lexer_class.is_a?(Class) && lexer_class < Lexer
|
|
22
|
+
raise ArgumentError, "capture: requires a named capture Symbol" unless capture.is_a?(Symbol)
|
|
23
|
+
|
|
24
|
+
config = lexer_class.__flexr_config
|
|
25
|
+
raise ArgumentError, "delegated lexers must use the parent encoding" if config.encoding != self.class.__flexr_config.encoding
|
|
26
|
+
raise ArgumentError, "a struct parent requires a struct child lexer" if @token_kind == :struct && config.token_kind != :struct
|
|
27
|
+
|
|
28
|
+
starting, ending = delegated_capture_range(capture)
|
|
29
|
+
return unless starting
|
|
30
|
+
raise ArgumentError, "delegated capture is outside the current match" if ending > @match_end
|
|
31
|
+
|
|
32
|
+
line, column = location_after(@capture_match.start_pos, starting, line: @line, column: @column)
|
|
33
|
+
source = @buffer.byteslice(starting, ending - starting).freeze
|
|
34
|
+
tokens = delegated_tokens(lexer_class, source, [starting, line, column, source.bytesize])
|
|
35
|
+
(@__flexr_delegation_tokens ||= []).concat(tokens)
|
|
36
|
+
@__flexr_delegation_end = [@__flexr_delegation_end || ending, ending].max
|
|
37
|
+
nil
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
def delegated_capture_range(capture)
|
|
43
|
+
starting, ending = capture_data.byteoffset(capture)
|
|
44
|
+
return unless starting
|
|
45
|
+
|
|
46
|
+
origin = @capture_match.start_pos
|
|
47
|
+
[origin + starting, origin + ending]
|
|
48
|
+
rescue IndexError
|
|
49
|
+
raise ArgumentError, "unknown delegated capture #{capture.inspect}"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def delegated_tokens(lexer_class, source, origin)
|
|
53
|
+
depth = @__flexr_delegation_depth.to_i + 1
|
|
54
|
+
if depth > MAX_DELEGATION_DEPTH
|
|
55
|
+
raise LexError.new("lexer delegation exceeds #{MAX_DELEGATION_DEPTH} levels of nesting",
|
|
56
|
+
filename: @filename, byte_pos: origin[0], line: origin[1])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
child = lexer_class.new(source, filename: @filename, error_mode: @error_mode, max_token_size: @max_token_size,
|
|
60
|
+
max_buffer_size: @buffer.max_buffer_size, max_lookahead_size: @max_lookahead_size,
|
|
61
|
+
max_state_stack: @max_state_stack, max_steps: @max_steps && (@max_steps - @steps),
|
|
62
|
+
cancellation: @cancellation && -> { cancelled? })
|
|
63
|
+
child.instance_variable_set(:@__flexr_delegation_depth, depth)
|
|
64
|
+
tokens = []
|
|
65
|
+
while (token = child.next_token)
|
|
66
|
+
tokens << (@token_kind == :struct ? rebase_delegated_token(token, origin) : [token[0], token[1]])
|
|
67
|
+
end
|
|
68
|
+
tokens
|
|
69
|
+
rescue LexError => e
|
|
70
|
+
raise unless child
|
|
71
|
+
|
|
72
|
+
raise rebase_delegated_error(e, origin)
|
|
73
|
+
ensure
|
|
74
|
+
@steps += child.steps if child
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def rebase_delegated_token(token, origin)
|
|
78
|
+
raise ArgumentError, "delegation requires positioned child tokens" unless token.is_a?(Token) && token.location
|
|
79
|
+
|
|
80
|
+
source = token.location
|
|
81
|
+
byte, line, column, size = origin
|
|
82
|
+
raise ArgumentError, "delegated token location is outside the captured input" unless
|
|
83
|
+
source.filename == @filename && source.byte_begin.between?(0, size) && source.byte_end.between?(source.byte_begin, size)
|
|
84
|
+
|
|
85
|
+
location = Location.new(filename: @filename, byte_begin: byte + source.byte_begin, byte_end: byte + source.byte_end,
|
|
86
|
+
line_begin: line + source.line_begin - 1, line_end: line + source.line_end - 1,
|
|
87
|
+
column_begin: source.column_begin + (source.line_begin == 1 ? column - 1 : 0),
|
|
88
|
+
column_end: source.column_end + (source.line_end == 1 ? column - 1 : 0))
|
|
89
|
+
changes = { location: location }
|
|
90
|
+
changes[:leading_trivia] = token.leading_trivia.map { |item| rebase_delegated_token(item, origin) } unless token.leading_trivia.empty?
|
|
91
|
+
token.with(**changes)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def rebase_delegated_error(error, origin)
|
|
95
|
+
byte, line = origin
|
|
96
|
+
rebased = error.dup
|
|
97
|
+
rebased.instance_variable_set(:@filename, @filename)
|
|
98
|
+
rebased.instance_variable_set(:@byte_pos, error.byte_pos && (byte + error.byte_pos))
|
|
99
|
+
rebased.instance_variable_set(:@line, error.line && (line + error.line - 1))
|
|
100
|
+
if (diagnostic = error.diagnostic) && diagnostic.location
|
|
101
|
+
diagnostic = diagnostic.dup
|
|
102
|
+
diagnostic.location = diagnostic.location.map { |position| byte + position }
|
|
103
|
+
rebased.instance_variable_set(:@diagnostic, diagnostic)
|
|
104
|
+
end
|
|
105
|
+
rebased
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def finish_delegation
|
|
109
|
+
raise ArgumentError, "delegated capture is outside the current match" if @__flexr_delegation_end > @match_end
|
|
110
|
+
|
|
111
|
+
tokens = @__flexr_delegation_tokens
|
|
112
|
+
return if @halted || tokens.empty?
|
|
113
|
+
|
|
114
|
+
tokens << @pending if @pending
|
|
115
|
+
@__flexr_delegated_location = last_location if @__flexr_indentation
|
|
116
|
+
@pending = tokens.shift
|
|
117
|
+
return if tokens.empty?
|
|
118
|
+
|
|
119
|
+
@__flexr_delegated_queue = tokens
|
|
120
|
+
extend DelegatedTokens unless is_a?(DelegatedTokens)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
module Runtime
|
|
5
|
+
def scan_until(delimiter)
|
|
6
|
+
scan_delimiter(delimiter, full_line: false)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def scan_until_line(delimiter)
|
|
10
|
+
scan_delimiter(delimiter, full_line: true)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def scan_delimiter(delimiter, full_line:)
|
|
16
|
+
raise ArgumentError, "delimiter scanning requires a matched rule action" unless @active_rule
|
|
17
|
+
raise ArgumentError, "delimiter must be a nonempty String" unless delimiter.is_a?(String) && !delimiter.empty?
|
|
18
|
+
|
|
19
|
+
delimiter = delimiter.b
|
|
20
|
+
raise ArgumentError, "line delimiter must be free of newline bytes" if
|
|
21
|
+
full_line && (delimiter.include?("\n") || delimiter.include?("\r"))
|
|
22
|
+
raise ArgumentError, "delimiter must be valid UTF-8" if
|
|
23
|
+
utf8_input? && !delimiter.dup.force_encoding(Encoding::UTF_8).valid_encoding?
|
|
24
|
+
|
|
25
|
+
size = delimiter.bytesize
|
|
26
|
+
fallbacks = delimiter_fallbacks(delimiter, size)
|
|
27
|
+
guarded = scan_steps_guarded?
|
|
28
|
+
cursor = @position
|
|
29
|
+
matched = 0
|
|
30
|
+
while (byte = @buffer.getbyte(cursor))
|
|
31
|
+
consume_step! if guarded
|
|
32
|
+
matched = advance_delimiter_prefix(delimiter, fallbacks, matched, byte)
|
|
33
|
+
cursor += 1
|
|
34
|
+
ensure_delimiter_extent!(cursor)
|
|
35
|
+
next unless matched == size
|
|
36
|
+
|
|
37
|
+
starting = cursor - size
|
|
38
|
+
ending = delimiter_end(starting, cursor, full_line: full_line)
|
|
39
|
+
if ending
|
|
40
|
+
ensure_delimiter_extent!(ending)
|
|
41
|
+
consume_step!(ending - cursor) if guarded && ending > cursor
|
|
42
|
+
body = @buffer.byteslice(@position, starting - @position)
|
|
43
|
+
@position = @match_end = ending
|
|
44
|
+
@matched = nil
|
|
45
|
+
return body
|
|
46
|
+
end
|
|
47
|
+
matched = fallbacks.fetch(matched - 1)
|
|
48
|
+
end
|
|
49
|
+
nil
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def delimiter_fallbacks(delimiter, size)
|
|
53
|
+
fallbacks = Array.new(size, 0)
|
|
54
|
+
matched = 0
|
|
55
|
+
(1...size).each do |index|
|
|
56
|
+
matched = advance_delimiter_prefix(delimiter, fallbacks, matched, delimiter.getbyte(index))
|
|
57
|
+
fallbacks[index] = matched
|
|
58
|
+
end
|
|
59
|
+
fallbacks
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def advance_delimiter_prefix(delimiter, fallbacks, matched, byte)
|
|
63
|
+
matched = fallbacks.fetch(matched - 1) while matched.positive? && delimiter.getbyte(matched) != byte
|
|
64
|
+
matched += 1 if delimiter.getbyte(matched) == byte
|
|
65
|
+
matched
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def delimiter_end(starting, ending, full_line:)
|
|
69
|
+
unless full_line
|
|
70
|
+
return ending if utf8_boundary?(starting) && utf8_boundary?(ending)
|
|
71
|
+
|
|
72
|
+
return
|
|
73
|
+
end
|
|
74
|
+
return unless __flexr_beginning_of_line_at?(starting, @match_start)
|
|
75
|
+
|
|
76
|
+
case @buffer.getbyte(ending)
|
|
77
|
+
when nil then ending
|
|
78
|
+
when 0x0a then ending + 1
|
|
79
|
+
when 0x0d then ending + 2 if @buffer.getbyte(ending + 1) == 0x0a
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def ensure_delimiter_extent!(ending)
|
|
84
|
+
return if ending - @text_start <= @max_token_size
|
|
85
|
+
|
|
86
|
+
raise TokenTooLargeError.new(filename: @filename, byte_pos: @text_start, line: @text_line, rule: @active_rule)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|