Skip to content

Commit e7467a0

Browse files
committedNov 17, 2013
Start cleaning up string parsing
1 parent cff7980 commit e7467a0

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed
 

‎lib/opal/parser/lexer.rb

+40-39
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def next_string_token
147147
end
148148
end
149149

150-
# see if we can read end of string/xstring/regecp markers
150+
# see if we can read end of string/xstring/regexp markers
151151
# if scan /#{str_parse[:end]}/
152152
if scan Regexp.new(Regexp.escape(str_parse[:end]))
153153
if words && !str_parse[:done_last_space]#&& space
@@ -161,7 +161,7 @@ def next_string_token
161161
if str_parse[:nesting] == 0
162162
@lex_state = :expr_end
163163

164-
if str_parse[:regexp]
164+
if str_parse[:type] == :regexp
165165
result = scan(/\w+/)
166166
return :tREGEXP_END, result
167167
end
@@ -180,7 +180,7 @@ def next_string_token
180180
@lex_state = :expr_end
181181
return :tSTRING_END, scanner.matched
182182

183-
elsif str_parse[:beg] == '/' || str_parse[:regexp]
183+
elsif str_parse[:beg] == '/' || str_parse[:type] == :regexp
184184
result = scan(/\w+/)
185185
@lex_state = :expr_end
186186
return :tREGEXP_END, result
@@ -250,7 +250,7 @@ def add_heredoc_content(str_buffer, str_parse)
250250
elsif expand && check(/#(?=[\$\@\{])/)
251251
break
252252
elsif scan(/\\/)
253-
if str_parse[:regexp]
253+
if str_parse[:type] == :regexp
254254
if scan(/(.)/)
255255
c = "\\" + scanner.matched
256256
end
@@ -329,7 +329,7 @@ def add_string_content(str_buffer, str_parse)
329329
#c = scanner.matched
330330

331331
elsif scan(/\\/)
332-
if str_parse[:regexp]
332+
if str_parse[:type] == :regexp
333333
if scan(/(.)/)
334334
c = "\\" + scanner.matched
335335
end
@@ -696,42 +696,43 @@ def yylex
696696
@lex_state = after_operator?() ? :expr_arg : :expr_beg
697697
return :tPIPE, '|'
698698

699-
elsif scan(/\%W/)
700-
start_word = scan(/./)
701-
end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word
702-
self.strterm = { :type => :dword, :beg => 'W', :end => end_word }
703-
scan(/\s*/)
704-
return :tWORDS_BEG, scanner.matched
705-
706-
elsif scan(/\%w/) or scan(/\%i/)
707-
start_word = scan(/./)
708-
end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word
709-
self.strterm = { :type => :sword, :beg => 'w', :end => end_word }
710-
scan(/\s*/)
711-
return :tAWORDS_BEG, scanner.matched
712-
713-
elsif scan(/\%[Qq]/)
714-
type = scanner.matched.end_with?('Q') ? :dquote : :squote
715-
start_word = scan(/./)
716-
end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word
717-
self.strterm = { :type => type, :beg => start_word, :end => end_word, :balance => true, :nesting => 0 }
718-
return :tSTRING_BEG, scanner.matched
719-
720-
elsif scan(/\%x/)
721-
start_word = scan(/./)
722-
end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word
723-
self.strterm = { :type => :xquote, :beg => start_word, :end => end_word, :balance => true, :nesting => 0 }
724-
return :tXSTRING_BEG, scanner.matched
725-
726-
elsif scan(/\%r/)
727-
start_word = scan(/./)
728-
end_word = { '(' => ')', '[' => ']', '{' => '}' }[start_word] || start_word
729-
self.strterm = { :type => :regexp, :beg => start_word, :end => end_word, :regexp => true, :balance => true, :nesting => 0 }
730-
return :tREGEXP_BEG, scanner.matched
699+
elsif scan(/\%[QqWwxr]/)
700+
str_type = scanner.matched[1]
701+
paren = scan(/./)
702+
703+
term = case paren
704+
when '(' then ')'
705+
when '[' then ']'
706+
when '{' then '}'
707+
else paren
708+
end
709+
710+
case str_type
711+
when 'Q'
712+
self.strterm = { :type => :dquote, :beg => paren, :end => term, :balance => true, :nesting => 0 }
713+
return :tSTRING_BEG, scanner.matched
714+
when 'q'
715+
self.strterm = { :type => :squote, :beg => paren, :end => term, :balance => true, :nesting => 0 }
716+
return :tSTRING_BEG, scanner.matched
717+
when 'W'
718+
self.strterm = { :type => :dword, :beg => 'W', :end => term }
719+
scan(/\s*/)
720+
return :tWORDS_BEG, scanner.matched
721+
when 'w', 'i'
722+
self.strterm = { :type => :sword, :beg => 'W', :end => term }
723+
scan(/\s*/)
724+
return :tAWORDS_BEG, scanner.matched
725+
when 'x'
726+
self.strterm = { :type => :xquote, :beg => paren, :end => term, :balance => true, :nesting => 0 }
727+
return :tXSTRING_BEG, scanner.matched
728+
when 'r'
729+
self.strterm = { :type => :regexp, :beg => paren, :end => term, :balance => true, :nesting => 0 }
730+
return :tREGEXP_BEG, scanner.matched
731+
end
731732

732733
elsif scan(/\//)
733734
if [:expr_beg, :expr_mid].include? @lex_state
734-
self.strterm = { :type => :regexp, :beg => '/', :end => '/', :regexp => true }
735+
self.strterm = { :type => :regexp, :beg => '/', :end => '/' }
735736
return :tREGEXP_BEG, scanner.matched
736737
elsif scan(/\=/)
737738
@lex_state = :expr_beg
@@ -740,7 +741,7 @@ def yylex
740741
@lex_state = :expr_arg
741742
elsif @lex_state == :expr_cmdarg || @lex_state == :expr_arg
742743
if !check(/\s/) && @space_seen
743-
self.strterm = { :type => :regexp, :beg => '/', :end => '/', :regexp => true }
744+
self.strterm = { :type => :regexp, :beg => '/', :end => '/' }
744745
return :tREGEXP_BEG, scanner.matched
745746
end
746747
else

0 commit comments

Comments
 (0)
Please sign in to comment.