EBNF Foreign Rules

Prev:EBNF Rule: Character Class
Next:EBNF Action

Rules can call out to other peg.ebnf defined parsers. The result of the foreign call then becomes the AST of the successful parse. Foreign rules are invoked using '<foreign word-name>' or '<foreign word-name rule>'. The latter allows calling a specific rule in a previously designed peg.ebnf parser. If the 'word-name' is not the name of a peg.ebnf defined parser then it must be a word with stack effect ( -- parser ). It must return a peg defined parser and it will be called to perform the parse for that rule.

USING: prettyprint peg.ebnf ; EBNF: parse-string [=[ StringBody = (!('"') .)* String= '"' StringBody:b '"' => [[ b >string ]] ]=] EBNF: parse-two-strings [=[ TwoStrings = <foreign parse-string String> <foreign parse-string String> ]=] EBNF: parse-two-strings [=[ TwoString = <foreign parse-string> <foreign parse-string> ]=]

: a-token ( -- parser ) "a" token ; EBNF: parse-abc [=[ abc = <foreign a-token> 'b' 'c' ]=]