gbnf -> lark syntax

This commit is contained in:
Michal Moskal 2025-01-26 08:50:59 -08:00
parent f245ca26f5
commit 16a5484048

View file

@ -353,10 +353,10 @@ static void test_simple_grammar() {
// Test case for a simple grammar // Test case for a simple grammar
test_grammar("simple grammar", test_grammar("simple grammar",
R"""( R"""(
root ::= expr start: expr
expr ::= term ("+" term)* expr: term ("+" term)*
term ::= number term: number
number ::= [0-9]+)""", number: /[0-9]+/ )""",
// Passing strings // Passing strings
{ {
"42", "42",
@ -377,14 +377,14 @@ static void test_complex_grammar() {
test_grammar("medium complexity grammar", test_grammar("medium complexity grammar",
// Grammar // Grammar
R"""( R"""(
root ::= expression start: expression
expression ::= term ws (("+"|"-") ws term)* expression: term ws (("+"|"-") ws term)*
term ::= factor ws (("*"|"/") ws factor)* term: factor ws (("*"|"/") ws factor)*
factor ::= number | variable | "(" expression ")" | function-call factor: number | variable | "(" expression ")" | function-call
number ::= [0-9]+ number: /[0-9]+/
variable ::= [a-zA-Z_][a-zA-Z0-9_]* variable: /[a-zA-Z_][a-zA-Z0-9_]*/
function-call ::= variable ws "(" (expression ("," ws expression)*)? ")" function-call: variable ws "(" (expression ("," ws expression)*)? ")"
ws ::= [ \t\n\r]?)""", ws: /[ \t\n\r]?/ )""",
// Passing strings // Passing strings
{ "42", { "42",
"1*2*3*4*5", "1*2*3*4*5",
@ -434,7 +434,7 @@ static void test_special_chars() {
test_grammar("special characters", test_grammar("special characters",
// Grammar // Grammar
R"""( R"""(
root ::= ... "abc" ... start: /.../ "abc" /.../
)""", )""",
// Passing strings // Passing strings
{ "abcabcabc", "aaaabcccc", { "abcabcabc", "aaaabcccc",
@ -450,7 +450,7 @@ static void test_quantifiers() {
test_grammar( test_grammar(
"* quantifier", "* quantifier",
// Grammar // Grammar
R"""(root ::= "a"*)""", R"""(start: "a"*)""",
// Passing strings // Passing strings
{ "", "a", "aaaaa", "aaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "", "a", "aaaaa", "aaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
// Failing strings // Failing strings
@ -458,14 +458,14 @@ static void test_quantifiers() {
test_grammar( test_grammar(
"+ quantifier", "+ quantifier",
// Grammar // Grammar
R"""(root ::= "a"+)""", R"""(start: "a"+)""",
// Passing strings // Passing strings
{ "a", "aaaaa", "aaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "a", "aaaaa", "aaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" },
// Failing strings // Failing strings
{ "", "b", "ab", "aab", "ba", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" }); { "", "b", "ab", "aab", "ba", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab" });
test_grammar("? quantifier", test_grammar("? quantifier",
// Grammar // Grammar
R"""(root ::= "a"?)""", R"""(start: "a"?)""",
// Passing strings // Passing strings
{ "", "a" }, { "", "a" },
// Failing strings // Failing strings
@ -478,9 +478,9 @@ static void test_quantifiers() {
test_grammar("mixed quantifiers", test_grammar("mixed quantifiers",
// Grammar // Grammar
R"""( R"""(
root ::= cons+ vowel* cons? (vowel cons)* start: cons+ vowel* cons? (vowel cons)*
vowel ::= [aeiouy] vowel: [aeiouy]
cons ::= [bcdfghjklmnpqrstvwxyz] cons: [bcdfghjklmnpqrstvwxyz]
)""", )""",
// Passing strings // Passing strings
{ {
@ -501,7 +501,7 @@ static void test_quantifiers() {
test_grammar("simple exact repetition", test_grammar("simple exact repetition",
// Grammar // Grammar
R"""( R"""(
root ::= [ab]{4} start: [ab]{4}
)""", )""",
// Passing strings // Passing strings
{ {
@ -518,7 +518,7 @@ static void test_quantifiers() {
test_grammar("simple min repetition", test_grammar("simple min repetition",
// Grammar // Grammar
R"""( R"""(
root ::= [ab]{4,} start: [ab]{4,}
)""", )""",
// Passing strings // Passing strings
{ {
@ -535,7 +535,7 @@ static void test_quantifiers() {
test_grammar("simple max repetition", test_grammar("simple max repetition",
// Grammar // Grammar
R"""( R"""(
root ::= [ab]{0,4} start: [ab]{0,4}
)""", )""",
// Passing strings // Passing strings
{ {
@ -552,7 +552,7 @@ static void test_quantifiers() {
test_grammar("min / max repetition", test_grammar("min / max repetition",
// Grammar // Grammar
R"""( R"""(
root ::= ("0x" [A-F0-9]{2} " "?){3,5} start: ("0x" [A-F0-9]{2} " "?){3,5}
)""", )""",
// Passing strings // Passing strings
{ {