Skip to content

Commit

Permalink
restrict to known units of measure
Browse files Browse the repository at this point in the history
- must be condensed to one word only
  • Loading branch information
Andy Weidenbaum committed Jun 9, 2018
1 parent 832cecd commit 013339b
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 37 deletions.
89 changes: 68 additions & 21 deletions lib/TXN/Parser/Grammar.pm6
Expand Up @@ -598,7 +598,15 @@ proto token amount {*}
token amount:commodity
{
# -42 oz·Au
<plus-or-minus>? <asset-quantity> \h+ <unit-of-measure> <asset-code>
| [
<plus-or-minus>? <asset-quantity> \h+
<unit-of-measure> \h* <of> \h* <asset-code>
]
# -42 oz of Au
| [
<plus-or-minus>? <asset-quantity> \h+
<unit-of-measure> \h+ of \h+ <asset-code>
]
}

token amount:asset
Expand Down Expand Up @@ -641,26 +649,65 @@ token asset-quantity:integer

token unit-of-measure
{
# square kilometers of
| <unit-of-measure-words> \h+ of \h+
# oz ·
| <unit-of-measure-words> \h* <of> \h*
}

token unit-of-measure-words
{
<unit-of-measure-word> [ \h+ <unit-of-measure-word> ]*
}

# anything but whitespace, C<'of'> and text containing C<token <of>>
token unit-of-measure-word
{
<!before 'of'> <unit-of-measure-word-char>+
}

token unit-of-measure-word-char
{
<+[\H] -of>
# meters
| 'm' | 'nm' | 'mm' | 'cm' | 'dc' | 'km'
# square meters
| 'm²' | 'nm²' | 'mm²' | 'cm²' | 'dc²' | 'km²'
# cubic meters
| 'm³' | 'nm³' | 'mm³' | 'cm³' | 'dc³' | 'km³'
# inches/feet/yards/miles/nautical miles
| 'in' | 'ft' | 'yd' | 'mi' | 'sm'
# square "
| 'in²' | 'ft²' | 'yd²' | 'mi²' | 'sm²'
# cubic "
| 'in³' | 'ft³' | 'yd³' | 'mi³' | 'sm³'
# acres
| 'acre' | 'are' | 'ha' | 'hectare'
# liters
| 'l' | 'nl' | 'ml' | 'cl' | 'dl' | 'hl'
| 'L' | 'nL' | 'mL' | 'cL' | 'dL' | 'hL'
# teaspoons
| 'tsp' | 'tspn'
# tablespoons
| 'tbsp' | 'tbspn'
# cups
| 'c' | 'cup'
# pints
| 'pt' | 'pint'
# quarts
| 'qt'
# gallons
| 'gal'
# US gallons
| 'usgal' | 'USgal'
# imperial gallons
| 'impgal'
# fluid ounces
| 'floz' | 'ozfl'
# grams
| 'g' | 'mg' | 'cg' | 'dg' | 'kg'
# grains
| 'gr'
# pennyweights
| 'dwt'
# ounces
| 'oz'
# troy ounces
| 'toz' | 'ozt'
# carats
| 'ct'
# pounds
| 'lb' | 'lbs'
# tons/tonnes
| 'ton' | 't'
# cubic tons/tonnes
| 'ton³' | 't³'
# twenty-foot equivalent unit
| 'teu' | 'TEU'
# cords
| 'cord'
# brass
| 'brass'
}

token of
Expand Down
2 changes: 1 addition & 1 deletion lib/TXN/Parser/Types.pm6
Expand Up @@ -82,7 +82,7 @@ enum Silo is export <ASSETS EXPENSES INCOME LIABILITIES EQUITY>;

subset UnitOfMeasure of Str is export where
{
TXN::Parser::Grammar.parse($_, :rule<unit-of-measure-words>);
TXN::Parser::Grammar.parse($_, :rule<unit-of-measure>);
}

# end UnitOfMeasure }}}
Expand Down
23 changes: 8 additions & 15 deletions t/grammar/02-posting.t
Expand Up @@ -218,16 +218,9 @@ subtest({

subtest({
my Str @unit-of-measure =
Q{cups of },
Q{c of },
Q{},
Q{c ·},
Q{c · },
Q{fluid ounces of },
Q{fl.oz of },
Q{fl.oz·},
Q{fl.oz ·},
Q{fl.oz · };
Q{cup},
Q{c},
Q{floz};

ok(
@unit-of-measure
Expand Down Expand Up @@ -355,10 +348,10 @@ subtest({
Q{BTC +฿100.00},
Q{BTC +฿1_0_0.0_0},
Q{1 floz·sprite},
Q{1.5 fl.oz·sprite},
Q{15_000.0 fl.oz·sprite},
Q{1 fl oz · sprite},
Q{1 fl oz of sprite};
Q{1.5 floz·sprite},
Q{15_000.0 floz·sprite},
Q{1 floz · sprite},
Q{1 floz of sprite};

ok(
@amount.grep({ .&is-valid-amount }).elems == @amount.elems,
Expand All @@ -384,7 +377,7 @@ subtest({
Q{Assets:Personal:FirstBank +฿1_000_000.00 USD},
Q{Expenses:Business:Cats:Food Ł5.99 LTC},
Q{Liabilities:Xray 1 floz·sprite},
Q{Equity:Yankee 1.06 fluid ounces of sprite @ 0.05 USD},
Q{Equity:Yankee 1.06 floz of sprite @ 0.05 USD},
Q{Assets:Zero -42 oz·Au};

ok(
Expand Down

0 comments on commit 013339b

Please sign in to comment.