As Patrick and Larry had observed, Perl 6 is better parsed with a combination of top-down (Rules) and bottom-up (OpTable) parsers. To reach this goal, today I hacked in Dynamic Rules support to Text.Parser.Rule, so we can define a rule term with any function that can parse a string into anything:
data DynamicTerm = MkDynamicTerm
{ dynLabel :: Str
, dynTerm :: Str -> Maybe (Dynamic, Str)
}
deriving (Data, Typeable)
As a concrete example, here is a grammar that parses a named rule declaration:
ruleGrammar :: Grammar
ruleGrammar = grammar
[ "ruleDecl" ~:~
"rule \\s+ (\\w+) \\s* \\{ <ruleBody> \\}"
, "ruleBody" ~&~ ruleTable
]
And the ruleTable is defined as an OpTable:
ruleTable :: OpTable Rule
ruleTable = mkOpTable
[ noWs (op _Lit Term wsLiteral)
++ noWs (op (_Group CapturePos) Circumfix "( )")
++ noWs (op (_Group NonCapture) Circumfix "[ ]")
-- ...and a lot more...
]
Because of the flexible Dynamic return type, the term derived from OpTable doesn't have to create a MatchObj structure, but can simply return the matched Rule. We can also hook Parsec and parser methods defined in Perl 6 to this engine -- it's very likely to happen during the development towards 6.281, if not sooner.
In other news, tewk finished porting the PILN minilang parser to Rules. It does not run yet, because I hadn't got around to add enumerated character class support, but there's always tomorrow... :-)
Recent Comments