-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.roc
253 lines (211 loc) · 6.57 KB
/
Parser.roc
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
module [parse, Program, Stack, Term]
import parser.Core
import parser.String
Program : {
defs : Dict Str (List Term),
body : List Term,
}
Stack : List Term
Term : [
Number I64,
String Str,
Quotation (List Term),
Builtin Str,
Def Str,
]
parse : Str -> Result Program Str
parse = \input ->
when String.parseStr program (clean input) is
Err (ParsingFailure msg) -> Err msg
Err (ParsingIncomplete remaining) -> Err "I wasn't able to parse all of the input. What I had left was:\n $(remaining)"
Ok p -> Ok p
# Remove comments and blank space
clean : Str -> Str
clean = \input ->
isComment = \line -> Str.trimStart line
|> Str.startsWith "#"
Str.split input "\n"
|> List.dropIf isComment
|> List.dropIf isBlank
|> Str.joinWith "\n"
isBlank : Str -> Bool
isBlank = \str ->
Str.toUtf8 str
|> List.all \x ->
List.contains [' ', '\t', '\n'] x
program =
toProgram = \d -> \b ->
{ defs: Dict.fromList d, body: b }
Core.const toProgram
|> Core.keep (Core.many def)
|> Core.keep body
expect
input =
"""
def1: "foo"
def2: def1 10
10 5 repeat
"""
d = Dict.fromList [("def1", [String "foo"]), ("def2", [Def "def1", Number 10])]
b = [Number 10, Number 5, Builtin "repeat"]
result = String.parseStr program input
result == Ok { defs: d, body: b }
body = terms (Core.oneOrMore space)
expect
result = String.parseStr body "-10 + 10"
result == Ok [Number -10, Builtin "+", Number 10]
expect
result = String.parseStr body "- 10 + 10"
result == Ok [Builtin "-", Number 10, Builtin "+", Number 10]
def =
defName =
Core.const (\x -> x)
|> Core.keep identifier
|> Core.skip (String.codeunit ':')
Core.const (\name -> \ts -> (name, ts))
|> Core.keep defName
|> Core.skip (Core.many hSpace)
|> Core.keep (terms (Core.oneOrMore hSpace))
|> Core.skip (Core.many hSpace)
|> Core.skip (String.codeunit '\n')
expect
result = String.parseStr def "name: \"foo\" + dup\n"
result == Ok ("name", [String "foo", Builtin "+", Builtin "dup"])
expect
result = String.parseStr (terms (Core.many hSpace)) "false \t + - [] "
result == Ok [Builtin "false", Builtin "+", Builtin "-", Quotation []]
identifier =
check = \str ->
converted =
when Str.fromUtf8 str is
Ok s -> s
Err _ -> crash "utf8 conversion error"
if
Str.isEmpty converted
then
Err "identifiers must contain at least one character"
else if
List.contains reserved converted
then
Err "'$(converted)' is a reserved word"
else
Ok converted
isAlphaNumeric = \c -> List.contains alphaNumeric c
Core.chompWhile isAlphaNumeric
|> Core.map check
|> Core.flatten
expect String.parseStr identifier "foo78" == Ok "foo78"
expect String.parseStr identifier "foo_" |> Result.isErr
expect String.parseStr identifier "" |> Result.isErr
alphaNumeric =
digits = List.range { start: At 48, end: At 57 }
caps = List.range { start: At 65, end: At 90 }
lowers = List.range { start: At 97, end: At 122 }
List.join [digits, caps, lowers]
terms = \spacer ->
Core.sepBy term spacer
term =
[number, quotation, string]
|> List.concat builtins
|> Core.oneOf
|> Core.alt (identifier |> Core.map Def)
expect String.parseStr term "quote" == Ok (Builtin "quote")
expect String.parseStr term "true" == Ok (Builtin "true")
expect String.parseStr term "789" == Ok (Number 789)
expect String.parseStr term "[dup]" == Ok (Quotation [Builtin "dup"])
expect String.parseStr term "[dup" |> Result.isErr
expect String.parseStr term "unknown" == Ok (Def "unknown")
builtins =
toParser = \s -> String.string s |> Core.map Builtin
reserved |> List.map toParser
reserved : List Str
reserved = [
"dup",
"swap",
"dig",
"+",
"-",
"*",
"=",
"apply",
"repeat",
"compose",
"quote",
"drop",
"branch",
"true",
"false",
"split",
]
number =
positive = String.digits |> Core.map \n -> Num.toI64 n |> Number
negative =
convert = \n -> Num.toI64 n * -1 |> Number
Core.const convert
|> Core.skip (String.codeunit '-')
|> Core.keep String.digits
Core.alt positive negative
expect String.parseStr number "12345" == Ok (Number 12345)
expect String.parseStr number "-567" == Ok (Number -567)
expect String.parseStr number "aldkfadlkfj" |> Result.isErr
string =
toStr = \list ->
when Str.fromUtf8 list is
Ok s -> String s
# TODO: make this use Core.fail instead of crashing
Err _ -> crash "I was unable to convert the contents of a string as utf8"
isNotQuote = \c -> c != '"'
Core.const toStr
|> Core.skip (String.string "\"")
|> Core.keep (Core.chompWhile isNotQuote)
|> Core.skip (String.string "\"")
expect
String.parseStr string "\"a string\"" == Ok (String "a string")
# buildPrimitiveParser is used here as a work around for a bug that sometimes comes up with recursive parsers such as this
# https://roc.zulipchat.com/#narrow/stream/231634-beginners/topic/Compiler.20stack.20overflow.20on.20recursive.20parser/near/377685052
quotation =
bracket = \b ->
Core.const (\_ -> {})
|> Core.keep (String.codeunit b)
|> Core.skip (Core.many hSpace)
Core.buildPrimitiveParser \input ->
Core.parsePartial
(
Core.between (terms (Core.many hSpace)) (bracket '[') (bracket ']')
|> Core.map (\list -> Quotation list)
)
input
expect
result = String.parseStr quotation "[]"
result == Ok (Quotation [])
expect
String.parseStr quotation "foo" |> Result.isErr
expect
out = String.parseStr quotation "[true]"
out == Ok (Quotation [Builtin "true"])
expect
result = String.parseStr quotation "[ false 7 + - [] ]"
result == Ok (Quotation [Builtin "false", Number 7, Builtin "+", Builtin "-", Quotation []])
space =
[' ', '\t', '\n']
|> List.map String.codeunit
|> Core.oneOf
hSpace =
[' ', '\t']
|> List.map String.codeunit
|> Core.oneOf
expect String.parseStr space "\n" |> Result.isOk
expect String.parseStr hSpace "\n" |> Result.isErr
expect
result = parse
"""
[1 swap dup apply] 1 swap dup apply
"""
result |> Result.isOk
expect
result = parse
"""
fact: 1 - isZero [drop] [dup dig * swap fact] branch apply
dup fact
"""
result |> Result.isOk