Skip to content

Commit 8f5fdac

Browse files
committed
grammar : recognize '|' at start of continuation line
1 parent 86b9470 commit 8f5fdac

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/llama-grammar.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,13 @@ const char * llama_grammar_parser::parse_alternates(
438438
bool is_nested) {
439439
llama_grammar_rule rule;
440440
const char * pos = parse_sequence(src, rule_name, rule, is_nested);
441-
while (*pos == '|') {
441+
while (true) {
442+
const char * next = parse_space(pos, true);
443+
if (*next != '|') {
444+
break;
445+
}
442446
rule.push_back({LLAMA_GRETYPE_ALT, 0});
443-
pos = parse_space(pos + 1, true);
447+
pos = parse_space(next + 1, true);
444448
pos = parse_sequence(pos, rule_name, rule, is_nested);
445449
}
446450
rule.push_back({LLAMA_GRETYPE_END, 0});

tests/test-grammar-parser.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ int main()
181181
{LLAMA_GRETYPE_END, 0},
182182
});
183183

184+
verify_parsing(R"""(
185+
root ::= "a"
186+
| "b"
187+
)""", {
188+
{"root", 0},
189+
}, {
190+
// root (index 0)
191+
{LLAMA_GRETYPE_CHAR, 'a'},
192+
{LLAMA_GRETYPE_ALT, 0},
193+
{LLAMA_GRETYPE_CHAR, 'b'},
194+
{LLAMA_GRETYPE_END, 0},
195+
});
196+
184197
verify_parsing(R"""(
185198
root ::= a+
186199
a ::= "a"

0 commit comments

Comments
 (0)