Skip to content

Commit 77b9e8a

Browse files
committed
perf(parser/expression): remove support for newlines within expressions
This is rarely used and unsupported by other assemblers, and comes with a significant performance cost (~10.5% overall). This also improves incorrect instruction syntax error messages for expressions (as those couldn't have newlines due to how they were parsed)
1 parent 43ed7a1 commit 77b9e8a

1 file changed

Lines changed: 14 additions & 23 deletions

File tree

src/parser/expression.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ pub fn parser<'tokens, I>() -> Parser!('tokens, I, Spanned<Expr>)
217217
where
218218
I: ValueInput<'tokens, Token = Token, Span = Span>,
219219
{
220-
// Newline tokens
221-
let newline = || just(Token::Ctrl('\n')).repeated();
222220
// Literal values
223221
let literal = select! {
224222
Token::Integer(x) => Expr::Integer(x),
@@ -232,11 +230,9 @@ where
232230
// Operator parser
233231
macro_rules! op {
234232
(:$name:literal: $($i:ident => $o:expr),+ $(,)?) => {
235-
newline().ignore_then(
236-
select! { $(Token::Operator(Operator::$i) => $o,)+ }
237-
.map_with(|x, e| (x, e.span()))
238-
.labelled(concat!($name, " operator"))
239-
)
233+
select! { $(Token::Operator(Operator::$i) => $o,)+ }
234+
.map_with(|x, e| (x, e.span()))
235+
.labelled(concat!($name, " operator"))
240236
};
241237
($($i:ident => $o:expr),+ $(,)?) => { op!(:"binary": $($i => $o,)+) };
242238
}
@@ -260,14 +256,10 @@ where
260256
}
261257

262258
recursive(|expr| {
263-
// NOTE: newlines before atoms (literal numbers/parenthesized expressions) and operators
264-
// are allowed so that expressions may span multiple lines. Newlines aren't allowed after
265-
// them to prevent them from consuming new lines required to end statements
266-
267259
// paren_expr: `paren_expr -> ( expression )`
268260
let paren_expr = expr.delimited_by(
269261
just(Token::Ctrl('(')),
270-
newline().ignore_then(just(Token::Ctrl(')'))),
262+
just(Token::Ctrl(')')),
271263
);
272264
// modifier: `modifier -> % ident paren_expr`
273265
let modifier = just(Token::Operator(Operator::Percent))
@@ -281,9 +273,8 @@ where
281273
// Remove span to replace it with one including the parenthesis
282274
let paren_expr = paren_expr.map(|(x, _)| x);
283275

284-
// atom: `atom -> \n* (literal | modifier | paren_expr)`
276+
// atom: `atom -> literal | modifier | paren_expr`
285277
let atom = choice((literal, modifier, paren_expr)).map_with(|atom, e| (atom, e.span()));
286-
let atom = newline().ignore_then(atom);
287278
let atom = atom.labelled("expression").as_context();
288279

289280
let high_precedence = op!(
@@ -394,7 +385,7 @@ mod test {
394385
test([
395386
("16", span(Expr::Integer(16u8.into()), 0..2), Ok(16.into())),
396387
(
397-
"\n\n16",
388+
"\t 16",
398389
span(Expr::Integer(16u8.into()), 2..4),
399390
Ok(16.into()),
400391
),
@@ -490,12 +481,12 @@ mod test {
490481
Ok((2.2, 1..4).into()),
491482
),
492483
(
493-
"\n\n+\n2",
484+
"\t + 2",
494485
un_op((UnaryOp::Plus, 2..3), int(2, 4..5)),
495486
Ok(2.into()),
496487
),
497488
(
498-
"\n\n+\n2.2",
489+
" \t+\t2.2",
499490
un_op((UnaryOp::Plus, 2..3), float(2.2, 4..7)),
500491
Ok((2.2, 4..7).into()),
501492
),
@@ -541,7 +532,7 @@ mod test {
541532
Ok(12.into()),
542533
),
543534
(
544-
"\n5 \n\n+ \n7",
535+
"\t5 \t\t+ \t7",
545536
bin_op((BinaryOp::Add, 5..6), int(5, 1..2), int(7, 8..9)),
546537
Ok(12.into()),
547538
),
@@ -600,7 +591,7 @@ mod test {
600591
Ok(35.into()),
601592
),
602593
(
603-
"\n5 \n\n* \n7",
594+
"\t5 \t\t* \t7",
604595
bin_op((BinaryOp::Mul, 5..6), int(5, 1..2), int(7, 8..9)),
605596
Ok(35.into()),
606597
),
@@ -689,7 +680,7 @@ mod test {
689680
Ok(0b0110.into()),
690681
),
691682
(
692-
"\n0b0101 \n\n^ \n0b0011",
683+
"\t0b0101 \t\t^ \t0b0011",
693684
bin_op(
694685
(BinaryOp::BitwiseXOR, 10..11),
695686
int(0b0101, 1..7),
@@ -698,7 +689,7 @@ mod test {
698689
Ok(0b0110.into()),
699690
),
700691
(
701-
"\n0b0101 \n\n^ \n1.1",
692+
"\t0b0101 \t\t^ \t1.1",
702693
bin_op(
703694
(BinaryOp::BitwiseXOR, 10..11),
704695
int(0b0101, 1..7),
@@ -878,7 +869,7 @@ mod test {
878869
Ok(0.into()),
879870
),
880871
(
881-
"1 + \n(\n2 - 3\n)",
872+
"1 + \t(\t2 - 3\t)",
882873
bin_op(
883874
(BinaryOp::Add, 2..3),
884875
int(1, 0..1),
@@ -916,7 +907,7 @@ mod test {
916907
Ok(0.into()),
917908
),
918909
(
919-
"\n- \n\n+ \n1",
910+
"\t- \t\t+ \t1",
920911
un_op(
921912
(UnaryOp::Minus, 1..2),
922913
un_op((UnaryOp::Plus, 5..6), int(1, 8..9)),

0 commit comments

Comments
 (0)