diff options
Diffstat (limited to 'ast.moon')
| -rw-r--r-- | ast.moon | 26 |
1 files changed, 19 insertions, 7 deletions
@@ -1,3 +1,9 @@ +unescape = (str) -> + str = str\gsub '\\"', '"' + str = str\gsub "\\'", "'" + str = str\gsub "\\\\", "\\" + str + class Atom new: (@raw, @style='', @value) => @@ -9,12 +15,15 @@ class Atom @raw when '"' "\"#{@raw}\"" + when "'" + "'#{@raw}'" else - error! + error "unknown atom style: '#{@style}'" - @make_num: (match) -> Atom match, '', tonumber match + @make_num: (match, ...) -> Atom match, '', tonumber match @make_sym: (match) -> Atom match, '', match - @make_str: (match) -> Atom match, '"', match + @make_strd: (match) -> Atom match, '"', unescape match + @make_strq: (match) -> Atom match, "'", unescape match class Xpr new: (parts, @style='(', tag) => @@ -29,12 +38,13 @@ class Xpr @tag = tag.value _walk_sexpr: => - if @style == '(' - coroutine.yield @ - + -- depth first for frag in *@ frag\_walk_sexpr! + if @style == '(' + coroutine.yield @ + walk_sexpr: => coroutine.wrap -> @_walk_sexpr! @@ -48,9 +58,11 @@ class Xpr switch @style when 'naked' buf - else + when '(' tag = if @tag then "[#{@tag}]" else '' '(' .. tag .. buf .. ')' + else + error "unknown sexpr style: '#{@style}'" make_sexpr: (tag, parts) -> if parts |
