Improvements for Dart interpolated strings

The Dart tokenizer was incomplete, resulting in some strings that use
string interpolation not being parsed correctly. This has been worked
around by making sure that the contents of string interpolation are all
included in the same string token.
This commit is contained in:
Maikel Steneker
2020-08-27 13:16:56 +02:00
parent 8c3277b8a2
commit 74a9950128
6 changed files with 41 additions and 16 deletions

View File

@ -351,16 +351,16 @@ SingleLineString
fragment
StringContentDQ
: ~('\\' | '"' /*| '$'*/ | '\n' | '\r')
: ~('\\' | '"' | '$' | '\n' | '\r')
| '\\' ~('\n' | '\r')
//| stringInterpolation
| StringInterpolation
;
fragment
StringContentSQ
: ~('\\' | '\'' /*| '$'*/ | '\n' | '\r')
: ~('\\' | '\'' | '$' | '\n' | '\r')
| '\\' ~('\n' | '\r')
//| stringInterpolation
| StringInterpolation
;
MultiLineString
@ -372,15 +372,16 @@ MultiLineString
fragment
StringContentTDQ
: ~('\\' | '"' /*| '$'*/)
: ~('\\' | '"' | '$')
| '"' ~'"' | '""' ~'"'
//| stringInterpolation
| StringInterpolation
;
fragment StringContentTSQ
: ~('\\' | '\'' /*| '$'*/)
fragment
StringContentTSQ
: ~('\\' | '\'' | '$')
| '\'' ~'\'' | '\'\'' ~'\''
//| stringInterpolation
| StringInterpolation
;
NEWLINE
@ -390,10 +391,17 @@ NEWLINE
;
// 16.5.1 String Interpolation
stringInterpolation
// : '$' IDENTIFIER_NO_DOLLAR
: '$' identifier// FIXME
| '${' expression '}'
fragment
StringInterpolation
: '$' IDENTIFIER_NO_DOLLAR
| '${' StringInterpolationContent* '}'
;
fragment
StringInterpolationContent
: ~('$' | '{' | '}')
| '$' IDENTIFIER_NO_DOLLAR
| '${' StringInterpolationContent* '}'
;
// 16.6 Symbols

View File

@ -54,7 +54,10 @@ public class DartTokenizerTest extends CpdTextComparisonTest {
doTest("imports");
}
@Test
public void testStringInterpolation() {
doTest("string_interpolation");
}
@Test
public void testRegex() {

View File

@ -1,3 +1,3 @@
var newline = '\n';
var dollar = '$';
var dollar = '$newLine';
var escaped_dollar = "\$";

View File

@ -8,7 +8,7 @@ L2
[var] 1 3
[dollar] 5 10
[=] 12 12
['$'] 14 16
['$newLine'] 14 23
L3
[var] 1 3
[escaped_dollar] 5 18

View File

@ -0,0 +1,2 @@
var stringInStringUnicode = "${""}";
var stringInStringNewline = "${"\n")}";

View File

@ -0,0 +1,12 @@
[Image] or [Truncated image[ Bcol Ecol
L1
[var] 1 3
[stringInStringUnicode] 5 25
[=] 27 27
["${"∆"}"] 29 36
L2
[var] 1 3
[stringInStringNewline] 5 25
[=] 27 27
["${"\\n")}"] 29 38
EOF