Skip to content

Commit c6ee0ca

Browse files
authored
Merge pull request #125 from Rawk/nom8
build: Upgrade nom to version 8
2 parents 1f09e97 + b50a7b6 commit c6ee0ca

28 files changed

+516
-360
lines changed

Cargo.lock

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rasn-compiler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ cli = ["clap", "colored", "walkdir"]
3333
chrono = "0.4.41"
3434
clap = { version = "4.5.38", optional = true, features = ["derive"] }
3535
colored = { version = "2", optional = true }
36-
nom = { version = "7.1.3", default-features = false, features = ["alloc"] }
36+
nom = { version = "8.0", default-features = false, features = ["alloc"] }
3737
num = { version = "0.4", default-features = false }
3838
proc-macro2 = "1.0"
3939
quote = "1.0"

rasn-compiler/src/input.rs

Lines changed: 49 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,22 @@
66
77
use std::{
88
fmt::Debug,
9-
ops::RangeTo,
9+
slice::SliceIndex,
1010
str::{CharIndices, Chars, FromStr},
1111
};
1212

13-
use nom::{
14-
AsBytes, Compare, ExtendInto, FindSubstring, FindToken, InputIter, InputLength, InputTake,
15-
InputTakeAtPosition, Offset, ParseTo, Slice,
16-
};
17-
18-
use crate::lexer::error::ParserResult;
13+
use nom::{AsBytes, Compare, ExtendInto, FindSubstring, FindToken, Offset, ParseTo, Parser};
1914

2015
/// Informs `Input` of a context switch.
21-
pub fn context_boundary<'a, F, O: Debug>(
16+
pub fn context_boundary<'a, F>(
2217
mut inner: F,
23-
) -> impl FnMut(Input<'a>) -> ParserResult<'a, O>
18+
) -> impl Parser<Input<'a>, Output = F::Output, Error = F::Error>
2419
where
25-
F: FnMut(Input<'a>) -> ParserResult<'a, O>,
20+
F: Parser<Input<'a>>,
2621
{
27-
move |mut input| {
22+
move |mut input: Input<'a>| {
2823
input.reset_context();
29-
inner(input)
24+
inner.parse(input)
3025
}
3126
}
3227

@@ -189,57 +184,9 @@ impl FindToken<char> for Input<'_> {
189184
}
190185
}
191186

192-
impl<'a> InputIter for Input<'a> {
193-
type Item = char;
194-
type Iter = CharIndices<'a>;
195-
type IterElem = Chars<'a>;
196-
197-
#[inline]
198-
fn iter_indices(&self) -> Self::Iter {
199-
self.inner.iter_indices()
200-
}
201-
202-
#[inline]
203-
fn iter_elements(&self) -> Self::IterElem {
204-
self.inner.iter_elements()
205-
}
206-
207-
#[inline]
208-
fn position<P>(&self, predicate: P) -> Option<usize>
209-
where
210-
P: Fn(Self::Item) -> bool,
211-
{
212-
self.inner.position(predicate)
213-
}
214-
215-
#[inline]
216-
fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
217-
self.inner.slice_index(count)
218-
}
219-
}
220-
221-
impl InputLength for Input<'_> {
222-
fn input_len(&self) -> usize {
223-
self.inner.len()
224-
}
225-
}
226-
227-
impl InputTake for Input<'_> {
228-
fn take(&self, count: usize) -> Self {
229-
self.slice(..count)
230-
}
231-
232-
fn take_split(&self, count: usize) -> (Self, Self) {
233-
(self.slice(count..), self.slice(..count))
234-
}
235-
}
236-
237-
impl<'a, R> Slice<R> for Input<'a>
238-
where
239-
&'a str: Slice<R> + Slice<RangeTo<usize>>,
240-
{
241-
fn slice(&self, range: R) -> Self {
242-
let inner = self.inner.slice(range);
187+
impl<'a> Input<'a> {
188+
pub fn slice(&self, range: impl SliceIndex<str, Output = str>) -> Self {
189+
let inner = &self.inner[range];
243190
let consumed_len = self.inner.offset(inner);
244191
if consumed_len == 0 {
245192
Input {
@@ -251,7 +198,7 @@ where
251198
inner,
252199
}
253200
} else {
254-
let consumed = self.inner.slice(..consumed_len);
201+
let consumed = &self.inner[..consumed_len];
255202
let line_breaks = consumed.match_indices('\n');
256203
let last_line_break = line_breaks.clone().last();
257204
let column = if let Some(last) = last_line_break {
@@ -273,8 +220,10 @@ where
273220
}
274221
}
275222

276-
impl InputTakeAtPosition for Input<'_> {
223+
impl<'a> nom::Input for Input<'a> {
277224
type Item = char;
225+
type Iter = Chars<'a>;
226+
type IterIndices = CharIndices<'a>;
278227

279228
fn split_at_position<P, E: nom::error::ParseError<Self>>(
280229
&self,
@@ -317,6 +266,41 @@ impl InputTakeAtPosition for Input<'_> {
317266
}
318267
}
319268

269+
fn input_len(&self) -> usize {
270+
self.inner.len()
271+
}
272+
273+
fn take(&self, count: usize) -> Self {
274+
self.slice(..count)
275+
}
276+
277+
fn take_from(&self, index: usize) -> Self {
278+
self.slice(index..)
279+
}
280+
281+
fn take_split(&self, count: usize) -> (Self, Self) {
282+
(self.slice(count..), self.slice(..count))
283+
}
284+
285+
fn position<P>(&self, predicate: P) -> Option<usize>
286+
where
287+
P: Fn(Self::Item) -> bool,
288+
{
289+
self.inner.position(predicate)
290+
}
291+
292+
fn iter_elements(&self) -> Self::Iter {
293+
self.inner.iter_elements()
294+
}
295+
296+
fn iter_indices(&self) -> Self::IterIndices {
297+
self.inner.iter_indices()
298+
}
299+
300+
fn slice_index(&self, count: usize) -> Result<usize, nom::Needed> {
301+
self.inner.slice_index(count)
302+
}
303+
320304
fn split_at_position1_complete<P, E: nom::error::ParseError<Self>>(
321305
&self,
322306
predicate: P,

rasn-compiler/src/lexer/bit_string.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use nom::{
55
combinator::{map, opt},
66
multi::{fold_many0, separated_list0},
77
sequence::{delimited, pair, preceded},
8+
Parser,
89
};
910

1011
use crate::{input::Input, intermediate::*};
@@ -45,7 +46,8 @@ pub fn bit_string_value(input: Input<'_>) -> ParserResult<'_, ASN1Value> {
4546
ASN1Value::BitStringNamedBits(named_bits.into_iter().map(String::from).collect())
4647
},
4748
),
48-
))(input)
49+
))
50+
.parse(input)
4951
}
5052

5153
/// Tries to parse an ASN1 BIT STRING
@@ -63,7 +65,8 @@ pub fn bit_string(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
6365
pair(opt(distinguished_values), opt(constraint)),
6466
),
6567
|m| ASN1Type::BitString(m.into()),
66-
)(input)
68+
)
69+
.parse(input)
6770
}
6871

6972
#[cfg(test)]

rasn-compiler/src/lexer/boolean.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use nom::{
33
bytes::complete::tag,
44
combinator::{into, map, opt, value},
55
sequence::preceded,
6+
Parser,
67
};
78

89
use crate::{
@@ -16,7 +17,8 @@ pub fn boolean_value(input: Input<'_>) -> ParserResult<'_, ASN1Value> {
1617
alt((
1718
value(ASN1Value::Boolean(true), skip_ws_and_comments(tag(TRUE))),
1819
value(ASN1Value::Boolean(false), skip_ws_and_comments(tag(FALSE))),
19-
))(input)
20+
))
21+
.parse(input)
2022
}
2123

2224
/// Tries to parse an ASN1 BOOLEAN
@@ -34,7 +36,8 @@ pub fn boolean(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
3436
skip_ws_and_comments(opt(constraint)),
3537
))),
3638
ASN1Type::Boolean,
37-
)(input)
39+
)
40+
.parse(input)
3841
}
3942

4043
#[cfg(test)]

rasn-compiler/src/lexer/character_string.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use nom::{
33
bytes::complete::tag,
44
character::complete::{char, u8},
55
combinator::{map, map_res, opt},
6-
sequence::{delimited, pair, terminated, tuple},
6+
sequence::{delimited, pair, terminated},
7+
Parser,
78
};
89

910
use crate::{input::Input, intermediate::*};
@@ -19,7 +20,8 @@ pub fn character_string_value(input: Input<'_>) -> ParserResult<'_, ASN1Value> {
1920
map(
2021
skip_ws_and_comments(alt((cstring, map(quadruple, |c| c.to_string())))),
2122
|m: String| ASN1Value::String(m),
22-
)(input)
23+
)
24+
.parse(input)
2325
}
2426

2527
/// Parses a ASN1 cstring
@@ -45,7 +47,8 @@ pub fn cstring(input: Input<'_>) -> ParserResult<'_, String> {
4547
// Replace any escaped quote with a single `"`
4648
// TODO: Remove whitespace around newlines in multiline strings.
4749
s.replace("\"\"", "\"")
48-
})(input)
50+
})
51+
.parse(input)
4952
}
5053

5154
/// Parses a string literal into its raw value.
@@ -56,7 +59,7 @@ pub fn cstring(input: Input<'_>) -> ParserResult<'_, String> {
5659
/// This is a raw string in the sense that it is the string slice as written
5760
/// in the source, including any indentation and double quotes (`""`).
5861
pub fn raw_string_literal(input: Input<'_>) -> ParserResult<'_, &'_ str> {
59-
delimited(char('"'), take_until_and_not("\"", "\"\""), char('"'))(input)
62+
delimited(char('"'), take_until_and_not("\"", "\"\""), char('"')).parse(input)
6063
}
6164

6265
/// A ASN1 character value can be specified "by reference to a registration number in the ISO
@@ -77,12 +80,12 @@ pub fn raw_string_literal(input: Input<'_>) -> ParserResult<'_, &'_ str> {
7780
/// __Currently, the rasn compiler only supports group `0`__
7881
fn quadruple(input: Input<'_>) -> ParserResult<'_, char> {
7982
map_res(
80-
in_braces(tuple((
83+
in_braces((
8184
terminated(skip_ws(u8), skip_ws(char(COMMA))),
8285
terminated(skip_ws(u8), skip_ws(char(COMMA))),
8386
terminated(skip_ws(u8), skip_ws(char(COMMA))),
8487
skip_ws(u8),
85-
))),
88+
)),
8689
|(group, plane, row, cell)| {
8790
if group > 0 {
8891
Err(MiscError("Currently, only group 0 is supported."))
@@ -93,7 +96,8 @@ fn quadruple(input: Input<'_>) -> ParserResult<'_, char> {
9396
))
9497
}
9598
},
96-
)(input.clone())
99+
)
100+
.parse(input)
97101
}
98102

99103
/// Tries to parse an ASN1 Character String type
@@ -125,7 +129,8 @@ pub fn character_string(input: Input<'_>) -> ParserResult<'_, ASN1Type> {
125129
opt(constraint),
126130
),
127131
|m| ASN1Type::CharacterString(m.into()),
128-
)(input)
132+
)
133+
.parse(input)
129134
}
130135

131136
#[cfg(test)]

0 commit comments

Comments
 (0)