Skip to content

Commit 4d7154e

Browse files
CIAvashalecthomas
authored andcommitted
Raku: Fix unterminated heredoc fixes #547
- Fix heredoc - Move testdata to raku directory - Add testdata for unterminated heredoc
1 parent 82795e1 commit 4d7154e

File tree

5 files changed

+53
-8
lines changed

5 files changed

+53
-8
lines changed

lexers/r/raku.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,24 @@ func rakuRules() Rules {
513513

514514
adverbre := regexp.MustCompile(`:to\b|:heredoc\b`)
515515
var heredocTerminator []rune
516+
var endHeredocPos int
516517
if adverbre.MatchString(string(adverbs)) {
517-
heredocTerminator = text[state.Pos:endPos]
518-
if len(heredocTerminator) > 0 {
519-
endHeredocPos := indexAt(text[endPos:], heredocTerminator, 0)
518+
if endPos != len(text) {
519+
heredocTerminator = text[state.Pos:endPos]
520520
nChars = len(heredocTerminator)
521-
endPos += endHeredocPos
522521
} else {
523-
endPos = len(text)
522+
endPos = state.Pos + 1
523+
heredocTerminator = []rune{}
524+
nChars = 0
525+
}
526+
527+
if nChars > 0 {
528+
endHeredocPos = indexAt(text[endPos:], heredocTerminator, 0)
529+
if endHeredocPos > -1 {
530+
endPos += endHeredocPos
531+
} else {
532+
endPos = len(text)
533+
}
524534
}
525535
}
526536

@@ -532,18 +542,22 @@ func rakuRules() Rules {
532542
case rakuQuote:
533543
if len(heredocTerminator) > 0 {
534544
// Length of heredoc terminator + closing chars + `;`
535-
heredocFristPunctuationLen := len(heredocTerminator) + len(openingChars) + 1
545+
heredocFristPunctuationLen := nChars + len(openingChars) + 1
536546

537547
state.NamedGroups[`opening_delimiters`] = string(openingChars) +
538548
string(text[state.Pos:state.Pos+heredocFristPunctuationLen])
539549

540550
state.NamedGroups[`value`] =
541551
string(text[state.Pos+heredocFristPunctuationLen : endPos])
542552

543-
state.NamedGroups[`closing_delimiters`] = string(heredocTerminator)
553+
if endHeredocPos > -1 {
554+
state.NamedGroups[`closing_delimiters`] = string(heredocTerminator)
555+
}
544556
} else {
545557
state.NamedGroups[`value`] = textBetweenBrackets
546-
state.NamedGroups[`closing_delimiters`] = string(closingChars)
558+
if nChars > 0 {
559+
state.NamedGroups[`closing_delimiters`] = string(closingChars)
560+
}
547561
}
548562
default:
549563
state.Groups = []string{state.Groups[0] + string(text[state.Pos:endPos+nChars])}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
say q:to//;
2+
3+
say 'something';
4+
5+
# Unterminated heredoc
6+
say q:to/Foo/;
7+
8+
say 'something';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{"type":"NameBuiltin","value":"say"},
3+
{"type":"Text","value":" "},
4+
{"type":"Keyword","value":"q"},
5+
{"type":"LiteralStringAffix","value":":to"},
6+
{"type":"Punctuation","value":"//"},
7+
{"type":"LiteralString","value":";"},
8+
{"type":"Text","value":"\n\n"},
9+
{"type":"NameBuiltin","value":"say"},
10+
{"type":"Text","value":" "},
11+
{"type":"Punctuation","value":"'"},
12+
{"type":"LiteralStringSingle","value":"something"},
13+
{"type":"Punctuation","value":"';"},
14+
{"type":"Text","value":"\n\n"},
15+
{"type":"CommentSingle","value":"# Unterminated heredoc"},
16+
{"type":"Text","value":"\n"},
17+
{"type":"NameBuiltin","value":"say"},
18+
{"type":"Text","value":" "},
19+
{"type":"Keyword","value":"q"},
20+
{"type":"LiteralStringAffix","value":":to"},
21+
{"type":"Punctuation","value":"/Foo/;"},
22+
{"type":"LiteralString","value":"\n\nsay 'something';\n"}
23+
]

0 commit comments

Comments
 (0)