Skip to content

Commit

Permalink
Parse __END__ constructs in the lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Dec 7, 2013
1 parent 1aef0d4 commit 17690dc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -51,6 +51,9 @@
* Fix bug where sometimes the wrong regexp flags would be generated in the
output javascript.

* Support parsing `__END__` constructs in ruby code, inside the lexer. The
content is gathered up by use of the parser.

* Support single character strings (using ? prefix) with escaped characters.

## 0.5.5 2013-11-25
Expand Down
12 changes: 12 additions & 0 deletions lib/opal/parser/lexer.rb
Expand Up @@ -23,6 +23,7 @@ class Lexer

attr_reader :line
attr_reader :scope
attr_reader :eof_content

attr_accessor :lex_state
attr_accessor :strterm
Expand Down Expand Up @@ -1143,6 +1144,17 @@ def yylex
cmdarg_push 0
return result

elsif scanner.bol? and skip(/\__END__(\n|$)/)
while true
if scanner.eos?
@eof_content = self.yylval
return false
end

scan(/(.*)/)
scan(/\n/)
end

elsif check(/[0-9]/)
return process_numeric

Expand Down
24 changes: 24 additions & 0 deletions spec/cli/lexer_spec.rb
Expand Up @@ -76,4 +76,28 @@
end
end
end

describe "__END__ content in a source file" do
it "ignores token and following content if at start of line and followed by newline" do
expect_parsed("42\n__END__").to eq([:int, 42])
expect_parsed("42\n__END__\nFred").to eq([:int, 42])
expect_parsed(" __END__").to eq([:call, nil, :__END__, [:arglist]])
expect_parsed("__END__ ").to eq([:call, nil, :__END__, [:arglist]])
expect_parsed("__END__ 42").to eq([:call, nil, :__END__, [:arglist, [:int, 42]]])
end

it "adds any __END__ content to lexer.eof_content" do
parser = Opal::Parser.new
parser.parse("42")
expect(parser.lexer.eof_content).to eq(nil)

parser = Opal::Parser.new
parser.parse("42\n__END__\nFord\nPerfect")
expect(parser.lexer.eof_content).to eq("Ford\nPerfect")

parser = Opal::Parser.new
parser.parse("42\n__END__\n")
expect(parser.lexer.eof_content).to eq("")
end
end
end
4 changes: 4 additions & 0 deletions spec/cli/spec_helper.rb
Expand Up @@ -5,6 +5,10 @@ def parsed(source, file='(string)')
Opal::Parser.new.parse(source, file)
end

def expect_parsed(source)
expect(parsed(source))
end

def expect_parsed_string(source)
expect(parsed(source)[1])
end
Expand Down

0 comments on commit 17690dc

Please sign in to comment.