flexr 1.1.0 → 1.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/.rubocop.yml +1 -0
- data/CHANGELOG.md +116 -0
- data/CONTRIBUTING.md +13 -1
- data/README.md +6 -2
- data/Rakefile +41 -5
- data/Steepfile +7 -0
- data/benchmark/golden/calculator_lexer.rb +276 -0
- data/benchmark/golden/graphql_lexer.rb +310 -0
- data/benchmark/golden/json_lexer.rb +292 -0
- data/benchmark/golden/regexp_tokenizer.rb +293 -0
- data/benchmark/golden/ruby_subset_lexer.rb +280 -0
- data/benchmark/golden/sql_lexer.rb +293 -0
- data/benchmark/golden/toml_lexer.rb +292 -0
- data/benchmark/golden/toy_lang_lexer.rb +273 -0
- data/benchmark/golden/with_lrama_lexer.rb +264 -0
- data/benchmark/golden/with_racc_lexer.rb +264 -0
- data/benchmark/lrama_lexer.rb +165 -0
- data/benchmark/matrix.rb +79 -0
- data/benchmark/profile.rb +109 -0
- data/benchmark/run.rb +4 -1
- data/benchmark/state_dispatch.rb +78 -0
- data/docs/README.md +1 -2
- data/docs/RELEASING.md +9 -2
- data/docs/migration-v2.md +73 -0
- data/docs/perf-log.md +108 -0
- data/docs/reference/README.md +2 -0
- data/docs/reference/cli.md +71 -5
- data/docs/reference/compatibility.md +9 -2
- data/docs/reference/diagnostics.md +338 -0
- data/docs/reference/dsl.md +8 -5
- data/docs/reference/errors.md +28 -0
- data/docs/reference/generated-artifacts.md +11 -0
- data/docs/reference/public-api.md +32 -8
- data/docs/reference/regexp.md +19 -7
- data/docs/reference/ruby-lsp.md +34 -0
- data/docs/reference/testing.md +57 -0
- data/examples/graphql/README.md +14 -0
- data/examples/graphql/lexer.flexr.rb +29 -0
- data/examples/sql/README.md +16 -0
- data/examples/sql/lexer.flexr.rb +23 -0
- data/examples/toml/README.md +14 -0
- data/examples/toml/lexer.flexr.rb +23 -0
- data/gemfiles/ruby_lsp.gemfile +4 -0
- data/gemfiles/types.gemfile +7 -0
- data/lib/flexr/automaton/accel.rb +11 -1
- data/lib/flexr/automaton/analysis.rb +161 -7
- data/lib/flexr/automaton/compiler.rb +175 -24
- data/lib/flexr/cli.rb +117 -17
- data/lib/flexr/codegen/firstmatch.rb +1 -7
- data/lib/flexr/codegen/table.rb +2 -2
- data/lib/flexr/configuration.rb +8 -0
- data/lib/flexr/deprecations.rb +26 -0
- data/lib/flexr/diagnostics.rb +39 -3
- data/lib/flexr/dsl.rb +11 -9
- data/lib/flexr/errors.rb +6 -6
- data/lib/flexr/generated.rb +27 -15
- data/lib/flexr/generator.rb +44 -29
- data/lib/flexr/grammar.rb +41 -0
- data/lib/flexr/ir.rb +2 -2
- data/lib/flexr/migration.rb +31 -0
- data/lib/flexr/minitest.rb +24 -0
- data/lib/flexr/options.rb +4 -2
- data/lib/flexr/regexp/matcher.rb +26 -0
- data/lib/flexr/regexp/normalizer.rb +4 -8
- data/lib/flexr/regexp/parser.rb +127 -21
- data/lib/flexr/regexp/tokenizer.rb +4 -4
- data/lib/flexr/rspec.rb +51 -0
- data/lib/flexr/runtime/core.rb +21 -15
- data/lib/flexr/runtime/errors.rb +6 -4
- data/lib/flexr/runtime/interpreter.rb +47 -45
- data/lib/flexr/runtime/location.rb +5 -4
- data/lib/flexr/runtime/token.rb +1 -0
- data/lib/flexr/runtime.rb +6 -2
- data/lib/flexr/source/passthrough.rb +14 -11
- data/lib/flexr/source/prism_reader.rb +45 -14
- data/lib/flexr/testing.rb +47 -0
- data/lib/flexr/trace.rb +197 -0
- data/lib/flexr/unicode/property.rb +1 -2
- data/lib/flexr/unicode/reference_regexp.rb +8 -9
- data/lib/flexr/version.rb +1 -1
- data/lib/flexr.rb +1 -0
- data/lib/ruby_lsp/flexr/addon.rb +114 -0
- data/sig/flexr.rbs +185 -0
- data/site/src/layouts/SiteLayout.astro +8 -2
- data/site/src/pages/index.astro +69 -155
- data/site/src/styles/custom.css +4 -287
- data/site/src/styles/landing.css +239 -0
- data/test/ruby_lsp_addon_spec.rb +135 -0
- data/tools/docs_verify.rb +1 -0
- data/tools/golden.rb +54 -0
- data/tools/release_notes.rb +16 -0
- metadata +44 -11
- data/benchmark/golden/calculator_lexer.sha256 +0 -1
- data/benchmark/golden/json_lexer.sha256 +0 -1
- data/benchmark/golden/regexp_tokenizer.sha256 +0 -1
- data/benchmark/golden/ruby_subset_lexer.sha256 +0 -1
- data/benchmark/golden/toy_lang_lexer.sha256 +0 -1
- data/benchmark/golden/with_lrama_lexer.sha256 +0 -1
- data/benchmark/golden/with_racc_lexer.sha256 +0 -1
- data/docs/releases/v1.1.0.md +0 -44
data/lib/flexr/trace.rb
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "runtime" unless defined?(Flexr::Lexer)
|
|
4
|
+
|
|
5
|
+
module Flexr
|
|
6
|
+
class Trace
|
|
7
|
+
module Actions
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def execute(rule)
|
|
11
|
+
@flexr_trace.record(rule) { super }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
public
|
|
15
|
+
|
|
16
|
+
def instance_exec(*args, &block)
|
|
17
|
+
if block.equal?(self.class.__flexr_spec.eof_rules[state])
|
|
18
|
+
@flexr_trace.record_eof { super }
|
|
19
|
+
else
|
|
20
|
+
super
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def handle_unmatched_byte
|
|
27
|
+
@flexr_trace.record_unmatched { super }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
attr_reader :steps
|
|
32
|
+
|
|
33
|
+
def initialize(lexer)
|
|
34
|
+
@lexer = lexer
|
|
35
|
+
@steps = []
|
|
36
|
+
@config = lexer.class.__flexr_config
|
|
37
|
+
@compiled = lexer.class.__flexr_compiled
|
|
38
|
+
@rules = lexer.class.__flexr_rules
|
|
39
|
+
@lexer.singleton_class.prepend(Actions)
|
|
40
|
+
@lexer.instance_variable_set(:@flexr_trace, self)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def run
|
|
44
|
+
@lexer.tokens
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def record(rule)
|
|
48
|
+
start = @lexer.instance_variable_get(:@match_start)
|
|
49
|
+
ending = @lexer.instance_variable_get(:@match_end)
|
|
50
|
+
state = @lexer.state
|
|
51
|
+
candidates = candidates_at(start)
|
|
52
|
+
selected = candidates.find { |candidate| candidate[:rule] == rule.index }
|
|
53
|
+
reason = if @config.backend == :firstmatch
|
|
54
|
+
"first_match"
|
|
55
|
+
elsif candidates.length == 1
|
|
56
|
+
"only_match"
|
|
57
|
+
elsif selected && candidates.count { |candidate| candidate[:total_length] == selected[:total_length] } > 1
|
|
58
|
+
"rule_order"
|
|
59
|
+
else
|
|
60
|
+
"longest"
|
|
61
|
+
end
|
|
62
|
+
yield
|
|
63
|
+
pending = @lexer.instance_variable_get(:@pending)
|
|
64
|
+
bytes = @lexer.buffer.byteslice(start...ending)
|
|
65
|
+
@steps << { byte_begin: start, byte_end: ending, next_byte: @lexer.byte_pos, state: state, next_state: @lexer.state,
|
|
66
|
+
text: display_text(bytes), bytes: bytes.bytes, candidates: candidates,
|
|
67
|
+
rule: rule.index, patterns: rule.patterns.map(&:inspect), reason: reason,
|
|
68
|
+
action: pending ? "emit" : "skip", token: pending&.inspect }
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def record_eof
|
|
72
|
+
position = @lexer.byte_pos
|
|
73
|
+
state = @lexer.state
|
|
74
|
+
yield
|
|
75
|
+
pending = @lexer.instance_variable_get(:@pending)
|
|
76
|
+
@steps << { byte_begin: position, byte_end: position, next_byte: @lexer.byte_pos,
|
|
77
|
+
state: state, next_state: @lexer.state, text: "", bytes: [], candidates: [],
|
|
78
|
+
rule: nil, patterns: [], reason: "eof", action: pending ? "emit" : "skip", token: pending&.inspect }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def record_unmatched
|
|
82
|
+
position = @lexer.byte_pos
|
|
83
|
+
state = @lexer.state
|
|
84
|
+
bytes = @lexer.buffer.byteslice(position, 1)
|
|
85
|
+
token = yield
|
|
86
|
+
@steps << { byte_begin: position, byte_end: position + 1, next_byte: @lexer.byte_pos,
|
|
87
|
+
state: state, next_state: @lexer.state, text: display_text(bytes), bytes: bytes.bytes,
|
|
88
|
+
candidates: [], rule: nil, patterns: [], reason: "unmatched",
|
|
89
|
+
action: token ? "emit" : "skip", token: token&.inspect }
|
|
90
|
+
token
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def render
|
|
94
|
+
steps.map do |step|
|
|
95
|
+
candidates = step[:candidates].map do |candidate|
|
|
96
|
+
"rule #{candidate[:rule]}(#{candidate[:length]}/#{candidate[:total_length]})"
|
|
97
|
+
end.join(" ")
|
|
98
|
+
target = step[:rule] ? "rule #{step[:rule]}" : step[:reason]
|
|
99
|
+
"@#{step[:byte_begin]} #{step[:text].inspect} state=#{step[:state]} " \
|
|
100
|
+
"candidates: #{candidates} -> #{target} #{step[:action]} (#{step[:reason]}) " \
|
|
101
|
+
"state=#{step[:next_state]}#{step[:token] && " #{step[:token]}"}"
|
|
102
|
+
end.join("\n")
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def candidates_at(start)
|
|
108
|
+
@config.backend == :firstmatch ? firstmatch_candidates(start) : dfa_candidates(start)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def dfa_candidates(start)
|
|
112
|
+
dfa = @compiled.machines.fetch(@lexer.state).dfa
|
|
113
|
+
best = {}
|
|
114
|
+
walk(dfa, start) do |acceptance, ending|
|
|
115
|
+
next if acceptance.bol_only && !@lexer.beginning_of_line?
|
|
116
|
+
|
|
117
|
+
rule = @rules.fetch(acceptance.rule_index)
|
|
118
|
+
candidate = candidate_for(rule, start, ending)
|
|
119
|
+
next unless candidate
|
|
120
|
+
previous = best[rule.index]
|
|
121
|
+
best[rule.index] = candidate if !previous || candidate[:total_length] >= previous[:total_length]
|
|
122
|
+
end
|
|
123
|
+
best.values.sort_by { |candidate| candidate[:rule] }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def firstmatch_candidates(start)
|
|
127
|
+
state = @config.states.fetch(@lexer.state)
|
|
128
|
+
@lexer.buffer.read_to_end
|
|
129
|
+
ending = @lexer.buffer.bytesize
|
|
130
|
+
if @config.encoding == Encoding::UTF_8
|
|
131
|
+
ending = start
|
|
132
|
+
ending += @lexer.buffer.utf8_character_length(ending) while
|
|
133
|
+
ending < @lexer.buffer.bytesize && @lexer.buffer.valid_utf8_at?(ending)
|
|
134
|
+
end
|
|
135
|
+
subject = @lexer.buffer.byteslice(start...ending).to_s
|
|
136
|
+
subject.force_encoding(@config.encoding)
|
|
137
|
+
@rules.filter_map do |rule|
|
|
138
|
+
next unless rule.states.include?(@lexer.state) || (state.inclusive && rule.states.include?(:initial))
|
|
139
|
+
matches = rule.patterns.each_with_index.filter_map do |pattern, index|
|
|
140
|
+
condition = rule.pattern_conditions.fetch(index)
|
|
141
|
+
next if condition.bol_only && !@lexer.beginning_of_line?
|
|
142
|
+
regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(::Regexp.escape(pattern))
|
|
143
|
+
reference = Regexp::Matcher.reference?(regexp, encoding: @config.encoding, unicode: @config.options[:unicode] == true)
|
|
144
|
+
match = Regexp::Matcher.match(regexp, subject, encoding: @config.encoding,
|
|
145
|
+
unicode: @config.options[:unicode] == true, reference: reference)
|
|
146
|
+
[match[0].bytesize, condition] if match&.begin(0)&.zero?
|
|
147
|
+
end
|
|
148
|
+
length, condition = matches.max_by(&:first)
|
|
149
|
+
next unless length
|
|
150
|
+
next if condition.end_anchor && !end_anchor?(start + length)
|
|
151
|
+
|
|
152
|
+
candidate_for(rule, start, start + length)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def candidate_for(rule, start, ending)
|
|
157
|
+
trailing = if rule.trailing
|
|
158
|
+
dfa = rule.trailing_dfa || (@trailing_dfas ||= {})[rule.index] ||= Flexr.compile_pattern(
|
|
159
|
+
rule.trailing, encoding: @config.encoding, options: { unicode: @config.options[:unicode] == true }
|
|
160
|
+
)
|
|
161
|
+
longest = nil
|
|
162
|
+
walk(dfa, ending) { |_acceptance, cursor| longest = cursor - ending }
|
|
163
|
+
longest
|
|
164
|
+
else
|
|
165
|
+
0
|
|
166
|
+
end
|
|
167
|
+
return if trailing.nil?
|
|
168
|
+
|
|
169
|
+
{ rule: rule.index, patterns: rule.patterns.map(&:inspect), length: ending - start,
|
|
170
|
+
trailing_length: trailing, total_length: ending - start + trailing }
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def walk(dfa, start)
|
|
174
|
+
state = dfa.start
|
|
175
|
+
cursor = start
|
|
176
|
+
loop do
|
|
177
|
+
if @lexer.utf8_boundary?(cursor)
|
|
178
|
+
dfa.accepts[state].each do |acceptance|
|
|
179
|
+
yield acceptance, cursor unless acceptance.end_anchor && !end_anchor?(cursor)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
break unless @lexer.buffer.ensure_available?(cursor + 1)
|
|
183
|
+
break unless (state = dfa.transition(state, @lexer.buffer.getbyte(cursor)))
|
|
184
|
+
|
|
185
|
+
cursor += 1
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def end_anchor?(position)
|
|
190
|
+
@lexer.buffer.eof?(position) || @lexer.buffer.getbyte(position) == 0x0a
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def display_text(bytes)
|
|
194
|
+
bytes.dup.force_encoding(Encoding::UTF_8).scrub
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
|
@@ -13,9 +13,8 @@ module Flexr
|
|
|
13
13
|
"Lower" => "Lowercase", "Lowercase" => "Lowercase",
|
|
14
14
|
"Upper" => "Uppercase", "Uppercase" => "Uppercase"
|
|
15
15
|
}.freeze
|
|
16
|
-
# rubocop:disable Style/MutableConstant
|
|
16
|
+
# rubocop:disable-next Style/MutableConstant
|
|
17
17
|
CACHE = {}
|
|
18
|
-
# rubocop:enable Style/MutableConstant
|
|
19
18
|
module_function
|
|
20
19
|
|
|
21
20
|
def ranges(name, negate: false)
|
|
@@ -3,9 +3,8 @@
|
|
|
3
3
|
module Flexr
|
|
4
4
|
module Unicode
|
|
5
5
|
module ReferenceRegexp
|
|
6
|
-
# rubocop:disable Style/MutableConstant
|
|
6
|
+
# rubocop:disable-next Style/MutableConstant
|
|
7
7
|
CACHE = {}
|
|
8
|
-
# rubocop:enable Style/MutableConstant
|
|
9
8
|
module_function
|
|
10
9
|
|
|
11
10
|
def match(pattern, subject, encoding:, options: 0, unicode: false)
|
|
@@ -22,12 +21,12 @@ module Flexr
|
|
|
22
21
|
return CACHE[key] if CACHE.key?(key)
|
|
23
22
|
|
|
24
23
|
parser = Regexp::Parser.new(pattern.source, options: effective_options, encoding: encoding, unicode: unicode)
|
|
25
|
-
source = source_for(parser.parse
|
|
24
|
+
source = source_for(parser.parse)
|
|
26
25
|
regexp_options = effective_options & ~::Regexp::IGNORECASE
|
|
27
26
|
CACHE[key] = ::Regexp.new(source, regexp_options).freeze
|
|
28
27
|
end
|
|
29
28
|
|
|
30
|
-
def source_for(node
|
|
29
|
+
def source_for(node)
|
|
31
30
|
case node
|
|
32
31
|
when Regexp::AST::Empty, Regexp::AST::Anchor then ""
|
|
33
32
|
when Regexp::AST::Fail then "(?!)"
|
|
@@ -37,7 +36,7 @@ module Flexr
|
|
|
37
36
|
ranges = node.ranges.flat_map do |range|
|
|
38
37
|
if range.first == Regexp::AST::Property
|
|
39
38
|
property_ranges = Unicode::Property.ranges(range[2])
|
|
40
|
-
property_ranges = casefold_ranges(property_ranges) if
|
|
39
|
+
property_ranges = casefold_ranges(property_ranges) if range[3]
|
|
41
40
|
range[1] ? complement(property_ranges) : property_ranges
|
|
42
41
|
else
|
|
43
42
|
[range]
|
|
@@ -45,13 +44,13 @@ module Flexr
|
|
|
45
44
|
end
|
|
46
45
|
ranges = complement(ranges) if node.negated
|
|
47
46
|
codepoint_class(ranges)
|
|
48
|
-
when Regexp::AST::Seq then node.children.map { |child| source_for(child
|
|
47
|
+
when Regexp::AST::Seq then node.children.map { |child| source_for(child) }.join
|
|
49
48
|
when Regexp::AST::Alt
|
|
50
|
-
"(?:#{node.children.map { |child| source_for(child
|
|
51
|
-
when Regexp::AST::Star then "(?:#{source_for(node.child
|
|
49
|
+
"(?:#{node.children.map { |child| source_for(child) }.join('|')})"
|
|
50
|
+
when Regexp::AST::Star then "(?:#{source_for(node.child)})*"
|
|
52
51
|
when Regexp::AST::Repeat
|
|
53
52
|
maximum = node.maximum.nil? ? "" : node.maximum
|
|
54
|
-
"(?:#{source_for(node.child
|
|
53
|
+
"(?:#{source_for(node.child)}){#{node.minimum},#{maximum}}"
|
|
55
54
|
else
|
|
56
55
|
raise CompileError, "unsupported reference AST node: #{node.class}"
|
|
57
56
|
end
|
data/lib/flexr/version.rb
CHANGED
data/lib/flexr.rb
CHANGED
|
@@ -11,4 +11,5 @@ module Flexr
|
|
|
11
11
|
autoload :Options, File.expand_path("flexr/options", __dir__)
|
|
12
12
|
autoload :RakeTask, File.expand_path("flexr/rake_task", __dir__)
|
|
13
13
|
autoload :CLI, File.expand_path("flexr/cli", __dir__)
|
|
14
|
+
autoload :Trace, File.expand_path("flexr/trace", __dir__)
|
|
14
15
|
end
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_lsp/addon"
|
|
4
|
+
require "flexr/cli"
|
|
5
|
+
|
|
6
|
+
RubyLsp::Addon.depend_on_ruby_lsp!("~> 0.26.11")
|
|
7
|
+
|
|
8
|
+
module RubyLsp
|
|
9
|
+
module Flexr
|
|
10
|
+
class Analysis
|
|
11
|
+
def diagnostics(source)
|
|
12
|
+
return @diagnostics if @source == source
|
|
13
|
+
|
|
14
|
+
diagnostics = begin
|
|
15
|
+
parsed = ::Flexr::Source::PrismReader.new(source).read
|
|
16
|
+
compiled = ::Flexr::CLI.compile(parsed, overrides: { warn_level: :all })
|
|
17
|
+
compiled.diagnostics.reject { |diagnostic| diagnostic.code == "FLEXR-W016" }
|
|
18
|
+
rescue ::Flexr::Error => e
|
|
19
|
+
e.diagnostic ? [e.diagnostic] : []
|
|
20
|
+
rescue ArgumentError => e
|
|
21
|
+
[::Flexr::Diagnostics.error("FLEXR-E017", e.message, location: [0, 0])]
|
|
22
|
+
end
|
|
23
|
+
@source = source.dup.freeze
|
|
24
|
+
@diagnostics = diagnostics
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def message(diagnostic)
|
|
28
|
+
[diagnostic.message, diagnostic.note,
|
|
29
|
+
("help: #{diagnostic.help}" if diagnostic.help)].compact.join("\n\n")
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Diagnostics
|
|
34
|
+
def initialize(analysis, encoding)
|
|
35
|
+
@analysis = analysis
|
|
36
|
+
@encoding = encoding
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def run_diagnostic(uri, document)
|
|
40
|
+
return [] unless uri.path&.end_with?(".flexr.rb")
|
|
41
|
+
|
|
42
|
+
source = document.source
|
|
43
|
+
@analysis.diagnostics(source).map do |diagnostic|
|
|
44
|
+
first, last = diagnostic.location || [0, 0]
|
|
45
|
+
severity = { error: Constant::DiagnosticSeverity::ERROR, warning: Constant::DiagnosticSeverity::WARNING,
|
|
46
|
+
info: Constant::DiagnosticSeverity::INFORMATION }.fetch(diagnostic.severity)
|
|
47
|
+
Interface::Diagnostic.new(
|
|
48
|
+
range: Interface::Range.new(start: position(source, first), end: position(source, last)),
|
|
49
|
+
message: @analysis.message(diagnostic), code: diagnostic.code, severity: severity, source: "flexr"
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def position(source, offset)
|
|
57
|
+
prefix = source.byteslice(0, [offset, source.bytesize].min)
|
|
58
|
+
line = prefix.count("\n")
|
|
59
|
+
column = prefix.byteslice(((prefix.b.rindex("\n") || -1) + 1)..).delete_suffix("\r")
|
|
60
|
+
character = case @encoding
|
|
61
|
+
when Encoding::UTF_8 then column.bytesize
|
|
62
|
+
when Encoding::UTF_16LE then column.encode(Encoding::UTF_16LE).bytesize / 2
|
|
63
|
+
else column.length
|
|
64
|
+
end
|
|
65
|
+
Interface::Position.new(line: line, character: character)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
class Hover
|
|
70
|
+
def initialize(builder, analysis, dispatcher)
|
|
71
|
+
@builder = builder
|
|
72
|
+
@analysis = analysis
|
|
73
|
+
dispatcher.register(self, :on_call_node_enter)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def on_call_node_enter(node)
|
|
77
|
+
return unless node.name == :rule && node.receiver.nil?
|
|
78
|
+
|
|
79
|
+
source = node.location.source_lines.join
|
|
80
|
+
diagnostics = @analysis.diagnostics(source).select do |diagnostic|
|
|
81
|
+
diagnostic.location &&
|
|
82
|
+
(node.location.start_offset...node.location.end_offset).cover?(diagnostic.location.first)
|
|
83
|
+
end
|
|
84
|
+
return if diagnostics.empty?
|
|
85
|
+
|
|
86
|
+
text = diagnostics.map { |diagnostic| "#{diagnostic.code}: #{@analysis.message(diagnostic)}" }.join("\n\n")
|
|
87
|
+
@builder.push("```text\n#{text}\n```", category: :documentation)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class Addon < ::RubyLsp::Addon
|
|
92
|
+
def activate(global_state, _message_queue)
|
|
93
|
+
@analysis = Analysis.new
|
|
94
|
+
global_state.register_formatter("flexr", Diagnostics.new(@analysis, global_state.encoding))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def deactivate
|
|
98
|
+
@analysis = nil
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def name
|
|
102
|
+
"flexr"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def version
|
|
106
|
+
::Flexr::VERSION
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def create_hover_listener(response_builder, _node_context, dispatcher)
|
|
110
|
+
Hover.new(response_builder, @analysis, dispatcher)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
data/sig/flexr.rbs
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
module Flexr
|
|
2
|
+
VERSION: String
|
|
3
|
+
|
|
4
|
+
type pattern = String | ::Regexp
|
|
5
|
+
type token = [untyped, untyped] | Runtime::Token
|
|
6
|
+
type source_location = [Integer, Integer]
|
|
7
|
+
type cancellation = (^() -> untyped) | (^(Lexer) -> untyped) | _Cancellation | _ContextCancellation
|
|
8
|
+
type recovery_action = :skip | :raise | :token | :halt
|
|
9
|
+
type error_handler = (^(LexError) -> recovery_action) | _ErrorHandler
|
|
10
|
+
|
|
11
|
+
interface _Cancellation
|
|
12
|
+
def call: () -> untyped
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
interface _ContextCancellation
|
|
16
|
+
def call: (Lexer) -> untyped
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
interface _ErrorHandler
|
|
20
|
+
def call: (LexError) -> recovery_action
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
interface _Input
|
|
24
|
+
def read: (Integer) -> String?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
interface _CompiledPattern
|
|
28
|
+
def accept?: (String) -> bool
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.compile_pattern: (pattern, ?options: Hash[Symbol, untyped], ?encoding: Encoding?) -> _CompiledPattern
|
|
32
|
+
def self.parse_pattern: (pattern, ?options: Hash[Symbol, untyped]) -> untyped
|
|
33
|
+
|
|
34
|
+
module DSL
|
|
35
|
+
def rule: (pattern | Array[pattern], ?skip: bool, ?emit: untyped, ?followed_by: pattern?) ?{ () [self: Lexer] -> untyped } -> nil
|
|
36
|
+
def state: (*(Symbol | String), ?inclusive: bool) { () [self: singleton(Lexer)] -> untyped } -> void
|
|
37
|
+
def all_states: () { () [self: singleton(Lexer)] -> untyped } -> void
|
|
38
|
+
def on_eof: () { () [self: Lexer] -> untyped } -> void
|
|
39
|
+
def emits: (*untyped) -> void
|
|
40
|
+
def backend: (:table | :direct | :auto | :firstmatch | String) -> void
|
|
41
|
+
def token_kind: (:array | :struct | :yield | String) -> void
|
|
42
|
+
def encoding: (Encoding | String) -> void
|
|
43
|
+
def option: (*(Symbol | String)) -> void
|
|
44
|
+
def accel: (:auto | :strscan | :regexp | :none | String) -> void
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class Lexer
|
|
48
|
+
extend DSL
|
|
49
|
+
|
|
50
|
+
def initialize: (String | _Input, ?filename: String?, ?error_mode: Symbol, ?max_token_size: Integer, ?max_lookahead_size: Integer?, ?max_buffer_size: Integer, ?max_state_stack: Integer, ?max_steps: Integer?, ?cancellation: cancellation?, ?retain_input: bool, ?chunk_size: Integer) -> void
|
|
51
|
+
def next_token: () -> token?
|
|
52
|
+
def tokens: () -> Array[token]
|
|
53
|
+
def each_token: () -> Enumerator[token, self]
|
|
54
|
+
| () { (untyped, ?untyped) -> untyped } -> self
|
|
55
|
+
def racc_next_token: () -> [untyped, untyped]
|
|
56
|
+
|
|
57
|
+
attr_reader filename: String?
|
|
58
|
+
attr_reader error_mode: Symbol
|
|
59
|
+
attr_reader max_token_size: Integer
|
|
60
|
+
attr_reader max_lookahead_size: Integer
|
|
61
|
+
attr_reader steps: Integer
|
|
62
|
+
attr_writer on_error: error_handler?
|
|
63
|
+
def input: () -> String
|
|
64
|
+
def text: () -> String
|
|
65
|
+
def text_bytesize: () -> Integer
|
|
66
|
+
def last_location: () -> Runtime::Location
|
|
67
|
+
def byte_pos: () -> Integer
|
|
68
|
+
def lineno: () -> Integer
|
|
69
|
+
alias line lineno
|
|
70
|
+
def beginning_of_line?: () -> bool
|
|
71
|
+
def state: () -> Symbol
|
|
72
|
+
def binary_input: () -> String
|
|
73
|
+
def emit: (untyped, ?untyped) -> token
|
|
74
|
+
def skip: () -> nil
|
|
75
|
+
def echo: () -> token
|
|
76
|
+
def error!: (String) -> token?
|
|
77
|
+
def push: (Symbol | String) -> Symbol
|
|
78
|
+
alias push_state push
|
|
79
|
+
def pop: () -> Symbol
|
|
80
|
+
alias pop_state pop
|
|
81
|
+
def begin_state: (Symbol | String) -> Symbol
|
|
82
|
+
alias state= begin_state
|
|
83
|
+
def less: (Integer) -> nil
|
|
84
|
+
def more: () -> true
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
module Runtime
|
|
88
|
+
class Token < ::Struct[untyped]
|
|
89
|
+
def self.new: (?type: untyped, ?value: untyped, ?location: Location?) -> instance
|
|
90
|
+
attr_accessor type: untyped
|
|
91
|
+
attr_accessor value: untyped
|
|
92
|
+
attr_accessor location: Location?
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
class Location < ::Struct[untyped]
|
|
96
|
+
def self.new: (?filename: String?, ?byte_begin: Integer?, ?byte_end: Integer?, ?line_begin: Integer?, ?column_begin: Integer?, ?line_end: Integer?, ?column_end: Integer?) -> instance
|
|
97
|
+
attr_accessor filename: String?
|
|
98
|
+
attr_accessor byte_begin: Integer?
|
|
99
|
+
attr_accessor byte_end: Integer?
|
|
100
|
+
attr_accessor line_begin: Integer?
|
|
101
|
+
attr_accessor line_end: Integer?
|
|
102
|
+
attr_accessor column_begin: Integer?
|
|
103
|
+
attr_accessor column_end: Integer?
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
class TokenTooLargeError < LexError
|
|
107
|
+
def initialize: (?String, ?filename: String?, ?byte_pos: Integer?, ?line: Integer?, ?text: String?, ?rule: Integer?) -> void
|
|
108
|
+
attr_reader code: String
|
|
109
|
+
attr_reader rule: Integer?
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
class Error < StandardError
|
|
114
|
+
def initialize: (?String?, ?diagnostic: Diagnostic?, ?filename: String?, ?byte_pos: Integer?) -> void
|
|
115
|
+
attr_reader diagnostic: Diagnostic?
|
|
116
|
+
attr_reader filename: String?
|
|
117
|
+
attr_reader byte_pos: Integer?
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
class LexError < Error
|
|
121
|
+
def initialize: (String, ?filename: String?, ?byte_pos: Integer?, ?line: Integer?, ?text: String?, ?diagnostic: Diagnostic?) -> void
|
|
122
|
+
attr_reader line: Integer?
|
|
123
|
+
attr_reader text: String?
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
class CompileError < Error
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
class UnsupportedRegexpError < CompileError
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
class Diagnostic < ::Struct[untyped]
|
|
133
|
+
def self.new: (?code: String?, ?severity: Symbol?, ?message: String?, ?location: source_location?, ?help: String?, ?note: String?) -> instance
|
|
134
|
+
attr_accessor code: String?
|
|
135
|
+
attr_accessor severity: Symbol?
|
|
136
|
+
attr_accessor message: String?
|
|
137
|
+
attr_accessor location: source_location?
|
|
138
|
+
attr_accessor help: String?
|
|
139
|
+
attr_accessor note: String?
|
|
140
|
+
def error?: () -> bool
|
|
141
|
+
def warning?: () -> bool
|
|
142
|
+
def to_h: () -> Hash[Symbol, untyped]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
class DiagnosticSet
|
|
146
|
+
include Enumerable[Diagnostic]
|
|
147
|
+
def initialize: () -> void
|
|
148
|
+
def each: () -> Enumerator[Diagnostic, Array[Diagnostic]]
|
|
149
|
+
| () { (Diagnostic) -> untyped } -> Array[Diagnostic]
|
|
150
|
+
def <<: (Diagnostic) -> self
|
|
151
|
+
def any_error?: () -> bool
|
|
152
|
+
def empty?: () -> bool
|
|
153
|
+
def to_a: () -> Array[Diagnostic]
|
|
154
|
+
def render: (?format: Symbol, ?color: Symbol | bool, ?source: String?, ?path: String?) -> String
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
module Diagnostics
|
|
158
|
+
def self.error: (String, String, ?location: source_location?, ?help: String?, ?note: String?) -> Diagnostic
|
|
159
|
+
def self.warning: (String, String, ?location: source_location?, ?help: String?, ?note: String?) -> Diagnostic
|
|
160
|
+
def self.info: (String, String, ?location: source_location?, ?help: String?, ?note: String?) -> Diagnostic
|
|
161
|
+
def self.raise!: (Diagnostic) -> bot
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
class Generator
|
|
165
|
+
def initialize: (String, ?output: String?, ?eval_mode: bool, ?options: Hash[Symbol, untyped]) -> void
|
|
166
|
+
def generate: () -> String
|
|
167
|
+
attr_reader diagnostics: Array[Diagnostic]
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
class RakeTask
|
|
171
|
+
def initialize: (?Symbol | String) ?{ (RakeTask) -> untyped } -> void
|
|
172
|
+
attr_accessor spec: String?
|
|
173
|
+
attr_accessor output: String?
|
|
174
|
+
attr_accessor warn_as_error: bool
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
module CLI
|
|
178
|
+
def self.run: (Array[String], ?out: IO | _Output, ?err: IO | _Output) -> Integer
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
interface _Output
|
|
182
|
+
def puts: (*untyped) -> nil
|
|
183
|
+
def write: (String) -> Integer
|
|
184
|
+
end
|
|
185
|
+
end
|
|
@@ -16,6 +16,13 @@ const base = import.meta.env.BASE_URL.endsWith('/') ? import.meta.env.BASE_URL :
|
|
|
16
16
|
<meta charset="UTF-8" />
|
|
17
17
|
<meta name="viewport" content="width=device-width" />
|
|
18
18
|
<meta name="description" content={description} />
|
|
19
|
+
<meta name="theme-color" content="#161518" />
|
|
20
|
+
<meta property="og:title" content={siteTitle} />
|
|
21
|
+
<meta property="og:description" content={description} />
|
|
22
|
+
<meta property="og:type" content="website" />
|
|
23
|
+
<link rel="preconnect" href="https://fonts.googleapis.com" />
|
|
24
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
|
|
25
|
+
<link href="https://fonts.googleapis.com/css2?family=Newsreader:ital,opsz,wght@0,6..72,400..700;1,6..72,400..600&family=Public+Sans:wght@400;500;600;700&display=swap" rel="stylesheet" />
|
|
19
26
|
<title>{siteTitle}</title>
|
|
20
27
|
</head>
|
|
21
28
|
<body>
|
|
@@ -25,11 +32,10 @@ const base = import.meta.env.BASE_URL.endsWith('/') ? import.meta.env.BASE_URL :
|
|
|
25
32
|
<a class="site-nav-brand" href={base} aria-label="flexr home">flexr<span>/</span></a>
|
|
26
33
|
<nav class="site-nav-links" aria-label="Primary navigation">
|
|
27
34
|
<a href={`${base}playground/`}>Playground</a>
|
|
28
|
-
<a href={`${base}learn/getting-started/`}>Learn</a>
|
|
29
35
|
<a href={`${base}reference/dsl/`}>Reference</a>
|
|
30
36
|
<a href="https://github.com/ydah/flexr">GitHub</a>
|
|
31
37
|
</nav>
|
|
32
|
-
<a class="site-nav-cta" href={`${base}learn/getting-started/`}>Get started
|
|
38
|
+
<a class="site-nav-cta" href={`${base}learn/getting-started/`}>Get started</a>
|
|
33
39
|
</div>
|
|
34
40
|
</header>
|
|
35
41
|
<div id="main-content">
|