Skip to content

Commit e3a5bfa

Browse files
committed
Convert parser to nom v8
1 parent a17c169 commit e3a5bfa

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ fancy-regex = "0.16.0"
2222
glob = "0.3.2"
2323
indexmap = "2.2.6"
2424
nom = "8.0.0"
25+
nom-language = "0.1.0"
2526
pyo3 = { version = "=0.27.1", features = ["chrono"] }
2627
rayon = "1.10.0"
2728
regex = "1.10.4"

src/refs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
types::{Mapping, Value},
66
};
77
use anyhow::{anyhow, Result};
8-
use nom::error::{convert_error, VerboseError};
8+
use nom_language::error::{convert_error, VerboseError};
99
use std::collections::HashSet;
1010

1111
#[derive(Debug, PartialEq, Eq)]

src/refs/parser.rs

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use nom::{
33
bytes::complete::{tag, take},
44
character::complete::none_of,
55
combinator::{all_consuming, map, not, peek},
6-
error::{context, VerboseError},
6+
error::context,
77
multi::many1,
8-
sequence::{delimited, preceded, tuple},
9-
IResult,
8+
sequence::{delimited, preceded},
9+
IResult, Parser,
1010
};
11+
use nom_language::error::VerboseError;
1112

1213
use super::Token;
1314

@@ -37,62 +38,67 @@ fn coalesce_literals(tokens: Vec<Token>) -> Vec<Token> {
3738
}
3839

3940
fn ref_open(input: &str) -> IResult<&str, &str, VerboseError<&str>> {
40-
context("ref_open", tag("${"))(input)
41+
context("ref_open", tag("${")).parse(input)
4142
}
4243

4344
fn ref_close(input: &str) -> IResult<&str, &str, VerboseError<&str>> {
44-
context("ref_close", tag("}"))(input)
45+
context("ref_close", tag("}")).parse(input)
4546
}
4647

4748
fn inv_open(input: &str) -> IResult<&str, &str, VerboseError<&str>> {
48-
context("inv_open", tag("$["))(input)
49+
context("inv_open", tag("$[")).parse(input)
4950
}
5051

5152
fn ref_escape_open(input: &str) -> IResult<&str, String, VerboseError<&str>> {
5253
map(
5354
context("ref_escape_open", preceded(tag("\\"), ref_open)),
5455
String::from,
55-
)(input)
56+
)
57+
.parse(input)
5658
}
5759

5860
fn inv_escape_open(input: &str) -> IResult<&str, String, VerboseError<&str>> {
5961
map(
6062
context("inv_escape_open", preceded(tag("\\"), inv_open)),
6163
String::from,
62-
)(input)
64+
)
65+
.parse(input)
6366
}
6467

6568
fn ref_escape_close(input: &str) -> IResult<&str, String, VerboseError<&str>> {
6669
map(
6770
context("ref_escape_close", preceded(tag("\\"), ref_close)),
6871
String::from,
69-
)(input)
72+
)
73+
.parse(input)
7074
}
7175

7276
fn double_escape(input: &str) -> IResult<&str, String, VerboseError<&str>> {
7377
map(
7478
context(
7579
"double_escape",
76-
tuple((tag(r"\\"), peek(alt((ref_open, ref_close))))),
80+
(tag(r"\\"), peek(alt((ref_open, ref_close)))),
7781
),
7882
|_| r"\".to_string(),
79-
)(input)
83+
)
84+
.parse(input)
8085
}
8186

8287
fn ref_not_open(input: &str) -> IResult<&str, (), VerboseError<&str>> {
8388
// don't advance parse position, just check for ref_open variants
8489
map(
8590
context(
8691
"ref_not_open",
87-
tuple((
92+
(
8893
not(tag("${")),
8994
not(tag("\\${")),
9095
not(tag("\\\\${")),
9196
not(tag("\\$[")),
92-
)),
97+
),
9398
),
9499
|_| (),
95-
)(input)
100+
)
101+
.parse(input)
96102
}
97103

98104
/// Parses a section of the input which can't contain a reference (escaped or otherwise)
@@ -102,32 +108,31 @@ fn ref_content(input: &str) -> IResult<&str, String, VerboseError<&str>> {
102108
map(
103109
context(
104110
"ref_not_close",
105-
tuple((not(tag("}")), not(tag("\\}")), not(tag("\\\\}")))),
111+
(not(tag("}")), not(tag("\\}")), not(tag("\\\\}"))),
106112
),
107113
|((), (), ())| (),
108-
)(input)
114+
)
115+
.parse(input)
109116
}
110117

111118
fn ref_text(input: &str) -> IResult<&str, String, VerboseError<&str>> {
112119
context(
113120
"ref_text",
114121
alt((
115122
map(many1(none_of("\\${}")), |ch| ch.iter().collect::<String>()),
116-
map(
117-
tuple((not(tag("}")), take(1usize))),
118-
|((), c): ((), &str)| c.to_string(),
119-
),
123+
map((not(tag("}")), take(1usize)), |((), c): ((), &str)| {
124+
c.to_string()
125+
}),
120126
)),
121-
)(input)
127+
)
128+
.parse(input)
122129
}
123130

124131
map(
125-
context(
126-
"ref_content",
127-
tuple((ref_not_open, ref_not_close, ref_text)),
128-
),
132+
context("ref_content", (ref_not_open, ref_not_close, ref_text)),
129133
|((), (), t)| t,
130-
)(input)
134+
)
135+
.parse(input)
131136
}
132137

133138
/// Parses a section of the contents of a reference which doesn't contain nested Reclass
@@ -145,15 +150,17 @@ fn ref_string(input: &str) -> IResult<&str, String, VerboseError<&str>> {
145150
))),
146151
),
147152
|s| s.join(""),
148-
)(input)
153+
)
154+
.parse(input)
149155
}
150156

151157
/// Parses the contents of a reference, taking into account that there may be nested references
152158
fn ref_item(input: &str) -> IResult<&str, Token, VerboseError<&str>> {
153159
context(
154160
"ref_item",
155161
alt((reference, map(ref_string, Token::Literal))),
156-
)(input)
162+
)
163+
.parse(input)
157164
}
158165

159166
/// Parses a single Reclass reference which may contain nested references
@@ -163,7 +170,8 @@ fn reference(input: &str) -> IResult<&str, Token, VerboseError<&str>> {
163170
map(delimited(ref_open, many1(ref_item), ref_close), |tokens| {
164171
Token::Ref(coalesce_literals(tokens))
165172
}),
166-
)(input)
173+
)
174+
.parse(input)
167175
}
168176

169177
/// Parses a section of the input which doesn't contain any Reclass references
@@ -175,27 +183,30 @@ fn string(input: &str) -> IResult<&str, String, VerboseError<&str>> {
175183
map(many1(none_of("${}\\")), |ch| ch.iter().collect::<String>()),
176184
map(take(1usize), std::string::ToString::to_string),
177185
)),
178-
)(input)
186+
)
187+
.parse(input)
179188
}
180189

181190
fn content(input: &str) -> IResult<&str, String, VerboseError<&str>> {
182191
context(
183192
"content",
184-
map(many1(tuple((ref_not_open, text))), |strings| {
193+
map(many1((ref_not_open, text)), |strings| {
185194
strings.iter().map(|((), s)| s.clone()).collect::<String>()
186195
}),
187-
)(input)
196+
)
197+
.parse(input)
188198
}
189199

190200
context(
191201
"string",
192202
alt((double_escape, ref_escape_open, inv_escape_open, content)),
193-
)(input)
203+
)
204+
.parse(input)
194205
}
195206

196207
/// Parses either a Reclass reference or a section of the input with no references
197208
fn item(input: &str) -> IResult<&str, Token, VerboseError<&str>> {
198-
context("item", alt((reference, map(string, Token::Literal))))(input)
209+
context("item", alt((reference, map(string, Token::Literal)))).parse(input)
199210
}
200211

201212
/// Parses a string containing zero or more Reclass references
@@ -207,7 +218,8 @@ pub fn parse_ref(input: &str) -> IResult<&str, Token, VerboseError<&str>> {
207218
} else {
208219
tokens.into_iter().next().unwrap()
209220
}
210-
})(input)
221+
})
222+
.parse(input)
211223
}
212224

213225
#[cfg(test)]

0 commit comments

Comments
 (0)