Support full swift 5 string grammar

- arbitrary balanced delimiters
- raw strings ignore escapes
- multiline raw strings

Also rename lexer rules, 'dashed' is not adapted
This commit is contained in:
Clément Fournier
2021-01-18 14:54:33 +01:00
parent b18c73d9b9
commit 7e9f17658c
3 changed files with 24 additions and 16 deletions

View File

@ -1018,7 +1018,7 @@ ImplicitParameterName : '$' DecimalLiteral ; // TODO: don't allow '_' here
// GRAMMAR OF A LITERAL
booleanLiteral: BooleanLiteral ;
literal : numericLiteral | MultiStringLiteral | DashedSingleStringLiteral | SingleStringLiteral | BooleanLiteral | NilLiteral ;
literal : numericLiteral | MultiStringLiteral | SingleStringLiteral | BooleanLiteral | NilLiteral | RawMultiStringLiteral | RawSingleStringLiteral ;
// GRAMMAR OF AN INTEGER LITERAL
@ -1073,17 +1073,15 @@ TRIPLEDQUOTES : '"""' ;
MultiStringLiteral : TRIPLEDQUOTES '\n' .*? '\n' TRIPLEDQUOTES ;
fragment MultiQuotedText : MultiQuotedTextItem+ ;
fragment MultiQuotedTextItem : MultiInterpolatedString
| ~[\\\u000A\u000D]
;
fragment MultiQuotedTextItem : MultiInterpolatedString | ~[\\\u000A\u000D] ;
fragment MultiInterpolatedString: '\\(' (MultiQuotedTextItem | SingleStringLiteral)* ')';
DashedSingleStringLiteral : '#"' DashedMultiQuotedText? '"#' ;
fragment DashedMultiQuotedText : DashedMultiQuotedTextItem+ ;
fragment DashedMultiQuotedTextItem : EscapedCharacter | DashedMultiInterpolatedString
| ~[\\\u000A\u000D]
;
fragment DashedMultiInterpolatedString: '\\#(' (DashedMultiQuotedTextItem | DashedSingleStringLiteral)* ')';
// swift 5 extended delimiter, eg ##"abc"##
RawSingleStringLiteral : '#"' RawSingleQuotedTextItem* '"#' | '#' RawSingleStringLiteral '#';
fragment RawSingleQuotedTextItem : ~[\u000A\u000D] ;
RawMultiStringLiteral : '#"""' RawMultiQuotedTextItem* '"""#' | '#' RawMultiStringLiteral '#';
fragment RawMultiQuotedTextItem : . ;
// StringLiteral : '"' QuotedText? '"' ;
SingleStringLiteral : '"' QuotedText? '"' ;

View File

@ -43,6 +43,11 @@ print(x[keyPath: id]) // prints 3
let rawString = #"Press "Continue" to close this dialog."#
extension URL {
func html(withTitle title: String) -> String {
return #"<a href="\#(absoluteString)">\#(title)</a>"#
return ##"<a \href="\#(absoluteString)">\#(title)</a>"##
}
}
let rawMultiString = ###"a\###"###
let rawMultiString2 = ###"""a\###
""hey""
"""###

View File

@ -220,14 +220,19 @@ L45
[{] 50 50
L46
[return] 9 14
[#"<a href="\\#(absoluteString)">\\[ 16 61
[##"<a \\href="\\#(absoluteString)"[ 16 64
L47
[}] 5 5
L48
[}] 1 1
L49
L50
[let] 1 3
[regex] 5 9
[=] 11 11
[#"w+"#] 13 18
[rawMultiString] 5 18
[=] 20 20
[###"a\\###"###] 22 34
L51
[let] 1 3
[rawMultiString2] 5 19
[=] 21 21
[###"""a\\###\n""hey""\n"""###] 23 6
EOF