Permalink
Browse files

More rounding out of the parser; runs eg/richards.js again.

I think this returns the parser to its previous working state... though
much slower than the old one, even on parslet's "tuning" branch.
  • Loading branch information...
1 parent a7e47ad commit bbab21961540da1774a15e579605276ed2804eb5 @matthewd committed Jan 22, 2011
Showing with 206 additions and 64 deletions.
  1. +148 −5 lib/capuchin/nodes.rb
  2. +53 −30 lib/capuchin/parser.rb
  3. +5 −29 lib/capuchin/visitor.rb
View
@@ -32,41 +32,78 @@ class RootNode < Node
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value
+ end
end
-class ThisNode < Node; end
-class NullNode < Node; end
-class TrueNode < Node; end
-class FalseNode < Node; end
+class ThisNode < Node
+ def visit(g); end
+end
+class NullNode < Node
+ def visit(g); end
+end
+class TrueNode < Node
+ def visit(g); end
+end
+class FalseNode < Node
+ def visit(g); end
+end
class LiteralNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g); end
end
class NumberNode < LiteralNode; end
class StringNode < LiteralNode; end
class RegexpNode < LiteralNode; end
-class ObjectLiteralNode < LiteralNode; end
+class ObjectLiteralNode < LiteralNode
+ def visit(g)
+ value.each do |prop|
+ g.accept prop
+ end
+ end
+end
+class ArrayNode < LiteralNode
+ def visit(g)
+ value.each do |el|
+ g.accept el
+ end
+ end
+end
class FunctionDeclNode < Node
attr_accessor :value, :function_body, :arguments
def initialize(value, function_body, arguments)
@value, @function_body, @arguments = value, function_body, arguments
end
+ def visit(g)
+ g.accept function_body
+ end
end
class FunctionExprNode < Node
attr_accessor :value, :function_body, :arguments
def initialize(value, function_body, arguments)
@value, @function_body, @arguments = value, function_body, arguments
end
+ def visit(g)
+ g.accept function_body
+ end
end
class FunctionCallNode < Node
attr_accessor :value, :arguments
def initialize(value, arguments)
@value, @arguments = value, arguments
end
+ def visit(g)
+ g.accept value
+ arguments.each do |arg|
+ g.accept arg
+ end
+ end
end
class NewExprNode < FunctionCallNode; end
@@ -75,51 +112,75 @@ class BreakNode < Node
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value if value
+ end
end
class ContinueNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value if value
+ end
end
class ReturnNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value if value
+ end
end
class ThrowNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value
+ end
end
class ExpressionStatementNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value
+ end
end
class UnaryNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value
+ end
end
class PrefixNode < UnaryNode
attr_accessor :operand
def initialize(value, operand)
super(value)
@operand = operand
end
+ def visit(g)
+ g.accept operand
+ end
end
class PostfixNode < UnaryNode
attr_accessor :operand
def initialize(value, operand)
super(value)
@operand = operand
end
+ def visit(g)
+ g.accept operand
+ end
end
class DeleteNode < UnaryNode; end
class VoidNode < UnaryNode; end
@@ -134,6 +195,10 @@ class BinaryNode < Node
def initialize(left, value)
@left, @value = left, value
end
+ def visit(g)
+ g.accept left
+ g.accept value
+ end
end
class MultiplyNode < BinaryNode; end
class DivideNode < BinaryNode; end
@@ -179,56 +244,125 @@ class ConditionalNode < Node
def initialize(conditions, value, else_)
@conditions, @value, @else = conditions, value, else_
end
+ def visit(g)
+ g.accept conditions
+ g.accept value
+ g.accept self.else
+ end
end
class VarStatementNode < Node
attr_accessor :value
def initialize(value)
@value = value
end
+ def visit(g)
+ g.accept value
+ end
end
class VarDeclNode < Node
attr_accessor :name, :value, :const
def initialize(name, value, const=false)
@name, @value, @const = name, value, const
end
+ def visit(g)
+ g.accept value if value
+ end
end
class PropertyNode < Node
attr_accessor :name, :value
def initialize(name, value)
@name, @value = name, value
end
+ def visit(g)
+ g.accept value
+ end
end
class ForNode < Node
attr_accessor :init, :test, :counter, :value
def initialize(init, test, counter, value)
@init, @test, @counter, @value = init, test, counter, value
end
+ def visit(g)
+ g.accept init if init
+ g.accept test if test
+ g.accept counter if counter
+ g.accept value
+ end
+end
+class DoWhileNode < Node
+ attr_accessor :left, :value
+ def initialize(left, value)
+ @left, @value = left, value
+ end
+ def visit(g)
+ g.accept left
+ g.accept value
+ end
+end
+class WhileNode < Node
+ attr_accessor :left, :value
+ def initialize(left, value)
+ @left, @value = left, value
+ end
+ def visit(g)
+ g.accept left
+ g.accept value
+ end
end
class IfNode < Node
attr_accessor :conditions, :value, :else
def initialize(conditions, value, else_)
@conditions, @value, @else = conditions, value, else_
end
+ def visit(g)
+ g.accept conditions
+ g.accept value
+ g.accept self.else if self.else
+ end
+end
+class TryNode < Node
+ attr_accessor :value, :catch_var, :catch_block, :finally_block
+ def initialize(value, catch_var, catch_block, finally_block)
+ @value, @catch_var, @catch_block, @finally_block = value, catch_var, catch_block, finally_block
+ end
+ def visit(g)
+ g.accept value
+ g.accept catch_block if catch_block
+ g.accept finally_block if finally_block
+ end
end
class SwitchNode < Node
attr_accessor :left, :value
def initialize(left, value)
@left, @value = left, value
end
+ def visit(g)
+ g.accept left
+ g.accept value
+ end
end
class CaseClauseNode < Node
attr_accessor :left, :value
def initialize(left, value)
@left, @value = left, value
end
+ def visit(g)
+ g.accept left
+ g.accept value
+ end
end
class BlockNode < Node
attr_accessor :statements
def initialize(statements)
@statements = statements
end
+ def visit(g)
+ statements.each do |st|
+ g.accept st
+ end
+ end
end
class DotAccessorNode < Node
@@ -266,6 +400,9 @@ def call_bytecode(v, g)
arg_count = yield(2)
g.send :js_invoke, arg_count + 1
end
+ def visit(g)
+ g.accept value
+ end
end
class BracketAccessorNode < Node
attr_accessor :value, :accessor
@@ -318,6 +455,10 @@ def call_bytecode(v, g)
arg_count = yield(2)
g.send :js_invoke, arg_count + 1
end
+ def visit(g)
+ g.accept value
+ g.accept accessor
+ end
end
class ResolveNode < Node
attr_accessor :value
@@ -364,6 +505,8 @@ def get_and_set_bytecode(v, g)
g.send :[]=, 2
end
end
+ def visit(g)
+ end
end
end
Oops, something went wrong.

0 comments on commit bbab219

Please sign in to comment.