Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inadvertent automatic semicolon insertion #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/rkelly/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def initialize
@tokens = []
@logger = nil
@terminator = false
@for_header_parentheses = 0
@prev_token = nil
@comments = []
end
Expand Down Expand Up @@ -88,7 +89,21 @@ def next_token
end
end while([:COMMENT, :S].include?(n_token.name))


if '(' == n_token.value
if 'for' == @prev_token.value
@for_header_parentheses = 1
elsif @for_header_parentheses > 0
@for_header_parentheses += 1
end
end

if ')' == n_token.value && @for_header_parentheses > 0
@for_header_parentheses -= 1
end

if @terminator &&
0 == @for_header_parentheses &&
((@prev_token && %w[continue break return throw].include?(@prev_token.value)) ||
(n_token && %w[++ --].include?(n_token.value)))
@position -= 1
Expand Down
16 changes: 16 additions & 0 deletions test/test_automatic_semicolon_insertion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ def test_no_for_insertion
assert_nil @parser.parse("for (a;b\n){}")
end

def test_no_for_insertion_with_prefix_plus_plus
assert_sexp([[:for, [:resolve, 'a'],
[:resolve, 'b'],
[:prefix, [:resolve, 'c'], '++'],
[:block, []]]],
@parser.parse("for (a;\nb;\n++c){}"))
end

def test_no_for_insertion_with_postfix_plus_plus
assert_sexp([[:for, [:resolve, 'a'],
[:resolve, 'b'],
[:postfix, [:resolve, 'c'], '++'],
[:block, []]]],
@parser.parse("for (a;\nb;\nc++){}"))
end

def assert_sexp(expected, node)
assert_equal(expected, node.to_sexp)
end
Expand Down