1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-21 21:17:50 +02:00

Raku: Fix unterminated heredoc fixes

- Fix heredoc
- Move testdata to raku directory
- Add testdata for unterminated heredoc
This commit is contained in:
Siavash Askari Nasr 2021-09-18 23:14:41 +04:30 committed by Alec Thomas
parent 82795e1420
commit 4d7154e8c7
5 changed files with 53 additions and 8 deletions

@ -513,14 +513,24 @@ func rakuRules() Rules {
adverbre := regexp.MustCompile(`:to\b|:heredoc\b`) adverbre := regexp.MustCompile(`:to\b|:heredoc\b`)
var heredocTerminator []rune var heredocTerminator []rune
var endHeredocPos int
if adverbre.MatchString(string(adverbs)) { if adverbre.MatchString(string(adverbs)) {
heredocTerminator = text[state.Pos:endPos] if endPos != len(text) {
if len(heredocTerminator) > 0 { heredocTerminator = text[state.Pos:endPos]
endHeredocPos := indexAt(text[endPos:], heredocTerminator, 0)
nChars = len(heredocTerminator) nChars = len(heredocTerminator)
endPos += endHeredocPos
} else { } else {
endPos = len(text) endPos = state.Pos + 1
heredocTerminator = []rune{}
nChars = 0
}
if nChars > 0 {
endHeredocPos = indexAt(text[endPos:], heredocTerminator, 0)
if endHeredocPos > -1 {
endPos += endHeredocPos
} else {
endPos = len(text)
}
} }
} }
@ -532,7 +542,7 @@ func rakuRules() Rules {
case rakuQuote: case rakuQuote:
if len(heredocTerminator) > 0 { if len(heredocTerminator) > 0 {
// Length of heredoc terminator + closing chars + `;` // Length of heredoc terminator + closing chars + `;`
heredocFristPunctuationLen := len(heredocTerminator) + len(openingChars) + 1 heredocFristPunctuationLen := nChars + len(openingChars) + 1
state.NamedGroups[`opening_delimiters`] = string(openingChars) + state.NamedGroups[`opening_delimiters`] = string(openingChars) +
string(text[state.Pos:state.Pos+heredocFristPunctuationLen]) string(text[state.Pos:state.Pos+heredocFristPunctuationLen])
@ -540,10 +550,14 @@ func rakuRules() Rules {
state.NamedGroups[`value`] = state.NamedGroups[`value`] =
string(text[state.Pos+heredocFristPunctuationLen : endPos]) string(text[state.Pos+heredocFristPunctuationLen : endPos])
state.NamedGroups[`closing_delimiters`] = string(heredocTerminator) if endHeredocPos > -1 {
state.NamedGroups[`closing_delimiters`] = string(heredocTerminator)
}
} else { } else {
state.NamedGroups[`value`] = textBetweenBrackets state.NamedGroups[`value`] = textBetweenBrackets
state.NamedGroups[`closing_delimiters`] = string(closingChars) if nChars > 0 {
state.NamedGroups[`closing_delimiters`] = string(closingChars)
}
} }
default: default:
state.Groups = []string{state.Groups[0] + string(text[state.Pos:endPos+nChars])} state.Groups = []string{state.Groups[0] + string(text[state.Pos:endPos+nChars])}

@ -0,0 +1,8 @@
say q:to//;
say 'something';
# Unterminated heredoc
say q:to/Foo/;
say 'something';

@ -0,0 +1,23 @@
[
{"type":"NameBuiltin","value":"say"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"q"},
{"type":"LiteralStringAffix","value":":to"},
{"type":"Punctuation","value":"//"},
{"type":"LiteralString","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"NameBuiltin","value":"say"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"'"},
{"type":"LiteralStringSingle","value":"something"},
{"type":"Punctuation","value":"';"},
{"type":"Text","value":"\n\n"},
{"type":"CommentSingle","value":"# Unterminated heredoc"},
{"type":"Text","value":"\n"},
{"type":"NameBuiltin","value":"say"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"q"},
{"type":"LiteralStringAffix","value":":to"},
{"type":"Punctuation","value":"/Foo/;"},
{"type":"LiteralString","value":"\n\nsay 'something';\n"}
]