-
-
Save rightfold/ed345900cf17fd90d646 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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