2019-06-04 16:12:42 -07:00
|
|
|
token whitespace = /[ \n\t]+/ [ skip ];
|
|
|
|
token upper_identifier = /[A-Z_][a-z_A-Z0-9]*/;
|
|
|
|
token lower_identifier = /[a-z_][a-z_A-Z0-9]*/;
|
2019-06-04 17:06:45 -07:00
|
|
|
token number = /[0-9]+/;
|
2019-06-04 16:12:42 -07:00
|
|
|
token kw_data = /data/;
|
|
|
|
token kw_type = /type/;
|
|
|
|
token kw_defn = /defn/;
|
2019-06-04 16:54:13 -07:00
|
|
|
token kw_let = /let/;
|
|
|
|
token kw_letrec = /letrec/;
|
|
|
|
token kw_in = /in/;
|
|
|
|
token kw_case = /case/;
|
|
|
|
token kw_of = /of/;
|
2019-06-04 16:12:42 -07:00
|
|
|
token tkn_ocurly = /{/;
|
|
|
|
token tkn_ccurly = /}/;
|
|
|
|
token tkn_oparen = /\(/;
|
|
|
|
token tkn_cparen = /\)/;
|
|
|
|
token tkn_eq = /=/;
|
|
|
|
token tkn_comma = /,/;
|
2019-06-04 16:54:13 -07:00
|
|
|
token tkn_arrow = /->/;
|
|
|
|
token op_add = /\+/;
|
|
|
|
token op_sub = /-/;
|
|
|
|
token op_mul = /\*/;
|
|
|
|
token op_div = /\//;
|
2019-06-04 16:12:42 -07:00
|
|
|
|
|
|
|
rule S = program;
|
|
|
|
|
|
|
|
rule program
|
|
|
|
= definition program?
|
|
|
|
;
|
|
|
|
|
|
|
|
rule definition
|
|
|
|
= kw_data upper_identifier tkn_eq data_body
|
|
|
|
| kw_type upper_identifier tkn_eq type_body
|
|
|
|
| kw_defn lower_identifier params? tkn_eq defn_body
|
|
|
|
;
|
|
|
|
|
|
|
|
rule data_body
|
|
|
|
= tkn_ocurly data_elems tkn_ccurly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule data_elems
|
|
|
|
= data_elem
|
|
|
|
| data_elem tkn_comma data_elems
|
|
|
|
;
|
|
|
|
|
|
|
|
rule data_elem
|
|
|
|
= upper_identifier
|
|
|
|
| upper_identifier tkn_oparen data_elem_types tkn_cparen
|
|
|
|
;
|
|
|
|
|
|
|
|
rule data_elem_types
|
|
|
|
= type
|
|
|
|
| type tkn_comma data_elem_types
|
|
|
|
;
|
|
|
|
|
|
|
|
rule type_body
|
|
|
|
= tkn_ocurly type tkn_ccurly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule params
|
|
|
|
= lower_identifier params?
|
|
|
|
;
|
|
|
|
|
|
|
|
rule defn_body
|
|
|
|
= tkn_ocurly expr tkn_ccurly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr
|
2019-06-04 16:54:13 -07:00
|
|
|
= expr_add
|
|
|
|
| expr_let
|
|
|
|
| expr_letrec
|
|
|
|
| expr_case
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_add
|
|
|
|
= expr_add op_add expr_mul
|
|
|
|
| expr_add op_sub expr_mul
|
|
|
|
| expr_mul
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_mul
|
|
|
|
= expr_mul op_mul expr_app
|
|
|
|
| expr_mul op_div expr_app
|
|
|
|
| expr_app
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_app
|
|
|
|
= expr_app? expr_app_bottom
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_app_bottom
|
|
|
|
= lower_identifier
|
|
|
|
| upper_identifier
|
2019-06-04 17:06:45 -07:00
|
|
|
| number
|
2019-06-04 16:54:13 -07:00
|
|
|
| tkn_oparen expr tkn_cparen
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_curly
|
|
|
|
= tkn_ocurly expr tkn_ccurly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_let
|
|
|
|
= kw_let lower_identifier tkn_eq expr_curly kw_in expr_curly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_letrec
|
|
|
|
= kw_letrec lower_identifier tkn_eq expr_curly kw_in expr_curly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_case
|
|
|
|
= kw_case expr kw_of tkn_ocurly expr_case_branches tkn_ccurly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_case_branches
|
|
|
|
= expr_case_branches? expr_case_branch
|
|
|
|
;
|
|
|
|
|
|
|
|
rule expr_case_branch
|
|
|
|
= pattern tkn_arrow expr_curly
|
|
|
|
;
|
|
|
|
|
|
|
|
rule pattern
|
2019-06-04 16:12:42 -07:00
|
|
|
= lower_identifier
|
2019-06-04 16:54:13 -07:00
|
|
|
| upper_identifier
|
|
|
|
| upper_identifier tkn_oparen patterns tkn_cparen
|
|
|
|
;
|
|
|
|
|
|
|
|
rule patterns
|
|
|
|
= pattern
|
|
|
|
| patterns tkn_comma pattern
|
2019-06-04 16:12:42 -07:00
|
|
|
;
|
|
|
|
|
|
|
|
rule type
|
|
|
|
= upper_identifier
|
|
|
|
;
|