Skip to content

Commit 00e283f

Browse files
committed
Cleanup heredoc parsing some more
1 parent 709080a commit 00e283f

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

lib/opal/parser/lexer.rb

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def peek_variable_name
211211

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

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

236-
add_heredoc_content str_buffer, str_parse
237+
until check(eos_regx) && scanner.bol?
238+
handled = true
239+
240+
if scanner.eos?
241+
raise "reached EOF while in heredoc"
242+
end
243+
244+
if scan(/\n/)
245+
str_buffer << scanner.matched
246+
elsif expand && check(/#(?=[\$\@\{])/)
247+
break
248+
elsif scan(/\\/)
249+
str_buffer << self.read_escape
250+
else
251+
handled = false
252+
end
253+
254+
unless handled
255+
reg = Regexp.new("[^#{Regexp.escape str_parse[:end]}\#\0\\\\\n]+|.")
256+
257+
scan reg
258+
str_buffer << scanner.matched
259+
end
260+
end
237261

238262
complete_str = str_buffer.join ''
239263
@line += complete_str.count("\n")
@@ -339,40 +363,6 @@ def parse_string
339363
return :tSTRING_CONTENT
340364
end
341365

342-
def add_heredoc_content(str_buffer, str_parse)
343-
eos_regx = /[ \t]*#{Regexp.escape(str_parse[:end])}(\r*\n|$)/
344-
expand = true
345-
346-
until scanner.eos?
347-
c = nil
348-
handled = true
349-
350-
if scan(/\n/)
351-
c = scanner.matched
352-
elsif check(eos_regx) && scanner.bol?
353-
break # eos!
354-
elsif expand && check(/#(?=[\$\@\{])/)
355-
break
356-
elsif scan(/\\/)
357-
c = self.read_escape
358-
else
359-
handled = false
360-
end
361-
362-
unless handled
363-
reg = Regexp.new("[^#{Regexp.escape str_parse[:end]}\#\0\\\\\n]+|.")
364-
365-
scan reg
366-
c = scanner.matched
367-
end
368-
369-
c ||= scanner.matched
370-
str_buffer << c
371-
end
372-
373-
raise "reached EOF while in string" if scanner.eos?
374-
end
375-
376366
def add_string_content(str_buffer, str_parse)
377367
scanner = @scanner
378368
# regexp for end of string/regexp

0 commit comments

Comments
 (0)