Skip to content

Instantly share code, notes, and snippets.

@EJTH
Created November 8, 2014 17:22
Show Gist options
  • Save EJTH/b1f7cc4d66f777eee160 to your computer and use it in GitHub Desktop.
Save EJTH/b1f7cc4d66f777eee160 to your computer and use it in GitHub Desktop.
PEG.JS - Danish number literal parser
start
= tal_1000000_9999999
/ tal_1_999999
/ tal_0
tal_1_999
= tal_100_plus
/ tal_100_gange
/ tal_1_99
tal_1_999999
= tal_1001_999999
/ tal_1001_999000
/ tal_1000_gange
/ tal_1_999
tal_1000000_9999999
= a:tal_1000000_gange og? ws? b:tal_1_999999 { return a+b; }
/ a:tal_1000000_gange
tal_1000000_gange
= a:tal_1_999 ws ("millioner"/"million") { return a*1000000; }
tal_1001_999999
= a:tal_1001_999000 og? ws? b:tal_1_999 { return a+b; }
tal_1001_999000
= a:tal_1000_gange og? ws? b:tal_1_999 { return a+b; }
tal_0
= "nul" { return 0; }
tal_1000_gange
= a:tal_1_999 ws tusinde { return a*1000; }
tusinde
= "tusinde" / "tusind"
tal_100_gange
= a:tal_1_99 ws "hundrede" { return a*100; }
tal_100_plus
= a:tal_100_gange og? ws? b:tal_1_99 { return a+b; }
tal_1_99
= tal_deci
/ tal_10_19
/ tal_1_9
tal_1_9
= ("en"/"et") { return 1; }
/ "to" { return 2; }
/ "tre" { return 3; }
/ "fire" { return 4; }
/ "fem" { return 5; }
/ "seks" { return 6; }
/ "syv" { return 7; }
/ "otte" { return 8; }
/ "ni" { return 9; }
tal_1_9_og
= a:tal_1_9 og { return a; }
tal_10
= "ti" { return 10; }
tal_10_19
= tal_10
/ tal_11_19
tal_11_19
= "elleve" { return 11; }
/ "tolv" { return 12; }
/ "tretten" { return 13; }
/ "fjorten" { return 14; }
/ "femten" { return 15; }
/ "seksten" { return 16; }
/ "sytten" { return 17; }
/ "atten" { return 18; }
/ "nitten" { return 19; }
tal_deci_rundt
= "tyve" { return 20; }
/ "tredive" { return 30; }
/ "fyrre" { return 40; }
/ "halvtreds" { return 50; }
/ "treds" { return 60; }
/ "halvfjerds" { return 70; }
/ "firs" { return 80; }
/ "halvfems" { return 90; }
tal_deci
= a:tal_1_9_og b:tal_deci_rundt { return a+b; }
/ a:tal_deci_rundt { return a; }
og
= ws? "og" ws?
ws
= " "+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment