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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//! What a String's object header says of it, read straight from the header:
|
|
2
|
+
//! its cached coderange and its encoding index. The one place the layer
|
|
3
|
+
//! reads `RBasic`'s flags for a String, so what rests on Ruby's object
|
|
4
|
+
//! layout is read here and nowhere else; what a reader makes of the answers
|
|
5
|
+
//! (`string::read`, `encoding`) is theirs.
|
|
6
|
+
|
|
7
|
+
#![allow(unsafe_code)]
|
|
8
|
+
|
|
9
|
+
use core::ffi::c_int;
|
|
10
|
+
|
|
11
|
+
use super::{AsSys, RString};
|
|
12
|
+
|
|
13
|
+
/// A String's coderange, as Ruby caches it.
|
|
14
|
+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
15
|
+
pub(crate) enum Coderange {
|
|
16
|
+
Unknown,
|
|
17
|
+
SevenBit,
|
|
18
|
+
Valid,
|
|
19
|
+
Broken,
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl RString {
|
|
23
|
+
/// The header's flags word.
|
|
24
|
+
#[inline]
|
|
25
|
+
fn flags(self) -> usize {
|
|
26
|
+
// SAFETY: a String is a heap object, whose header starts with
|
|
27
|
+
// `RBasic`; reading its flags runs no Ruby.
|
|
28
|
+
unsafe { nupur_sys::non_raising::rbasic_flags(self.as_sys()) }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/// The String's cached coderange - a read of its header flags, which
|
|
32
|
+
/// never scans and never allocates.
|
|
33
|
+
#[inline]
|
|
34
|
+
pub(crate) fn enc_coderange(self) -> Coderange {
|
|
35
|
+
const MASK: usize = nupur_sys::ruby_coderange_type::RUBY_ENC_CODERANGE_MASK as usize;
|
|
36
|
+
const SEVEN: usize = nupur_sys::ruby_coderange_type::RUBY_ENC_CODERANGE_7BIT as usize;
|
|
37
|
+
const VALID: usize = nupur_sys::ruby_coderange_type::RUBY_ENC_CODERANGE_VALID as usize;
|
|
38
|
+
const BROKEN: usize = nupur_sys::ruby_coderange_type::RUBY_ENC_CODERANGE_BROKEN as usize;
|
|
39
|
+
match self.flags() & MASK {
|
|
40
|
+
SEVEN => Coderange::SevenBit,
|
|
41
|
+
VALID => Coderange::Valid,
|
|
42
|
+
BROKEN => Coderange::Broken,
|
|
43
|
+
_ => Coderange::Unknown,
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// The String's encoding index, read from its flags where Ruby keeps it
|
|
48
|
+
/// inline (every encoding below `RUBY_ENCODING_INLINE_MAX`, the builtin
|
|
49
|
+
/// three among them), and looked up only past that.
|
|
50
|
+
#[inline]
|
|
51
|
+
pub(crate) fn encoding_index(self) -> c_int {
|
|
52
|
+
use nupur_sys::ruby_encoding_consts::{
|
|
53
|
+
RUBY_ENCODING_INLINE_MAX, RUBY_ENCODING_MASK, RUBY_ENCODING_SHIFT,
|
|
54
|
+
};
|
|
55
|
+
/* Seven bits, which a `c_int` holds. */
|
|
56
|
+
let bits = (self.flags() & RUBY_ENCODING_MASK as usize) >> RUBY_ENCODING_SHIFT as u32;
|
|
57
|
+
let inline = c_int::try_from(bits).unwrap_or(c_int::MAX);
|
|
58
|
+
if inline == RUBY_ENCODING_INLINE_MAX as c_int {
|
|
59
|
+
// SAFETY: a live String, by type.
|
|
60
|
+
unsafe { nupur_sys::non_raising::enc_get_index_of_string(self.as_sys()) }
|
|
61
|
+
} else {
|
|
62
|
+
inline
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//! Interned names: an [`Id`] as `rb_intern` makes one, and the [`HiddenId`]
|
|
2
|
+
//! of an instance variable no Ruby code can reach.
|
|
3
|
+
|
|
4
|
+
use crate::rb::error::Error;
|
|
5
|
+
use crate::rb::ruby::Ruby;
|
|
6
|
+
|
|
7
|
+
/// An interned name - a method's, an ivar's, a constant's - as `rb_intern`
|
|
8
|
+
/// makes it (`raw::intern`). Only the layer turns one into the raw `ID` it
|
|
9
|
+
/// hands C, so a caller can keep and compare one but never forge one.
|
|
10
|
+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
11
|
+
#[repr(transparent)]
|
|
12
|
+
pub struct Id(nupur_sys::ID);
|
|
13
|
+
|
|
14
|
+
impl Id {
|
|
15
|
+
/// The interned name `name` (`rb_intern`); a new name grows the symbol
|
|
16
|
+
/// table, so it can raise `NoMemoryError`.
|
|
17
|
+
pub fn intern(ruby: &Ruby, name: &core::ffi::CStr) -> Result<Id, Error> {
|
|
18
|
+
crate::raw::intern_cstr(ruby, name)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
#[inline]
|
|
22
|
+
pub(crate) const fn from_raw(id: nupur_sys::ID) -> Id {
|
|
23
|
+
Id(id)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#[inline]
|
|
27
|
+
pub(crate) const fn as_raw(self) -> nupur_sys::ID {
|
|
28
|
+
self.0
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/// The name of a hidden instance variable: an [`Id`] interned from a name
|
|
33
|
+
/// that does not start with `@`, so no Ruby code can read or write it.
|
|
34
|
+
///
|
|
35
|
+
/// What the ivar reads and writes take ([`crate::rb::object::ivar_get`],
|
|
36
|
+
/// [`crate::rb::object::ivar_set`]): Ruby's Ractor checks on an ivar - of a
|
|
37
|
+
/// class or module, or of a shareable object, from another Ractor - apply to
|
|
38
|
+
/// an `@` name alone (`rb_is_instance_id`, in every release from 3.3), so
|
|
39
|
+
/// with one of these the read raises nothing in any Ractor.
|
|
40
|
+
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
41
|
+
#[repr(transparent)]
|
|
42
|
+
pub struct HiddenId(Id);
|
|
43
|
+
|
|
44
|
+
impl HiddenId {
|
|
45
|
+
/// The hidden name `name` (`rb_intern`): `ArgumentError` for one that
|
|
46
|
+
/// starts with `@`, and `NoMemoryError` as [`Id::intern`].
|
|
47
|
+
pub fn intern(ruby: &Ruby, name: &core::ffi::CStr) -> Result<HiddenId, Error> {
|
|
48
|
+
if name.to_bytes().first() == Some(&b'@') {
|
|
49
|
+
return Err(Error::new(
|
|
50
|
+
crate::rb::Builtin::ArgumentError,
|
|
51
|
+
"a hidden ivar's name does not start with @",
|
|
52
|
+
));
|
|
53
|
+
}
|
|
54
|
+
Id::intern(ruby, name).map(HiddenId)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#[inline]
|
|
58
|
+
pub(crate) const fn as_raw(self) -> nupur_sys::ID {
|
|
59
|
+
self.0.as_raw()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//! An Integer read as a Rust number - checked, never cut - or written as
|
|
2
|
+
//! decimal digits: a Fixnum from its own bits, a Bignum by Ruby.
|
|
3
|
+
|
|
4
|
+
#![allow(unsafe_code)]
|
|
5
|
+
|
|
6
|
+
use nupur_sys::non_raising;
|
|
7
|
+
|
|
8
|
+
use super::{Integer, RString};
|
|
9
|
+
use crate::rb::error::{Builtin, Error};
|
|
10
|
+
use crate::rb::ruby::Ruby;
|
|
11
|
+
|
|
12
|
+
impl Integer {
|
|
13
|
+
/// As a `u64`; a negative value or one out of range is `RangeError`.
|
|
14
|
+
#[inline]
|
|
15
|
+
pub fn to_u64(self) -> Result<u64, Error> {
|
|
16
|
+
if self.is_negative() {
|
|
17
|
+
return Err(Error::new(
|
|
18
|
+
Builtin::RangeError,
|
|
19
|
+
"can't convert negative integer to unsigned",
|
|
20
|
+
));
|
|
21
|
+
}
|
|
22
|
+
if non_raising::fixnum_p(self.0 .0) {
|
|
23
|
+
/* Not negative, checked above. */
|
|
24
|
+
return u64::try_from(non_raising::fix2long(self.0 .0))
|
|
25
|
+
.map_err(|_| crate::rb::error::internal_error("a negative Fixnum"));
|
|
26
|
+
}
|
|
27
|
+
crate::raw::big2ull(self)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/// As an `i64`; a value out of its range is Ruby's `RangeError`.
|
|
31
|
+
#[inline]
|
|
32
|
+
pub fn to_i64(self) -> Result<i64, Error> {
|
|
33
|
+
if non_raising::fixnum_p(self.0 .0) {
|
|
34
|
+
return Ok(non_raising::fix2long(self.0 .0));
|
|
35
|
+
}
|
|
36
|
+
crate::raw::big2ll(self)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// As a `usize`; a negative value or one out of range is `RangeError`.
|
|
40
|
+
#[inline]
|
|
41
|
+
pub fn to_usize(self) -> Result<usize, Error> {
|
|
42
|
+
/* Through `to_u64`, so a value past a narrower `usize` is refused
|
|
43
|
+
* rather than truncated. */
|
|
44
|
+
usize::try_from(self.to_u64()?).map_err(|_| {
|
|
45
|
+
Error::new(
|
|
46
|
+
Builtin::RangeError,
|
|
47
|
+
"integer too big to convert into a usize",
|
|
48
|
+
)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// As a `usize`, or `None` when it is negative or past one - and only
|
|
53
|
+
/// then: any other failure of the conversion is passed on, never read as
|
|
54
|
+
/// "out of range" (for a limit, that would lift it). "Past one" is
|
|
55
|
+
/// decided from the value's size, so no `RangeError` is made and dropped.
|
|
56
|
+
pub fn to_usize_within(self) -> Result<Option<usize>, Error> {
|
|
57
|
+
if self.is_negative() {
|
|
58
|
+
return Ok(None);
|
|
59
|
+
}
|
|
60
|
+
if !non_raising::fixnum_p(self.0 .0) && !self.magnitude_fits_u64() {
|
|
61
|
+
return Ok(None);
|
|
62
|
+
}
|
|
63
|
+
Ok(usize::try_from(self.to_u64()?).ok())
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/// Whether a Bignum's magnitude fits a `u64`: `rb_absint_size`, which
|
|
67
|
+
/// reads its digits and raises nothing - so "too large" is answered
|
|
68
|
+
/// without making the `RangeError` `rb_big2ull` would raise.
|
|
69
|
+
#[inline]
|
|
70
|
+
fn magnitude_fits_u64(self) -> bool {
|
|
71
|
+
// SAFETY: `self` is a live Integer, by type.
|
|
72
|
+
let bytes = unsafe { non_raising::absint_size_of_integer(self.0 .0) };
|
|
73
|
+
u64::try_from(bytes).is_ok_and(|b| b <= 8)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/// The decimal digits, with a `-` for a negative value, as a new
|
|
77
|
+
/// String: a Fixnum written here, a Bignum by Ruby (`rb_big2str`). A
|
|
78
|
+
/// failed allocation is `NoMemoryError`.
|
|
79
|
+
pub fn to_decimal(self, ruby: &Ruby) -> Result<RString, Error> {
|
|
80
|
+
if non_raising::fixnum_p(self.0 .0) {
|
|
81
|
+
let mut buf = [0u8; 24];
|
|
82
|
+
let n = write_i64(non_raising::fix2long(self.0 .0), &mut buf);
|
|
83
|
+
/* Digits and a sign: ASCII, which `from_utf8` confirms in a step. */
|
|
84
|
+
let digits = core::str::from_utf8(buf.get(..n).unwrap_or_default())
|
|
85
|
+
.map_err(|_| crate::rb::error::internal_error("a Fixnum's digits are not ASCII"))?;
|
|
86
|
+
return crate::string::utf8_str(ruby, digits);
|
|
87
|
+
}
|
|
88
|
+
crate::raw::big2str(self)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/// Below zero? Reads the sign, converts nothing.
|
|
92
|
+
#[inline]
|
|
93
|
+
pub fn is_negative(self) -> bool {
|
|
94
|
+
if non_raising::fixnum_p(self.0 .0) {
|
|
95
|
+
return non_raising::fix2long(self.0 .0) < 0;
|
|
96
|
+
}
|
|
97
|
+
// SAFETY: a non-Fixnum Integer is a live Bignum.
|
|
98
|
+
unsafe { non_raising::bignum_negative_p(self.0 .0) }
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// `n` in decimal into `buf`, which holds any `i64` (20 digits and a sign);
|
|
103
|
+
/// how many bytes.
|
|
104
|
+
fn write_i64(n: i64, buf: &mut [u8; 24]) -> usize {
|
|
105
|
+
let mut digits = [0u8; 20];
|
|
106
|
+
let mut m = n.unsigned_abs();
|
|
107
|
+
let mut k = 0;
|
|
108
|
+
loop {
|
|
109
|
+
if let Some(d) = digits.get_mut(k) {
|
|
110
|
+
*d = b'0' + u8::try_from(m % 10).unwrap_or(0);
|
|
111
|
+
}
|
|
112
|
+
k += 1;
|
|
113
|
+
m /= 10;
|
|
114
|
+
if m == 0 {
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
let mut at = 0;
|
|
119
|
+
if n < 0 {
|
|
120
|
+
if let Some(sign) = buf.first_mut() {
|
|
121
|
+
*sign = b'-';
|
|
122
|
+
}
|
|
123
|
+
at = 1;
|
|
124
|
+
}
|
|
125
|
+
for d in digits.iter().take(k).rev() {
|
|
126
|
+
if let Some(slot) = buf.get_mut(at) {
|
|
127
|
+
*slot = *d;
|
|
128
|
+
}
|
|
129
|
+
at += 1;
|
|
130
|
+
}
|
|
131
|
+
at
|
|
132
|
+
}
|