41 |
41 |
42 flags = re.DOTALL | re.MULTILINE |
42 flags = re.DOTALL | re.MULTILINE |
43 |
43 |
44 def heredoc_callback(self, match, ctx): |
44 def heredoc_callback(self, match, ctx): |
45 # okay, this is the hardest part of parsing Ruby... |
45 # okay, this is the hardest part of parsing Ruby... |
46 # match: 1 = <<-?, 2 = quote? 3 = name 4 = quote? 5 = rest of line |
46 # match: 1 = <<[-~]?, 2 = quote? 3 = name 4 = quote? 5 = rest of line |
47 |
47 |
48 start = match.start(1) |
48 start = match.start(1) |
49 yield start, Operator, match.group(1) # <<-? |
49 yield start, Operator, match.group(1) # <<[-~]? |
50 yield match.start(2), String.Heredoc, match.group(2) # quote ", ', ` |
50 yield match.start(2), String.Heredoc, match.group(2) # quote ", ', ` |
51 yield match.start(3), String.Delimiter, match.group(3) # heredoc name |
51 yield match.start(3), String.Delimiter, match.group(3) # heredoc name |
52 yield match.start(4), String.Heredoc, match.group(4) # quote again |
52 yield match.start(4), String.Heredoc, match.group(4) # quote again |
53 |
53 |
54 heredocstack = ctx.__dict__.setdefault('heredocstack', []) |
54 heredocstack = ctx.__dict__.setdefault('heredocstack', []) |
55 outermost = not bool(heredocstack) |
55 outermost = not bool(heredocstack) |
56 heredocstack.append((match.group(1) == '<<-', match.group(3))) |
56 heredocstack.append((match.group(1) in ('<<-', '<<~'), match.group(3))) |
57 |
57 |
58 ctx.pos = match.start(5) |
58 ctx.pos = match.start(5) |
59 ctx.end = match.end(5) |
59 ctx.end = match.end(5) |
60 # this may find other heredocs |
60 # this may find other heredocs |
61 for i, t, v in self.get_tokens_unprocessed(context=ctx): |
61 for i, t, v in self.get_tokens_unprocessed(context=ctx): |
245 'test', 'throw', 'to_a', 'to_s', 'trace_var', 'trap', 'untaint', |
245 'test', 'throw', 'to_a', 'to_s', 'trace_var', 'trap', 'untaint', |
246 'untrace_var', 'warn'), prefix=r'(?<!\.)', suffix=r'\b'), |
246 'untrace_var', 'warn'), prefix=r'(?<!\.)', suffix=r'\b'), |
247 Name.Builtin), |
247 Name.Builtin), |
248 (r'__(FILE|LINE)__\b', Name.Builtin.Pseudo), |
248 (r'__(FILE|LINE)__\b', Name.Builtin.Pseudo), |
249 # normal heredocs |
249 # normal heredocs |
250 (r'(?<!\w)(<<-?)(["`\']?)([a-zA-Z_]\w*)(\2)(.*?\n)', |
250 (r'(?<!\w)(<<[-~]?)(["`\']?)([a-zA-Z_]\w*)(\2)(.*?\n)', |
251 heredoc_callback), |
251 heredoc_callback), |
252 # empty string heredocs |
252 # empty string heredocs |
253 (r'(<<-?)("|\')()(\2)(.*?\n)', heredoc_callback), |
253 (r'(<<[-~]?)("|\')()(\2)(.*?\n)', heredoc_callback), |
254 (r'__END__', Comment.Preproc, 'end-part'), |
254 (r'__END__', Comment.Preproc, 'end-part'), |
255 # multiline regex (after keywords or assignments) |
255 # multiline regex (after keywords or assignments) |
256 (r'(?:^|(?<=[=<>~!:])|' |
256 (r'(?:^|(?<=[=<>~!:])|' |
257 r'(?<=(?:\s|;)when\s)|' |
257 r'(?<=(?:\s|;)when\s)|' |
258 r'(?<=(?:\s|;)or\s)|' |
258 r'(?<=(?:\s|;)or\s)|' |