GbnfRecognizer class final

Parses a GBNF grammar with llama.cpp's syntax (llama-grammar.cpp at llama.cpp v0.4.0) and tells whether it admits a string.

Supported: rules name ::= … (names of letters, digits and -), literals "…", character classes […] and [^…] with ranges, ., groups, alternation, the quantifiers ? * + {m} {m,} {m,n}, rule references and # comments; escapes \n \r \t \\ \" \[ \] \- \xHH \uHHHH \UHHHHHHHH, as llama.cpp's parse_char reads them (any other escape is an error there, so it is one here). A newline ends a rule except inside a group or after |.

Repetitions follow llama.cpp's limits (handle_repetitions): a {m,n} maximum above 2000 means no maximum; a minimum above 2000 is an error, and so is a repetition whose bound (the maximum, else the minimum) times the rules its item synthesizes reaches 2000. A group synthesizes one rule, plus every rule its content does: a nested group, the helper rules of a repetition (n - m for {m,n}, one for an open one), a rule name met for the first time. A rule reference synthesizes none.

Construction throws FormatException on a syntax error, a rule defined twice, a reference to an undefined rule, a missing root, a repetition llama.cpp refuses or left recursion, which llama.cpp refuses too: a rule that can reach itself before any character, or an open repetition (*, +, {m,}) of an item that llama.cpp takes to be possibly empty (a group or a rule with an alternative that writes nothing, a repetition with no minimum).

Constructors

GbnfRecognizer(String grammar, {String root = 'root'})

Properties

hashCode → int
The hash code for this object.
no setterinherited
root → String
The start rule.
final
ruleNames → Iterable<String>
The names of the rules, in definition order.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

accepts(String text, {String? rule}) → bool
Whether rule (default: root) derives exactly text.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited