-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathparse.ls
24 lines (18 loc) · 817 Bytes
/
parse.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Serves as an adapter from the S-expression parser's format to the internal
# AST objects.
parse-sexpr = require \sexpr-plus .parse
list = (values, location) -> { type : \list values, location }
atom = (value, location) -> { type : \atom value, location }
string = (value, location) -> { type : \string value, location }
convert = (tree) ->
# Parser returns locations with 1-based columns, but source map
# expects 0-based, so let's fix that.
tree.location
..start.column -= 1
..end .column -= 1
switch tree.type
| \list => list (tree.content.map convert), tree.location
| \atom => atom tree.content, tree.location
| \string => string tree.content, tree.location
| null => throw Error "Unexpected type `#that` (of `#tree`)"
module.exports = parse-sexpr >> (.map convert)