Skip to content

Commit

Permalink
Update for sexpr parser with object API
Browse files Browse the repository at this point in the history
Moving to parse s-expressions to objects is necessary for eventually
enabling returning also source-location information along with the
parsed nodes, for more informative errors (#9) and source maps (#18).
  • Loading branch information
anko committed Sep 25, 2015
1 parent 0950f72 commit 1682355
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"esvalid": "1.1.0",
"nopt": "^3.0.3",
"prelude-ls": "^1.1.1",
"sexpr-plus": "^5.0.1",
"sexpr-plus": "^6.0.0",
"uuid": "^2.0.1"
}
}
27 changes: 8 additions & 19 deletions src/parse.ls
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
# Turns an input S-expression string into a simple tree format based on JS
# objects.
# Serves as an adapter from the S-expression parser's format to the internal
# AST objects.

parse-sexpr = require \sexpr-plus .parse
{ list, atom, string } = require \./ast

# This serves as an adapter from the s-expression module's way of returning
# things to eslisp AST objects.
make-explicit = (tree) ->
sexpr-type = (thing) ->
switch typeof! thing
| \String =>
switch typeof thing
| \object => \string
| \string => \atom
| \Array => \list
| otherwise => null

switch sexpr-type tree
| \list => list tree.map make-explicit
| \atom => atom tree.to-string!
| \string => string tree.to-string!
convert = (tree) ->
switch tree.type
| \list => list tree.content.map convert
| \atom => atom tree.content
| \string => string tree.content
| null => throw Error "Unexpected type `#that` (of `#tree`)"

module.exports = parse-sexpr >> make-explicit
module.exports = parse-sexpr >> convert

0 comments on commit 1682355

Please sign in to comment.