-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhs.peg
147 lines (107 loc) · 3.81 KB
/
hs.peg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
start
= statement_list
statement_list
= '\n'* h:statement S? '\n'+ t:statement_list { return [h].concat(t); }
/ '\n'* o:statement S? '\n'* { return [o] }
S = [ \t]+
statement
= record
/ data
/ set
record
= 'record' S c:constructor S? '{' S? l:record_list S? '}' { return { type: 'record', name: c, members: l } }
record_list
= n:record_item S? ',' S? l:record_list { return [n].concat(l); }
/ n:record_item { return [n]; }
record_item
= n:name S? ':' S? e:expression { return { name: n, expr: e }; }
data
= 'data' S c:data_cons S? '=' S? l:data_term_list { return { type: 'data', name: c, types: l} }
data_term_list
= n:data_term S? '|' S? l:data_term_list { return [n].concat(l); }
/ n:data_term { return [n]; }
data_term
= c:data_cons S l:constructor_list { return { cons: c, types: l} }
/ c:data_cons { return { cons: c, type: []} }
constructor_list
= c:type_atom S l:constructor_list { return [c].concat(l); }
/ c:type_atom { return [c]; }
type_atom
= constructor
/ '[' S? c:constructor S? ']' { return { type: 'List', parameters: [ c ] } }
data_cons
= c:constructor { return c; }
/ ':' c:constructor ':' { return { foreign: c }; }
set
= lhs:value S? bind:pattern_list? S? '=' S? rhs:expression {
if (bind == "") {
return { type: 'set', lhs:lhs, rhs:rhs };
} else {
return { type: 'fun', name:lhs, bind:bind, rhs:rhs };
}
}
value = name
pattern_list
= n:pattern S+ l:pattern_list { return [n].concat(l); }
/ n:pattern { return [n]; }
pattern
= name
/ c:constructor { return { fun: {cons: c}, args: []} }
/ '(' S? e:expression S? ')' { return e; }
expression
= do
/ f:expr_unit S r:expr_unit_list { if (f.fun) { f = f.fun; }; return {type: 'evaluate', fun: f, args: r} }
/ name
/ c:constructor { return {type: 'evaluate', fun: {cons: c}, args: []}; }
/ '[' S? l:expression_list S? ']' { return {type: 'list', value: l}; }
/ literal
do
= 'do' S? '\n' expr_list:indented_expression+ { return {type: 'do', list: expr_list } }
indented_expression
= S e:expression S? '\n' { return e; }
literal
= x:literal_raw { return {type: 'literal', value: x.value, it: x.it}; }
literal_raw
= n:number { return {it: 'Num', value: n}; }
/ s:string { return {it: 'String', value: s}; }
/ o:object { return {it: 'StringMap', value: o}; }
expression_list
= h:expression S? ',' S? t:expression_list { return [h].concat(t); }
/ s:expression { return [s]; }
number = a:('-'?[0-9.]+) { return Number(a.join('')); }
string
= '"' a:([^"]*) '"' { return a.join(''); }
/ "'" a:([^']*) "'" { return a.join(''); }
object = '{' S? l:kv_list S? '}' { return l; }
kv_list
= h:kv S? ',' S? t:kv_list { t[h.k] = h.v; return t }
/ h:kv { var o = {}; o[h.k] = h.v; return o; }
kv
= k:kv_name S? ':' S? v: literal { var r = {k: k, v: v}; return r; }
kv_name
= local_name
/ string
expr_unit
= name
/ c:constructor { return {type: 'evaluate', fun: {cons: c}, args: []}; }
/ operator
/ '(' S? e:expression S? ')' { return e }
/ literal
/ '[' S? e:expr_unit_comma_list S? ']' { return {type: 'list', value: e}; }
expr_unit_comma_list
= h:expression S? ',' S? t:expr_unit_comma_list { return [h].concat(t); }
/ h:expression { return [h]; }
operator
= '+'
/ '(' o:operator ')' { return { type: 'infix', op: o }}
expr_unit_list
= h:expr_unit S t:expr_unit_list { return [h].concat(t); }
/ s:expr_unit { return [s] }
name
= name:local_name { return {type: 'reference', local: name} }
/ name:js_name { return {js: name} }
local_name = name:ident { return name }
js_name = ':' name:js_ident ':' { return name }
ident = fst:[a-z] rst:[a-zA-Z_0-9]* { return fst + rst.join(''); }
js_ident = fst:[a-z] rst:[a-zA-Z_0-9.]* { return fst + rst.join(''); }
constructor = fst:[A-Z] rst:[a-zA-Z_0-9]* { return fst + rst.join(''); }