Skip to content

Instantly share code, notes, and snippets.

@CIAvash
Last active January 1, 2022 19:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save CIAvash/e7d877b9c394f9b8b342c8f9fde90fe1 to your computer and use it in GitHub Desktop.
File Grammar - answer to https://paste.rs/woZ.pl
#!/usr/bin/env raku
grammar FileGrammar {
# The top-level file is a list of statements
token TOP { ^ <line>* %% \v* $ }
# A line is either a list of keyword-payload or scope
token line { <statement> | <scope> }
# A statement is a simple keyword-payload mapping
token statement { <keyword> \s <payload> }
# The set of simple key-value keywords
token keyword { < ANCHOR BG COUNT ENV MODE SPACING > }
# A simple payload
token payload { \S+ %% \h* }
# A scope is a scope-word + list of scope-lines
token scope { <scope-word> \v <scope-line>* }
# The set of scope-opening keywords
token scope-word { < INPUTS OUTPUTS > }
# A scope line is either a simple payload or a full statement
token scope-line { \h* [ <statement> | <payload> ] \v+ }
}
class Actions {
method TOP($/) {
make $<line>».made
}
method line($/) {
make .made with $<statement> || $<scope>;
}
method statement($/) {
make { Keyword => $<keyword>.made, Payload => $<payload>.made }
}
method scope($/) {
make { Keyword => $<scope-word>.made, Payload => $<scope-line>».made }
}
method scope-line($/) {
make $<statement> ?? $<statement>.made !! { Payload => $<payload>.made }
}
method keyword($/) { make ~$/ }
method payload($/) { make ~$/ }
method scope-word($/) { make ~$/ }
}
my @parse = FileGrammar.parse(q:to/EOI/, actions => Actions.new).made;
BG foo.png
SPACING 2 2
INPUTS
copper_ingot 7
tin_ingot 1
OUTPUTS
bronze_ingot 8
EOI
for @parse -> %line {
dd %line
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment