Skip to content

Commit

Permalink
Some cleanup and initial doc extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Oct 28, 2011
1 parent d24473e commit 5a499c3
Showing 1 changed file with 78 additions and 4 deletions.
82 changes: 78 additions & 4 deletions lib/lattescript/stringifier.rb
Expand Up @@ -15,6 +15,29 @@ def visit(node)
send("visit_#{type}", node)
end

def visit_Program(program)
map(program.elements)
end

def visit_ExpressionStatement(statement)
accept(statement.expression)
end

def visit_SequenceExpression(expression)
[expression.parens, map(expression.expressions)]
end

def visit_Literal(literal)
case val = literal.val
when nil
"null"
when LatteScript::AST::Node
accept val
else
val
end
end

def visit_Number(number)
number.val
end
Expand Down Expand Up @@ -103,8 +126,7 @@ def visit_IfStatement(statement)
[
accept(statement.test),
accept(statement.consequent),
accept(statement.alternate),
accept(statement.body)
statement.alternate && accept(statement.alternate)
]
end

Expand Down Expand Up @@ -167,7 +189,7 @@ def visit_CatchClause(clause)

def visit_FunctionDeclaration(decl)
[
accept(decl.id),
decl.id && accept(decl.id),
map(decl.params),
map(decl.body)
]
Expand All @@ -189,11 +211,63 @@ def visit_ConditionalExpression(expr)
[accept(expr.test), accept(expr.consequent), accept(expr.alternate)]
end

def visit_comment(comment)
def visit_Comment(comment)
[comment.type, comment.body, comment.newline]
end
end

require "pp"

class CommentScanner < Visitor
def visit_CommentedStatement(comment)
unless comment.comments.empty?
case statement = comment.statement
when AST::SequenceExpression, AST::ExpressionStatement
statement = statement.expression.expressions[0]
end

print_comment(statement, comment.comments)
#puts
#puts "-" * 10 + statement.class.name + "-" * 10
#puts comment.comments.map { |c| c.body }.join("\n")
#puts "-" * (10 + statement.class.name.size + 10)
#puts

#puts Stringifier.to_string(Marshal.load(Marshal.dump(statement)))
#puts
end

super
end

def visit_Property(property)
unless property.comments.empty?
print_comment(property, property.comments)
#puts
#puts "-" * 10 + "Property" + "-" * 10
#puts property.comments.map { |c| c.body }.join("\n")
#puts "-" * (10 + "Property".size + 10)
#puts

#puts Stringifier.to_string(Marshal.load(Marshal.dump(property)))
#puts
end

super
end

def print_comment(node, comments)
puts
puts "-" * 10 + node.class.name + "-" * 10
puts comments.map { |c| c.body }.join("\n")
puts "-" * (10 + node.class.name.size + 10)
puts

puts Stringifier.to_string(Marshal.load(Marshal.dump(node)))
puts
end
end

class Stringifier < Visitor
def self.to_string(ast)
stringifier = new(ast)
Expand Down

0 comments on commit 5a499c3

Please sign in to comment.