-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update for sexpr parser with object API
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
Showing
2 changed files
with
9 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |