Skip to content

Commit

Permalink
added new grammar rule for SourceElementList to organize the grammar …
Browse files Browse the repository at this point in the history
…a little better
  • Loading branch information
Richard Jordan committed Jul 30, 2008
1 parent 15817e4 commit c4fda2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
29 changes: 12 additions & 17 deletions lib/parser.y
Expand Up @@ -40,8 +40,12 @@ preclow

rule
SourceElements:
/* nothing */ { result = SourceElementsNode.new([]) }
| SourceElementList { result = SourceElementsNode.new([val].flatten) }

SourceElementList:
SourceElement
| SourceElements SourceElement { result = val.flatten }
| SourceElementList SourceElement { result = val.flatten }
;

SourceElement:
Expand Down Expand Up @@ -500,12 +504,8 @@ rule


Block:
'{' '}' {
result = BlockNode.new(SourceElementsNode.new([]))
debug(result)
}
| '{' SourceElements '}' {
result = BlockNode.new(SourceElementsNode.new([val[1]].flatten))
'{' SourceElements '}' {
result = BlockNode.new(val[1])
debug(result)
}
;
Expand Down Expand Up @@ -750,18 +750,14 @@ rule
;

CaseClause:
CASE Expr ':' { result = CaseClauseNode.new(val[1]) }
| CASE Expr ':' SourceElements {
result = CaseClauseNode.new(val[1], SourceElementsNode.new([val[3]].flatten))
CASE Expr ':' SourceElements {
result = CaseClauseNode.new(val[1], val[3])
}
;

DefaultClause:
DEFAULT ':' {
result = CaseClauseNode.new(nil, SourceElementsNode.new([]))
}
| DEFAULT ':' SourceElements {
result = CaseClauseNode.new(nil, SourceElementsNode.new([val[2]].flatten))
DEFAULT ':' SourceElements {
result = CaseClauseNode.new(nil, val[2])
}
;

Expand Down Expand Up @@ -846,8 +842,7 @@ rule
;

FunctionBody:
/* not in spec */ { result = FunctionBodyNode.new(SourceElementsNode.new([])) }
| SourceElements { result = FunctionBodyNode.new(SourceElementsNode.new([val[0]].flatten)) }
SourceElements { result = FunctionBodyNode.new(val[0]) }
;
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rkelly/parser.rb
Expand Up @@ -16,7 +16,7 @@ def initialize
def parse(javascript)
@tokens = TOKENIZER.tokenize(javascript)
@position = 0
SourceElementsNode.new([do_parse].flatten)
do_parse
end

private
Expand Down
6 changes: 3 additions & 3 deletions test/test_automatic_semicolon_insertion.rb
Expand Up @@ -120,15 +120,15 @@ def test_insertion_after_return
end

def test_insertion_after_throw
assert_sexp([nil], @parser.parse("throw\nfoo"))
assert_nil @parser.parse("throw\nfoo")
end

def test_no_empty_statement_insertion
assert_sexp([nil], @parser.parse("if (a > b)\nelse c = d"))
assert_nil @parser.parse("if (a > b)\nelse c = d")
end

def test_no_for_insertion
assert_sexp([nil], @parser.parse("for (a;b\n){}"))
assert_nil @parser.parse("for (a;b\n){}")
end

def assert_sexp(expected, node)
Expand Down

0 comments on commit c4fda2d

Please sign in to comment.