1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
use lang.bs;
use core.lang;
optional delimiter = SDelimiter;
required delimiter = SRequiredDelimiter;
// Rule to generate proper overloads for the parser.
SPlainFileItem => parserOverloads(parser) : SParser(env) parser;
// Create the actual parser (we want to be able to use the -> syntax).
Parser SParser(Scope env);
SParser => parser
: lang.bs.SName name #fnName, ":", "parser" #keyword, "(", SParserType(env, name) parser - SParserOptions(parser), ")", SFreeOptions@ -> setOptions, "{" [, SParserBody -> setSyntax, ]+ "}";
// Parser types. This creates the actual parser instance to use, also makes the system extensible.
Parser SParserType(Scope env, SStr name) #keyword;
SParserType => RecursiveParser(env, name) : "recursive" ~ "descent";
SParserType => RecursiveParser(env, name) : "recursive";
SParserType => BacktrackingParser(env, name) : "backtracking" - (~ "recursive")?;
SParserType => BacktrackingParser(env, name) : "backtracking" ~ "recursive" ~ "descent";
// Additional options for the parser.
void SParserOptions(Parser parser);
SParserOptions : ;
SParserOptions : ",", SParserOption(parser), SParserOptions(parser);
void SParserOption(Parser parser);
SParserOption => makeBinary(parser) : "binary" #keyword;
SParserOption => makeDual(parser) : "text" #keyword ~ "and" #keyword ~ "binary" #keyword;
// Body for a parser.
// Note: We don't currently support 'extend', extra syntax may be included from the regular BS use stmts instead.
ParserSyntax SParserBody();
SParserBody => ParserSyntax() : (lang.bnf.SDocFileItem() -> push, ";", )*;
// Additional things that may appear in our syntax.
SParserBody..lang.bnf.SFileItem => StartDecl(rule) : "start" #keyword, "=", lang.bnf.SName rule;
|