Skip to content

Instantly share code, notes, and snippets.

@rightfold

rightfold/.pm6 Secret

Created January 24, 2016 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rightfold/ed345900cf17fd90d646 to your computer and use it in GitHub Desktop.
Save rightfold/ed345900cf17fd90d646 to your computer and use it in GitHub Desktop.
use v6.c;
use LCX::Compiler::Data::Symbol;
module LCX::Compiler::Reader {
role BaseGrammar {
rule TOP { <ws> $<terms>=<term>* }
proto rule term {*}
rule term:sym<list> {
'(' $<elements>=<term>* ')'
}
rule term:sym<symbol> {
$<name>=<identifier>
}
token identifier {
<[a..zA..Z\-]>+
}
}
role BaseActions {
method TOP($/) {
make $<terms>>>.made;
}
method term:sym<list>($/) {
make $<elements>>>.made;
}
method term:sym<symbol>($/) {
make symbol($<name>.made);
}
method identifier($/) {
make ~$/;
}
}
our class Extension {
has $.grammar;
has $.actions;
}
sub read-lcx(Str:D $text, @extensions?) is export {
constant grammar := Metamodel::ClassHOW.new_type;
grammar.^add_parent(Grammar);
grammar.^add_role($_) for BaseGrammar, |@extensions>>.grammar;
grammar.^compose;
constant actions := Metamodel::ClassHOW.new_type;
actions.^add_role($_) for BaseActions, |@extensions>>.actions;
actions.^compose;
grammar.parse($text, actions => actions).made;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment