Skip to content

Commit

Permalink
Support aliases for '->' operator, fixes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Mar 30, 2017
1 parent 080eb56 commit 5051deb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Features
```
Supported units: `A`, `ampere`, `B`, `becquerel`, `bit`, `bits`, `bps`, `Bq`, `byte`, `Byte`, `bytes`, `Bytes`, `C`, `candela`, `cd`, `coulomb`, `d`, `day`, `days`, `deg`, `degree`, `degrees`, `electronvolt`, `eV`, `F`, `farad`, `feet`, `foot`, `ft`, `g`, `gram`, `grams`, `gray`, `Gy`, `H`, `h`, `ha`, `hectare`, `henry`, `hertz`, `hour`, `hours`, `Hz`, `in`, `inch`, `inches`, `J`, `joule`, `joules`, `K`, `kat`, `katal`, `kelvin`, `L`, `lb`, `liter`, `liters`, `lm`, `lumen`, `lux`, `lx`, `m`, `meter`, `meters`, `mile`, `miles`, `min`, `minute`, `minutes`, `mol`, `mole`, `month`, `months`, `mph`, `N`, `newton`, `ohm`, `ounce`, `ounces`, `oz`, `Pa`, `pascal`, `pound`, `pounds`, `rad`, `radian`, `radians`, `S`, `s`, `sec`, `second`, `seconds`, `siemens`, `sievert`, `Sv`, `T`, `t`, `tesla`, `ton`, `tonne`, `tonnes`, `tons`, `V`, `volt`, `W`, `w`, `watt`, `watts`, `Wb`, `weber`, `week`, `weeks`, `yard`, `yards`, `yd`, `year`, `years`, `°`, `Ω`.

- Explicit unit conversions with the `->` operator:
- Explicit unit conversions with the `->` operator (alias: `to`):
```
60mph -> m/s
500km/day -> km/h
Expand Down
6 changes: 3 additions & 3 deletions src/Insect/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ insectLanguage = LanguageDef
, nestedComments: false
, identStart: identStart
, identLetter: identLetter
, opStart: oneOf ['+', '-', '*', '·', '⋅', '×', '/', '÷', '^', '=']
, opStart: oneOf ['+', '-', '*', '·', '⋅', '×', '/', '÷', '^', '→', '=']
, opLetter: oneOf ['>', '*']
, reservedNames: commands <> ["²", "³"]
, reservedNames: commands <> ["²", "³", "to"]
, reservedOpNames: ["->", "+", "-", "*", "·", "", "×", "/", "÷", "^", "**",
"="]
, caseSensitive: true
Expand Down Expand Up @@ -386,7 +386,7 @@ expression =
<|> reservedOp "×"
subOp = reservedOp "-"
addOp = reservedOp "+"
arrOp = reservedOp "->"
arrOp = reservedOp "->" <|> reservedOp "" <|> reserved "to"

square q = BinOp Pow q (Scalar $ fromNumber 2.0)
cube q = BinOp Pow q (Scalar $ fromNumber 3.0)
Expand Down
10 changes: 10 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,10 @@ main = runTest do
[ "2.3m -> in"
, " 2.3 meters->inches "
, " 2.3 m -> in "
, "2.3m→in"
, " 2.3 m → in "
, "2.3m to in"
, " 2.3 m to in "
]

allParseAs (Expression (BinOp ConvertTo (q 120.0 minute) (Unit hour)))
Expand All @@ -494,6 +498,11 @@ main = runTest do
]

shouldFail "2.3m->"
shouldFail "-> km"
shouldFail "2.3m →"
shouldFail "2.3m to"
shouldFail "to km"
shouldFail "to"

test "Complex units" do
allParseAs (Expression (BinOp ConvertTo (BinOp Mul (scalar 36.0) (BinOp Div (Unit (kilo meter)) (Unit hour))) (Unit (mile ./ hour))))
Expand All @@ -516,6 +525,7 @@ main = runTest do
shouldParseAs (Expression (Variable "_prefixed")) "_prefixed"
shouldParseAs (Expression (Variable "x'")) "x'"
shouldParseAs (Expression (Variable "t''")) "t''"
shouldParseAs (Expression (Variable "to_")) "to_"

shouldFail "xs,as"
shouldFail "hello$"
Expand Down

0 comments on commit 5051deb

Please sign in to comment.