Skip to content

Commit

Permalink
Cleanup heredoc parsing some more
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Dec 2, 2013
1 parent 709080a commit 00e283f
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions lib/opal/parser/lexer.rb
Expand Up @@ -211,6 +211,7 @@ def peek_variable_name

def here_document(str_parse)
eos_regx = /[ \t]*#{Regexp.escape(str_parse[:end])}(\r*\n|$)/
expand = true

if check(eos_regx)
scan(/[ \t]*#{Regexp.escape(str_parse[:end])}/)
Expand All @@ -233,7 +234,30 @@ def here_document(str_parse)
str_buffer << '#'
end

add_heredoc_content str_buffer, str_parse
until check(eos_regx) && scanner.bol?
handled = true

if scanner.eos?
raise "reached EOF while in heredoc"
end

if scan(/\n/)
str_buffer << scanner.matched
elsif expand && check(/#(?=[\$\@\{])/)
break
elsif scan(/\\/)
str_buffer << self.read_escape
else
handled = false
end

unless handled
reg = Regexp.new("[^#{Regexp.escape str_parse[:end]}\#\0\\\\\n]+|.")

scan reg
str_buffer << scanner.matched
end
end

complete_str = str_buffer.join ''
@line += complete_str.count("\n")
Expand Down Expand Up @@ -339,40 +363,6 @@ def parse_string
return :tSTRING_CONTENT
end

def add_heredoc_content(str_buffer, str_parse)
eos_regx = /[ \t]*#{Regexp.escape(str_parse[:end])}(\r*\n|$)/
expand = true

until scanner.eos?
c = nil
handled = true

if scan(/\n/)
c = scanner.matched
elsif check(eos_regx) && scanner.bol?
break # eos!
elsif expand && check(/#(?=[\$\@\{])/)
break
elsif scan(/\\/)
c = self.read_escape
else
handled = false
end

unless handled
reg = Regexp.new("[^#{Regexp.escape str_parse[:end]}\#\0\\\\\n]+|.")

scan reg
c = scanner.matched
end

c ||= scanner.matched
str_buffer << c
end

raise "reached EOF while in string" if scanner.eos?
end

def add_string_content(str_buffer, str_parse)
scanner = @scanner
# regexp for end of string/regexp
Expand Down

0 comments on commit 00e283f

Please sign in to comment.