lexer_kit 0.6.1 → 0.7.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/Cargo.lock +265 -0
- data/Cargo.toml +37 -0
- data/README.md +7 -3
- data/crates/lexer_kit_core/Cargo.toml +17 -0
- data/crates/lexer_kit_core/src/dfa.rs +437 -0
- data/crates/lexer_kit_core/src/format.rs +599 -0
- data/crates/lexer_kit_core/src/lib.rs +18 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
- data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
- data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
- data/ext/lexer_kit_rust/Cargo.toml +7 -3
- data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
- data/ext/lexer_kit_rust/src/lib.rs +200 -215
- data/lib/lexer_kit/builder/compiler.rb +50 -15
- data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
- data/lib/lexer_kit/builder/validator.rb +45 -0
- data/lib/lexer_kit/builder.rb +34 -7
- data/lib/lexer_kit/cli/commands.rb +17 -7
- data/lib/lexer_kit/core/source.rb +3 -2
- data/lib/lexer_kit/debug/disassembler.rb +0 -2
- data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
- data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
- data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
- data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
- data/lib/lexer_kit/errors.rb +2 -2
- data/lib/lexer_kit/format/lkb1.rb +3 -3
- data/lib/lexer_kit/format/lkt1.rb +3 -1
- data/lib/lexer_kit/format.rb +4 -4
- data/lib/lexer_kit/ir/compiled_program.rb +39 -26
- data/lib/lexer_kit/ir/constant_pool.rb +33 -11
- data/lib/lexer_kit/ir/dfa_table.rb +9 -19
- data/lib/lexer_kit/ir/instruction.rb +3 -0
- data/lib/lexer_kit/ir/jump_table.rb +3 -13
- data/lib/lexer_kit/ir/keyword_table.rb +0 -9
- data/lib/lexer_kit/ir/limits.rb +48 -0
- data/lib/lexer_kit/ir/serializer.rb +69 -30
- data/lib/lexer_kit/ir.rb +1 -0
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +15 -2
- data/vendor/nupur/Cargo.toml +24 -0
- data/vendor/nupur/LICENSE +176 -0
- data/vendor/nupur/NOTICE +64 -0
- data/vendor/nupur/README.ja.md +150 -0
- data/vendor/nupur/README.md +257 -0
- data/vendor/nupur/src/abi.rs +143 -0
- data/vendor/nupur/src/args.rs +318 -0
- data/vendor/nupur/src/build.rs +101 -0
- data/vendor/nupur/src/caught.rs +171 -0
- data/vendor/nupur/src/cname.rs +37 -0
- data/vendor/nupur/src/describe.rs +175 -0
- data/vendor/nupur/src/encoding.rs +240 -0
- data/vendor/nupur/src/gvl.rs +181 -0
- data/vendor/nupur/src/hash.rs +52 -0
- data/vendor/nupur/src/inject.rs +20 -0
- data/vendor/nupur/src/lib.rs +280 -0
- data/vendor/nupur/src/lint_probe.rs +142 -0
- data/vendor/nupur/src/msg.rs +193 -0
- data/vendor/nupur/src/no_unwind.rs +39 -0
- data/vendor/nupur/src/raw/check.rs +71 -0
- data/vendor/nupur/src/raw/data.rs +28 -0
- data/vendor/nupur/src/raw/define.rs +215 -0
- data/vendor/nupur/src/raw/exception.rs +69 -0
- data/vendor/nupur/src/raw/iterate.rs +115 -0
- data/vendor/nupur/src/raw/mod.rs +105 -0
- data/vendor/nupur/src/raw/protect.rs +144 -0
- data/vendor/nupur/src/raw/string.rs +148 -0
- data/vendor/nupur/src/raw/symbol.rs +38 -0
- data/vendor/nupur/src/raw/values.rs +293 -0
- data/vendor/nupur/src/rb/call.rs +118 -0
- data/vendor/nupur/src/rb/class.rs +170 -0
- data/vendor/nupur/src/rb/convert.rs +134 -0
- data/vendor/nupur/src/rb/error.rs +355 -0
- data/vendor/nupur/src/rb/global.rs +141 -0
- data/vendor/nupur/src/rb/kept.rs +55 -0
- data/vendor/nupur/src/rb/method/frame.rs +81 -0
- data/vendor/nupur/src/rb/method.rs +468 -0
- data/vendor/nupur/src/rb/mod.rs +40 -0
- data/vendor/nupur/src/rb/object.rs +66 -0
- data/vendor/nupur/src/rb/ruby.rs +107 -0
- data/vendor/nupur/src/rb/value/header.rs +65 -0
- data/vendor/nupur/src/rb/value/id.rs +61 -0
- data/vendor/nupur/src/rb/value/integer.rs +132 -0
- data/vendor/nupur/src/rb/value.rs +454 -0
- data/vendor/nupur/src/stack.rs +32 -0
- data/vendor/nupur/src/string/make.rs +234 -0
- data/vendor/nupur/src/string/mod.rs +24 -0
- data/vendor/nupur/src/string/read.rs +411 -0
- data/vendor/nupur/src/table.rs +58 -0
- data/vendor/nupur/src/typed/callbacks.rs +125 -0
- data/vendor/nupur/src/typed/data_type.rs +132 -0
- data/vendor/nupur/src/typed/embedded.rs +103 -0
- data/vendor/nupur/src/typed/external.rs +76 -0
- data/vendor/nupur/src/typed/hidden.rs +95 -0
- data/vendor/nupur/src/typed/mod.rs +373 -0
- data/vendor/nupur/src/utf8_copy.rs +262 -0
- data/vendor/nupur/sys/Cargo.toml +29 -0
- data/vendor/nupur/sys/src/data_type.rs +213 -0
- data/vendor/nupur/sys/src/int.rs +248 -0
- data/vendor/nupur/sys/src/lib.rs +61 -0
- data/vendor/nupur/sys/src/lint_probe.rs +93 -0
- data/vendor/nupur/sys/src/no_unwind.rs +99 -0
- data/vendor/nupur/sys/src/non_raising.rs +585 -0
- data/vendor/nupur/sys/src/protect.rs +349 -0
- data/vendor/nupur/sys/src/raising.rs +257 -0
- data/vendor/nupur/sys/src/value.rs +279 -0
- data/vendor/nupur.sha256 +68 -0
- metadata +92 -12
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -40,6 +40,8 @@ module LexerKit
|
|
|
40
40
|
def check_pair(token1, token2)
|
|
41
41
|
# Skip if both have the same name (same token defined multiple times is ok)
|
|
42
42
|
return if token1.name == token2.name
|
|
43
|
+
# A delimited token has no pattern: it scans its whole mode
|
|
44
|
+
return if token1.delimited? || token2.delimited?
|
|
43
45
|
|
|
44
46
|
pattern1 = token1.pattern
|
|
45
47
|
pattern2 = token2.pattern
|
|
@@ -51,10 +53,15 @@ module LexerKit
|
|
|
51
53
|
elsif pattern1.is_a?(Regexp) && pattern2.is_a?(String)
|
|
52
54
|
check_literal_regex_pair(token2, token1)
|
|
53
55
|
else
|
|
54
|
-
|
|
56
|
+
# A literal against another kind of pattern (e.g. utf8_range): compare as regexes
|
|
57
|
+
check_regex_pair(token1, token2, as_regex(pattern1), as_regex(pattern2))
|
|
55
58
|
end
|
|
56
59
|
end
|
|
57
60
|
|
|
61
|
+
def as_regex(pattern)
|
|
62
|
+
pattern.is_a?(String) ? Regexp.new(Regexp.escape(pattern)) : pattern
|
|
63
|
+
end
|
|
64
|
+
|
|
58
65
|
def check_literal_pair(token1, token2)
|
|
59
66
|
lit1 = token1.pattern
|
|
60
67
|
lit2 = token2.pattern
|
|
@@ -89,8 +96,8 @@ module LexerKit
|
|
|
89
96
|
literal = literal_token.pattern
|
|
90
97
|
regex = regex_token.pattern
|
|
91
98
|
|
|
92
|
-
# Check if the
|
|
93
|
-
return unless regex.match?(literal)
|
|
99
|
+
# Check if the regex matches the whole literal
|
|
100
|
+
return unless Regexp.new("\\A(?:#{regex.source})\\z", regex.options).match?(literal)
|
|
94
101
|
|
|
95
102
|
@conflicts << Conflict.new(
|
|
96
103
|
type: :literal_matches_regex,
|
|
@@ -101,14 +108,11 @@ module LexerKit
|
|
|
101
108
|
)
|
|
102
109
|
end
|
|
103
110
|
|
|
104
|
-
def check_regex_pair(token1, token2)
|
|
111
|
+
def check_regex_pair(token1, token2, pattern1 = token1.pattern, pattern2 = token2.pattern)
|
|
105
112
|
# For regex pairs, we need to check if they can match the same string
|
|
106
113
|
# We do this by building a combined DFA and checking for states
|
|
107
114
|
# that accept both patterns
|
|
108
115
|
|
|
109
|
-
pattern1 = token1.pattern
|
|
110
|
-
pattern2 = token2.pattern
|
|
111
|
-
|
|
112
116
|
# Check first byte overlap
|
|
113
117
|
regex1 = DFA::RegexAST::Regex.parse(pattern1)
|
|
114
118
|
regex2 = DFA::RegexAST::Regex.parse(pattern2)
|
|
@@ -163,9 +167,9 @@ module LexerKit
|
|
|
163
167
|
dfa1 = DFA.compile_regex(regex1, 1)
|
|
164
168
|
dfa2 = DFA.compile_regex(regex2, 2)
|
|
165
169
|
|
|
166
|
-
# BFS to find accepting strings
|
|
167
|
-
# State: [dfa1_state, dfa2_state,
|
|
168
|
-
queue = [[1, 1, ""]]
|
|
170
|
+
# BFS over bytes to find accepting strings
|
|
171
|
+
# State: [dfa1_state, dfa2_state, accumulated bytes]
|
|
172
|
+
queue = [[1, 1, "".b]]
|
|
169
173
|
visited = Set.new
|
|
170
174
|
max_depth = 10
|
|
171
175
|
|
|
@@ -179,26 +183,20 @@ module LexerKit
|
|
|
179
183
|
|
|
180
184
|
# Check if both DFAs accept at this point
|
|
181
185
|
if dfa1.accept(dfa1_state) && dfa2.accept(dfa2_state) && !str.empty?
|
|
182
|
-
|
|
186
|
+
text = str.dup.force_encoding(Encoding::UTF_8)
|
|
187
|
+
examples << (text.valid_encoding? ? text : str)
|
|
183
188
|
next
|
|
184
189
|
end
|
|
185
190
|
|
|
186
191
|
# Explore transitions
|
|
187
|
-
(0..
|
|
192
|
+
(0..255).each do |byte|
|
|
188
193
|
next_state1 = dfa1.transition(dfa1_state, byte)
|
|
189
194
|
next_state2 = dfa2.transition(dfa2_state, byte)
|
|
190
195
|
|
|
191
196
|
# Both must have valid transitions
|
|
192
197
|
next if next_state1.zero? || next_state2.zero?
|
|
193
198
|
|
|
194
|
-
|
|
195
|
-
byte.chr
|
|
196
|
-
rescue StandardError
|
|
197
|
-
next
|
|
198
|
-
end
|
|
199
|
-
next unless char.valid_encoding?
|
|
200
|
-
|
|
201
|
-
queue << [next_state1, next_state2, str + char]
|
|
199
|
+
queue << [next_state1, next_state2, str + byte.chr]
|
|
202
200
|
end
|
|
203
201
|
end
|
|
204
202
|
|
|
@@ -13,10 +13,55 @@ module LexerKit
|
|
|
13
13
|
def validate!
|
|
14
14
|
validate_mode_references!
|
|
15
15
|
validate_reserved_token_names!
|
|
16
|
+
validate_patterns!
|
|
17
|
+
validate_modes!
|
|
18
|
+
validate_version!
|
|
16
19
|
end
|
|
17
20
|
|
|
18
21
|
private
|
|
19
22
|
|
|
23
|
+
def validate_patterns!
|
|
24
|
+
@builder.token_defs.each do |token_def|
|
|
25
|
+
next if token_def.delimited?
|
|
26
|
+
|
|
27
|
+
pattern = token_def.pattern
|
|
28
|
+
if pattern.is_a?(String)
|
|
29
|
+
next unless pattern.empty?
|
|
30
|
+
|
|
31
|
+
error!(token_def.location, "token :#{token_def.name} has an empty pattern")
|
|
32
|
+
end
|
|
33
|
+
next if token_def.regex?
|
|
34
|
+
|
|
35
|
+
error!(token_def.location, "token :#{token_def.name} needs a String or Regexp pattern, got #{pattern.inspect}")
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A delimited token scans a whole mode, so it is the mode's only token.
|
|
40
|
+
def validate_modes!
|
|
41
|
+
@builder.mode_defs.each_value do |mode_def|
|
|
42
|
+
if mode_def.tokens.empty?
|
|
43
|
+
error!(mode_def.location, "mode :#{mode_def.name} defines no tokens")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
delimited = mode_def.tokens.select(&:delimited?)
|
|
47
|
+
next if delimited.empty? || mode_def.tokens.size == 1
|
|
48
|
+
|
|
49
|
+
other = mode_def.tokens.find { |t| !t.equal?(delimited.first) }
|
|
50
|
+
error!(other.location, "mode :#{mode_def.name} has a delimited token, so it can have no other token")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def validate_version!
|
|
55
|
+
version = @builder.version
|
|
56
|
+
return if version.is_a?(Integer) && version >= 0 && version <= IR::Limits::U32_MAX
|
|
57
|
+
|
|
58
|
+
error!(nil, "version must be an Integer from 0 to #{IR::Limits::U32_MAX}, got #{version.inspect}")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def error!(location, message)
|
|
62
|
+
raise LexerKit::BuildError.from_location(location, message)
|
|
63
|
+
end
|
|
64
|
+
|
|
20
65
|
def validate_reserved_token_names!
|
|
21
66
|
@builder.token_defs.each do |token_def|
|
|
22
67
|
next unless token_def.name == :INVALID
|
data/lib/lexer_kit/builder.rb
CHANGED
|
@@ -22,10 +22,10 @@ module LexerKit
|
|
|
22
22
|
def token(name, pattern, skip: false, push: nil, pop: false, meta: nil)
|
|
23
23
|
location = caller_locations(1, 1).first
|
|
24
24
|
token_def = TokenDef.new(
|
|
25
|
-
name: name,
|
|
25
|
+
name: dsl_name(name, location),
|
|
26
26
|
pattern: pattern,
|
|
27
27
|
skip: skip,
|
|
28
|
-
push: push,
|
|
28
|
+
push: push && dsl_name(push, location),
|
|
29
29
|
pop: pop,
|
|
30
30
|
meta: meta,
|
|
31
31
|
location: location
|
|
@@ -38,7 +38,7 @@ module LexerKit
|
|
|
38
38
|
# @param name [Symbol] keyword token name
|
|
39
39
|
# @param value [String] keyword string
|
|
40
40
|
def keyword(name, value)
|
|
41
|
-
|
|
41
|
+
add_keyword(name, value, caller_locations(1, 1).first)
|
|
42
42
|
end
|
|
43
43
|
|
|
44
44
|
# Define multiple keywords at once
|
|
@@ -50,20 +50,22 @@ module LexerKit
|
|
|
50
50
|
# # keyword :ELSE, "else"
|
|
51
51
|
# # keyword :WHILE, "while"
|
|
52
52
|
def define_keywords(*names)
|
|
53
|
+
location = caller_locations(1, 1).first
|
|
53
54
|
names.each do |name|
|
|
54
55
|
token_name = name.to_s.upcase.to_sym
|
|
55
56
|
value = name.to_s.downcase
|
|
56
|
-
|
|
57
|
+
add_keyword(token_name, value, location)
|
|
57
58
|
end
|
|
58
59
|
end
|
|
59
60
|
|
|
60
61
|
# Define a mode
|
|
61
62
|
# @param name [Symbol] mode name
|
|
62
63
|
# @yield mode definition block
|
|
64
|
+
# Defining a mode again adds to it.
|
|
63
65
|
def mode(name, &block)
|
|
64
66
|
location = caller_locations(1, 1).first
|
|
65
|
-
|
|
66
|
-
@mode_defs[name]
|
|
67
|
+
name = dsl_name(name, location)
|
|
68
|
+
@mode_defs[name] ||= ModeDef.new(name, location: location)
|
|
67
69
|
|
|
68
70
|
old_mode = @current_mode
|
|
69
71
|
@current_mode = name
|
|
@@ -96,6 +98,7 @@ module LexerKit
|
|
|
96
98
|
raise LexerKit::BuildError.from_location(location, message)
|
|
97
99
|
end
|
|
98
100
|
location = caller_locations(1, 1).first
|
|
101
|
+
name = dsl_name(name, location)
|
|
99
102
|
|
|
100
103
|
token_def = add_delimited_token(
|
|
101
104
|
name: name,
|
|
@@ -173,10 +176,16 @@ module LexerKit
|
|
|
173
176
|
|
|
174
177
|
# Compile to IR
|
|
175
178
|
# @return [IR::CompiledProgram]
|
|
179
|
+
# @raise [BuildError, CompileError] if the definitions are invalid or do not fit the program binary
|
|
176
180
|
def compile
|
|
177
181
|
Validator.new(self).validate!
|
|
178
182
|
program = Compiler.new(self).compile
|
|
179
|
-
|
|
183
|
+
begin
|
|
184
|
+
binary = program.to_binary
|
|
185
|
+
rescue ArgumentError => e
|
|
186
|
+
raise LexerKit::CompileError, e.message
|
|
187
|
+
end
|
|
188
|
+
program.load_rust_native(binary) if LexerKit.native?
|
|
180
189
|
program
|
|
181
190
|
end
|
|
182
191
|
|
|
@@ -203,6 +212,24 @@ module LexerKit
|
|
|
203
212
|
@mode_defs[@current_mode]
|
|
204
213
|
end
|
|
205
214
|
|
|
215
|
+
# A token or mode name as a Symbol: a String is converted, anything else is an error.
|
|
216
|
+
def dsl_name(name, location)
|
|
217
|
+
return name if name.is_a?(Symbol)
|
|
218
|
+
return name.to_sym if name.is_a?(String) && !name.empty?
|
|
219
|
+
|
|
220
|
+
raise LexerKit::BuildError.from_location(location, "a name must be a Symbol or String, got #{name.inspect}")
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Define a keyword, reporting errors at location (the user's call, not this file).
|
|
224
|
+
def add_keyword(name, value, location)
|
|
225
|
+
name = dsl_name(name, location)
|
|
226
|
+
if name == :INVALID
|
|
227
|
+
raise LexerKit::BuildError.from_location(location, ":INVALID is reserved for error tokens")
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
@keywords[value.freeze] = name
|
|
231
|
+
end
|
|
232
|
+
|
|
206
233
|
# Generate unique internal mode name for scan_until etc.
|
|
207
234
|
def next_internal_mode_name(base_name)
|
|
208
235
|
@internal_mode_counter += 1
|
|
@@ -69,7 +69,11 @@ module LexerKit
|
|
|
69
69
|
compiled = builder.compile
|
|
70
70
|
|
|
71
71
|
# Write output
|
|
72
|
-
output_path = options[:output] || path
|
|
72
|
+
output_path = options[:output] || default_output_path(path)
|
|
73
|
+
if File.expand_path(output_path) == File.expand_path(path)
|
|
74
|
+
warn "error: Output file is the input file: #{output_path}"
|
|
75
|
+
return 1
|
|
76
|
+
end
|
|
73
77
|
if output_path.end_with?(".lkb1")
|
|
74
78
|
Format::LKB1.save(compiled, path: output_path)
|
|
75
79
|
else
|
|
@@ -79,6 +83,11 @@ module LexerKit
|
|
|
79
83
|
|
|
80
84
|
0
|
|
81
85
|
end
|
|
86
|
+
|
|
87
|
+
# file.rb => file.lkt1; any other name gets .lkt1 appended
|
|
88
|
+
def self.default_output_path(path)
|
|
89
|
+
path.end_with?(".rb") ? "#{path.delete_suffix('.rb')}.lkt1" : "#{path}.lkt1"
|
|
90
|
+
end
|
|
82
91
|
end
|
|
83
92
|
|
|
84
93
|
# Show lexer information
|
|
@@ -110,8 +119,8 @@ module LexerKit
|
|
|
110
119
|
puts
|
|
111
120
|
|
|
112
121
|
puts "Tokens (#{lexer.tokens.size}):"
|
|
113
|
-
lexer.tokens.
|
|
114
|
-
puts " #{
|
|
122
|
+
lexer.tokens.each do |name|
|
|
123
|
+
puts " #{lexer.token_id(name).to_s.rjust(3)}: #{name}"
|
|
115
124
|
end
|
|
116
125
|
puts
|
|
117
126
|
|
|
@@ -180,11 +189,11 @@ module LexerKit
|
|
|
180
189
|
lexer_path = argv.shift
|
|
181
190
|
lexer = CLI.load_lexer(lexer_path)
|
|
182
191
|
|
|
183
|
-
# Read input
|
|
192
|
+
# Read input as bytes: token offsets are byte offsets
|
|
184
193
|
input = if argv.empty?
|
|
185
|
-
$stdin.read
|
|
194
|
+
$stdin.binmode.read
|
|
186
195
|
else
|
|
187
|
-
File.
|
|
196
|
+
File.binread(argv.shift)
|
|
188
197
|
end
|
|
189
198
|
|
|
190
199
|
# Collect tokens
|
|
@@ -197,7 +206,8 @@ module LexerKit
|
|
|
197
206
|
tokens << {
|
|
198
207
|
id: tok_id,
|
|
199
208
|
name: lexer.token_name(tok_id),
|
|
200
|
-
|
|
209
|
+
# A token may end inside a multibyte character, so its text is scrubbed for output
|
|
210
|
+
text: input.byteslice(start, len).force_encoding(Encoding::UTF_8).scrub,
|
|
201
211
|
line: line,
|
|
202
212
|
col: col,
|
|
203
213
|
start: start,
|
|
@@ -7,10 +7,11 @@ module LexerKit
|
|
|
7
7
|
class Source
|
|
8
8
|
attr_reader :bytes, :filename
|
|
9
9
|
|
|
10
|
-
# @param input [String] input string (
|
|
10
|
+
# @param input [String] input string (kept for char conversion, and as BINARY for byte operations).
|
|
11
|
+
# The caller's String is not frozen: an unfrozen input is copied.
|
|
11
12
|
# @param filename [String, nil] optional filename for diagnostics
|
|
12
13
|
def initialize(input, filename: nil)
|
|
13
|
-
@original_string = input.freeze
|
|
14
|
+
@original_string = input.frozen? ? input : input.dup.freeze
|
|
14
15
|
@bytes = input.dup.force_encoding(Encoding::BINARY).freeze
|
|
15
16
|
@filename = filename&.freeze
|
|
16
17
|
@line_starts = nil
|
|
@@ -56,7 +56,6 @@ module LexerKit
|
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def format_arg(instr)
|
|
59
|
-
# rubocop:disable Lint/DuplicateBranch
|
|
60
59
|
case instr.opcode
|
|
61
60
|
when IR::Opcode::DFA_RUN
|
|
62
61
|
"dfa=#{instr.arg}"
|
|
@@ -128,7 +127,6 @@ module LexerKit
|
|
|
128
127
|
else
|
|
129
128
|
instr.arg.to_s
|
|
130
129
|
end
|
|
131
|
-
# rubocop:enable Lint/DuplicateBranch
|
|
132
130
|
end
|
|
133
131
|
|
|
134
132
|
def format_byte(byte)
|
|
@@ -74,7 +74,7 @@ module LexerKit
|
|
|
74
74
|
class_count = byte_class_result[:class_count]
|
|
75
75
|
transitions = byte_class_result[:transitions]
|
|
76
76
|
|
|
77
|
-
# Minimize DFA
|
|
77
|
+
# Minimize DFA by partition refinement
|
|
78
78
|
minimizer = DFAMinimizer.new(
|
|
79
79
|
state_count: @state_count,
|
|
80
80
|
transitions: transitions,
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module LexerKit
|
|
4
4
|
module DFA
|
|
5
|
-
# DFAMinimizer reduces DFA state count
|
|
5
|
+
# DFAMinimizer reduces DFA state count by partition refinement (Moore's algorithm).
|
|
6
6
|
# Merges equivalent states while preserving language recognition.
|
|
7
7
|
class DFAMinimizer
|
|
8
8
|
# @param state_count [Integer] number of states (including dead state 0)
|
|
@@ -24,7 +24,7 @@ module LexerKit
|
|
|
24
24
|
# 1. Initial partition: group by acceptance status and token_id
|
|
25
25
|
partitions = build_initial_partitions
|
|
26
26
|
|
|
27
|
-
# 2.
|
|
27
|
+
# 2. Split partitions until no state's transitions tell its partition apart
|
|
28
28
|
partitions = refine_partitions(partitions)
|
|
29
29
|
|
|
30
30
|
# 3. Build minimized DFA
|
|
@@ -52,6 +52,10 @@ module LexerKit
|
|
|
52
52
|
when LexerKit::RegexAstProvider
|
|
53
53
|
pattern.to_regex
|
|
54
54
|
else
|
|
55
|
+
if pattern.is_a?(Regexp) && pattern.options.anybits?(Regexp::EXTENDED | Regexp::MULTILINE)
|
|
56
|
+
raise LexerKit::ParseError, "regex flags other than /i are not supported: #{pattern.inspect}"
|
|
57
|
+
end
|
|
58
|
+
|
|
55
59
|
source = pattern.is_a?(Regexp) ? pattern.source : pattern
|
|
56
60
|
case_insensitive = pattern.is_a?(Regexp) && pattern.casefold?
|
|
57
61
|
ast = RegexParser.new(source).parse
|
|
@@ -14,6 +14,13 @@ module LexerKit
|
|
|
14
14
|
# Include AST types from RegexAST module
|
|
15
15
|
include RegexAST
|
|
16
16
|
|
|
17
|
+
# Byte ranges of the class escapes \d, \w and \s (ASCII only)
|
|
18
|
+
CLASS_ESCAPES = {
|
|
19
|
+
"d" => [[0x30, 0x39]],
|
|
20
|
+
"w" => [[0x30, 0x39], [0x41, 0x5A], [0x61, 0x7A], [0x5F, 0x5F]],
|
|
21
|
+
"s" => [[0x09, 0x0D], [0x20, 0x20]]
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
17
24
|
def initialize(source)
|
|
18
25
|
@source = source.is_a?(Regexp) ? source.source : source
|
|
19
26
|
@pos = 0
|
|
@@ -145,9 +152,8 @@ module LexerKit
|
|
|
145
152
|
advance
|
|
146
153
|
Any.new(meta: meta_for_span(start_pos, @pos, literal_text: "."))
|
|
147
154
|
when "^", "$"
|
|
148
|
-
#
|
|
149
|
-
|
|
150
|
-
Literal.new(byte: nil, meta: meta_for_span(start_pos, @pos, literal_text: @source[start_pos...@pos]))
|
|
155
|
+
# A token is matched where the previous one ended: a line anchor has no meaning there
|
|
156
|
+
error("anchor #{peek} is not supported at position #{@pos}")
|
|
151
157
|
when nil, "|", ")", "*", "+", "?", "{"
|
|
152
158
|
error("unexpected '#{peek}' at position #{@pos}")
|
|
153
159
|
else
|
|
@@ -177,7 +183,9 @@ module LexerKit
|
|
|
177
183
|
while peek && peek != "]"
|
|
178
184
|
start_item = parse_char_class_item
|
|
179
185
|
|
|
180
|
-
if
|
|
186
|
+
if start_item[:type] == :ranges
|
|
187
|
+
start_item[:value].each { |lo, hi| collector.add_range({ type: :byte, value: lo }, { type: :byte, value: hi }) }
|
|
188
|
+
elsif range_follows?
|
|
181
189
|
advance # skip -
|
|
182
190
|
end_item = parse_char_class_item
|
|
183
191
|
collector.add_range(start_item, end_item)
|
|
@@ -198,8 +206,19 @@ module LexerKit
|
|
|
198
206
|
end
|
|
199
207
|
|
|
200
208
|
def parse_char_class_item
|
|
209
|
+
error("POSIX bracket expressions like [[:alpha:]] are not supported") if peek == "[" && @source[@pos + 1] == ":"
|
|
210
|
+
error("nested character classes are not supported; escape [ as \\[") if peek == "["
|
|
211
|
+
error("character class intersection (&&) is not supported") if peek == "&" && @source[@pos + 1] == "&"
|
|
212
|
+
|
|
201
213
|
if peek == "\\"
|
|
202
214
|
advance
|
|
215
|
+
if CLASS_ESCAPES.key?(peek)
|
|
216
|
+
ranges = CLASS_ESCAPES[peek]
|
|
217
|
+
advance
|
|
218
|
+
return { type: :ranges, value: ranges }
|
|
219
|
+
end
|
|
220
|
+
error("\\#{peek} is not supported inside a character class") if %w[D W S].include?(peek)
|
|
221
|
+
|
|
203
222
|
bytes = parse_escape_char
|
|
204
223
|
if bytes.length == 1
|
|
205
224
|
return { type: :byte, value: bytes[0] }
|
|
@@ -285,12 +304,25 @@ module LexerKit
|
|
|
285
304
|
when nil
|
|
286
305
|
error("unexpected end of pattern after \\")
|
|
287
306
|
else
|
|
288
|
-
|
|
307
|
+
unsupported_escape! if peek.match?(/[0-9A-Za-z]/)
|
|
308
|
+
char = peek
|
|
289
309
|
advance
|
|
290
|
-
|
|
310
|
+
bytes = char.bytes
|
|
311
|
+
meta = meta_for_span(start_pos, @pos, literal_text: @source[start_pos...@pos], codepoint: char.ord, bytes: bytes)
|
|
312
|
+
if bytes.size == 1
|
|
313
|
+
Literal.new(byte: bytes[0], meta: meta)
|
|
314
|
+
else
|
|
315
|
+
Concat.new(children: bytes.map { |b| Literal.new(byte: b, meta: nil) }, meta: meta)
|
|
316
|
+
end
|
|
291
317
|
end
|
|
292
318
|
end
|
|
293
319
|
|
|
320
|
+
# A letter or digit escape that is not listed above: an anchor (\A, \z), a backreference
|
|
321
|
+
# (\1), \h, \p{...} and so on. Reading it as the plain character would change the pattern.
|
|
322
|
+
def unsupported_escape!
|
|
323
|
+
error("unsupported escape \\#{peek} at position #{@pos}")
|
|
324
|
+
end
|
|
325
|
+
|
|
294
326
|
def consume_hex_digits(count)
|
|
295
327
|
digits = +""
|
|
296
328
|
count.times do
|
|
@@ -335,9 +367,10 @@ module LexerKit
|
|
|
335
367
|
[codepoint].pack("U").bytes
|
|
336
368
|
when nil then error("unexpected end of pattern after \\")
|
|
337
369
|
else
|
|
338
|
-
|
|
370
|
+
unsupported_escape! if peek.match?(/[0-9A-Za-z]/)
|
|
371
|
+
char = peek
|
|
339
372
|
advance
|
|
340
|
-
|
|
373
|
+
char.bytes
|
|
341
374
|
end
|
|
342
375
|
end
|
|
343
376
|
|
data/lib/lexer_kit/errors.rb
CHANGED
|
@@ -8,7 +8,7 @@ module LexerKit
|
|
|
8
8
|
# Binary/File Errors
|
|
9
9
|
# ===========================================================================
|
|
10
10
|
|
|
11
|
-
# Raised when binary
|
|
11
|
+
# Raised when a program binary cannot be decoded, by the Ruby decoder or the native extension
|
|
12
12
|
class InvalidBinaryError < Error; end
|
|
13
13
|
|
|
14
14
|
# Raised when file integrity check fails (SHA256 mismatch, etc.)
|
|
@@ -68,7 +68,7 @@ module LexerKit
|
|
|
68
68
|
# Runtime Errors
|
|
69
69
|
# ===========================================================================
|
|
70
70
|
|
|
71
|
-
# Raised when
|
|
71
|
+
# Raised when the native extension is required but not loaded
|
|
72
72
|
class NativeExtensionError < Error; end
|
|
73
73
|
|
|
74
74
|
# Raised during VM execution (stack overflow, infinite loop, etc.)
|
|
@@ -62,14 +62,14 @@ module LexerKit
|
|
|
62
62
|
decoded = Decoder.new(bytes).decode_container
|
|
63
63
|
header = decoded[:header]
|
|
64
64
|
|
|
65
|
-
unless header[:format_version]
|
|
65
|
+
unless IR::Serializer::READABLE_FORMAT_VERSIONS.include?(header[:format_version])
|
|
66
66
|
raise LexerKit::IntegrityError, "Unsupported format version: #{header[:format_version]}"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
# from_binary loads the native program too
|
|
69
70
|
program = IR::CompiledProgram.from_binary(decoded[:payload])
|
|
70
|
-
program.load_native! if LexerKit.native?
|
|
71
71
|
new(program, meta: decoded[:meta])
|
|
72
|
-
rescue ArgumentError => e
|
|
72
|
+
rescue ArgumentError, LexerKit::InvalidBinaryError => e
|
|
73
73
|
raise LexerKit::IntegrityError, e.message
|
|
74
74
|
end
|
|
75
75
|
|
|
@@ -71,11 +71,13 @@ module LexerKit
|
|
|
71
71
|
raise LexerKit::IntegrityError, "Length mismatch: expected #{container[:uncompressed_len]}, got #{binary.bytesize}"
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
+
# from_binary loads the native program too
|
|
74
75
|
program = IR::CompiledProgram.from_binary(binary)
|
|
75
|
-
program.load_native! if LexerKit.native?
|
|
76
76
|
new(program)
|
|
77
77
|
rescue JSON::ParserError => e
|
|
78
78
|
raise LexerKit::IntegrityError, "Invalid JSON: #{e.message}"
|
|
79
|
+
rescue LexerKit::InvalidBinaryError => e
|
|
80
|
+
raise LexerKit::IntegrityError, e.message
|
|
79
81
|
rescue ArgumentError => e
|
|
80
82
|
# Base64 decode errors
|
|
81
83
|
raise LexerKit::IntegrityError, "Decoding error: #{e.message}"
|
data/lib/lexer_kit/format.rb
CHANGED
|
@@ -8,12 +8,12 @@ module LexerKit
|
|
|
8
8
|
# Supports LKT1 (JSON-based) and LKB1 (binary) formats.
|
|
9
9
|
#
|
|
10
10
|
# @example Loading a lexer
|
|
11
|
-
# program = LexerKit::Format::LKT1.load("lexer.lkt1")
|
|
12
|
-
# program = LexerKit::Format::LKB1.load("lexer.lkb1")
|
|
11
|
+
# program = LexerKit::Format::LKT1.load("lexer.lkt1").program
|
|
12
|
+
# program = LexerKit::Format::LKB1.load("lexer.lkb1").program
|
|
13
13
|
#
|
|
14
14
|
# @example Saving a lexer
|
|
15
|
-
# LexerKit::Format::LKT1.save(program, "lexer.lkt1")
|
|
16
|
-
# LexerKit::Format::LKB1.save(program, "lexer.lkb1")
|
|
15
|
+
# LexerKit::Format::LKT1.save(program, path: "lexer.lkt1")
|
|
16
|
+
# LexerKit::Format::LKB1.save(program, path: "lexer.lkb1")
|
|
17
17
|
module Format
|
|
18
18
|
end
|
|
19
19
|
end
|