lexer_kit 0.6.0 → 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/trie.rb +1 -1
- data/lib/lexer_kit/version.rb +1 -1
- data/lib/lexer_kit.rb +20 -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 +102 -8
- data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
- data/ext/lexer_kit_rust/src/dfa.rs +0 -217
|
@@ -90,16 +90,19 @@ module LexerKit
|
|
|
90
90
|
@mode_offsets[name]
|
|
91
91
|
end
|
|
92
92
|
|
|
93
|
-
# Low-level lexing with callback (for performance-critical code)
|
|
93
|
+
# Low-level lexing with callback (for performance-critical code).
|
|
94
|
+
# The tokens are collected before the block runs, so the block may lex again.
|
|
94
95
|
# @param bytes [String] input bytes
|
|
95
96
|
# @yield [Integer, Integer, Integer] token_id, start, length
|
|
97
|
+
# @return [nil, Enumerator] an Enumerator without a block
|
|
96
98
|
# @raise [NativeExtensionError] if Rust extension is not loaded
|
|
97
99
|
def lowlevel_each(bytes, &)
|
|
98
|
-
bytes
|
|
99
|
-
|
|
100
|
+
return enum_for(:lowlevel_each, bytes) unless block_given?
|
|
101
|
+
|
|
102
|
+
LexerKit.require_native!
|
|
100
103
|
|
|
101
104
|
ensure_rust_native!
|
|
102
|
-
|
|
105
|
+
each_rust_native(bytes.b, &)
|
|
103
106
|
end
|
|
104
107
|
|
|
105
108
|
# Check if a token ID is an error token
|
|
@@ -145,10 +148,11 @@ module LexerKit
|
|
|
145
148
|
# @return [Runner]
|
|
146
149
|
# @raise [NativeExtensionError] if Rust extension is not loaded
|
|
147
150
|
def stream(input, filename: nil)
|
|
148
|
-
|
|
151
|
+
LexerKit.require_native!
|
|
149
152
|
|
|
150
153
|
ensure_rust_native!
|
|
151
|
-
|
|
154
|
+
# The stream keeps its input: a copy, so a later change to the caller's String cannot change its tokens
|
|
155
|
+
lex_stream = create_rust_stream(input.frozen? ? input : input.dup.freeze)
|
|
152
156
|
Runner.new(self, lex_stream, filename: filename)
|
|
153
157
|
end
|
|
154
158
|
|
|
@@ -161,16 +165,22 @@ module LexerKit
|
|
|
161
165
|
bytes = input.b
|
|
162
166
|
source = Core::Source.new(bytes, filename: filename)
|
|
163
167
|
|
|
168
|
+
# Straight from the flat Array: a yield per token would cost as much as making the Token.
|
|
169
|
+
flat = native_tokens(bytes)
|
|
164
170
|
tokens = []
|
|
165
|
-
|
|
171
|
+
i = 0
|
|
172
|
+
n = flat.size
|
|
173
|
+
while i < n
|
|
174
|
+
tok_id = flat[i]
|
|
166
175
|
tokens << Core::Token.new(
|
|
167
176
|
id: tok_id,
|
|
168
177
|
name: token_name(tok_id),
|
|
169
|
-
start:
|
|
170
|
-
len:
|
|
178
|
+
start: flat[i + 1],
|
|
179
|
+
len: flat[i + 2],
|
|
171
180
|
source: source,
|
|
172
181
|
meta: @token_meta[tok_id]
|
|
173
182
|
)
|
|
183
|
+
i += 3
|
|
174
184
|
end
|
|
175
185
|
tokens
|
|
176
186
|
end
|
|
@@ -180,34 +190,27 @@ module LexerKit
|
|
|
180
190
|
def load_native!
|
|
181
191
|
return self unless LexerKit.native?
|
|
182
192
|
|
|
183
|
-
load_rust_native(
|
|
193
|
+
load_rust_native(to_binary)
|
|
184
194
|
self
|
|
185
195
|
end
|
|
186
196
|
|
|
187
|
-
# Convert to data format for Rust native loading
|
|
188
|
-
# @return [Hash] data for Rust extension
|
|
189
|
-
def to_native_data
|
|
190
|
-
{
|
|
191
|
-
instructions: @instructions.map(&:to_binary).join,
|
|
192
|
-
dfa_tables: @dfa_tables.map(&:to_native_format),
|
|
193
|
-
jump_tables: @jump_tables.map(&:to_native_format),
|
|
194
|
-
keyword_tables: @keyword_tables.map(&:to_native_format),
|
|
195
|
-
constant_pool: @constant_pool.entries,
|
|
196
|
-
modes: @mode_offsets.map { |name, offset| [name.to_s, offset] }
|
|
197
|
-
}
|
|
198
|
-
end
|
|
199
|
-
|
|
200
197
|
# Encode to binary
|
|
201
198
|
# @return [String]
|
|
202
199
|
def to_binary
|
|
203
200
|
Serializer.to_binary(self)
|
|
204
201
|
end
|
|
205
202
|
|
|
206
|
-
# Decode from binary
|
|
203
|
+
# Decode from binary. The program is ready to lex, so it may be frozen.
|
|
207
204
|
# @param bytes [String]
|
|
208
205
|
# @return [CompiledProgram]
|
|
209
206
|
def self.from_binary(bytes)
|
|
210
|
-
Serializer.from_binary(bytes)
|
|
207
|
+
Serializer.from_binary(bytes).load_native!
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# Loads the native program first: a frozen program cannot store it later.
|
|
211
|
+
def freeze
|
|
212
|
+
ensure_rust_native! if LexerKit.native? && !frozen?
|
|
213
|
+
super
|
|
211
214
|
end
|
|
212
215
|
|
|
213
216
|
def inspect
|
|
@@ -216,12 +219,22 @@ module LexerKit
|
|
|
216
219
|
|
|
217
220
|
private
|
|
218
221
|
|
|
222
|
+
# The tokens of bytes as one flat Array, [id, start, len, id, start, len, ...]: for
|
|
223
|
+
# tokenize, which makes a Token of each and so needs no yield.
|
|
224
|
+
# @raise [NativeExtensionError] if Rust extension is not loaded
|
|
225
|
+
def native_tokens(bytes)
|
|
226
|
+
LexerKit.require_native!
|
|
227
|
+
|
|
228
|
+
ensure_rust_native!
|
|
229
|
+
lex_rust_native(bytes)
|
|
230
|
+
end
|
|
231
|
+
|
|
219
232
|
# Ensure Rust native is loaded
|
|
220
233
|
# @api private
|
|
221
234
|
def ensure_rust_native!
|
|
222
235
|
return if respond_to?(:rust_native_loaded?) && rust_native_loaded?
|
|
223
236
|
|
|
224
|
-
load_rust_native(
|
|
237
|
+
load_rust_native(to_binary)
|
|
225
238
|
end
|
|
226
239
|
end
|
|
227
240
|
end
|
|
@@ -60,45 +60,67 @@ module LexerKit
|
|
|
60
60
|
@entries.size
|
|
61
61
|
end
|
|
62
62
|
|
|
63
|
-
# Encode to binary
|
|
63
|
+
# Encode to binary (format version 4: entry lengths are u32)
|
|
64
64
|
# @return [String]
|
|
65
|
+
# @raise [ArgumentError] if the count or an entry length does not fit its field
|
|
65
66
|
def to_binary
|
|
66
67
|
parts = []
|
|
67
68
|
|
|
68
69
|
# Count (u16)
|
|
69
|
-
parts << [@entries.size].pack("S>")
|
|
70
|
+
parts << [Limits.check!(@entries.size, Limits::U16_MAX, "number of constants")].pack("S>")
|
|
70
71
|
|
|
71
|
-
# Entries: [length (
|
|
72
|
+
# Entries: [length (u32), bytes...]
|
|
72
73
|
@entries.each do |entry|
|
|
73
|
-
parts << [entry.bytesize].pack("
|
|
74
|
+
parts << [Limits.check!(entry.bytesize, Limits::U32_MAX, "length of a constant")].pack("L>")
|
|
74
75
|
parts << entry
|
|
75
76
|
end
|
|
76
77
|
|
|
77
78
|
parts.join
|
|
78
79
|
end
|
|
79
80
|
|
|
80
|
-
# Decode from binary
|
|
81
|
+
# Decode from binary.
|
|
82
|
+
#
|
|
83
|
+
# Entries keep their positions: constant IDs in the instructions refer to them, so
|
|
84
|
+
# duplicates are kept as separate entries (as `add_uninterned` and `replace` make them).
|
|
81
85
|
# @param bytes [String]
|
|
86
|
+
# @param format_version [Integer] 3 (u16 entry lengths) or 4 (u32 entry lengths)
|
|
82
87
|
# @return [Array(ConstantPool, Integer)] [pool, bytes_consumed]
|
|
83
|
-
|
|
88
|
+
# @raise [ArgumentError] if the data is truncated
|
|
89
|
+
def self.from_binary(bytes, format_version: Serializer::FORMAT_VERSION)
|
|
90
|
+
length_format, length_size = format_version >= 4 ? ["L>", 4] : ["S>", 2]
|
|
84
91
|
pos = 0
|
|
85
92
|
|
|
86
|
-
count = bytes
|
|
93
|
+
count = read(bytes, pos, 2).unpack1("S>")
|
|
87
94
|
pos += 2
|
|
88
95
|
|
|
89
96
|
pool = new
|
|
90
97
|
count.times do
|
|
91
|
-
len = bytes
|
|
92
|
-
pos +=
|
|
98
|
+
len = read(bytes, pos, length_size).unpack1(length_format)
|
|
99
|
+
pos += length_size
|
|
93
100
|
|
|
94
|
-
|
|
95
|
-
pool.add(value)
|
|
101
|
+
pool.append_decoded(read(bytes, pos, len))
|
|
96
102
|
pos += len
|
|
97
103
|
end
|
|
98
104
|
|
|
99
105
|
[pool, pos]
|
|
100
106
|
end
|
|
101
107
|
|
|
108
|
+
def self.read(bytes, pos, len)
|
|
109
|
+
data = bytes.byteslice(pos, len)
|
|
110
|
+
raise ArgumentError, "constant pool is truncated" if data.nil? || data.bytesize != len
|
|
111
|
+
|
|
112
|
+
data
|
|
113
|
+
end
|
|
114
|
+
private_class_method :read
|
|
115
|
+
|
|
116
|
+
# Append a decoded entry at the next position (no interning).
|
|
117
|
+
# @api private
|
|
118
|
+
def append_decoded(value)
|
|
119
|
+
value = value.b.freeze
|
|
120
|
+
@index[value] = @entries.size unless @index.key?(value)
|
|
121
|
+
@entries << value
|
|
122
|
+
end
|
|
123
|
+
|
|
102
124
|
def inspect
|
|
103
125
|
"#<ConstantPool size=#{@entries.size}>"
|
|
104
126
|
end
|
|
@@ -5,9 +5,9 @@ module LexerKit
|
|
|
5
5
|
# DFATable represents a compiled DFA for pattern matching.
|
|
6
6
|
# Uses byte class compression to reduce table size.
|
|
7
7
|
class DFATable
|
|
8
|
-
# State 0 indicates no valid transition (must match
|
|
8
|
+
# State 0 indicates no valid transition (must match DFA_DEAD_STATE in crates/lexer_kit_core/src/types.rs)
|
|
9
9
|
DEAD_STATE = 0
|
|
10
|
-
# Token ID indicating non-accepting state (must match
|
|
10
|
+
# Token ID indicating non-accepting state (must match DFA_NO_ACCEPT in crates/lexer_kit_core/src/types.rs)
|
|
11
11
|
NO_ACCEPT = 0xFFFF
|
|
12
12
|
|
|
13
13
|
attr_reader :state_count, :class_count, :byte_class, :transitions, :accept_states
|
|
@@ -46,19 +46,25 @@ module LexerKit
|
|
|
46
46
|
parts = []
|
|
47
47
|
|
|
48
48
|
# Header: state_count (u16), class_count (u16)
|
|
49
|
+
Limits.check!(@state_count, Limits::U16_MAX, "number of DFA states")
|
|
50
|
+
Limits.check!(@class_count, Limits::U16_MAX, "number of DFA byte classes")
|
|
49
51
|
parts << [@state_count, @class_count].pack("S>S>")
|
|
50
52
|
|
|
51
53
|
# Byte class table (256 bytes)
|
|
52
54
|
parts << @byte_class.pack("C256")
|
|
53
55
|
|
|
54
56
|
# Transitions (state_count × class_count × u16)
|
|
57
|
+
max_target = @transitions.max
|
|
58
|
+
Limits.check!(max_target, Limits::U16_MAX, "DFA transition target") if max_target
|
|
55
59
|
parts << @transitions.pack("S>*")
|
|
56
60
|
|
|
57
61
|
# Accept states count (u16)
|
|
58
|
-
parts << [@accept_states.size].pack("S>")
|
|
62
|
+
parts << [Limits.check!(@accept_states.size, Limits::U16_MAX, "number of DFA accept states")].pack("S>")
|
|
59
63
|
|
|
60
64
|
# Accept states: [state (u16), token_id (u16)] pairs
|
|
61
65
|
@accept_states.each do |state, token_id|
|
|
66
|
+
Limits.check!(state, Limits::U16_MAX, "DFA accept state")
|
|
67
|
+
Limits.check!(token_id, Limits::U16_MAX, "token ID of a DFA accept state")
|
|
62
68
|
parts << [state, token_id].pack("S>S>")
|
|
63
69
|
end
|
|
64
70
|
|
|
@@ -104,22 +110,6 @@ module LexerKit
|
|
|
104
110
|
def inspect
|
|
105
111
|
"#<DFATable states=#{@state_count} classes=#{@class_count} accepts=#{@accept_states.size}>"
|
|
106
112
|
end
|
|
107
|
-
|
|
108
|
-
# Convert to format suitable for C native loading
|
|
109
|
-
# Returns dense accept_tokens array for O(1) lookup
|
|
110
|
-
# @return [Hash] data for C extension
|
|
111
|
-
def to_native_format
|
|
112
|
-
# Build dense array: accept_tokens[state] = token_id
|
|
113
|
-
accept_tokens = Array.new(@state_count, NO_ACCEPT)
|
|
114
|
-
@accept_states.each { |state, token_id| accept_tokens[state] = token_id }
|
|
115
|
-
{
|
|
116
|
-
state_count: @state_count,
|
|
117
|
-
class_count: @class_count,
|
|
118
|
-
byte_class: @byte_class.pack("C*"),
|
|
119
|
-
transitions: @transitions.pack("S>*"),
|
|
120
|
-
accept_tokens: accept_tokens.pack("S>*")
|
|
121
|
-
}
|
|
122
|
-
end
|
|
123
113
|
end
|
|
124
114
|
end
|
|
125
115
|
end
|
|
@@ -16,7 +16,10 @@ module LexerKit
|
|
|
16
16
|
|
|
17
17
|
# Encode to binary (4 bytes: 1 opcode + 3 arg)
|
|
18
18
|
# @return [String] binary representation
|
|
19
|
+
# @raise [ArgumentError] if the opcode or argument does not fit (u8, u24)
|
|
19
20
|
def to_binary
|
|
21
|
+
Limits.check!(@opcode, Limits::U8_MAX, "opcode")
|
|
22
|
+
Limits.check!(@arg, Limits::U24_MAX, "operand of #{Opcode.name(@opcode)}")
|
|
20
23
|
[
|
|
21
24
|
@opcode,
|
|
22
25
|
(@arg >> 16) & 0xFF,
|
|
@@ -24,6 +24,7 @@ module LexerKit
|
|
|
24
24
|
# Encode to binary
|
|
25
25
|
# @return [String]
|
|
26
26
|
def to_binary
|
|
27
|
+
Limits.check!(@default_offset, Limits::U24_MAX, "jump table default offset")
|
|
27
28
|
parts = []
|
|
28
29
|
|
|
29
30
|
# Default offset (u24)
|
|
@@ -38,6 +39,8 @@ module LexerKit
|
|
|
38
39
|
|
|
39
40
|
# Entries: [byte (u8), offset (u24)] pairs
|
|
40
41
|
@entries.each do |byte, offset|
|
|
42
|
+
Limits.check!(byte, Limits::U8_MAX, "jump table byte")
|
|
43
|
+
Limits.check!(offset, Limits::U24_MAX, "jump table offset")
|
|
41
44
|
parts << [
|
|
42
45
|
byte,
|
|
43
46
|
(offset >> 16) & 0xFF,
|
|
@@ -76,19 +79,6 @@ module LexerKit
|
|
|
76
79
|
def inspect
|
|
77
80
|
"#<JumpTable entries=#{@entries.size} default=#{@default_offset}>"
|
|
78
81
|
end
|
|
79
|
-
|
|
80
|
-
# Convert to format suitable for C native loading
|
|
81
|
-
# Returns dense 256-entry lookup table for O(1) access
|
|
82
|
-
# @return [Hash] data for C extension
|
|
83
|
-
def to_native_format
|
|
84
|
-
# Build dense lookup table: all 256 bytes → offset
|
|
85
|
-
lookup = Array.new(256, @default_offset)
|
|
86
|
-
@entries.each { |byte, offset| lookup[byte] = offset }
|
|
87
|
-
{
|
|
88
|
-
lookup: lookup.pack("L>*"), # 256 * 4 = 1024 bytes
|
|
89
|
-
default_offset: @default_offset # For EOF case
|
|
90
|
-
}
|
|
91
|
-
end
|
|
92
82
|
end
|
|
93
83
|
end
|
|
94
84
|
end
|
|
@@ -61,15 +61,6 @@ module LexerKit
|
|
|
61
61
|
parts.join
|
|
62
62
|
end
|
|
63
63
|
|
|
64
|
-
# Convert to format suitable for C native loading
|
|
65
|
-
# @return [Hash] data for C extension
|
|
66
|
-
def to_native_format
|
|
67
|
-
{
|
|
68
|
-
base_token_id: @base_token_id,
|
|
69
|
-
keywords: @keywords.map { |key, token_id| [key.b, token_id] }
|
|
70
|
-
}
|
|
71
|
-
end
|
|
72
|
-
|
|
73
64
|
# Decode from binary
|
|
74
65
|
# @param bytes [String]
|
|
75
66
|
# @return [Array(KeywordTable, Integer)] table and consumed bytes
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LexerKit
|
|
4
|
+
module IR
|
|
5
|
+
# Widths of the fields of the program binary (docs/ir-spec.md §5).
|
|
6
|
+
#
|
|
7
|
+
# `pack` truncates a value that does not fit its field instead of raising, so every
|
|
8
|
+
# encoder checks its values here first. A value that does not fit is an error, never
|
|
9
|
+
# a silently different program.
|
|
10
|
+
module Limits
|
|
11
|
+
U8_MAX = 0xFF
|
|
12
|
+
U16_MAX = 0xFFFF
|
|
13
|
+
U24_MAX = 0xFF_FFFF
|
|
14
|
+
U32_MAX = 0xFFFF_FFFF
|
|
15
|
+
|
|
16
|
+
# Packed instruction arguments: (upper << 14) | lower (docs/ir-spec.md §3).
|
|
17
|
+
PACKED_UPPER_MAX = 0x3FF # 10 bits
|
|
18
|
+
PACKED_LOWER_MAX = 0x3FFF # 14 bits
|
|
19
|
+
PACKED_LOWER_BITS = 14
|
|
20
|
+
|
|
21
|
+
module_function
|
|
22
|
+
|
|
23
|
+
# @param value [Integer]
|
|
24
|
+
# @param max [Integer]
|
|
25
|
+
# @param what [String] what the value is, for the error message
|
|
26
|
+
# @return [Integer] value
|
|
27
|
+
# @raise [ArgumentError] if value is not an integer in 0..max
|
|
28
|
+
def check!(value, max, what)
|
|
29
|
+
return value if value.is_a?(Integer) && value >= 0 && value <= max
|
|
30
|
+
|
|
31
|
+
raise ArgumentError, "#{what} (#{value.inspect}) does not fit in the program binary: the maximum is #{max}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# UTF-8 bytes of a string whose length is written as u16.
|
|
35
|
+
# @return [String] binary string
|
|
36
|
+
# @raise [ArgumentError] if the string is too long or cannot be converted to UTF-8
|
|
37
|
+
def u16_string!(string, what)
|
|
38
|
+
bytes = begin
|
|
39
|
+
string.to_s.encode(Encoding::UTF_8).b
|
|
40
|
+
rescue EncodingError => e
|
|
41
|
+
raise ArgumentError, "#{what} (#{string.inspect}) cannot be written as UTF-8: #{e.message}"
|
|
42
|
+
end
|
|
43
|
+
check!(bytes.bytesize, U16_MAX, "length of #{what}")
|
|
44
|
+
bytes
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
3
5
|
module LexerKit
|
|
4
6
|
module IR
|
|
5
7
|
# Serializer handles binary encoding/decoding of CompiledProgram.
|
|
6
8
|
# Extracted from CompiledProgram to separate serialization concerns.
|
|
7
9
|
class Serializer
|
|
8
10
|
MAGIC = "LKT1"
|
|
9
|
-
|
|
11
|
+
# v3: added token_meta
|
|
12
|
+
# v4: constant lengths are u32 (v3 wrote u16 and truncated longer constants)
|
|
13
|
+
FORMAT_VERSION = 4
|
|
14
|
+
# Versions decode reads. Version 3 is read so that artifacts made by lexer_kit 0.6
|
|
15
|
+
# and earlier keep working; encode writes FORMAT_VERSION only.
|
|
16
|
+
READABLE_FORMAT_VERSIONS = [3, 4].freeze
|
|
10
17
|
|
|
11
18
|
# Encode a CompiledProgram to binary format.
|
|
12
19
|
# @param program [CompiledProgram]
|
|
@@ -18,8 +25,14 @@ module LexerKit
|
|
|
18
25
|
# Decode binary data to a CompiledProgram.
|
|
19
26
|
# @param bytes [String] binary data
|
|
20
27
|
# @return [CompiledProgram]
|
|
28
|
+
# @raise [InvalidBinaryError] if the binary is malformed
|
|
21
29
|
def self.from_binary(bytes)
|
|
22
30
|
new.decode(bytes)
|
|
31
|
+
rescue InvalidBinaryError
|
|
32
|
+
raise
|
|
33
|
+
rescue ArgumentError, TypeError, NoMethodError, RangeError, IndexError, EncodingError, JSON::ParserError => e
|
|
34
|
+
# The table decoders read untrusted bytes: whatever a malformed binary breaks becomes one error
|
|
35
|
+
raise InvalidBinaryError, "invalid program binary: #{e.message}"
|
|
23
36
|
end
|
|
24
37
|
|
|
25
38
|
# Encode CompiledProgram to binary.
|
|
@@ -35,44 +48,45 @@ module LexerKit
|
|
|
35
48
|
parts << [FORMAT_VERSION].pack("S>")
|
|
36
49
|
|
|
37
50
|
# User version (u32)
|
|
38
|
-
parts << [program.version].pack("L>")
|
|
51
|
+
parts << [Limits.check!(program.version, Limits::U32_MAX, "user version")].pack("L>")
|
|
39
52
|
|
|
40
53
|
# Token names
|
|
41
|
-
encode_string_array(parts, program.token_names)
|
|
54
|
+
encode_string_array(parts, program.token_names, "token name")
|
|
42
55
|
|
|
43
56
|
# Mode names
|
|
44
|
-
encode_string_array(parts, program.mode_names)
|
|
57
|
+
encode_string_array(parts, program.mode_names, "mode name")
|
|
45
58
|
|
|
46
|
-
# Modes (name → offset
|
|
59
|
+
# Modes (name → offset), in mode ID order: PUSH_MODE operands index this list
|
|
47
60
|
modes = program.mode_offsets
|
|
48
|
-
|
|
61
|
+
check_mode_order!(program.mode_names, modes.keys)
|
|
62
|
+
parts << [Limits.check!(modes.size, Limits::U16_MAX, "number of modes")].pack("S>")
|
|
49
63
|
modes.each do |name, offset|
|
|
50
|
-
name_bytes = name
|
|
64
|
+
name_bytes = Limits.u16_string!(name, "a mode name")
|
|
51
65
|
parts << [name_bytes.bytesize].pack("S>")
|
|
52
66
|
parts << name_bytes
|
|
53
|
-
parts << [offset].pack("L>")
|
|
67
|
+
parts << [Limits.check!(offset, Limits::U32_MAX, "offset of mode #{name}")].pack("L>")
|
|
54
68
|
end
|
|
55
69
|
|
|
56
70
|
# Constant pool
|
|
57
71
|
pool_binary = program.constant_pool.to_binary
|
|
58
|
-
parts << [pool_binary.bytesize].pack("L>")
|
|
72
|
+
parts << [Limits.check!(pool_binary.bytesize, Limits::U32_MAX, "size of the constant pool")].pack("L>")
|
|
59
73
|
parts << pool_binary
|
|
60
74
|
|
|
61
75
|
# DFA tables
|
|
62
|
-
encode_table_array(parts, program.dfa_tables)
|
|
76
|
+
encode_table_array(parts, program.dfa_tables, "DFA table")
|
|
63
77
|
|
|
64
78
|
# Jump tables
|
|
65
|
-
encode_table_array(parts, program.jump_tables)
|
|
79
|
+
encode_table_array(parts, program.jump_tables, "jump table")
|
|
66
80
|
|
|
67
81
|
# Keyword tables
|
|
68
|
-
encode_table_array(parts, program.keyword_tables)
|
|
82
|
+
encode_table_array(parts, program.keyword_tables, "keyword table")
|
|
69
83
|
|
|
70
84
|
# Token metadata
|
|
71
85
|
encode_token_meta(parts, program.token_meta)
|
|
72
86
|
|
|
73
87
|
# Instructions
|
|
74
88
|
instructions = program.instructions
|
|
75
|
-
parts << [instructions.size].pack("L>")
|
|
89
|
+
parts << [Limits.check!(instructions.size, Limits::U32_MAX, "number of instructions")].pack("L>")
|
|
76
90
|
instructions.each do |instr|
|
|
77
91
|
parts << instr.to_binary
|
|
78
92
|
end
|
|
@@ -93,7 +107,9 @@ module LexerKit
|
|
|
93
107
|
|
|
94
108
|
# Format version
|
|
95
109
|
format_version = read_uint16
|
|
96
|
-
|
|
110
|
+
unless READABLE_FORMAT_VERSIONS.include?(format_version)
|
|
111
|
+
raise ArgumentError, "unsupported format version: #{format_version}"
|
|
112
|
+
end
|
|
97
113
|
|
|
98
114
|
# User version
|
|
99
115
|
version = read_uint32
|
|
@@ -108,14 +124,18 @@ module LexerKit
|
|
|
108
124
|
mode_count = read_uint16
|
|
109
125
|
modes = {}
|
|
110
126
|
mode_count.times do
|
|
111
|
-
name =
|
|
127
|
+
name = read_symbol
|
|
112
128
|
offset = read_uint32
|
|
113
129
|
modes[name] = offset
|
|
114
130
|
end
|
|
115
131
|
|
|
132
|
+
check_mode_order!(mode_names, modes.keys)
|
|
133
|
+
|
|
116
134
|
# Constant pool
|
|
117
135
|
pool_len = read_uint32
|
|
118
|
-
|
|
136
|
+
pool_bytes = read_bytes(pool_len)
|
|
137
|
+
constant_pool, consumed = ConstantPool.from_binary(pool_bytes, format_version: format_version)
|
|
138
|
+
raise ArgumentError, "trailing bytes after the constant pool" unless consumed == pool_bytes.bytesize
|
|
119
139
|
|
|
120
140
|
# DFA tables
|
|
121
141
|
dfa_tables = decode_table_array(DFATable)
|
|
@@ -127,7 +147,7 @@ module LexerKit
|
|
|
127
147
|
keyword_tables = decode_table_array(KeywordTable)
|
|
128
148
|
|
|
129
149
|
# Token metadata
|
|
130
|
-
token_meta =
|
|
150
|
+
token_meta = decode_token_meta
|
|
131
151
|
|
|
132
152
|
# Instructions
|
|
133
153
|
instr_count = read_uint32
|
|
@@ -135,6 +155,7 @@ module LexerKit
|
|
|
135
155
|
instr_count.times do
|
|
136
156
|
instructions << Instruction.from_binary(read_bytes(4))
|
|
137
157
|
end
|
|
158
|
+
raise ArgumentError, "trailing bytes after the program" unless @pos == @bytes.bytesize
|
|
138
159
|
|
|
139
160
|
CompiledProgram.new(
|
|
140
161
|
instructions: instructions,
|
|
@@ -152,32 +173,51 @@ module LexerKit
|
|
|
152
173
|
|
|
153
174
|
private
|
|
154
175
|
|
|
176
|
+
# The mode list (name → offset) must be in mode ID order, the order of mode_names:
|
|
177
|
+
# PUSH_MODE operands are mode IDs, and the VM finds a mode's offset by its position in
|
|
178
|
+
# the mode list. A program whose two lists disagree would enter the wrong mode, so it is
|
|
179
|
+
# rejected rather than reordered.
|
|
180
|
+
def check_mode_order!(mode_names, mode_list_names)
|
|
181
|
+
return if mode_names.map(&:to_sym) == mode_list_names.map(&:to_sym)
|
|
182
|
+
|
|
183
|
+
raise ArgumentError,
|
|
184
|
+
"mode list #{mode_list_names.inspect} is not in mode ID order #{mode_names.inspect}"
|
|
185
|
+
end
|
|
186
|
+
|
|
155
187
|
# Encode an array of symbol names as length-prefixed strings.
|
|
156
|
-
def encode_string_array(parts, names)
|
|
157
|
-
parts << [names.size].pack("S>")
|
|
188
|
+
def encode_string_array(parts, names, what)
|
|
189
|
+
parts << [Limits.check!(names.size, Limits::U16_MAX, "number of #{what}s")].pack("S>")
|
|
158
190
|
names.each do |name|
|
|
159
|
-
name_bytes = name
|
|
191
|
+
name_bytes = Limits.u16_string!(name, "a #{what}")
|
|
160
192
|
parts << [name_bytes.bytesize].pack("S>")
|
|
161
193
|
parts << name_bytes
|
|
162
194
|
end
|
|
163
195
|
end
|
|
164
196
|
|
|
165
197
|
# Encode an array of tables (DFA, Jump, Keyword).
|
|
166
|
-
def encode_table_array(parts, tables)
|
|
167
|
-
parts << [tables.size].pack("S>")
|
|
198
|
+
def encode_table_array(parts, tables, what)
|
|
199
|
+
parts << [Limits.check!(tables.size, Limits::U16_MAX, "number of #{what}s")].pack("S>")
|
|
168
200
|
tables.each do |table|
|
|
169
201
|
table_binary = table.to_binary
|
|
170
|
-
parts << [table_binary.bytesize].pack("L>")
|
|
202
|
+
parts << [Limits.check!(table_binary.bytesize, Limits::U32_MAX, "size of a #{what}")].pack("L>")
|
|
171
203
|
parts << table_binary
|
|
172
204
|
end
|
|
173
205
|
end
|
|
174
206
|
|
|
207
|
+
# Decode a name, written as its UTF-8 bytes.
|
|
208
|
+
def read_symbol
|
|
209
|
+
name = read_length_prefixed_string.force_encoding(Encoding::UTF_8)
|
|
210
|
+
raise ArgumentError, "name is not valid UTF-8: #{name.b.inspect}" unless name.valid_encoding?
|
|
211
|
+
|
|
212
|
+
name.to_sym
|
|
213
|
+
end
|
|
214
|
+
|
|
175
215
|
# Decode an array of symbols.
|
|
176
216
|
def decode_symbol_array
|
|
177
217
|
count = read_uint16
|
|
178
218
|
symbols = []
|
|
179
219
|
count.times do
|
|
180
|
-
symbols <<
|
|
220
|
+
symbols << read_symbol
|
|
181
221
|
end
|
|
182
222
|
symbols
|
|
183
223
|
end
|
|
@@ -197,6 +237,8 @@ module LexerKit
|
|
|
197
237
|
# Read raw bytes.
|
|
198
238
|
def read_bytes(len)
|
|
199
239
|
data = @bytes.byteslice(@pos, len)
|
|
240
|
+
raise ArgumentError, "program binary is truncated at byte #{@pos}" if data.nil? || data.bytesize != len
|
|
241
|
+
|
|
200
242
|
@pos += len
|
|
201
243
|
data
|
|
202
244
|
end
|
|
@@ -220,19 +262,16 @@ module LexerKit
|
|
|
220
262
|
# Encode token metadata hash.
|
|
221
263
|
# Format: [count: u16] [token_id: u16, json_len: u16, json: bytes]×n
|
|
222
264
|
def encode_token_meta(parts, token_meta)
|
|
223
|
-
|
|
224
|
-
parts << [token_meta.size].pack("S>")
|
|
265
|
+
parts << [Limits.check!(token_meta.size, Limits::U16_MAX, "number of token metadata entries")].pack("S>")
|
|
225
266
|
token_meta.each do |token_id, meta|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
parts << [token_id, json_bytes.bytesize].pack("S>S>")
|
|
267
|
+
json_bytes = Limits.u16_string!(JSON.generate(meta), "token metadata")
|
|
268
|
+
parts << [Limits.check!(token_id, Limits::U16_MAX, "token ID of token metadata"), json_bytes.bytesize].pack("S>S>")
|
|
229
269
|
parts << json_bytes
|
|
230
270
|
end
|
|
231
271
|
end
|
|
232
272
|
|
|
233
273
|
# Decode token metadata hash.
|
|
234
274
|
def decode_token_meta
|
|
235
|
-
require "json"
|
|
236
275
|
count = read_uint16
|
|
237
276
|
token_meta = {}
|
|
238
277
|
count.times do
|
data/lib/lexer_kit/ir.rb
CHANGED
data/lib/lexer_kit/trie.rb
CHANGED
|
@@ -161,7 +161,7 @@ module LexerKit
|
|
|
161
161
|
return if labels.empty?
|
|
162
162
|
|
|
163
163
|
# Check for Integer keys (ambiguous with direct offsets)
|
|
164
|
-
integer_keys = labels.keys.
|
|
164
|
+
integer_keys = labels.keys.grep(Integer)
|
|
165
165
|
return if integer_keys.empty?
|
|
166
166
|
|
|
167
167
|
raise ArgumentError, "labels must have Symbol keys only (Integer keys are ambiguous), found Integer keys: #{integer_keys.inspect}"
|
data/lib/lexer_kit/version.rb
CHANGED