-
-
Save perlpilot/85791ca10f671fa51132d09b7112f744 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
#!/usr/bin/env perl6 | |
use Grammar::ErrorReporting; | |
use Grammar::Tracer; | |
grammar Ingredient::Grammar does Grammar::ErrorReporting { | |
token TOP { <ingredient> } | |
token ingredient { | |
<number>? | |
<.ws>? | |
<unit>? | |
<.ws>? | |
<items> | |
<.ws>? | |
<alternative>? | |
<.ws>? | |
<prep>? | |
} | |
token number { <:N>+ || <.error('Not a number')> } | |
proto token unit {*} | |
token unit:sym<g> { 'g' } | |
token unit:sym<kg> { 'kg' } | |
token unit:sym<l> { 'l' } | |
token unit:sym<ml> { 'ml' } | |
token unit:sym<tbsp> { 'tbsp' } | |
token unit:sym<tsp> { 'tsp' } | |
rule items { .+ [<?before or> | <?before ','>] || .+ } | |
rule alternative { 'or' <items> } | |
rule prep {',' <items>} | |
} | |
say Ingredient::Grammar.parse("23 tbsp salt, 3 cups flour"); |
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
/home/duff/tmp | |
➤ ./foo | |
TOP | |
| ingredient | |
| | number | |
| | * MATCH "23" | |
| | unit | |
| | | unit:sym<tbsp> | |
| | | * MATCH "tbsp" | |
| | * MATCH "tbsp" | |
| | items | |
| | * MATCH "salt" | |
| | alternative | |
| | * FAIL | |
| | prep | |
| | | items | |
| | | * MATCH "3 cups flour" | |
| | * MATCH ", 3 cups flour" | |
| * MATCH "23 tbsp salt, 3 cups flour" | |
* MATCH "23 tbsp salt, 3 cups flour" | |
「23 tbsp salt, 3 cups flour」 | |
ingredient => 「23 tbsp salt, 3 cups flour」 | |
number => 「23」 | |
unit => 「tbsp」 | |
items => 「salt」 | |
prep => 「, 3 cups flour」 | |
items => 「3 cups flour」 | |
/home/duff/tmp | |
➤ perl6 -v | |
This is Rakudo version 2017.08-33-g74ca5ce built on MoarVM version 2017.08.1-19-g151a256 | |
implementing Perl 6.c. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment