Skip to content

Commit

Permalink
fix ebnf writer bugs (#1214)
Browse files Browse the repository at this point in the history
- `Scanner::Not` was serialized to a sequence, not a choice.
- render negative lookaheads, as it was hidden.
- change `Expression::Range` to primary expression.
  • Loading branch information
OmarTawfik authored Jan 9, 2025
1 parent 3936e23 commit a640e59
Show file tree
Hide file tree
Showing 17 changed files with 143 additions and 129 deletions.
9 changes: 6 additions & 3 deletions crates/codegen/ebnf/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl Builder {
let expression = if chars.len() == 1 {
Expression::new_atom(chars[0].to_string())
} else {
Expression::new_sequence(
Expression::new_choice(
chars
.iter()
.map(|ch| Expression::new_atom(ch.to_string()))
Expand All @@ -485,8 +485,11 @@ impl Builder {
Scanner::Atom { atom } => Expression::new_atom(atom.clone()),
Scanner::TrailingContext {
scanner,
not_followed_by: _,
} => Self::build_scanner(scanner),
not_followed_by,
} => Expression::new_sequence(vec![
Self::build_scanner(scanner),
Expression::new_negative_look_ahead(Self::build_scanner(not_followed_by).into()),
]),
Scanner::Fragment { reference } => Self::build_ref(None, reference),
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/codegen/ebnf/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ pub enum Expression {
Atom {
atom: String,
},
NegativeLookAhead {
expression: Box<Self>,
},
Reference {
leading_comment: Option<String>,
reference: Identifier,
Expand All @@ -101,7 +104,7 @@ impl Expression {
// This separates members of the same precedence, like both "a b (c | d)" and "a | b | (c d)".
match self {
// Binary
Self::Choice { .. } | Self::Range { .. } | Self::Sequence { .. } => 1,
Self::Choice { .. } | Self::Sequence { .. } => 1,

// Prefix
Self::Not { .. } => 2,
Expand All @@ -110,7 +113,10 @@ impl Expression {
Self::OneOrMore { .. } | Self::Optional { .. } | Self::ZeroOrMore { .. } => 3,

// Primary
Self::Atom { .. } | Self::Reference { .. } => 4,
Self::Range { .. }
| Self::Atom { .. }
| Self::NegativeLookAhead { .. }
| Self::Reference { .. } => 4,
}
}
}
9 changes: 7 additions & 2 deletions crates/codegen/ebnf/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,18 @@ impl<'s, W: EbnfWriter> Serializer<'s, W> {
inclusive_start,
inclusive_end,
} => {
self.serialize_child_expr(parent, inclusive_start)?;
self.serialize_expr(inclusive_start)?;
self.serialize_punctuation("…")?;
self.serialize_child_expr(parent, inclusive_end)?;
self.serialize_expr(inclusive_end)?;
}
Expression::Atom { atom } => {
self.serialize_string_literal(atom)?;
}
Expression::NegativeLookAhead { expression } => {
self.serialize_punctuation("(?!")?;
self.serialize_expr(expression)?;
self.serialize_punctuation(")")?;
}
Expression::Reference {
leading_comment,
reference,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a640e59

Please sign in to comment.