Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created November 1, 2016 01:51
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 gfldex/fcd52d39ae9bf4288f3a5952d13717c0 to your computer and use it in GitHub Desktop.
Save gfldex/fcd52d39ae9bf4288f3a5952d13717c0 to your computer and use it in GitHub Desktop.
grammar Schematics {
my $curr_product;
my $curr_line = 0;
token TOP { ^ <line>+ $ }
rule line {
{ $curr_line++ }
<product> \h* '<-' \h* [ <components> | <isk> ]
}
token itemname { [ '-' | \w | [\h\w] ]+ }
token amount { ['-']?\d+[.\d+]? }
token product {
[ (<amount>) \h ]? (<itemname>)
{
$curr_product = Product.new(:name($1.Str), :stacksize($0 ?? $0.Str.Int !! 1));
}
}
token components {
[
<isk>
| [ (<amount>) \h (<itemname>) ] { $curr_product.components.push($1.Str => ($0 / $curr_product.stacksize).Rat ); }
]
[\h* ',' \h* <components>]*
| { die "match failed at: ", $curr_line }
}
token isk {
(<amount>) \s* 'ISK'
{
$curr_product.components.push( ISK.new(:amount($0.Num)) );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment