Skip to content

Commit

Permalink
feat: grammar for typed bnf
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin-Yeung committed Nov 10, 2024
1 parent 9099355 commit 5bc2185
Show file tree
Hide file tree
Showing 7 changed files with 989 additions and 68 deletions.
11 changes: 10 additions & 1 deletion src/grammar/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ mod test {
insta::assert_debug_snapshot!(grammar);
}

#[test]
fn typed() {
let text = r#"
<E> ::= <E: "int"> "+" <E: "int"> ;
"#;
let grammar = RawGrammar::parse(text).unwrap();
insta::assert_debug_snapshot!(grammar);
}

#[test]
fn repeat() {
let text = r#"
Expand All @@ -52,7 +61,7 @@ mod test {

#[test]
fn invalid_token() {
let text = ":";
let text = "*";
let err = RawGrammar::parse(text).err().unwrap();
let ui = report_with_unnamed_source(err, text);
insta::assert_snapshot!(ui);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ expression: ui
---
Γ— Invalid token
╭────
1 β”‚ :
1 β”‚ *
Β· ┬
Β· ╰── this token is invalid
╰────
56 changes: 56 additions & 0 deletions src/grammar/snapshots/bnfgen__grammar__test__typed.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
source: src/grammar/mod.rs
expression: grammar
---
RawGrammar {
rules: [
Rule {
name: "E",
production: WeightedProduction {
alts: [
Alternative {
span: Span {
start: 21,
end: 46,
},
weight: 1,
invoke_limit: Unlimited,
symbols: [
Symbol {
kind: NonTerminal(
"E",
),
span: Span {
start: 21,
end: 31,
},
},
Symbol {
kind: Terminal(
"+",
),
span: Span {
start: 32,
end: 35,
},
},
Symbol {
kind: NonTerminal(
"E",
),
span: Span {
start: 36,
end: 46,
},
},
],
},
],
},
span: Span {
start: 13,
end: 48,
},
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ expression: ui
╭────
1 β”‚ <start> ::= "Hello" | "World"
Β· ┬
Β· ╰── expect "{", "|", ";", "re", "str", "nonterminal"
Β· ╰── expect "{", "|", ";", "<", "re", "str"
╰────
16 changes: 13 additions & 3 deletions src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ extern {
"|" => Token::Or,
"," => Token::Comma,
"::=" => Token::Def,
":" => Token::Colon,
";" => Token::Semi,
"<" => Token::LAngle,
">" => Token::RAngle,
"re" => Token::Re,
"str" => Token::Str(<String>),
"nonterminal" => Token::NonTerminal(<String>),
"id" => Token::Id(<String>),
"int" => Token::Int(<usize>),
}
}
Expand All @@ -43,7 +46,7 @@ pub RawGrammar: RawGrammar = {
};

Rule: Rule = {
<l: @L> <name: "nonterminal"> "::=" <alts: Alternatives> ";" <r: @R> => {
<l: @L> "<" <name: "id"> ">" "::=" <alts: Alternatives> ";" <r: @R> => {
Rule {
name,
production: WeightedProduction {
Expand Down Expand Up @@ -96,7 +99,14 @@ Symbol: Symbol = {
span: Span::new(l, r),
}
},
<l: @L> <nt: "nonterminal"> <r: @R> => {
<l: @L> "<" <nt: "id"> ">" <r: @R> => {
Symbol {
kind: SymbolKind::NonTerminal(Rc::new(nt)),
span: Span::new(l, r),
}
},
// typed non-terminal
<l: @L> "<" <nt: "id"> ":" <_ty: "str"> ">" <r: @R> => {
Symbol {
kind: SymbolKind::NonTerminal(Rc::new(nt)),
span: Span::new(l, r),
Expand Down
Loading

0 comments on commit 5bc2185

Please sign in to comment.