Skip to content

Support slashy interpolated string #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions modules/compiler/src/main/antlr/ScriptLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ GStringBegin
TdqGStringBegin
: TdqStringQuotationMark -> pushMode(TDQ_GSTRING_MODE)
;
SlashyGStringBegin
: Slash { this.isRegexAllowed() && _input.LA(1) != '*' }? SlashyStringCharacter* Dollar { isFollowedByJavaLetterInGString(_input) }? -> pushMode(SLASHY_GSTRING_MODE)
;

mode DQ_GSTRING_MODE;
GStringEnd
Expand Down Expand Up @@ -174,6 +177,23 @@ TdqGStringExprStart
: '${' -> pushMode(DEFAULT_MODE)
;

mode SLASHY_GSTRING_MODE;
SlashyGStringEnd
: Dollar? Slash -> popMode
;

SlashyGStringPath
: Dollar IdentifierInGString (Dot IdentifierInGString)*
;

SlashyGStringText
: SlashyStringCharacter+
;

SlashyGStringExprStart
: '${' -> pushMode(DEFAULT_MODE)
;

mode DEFAULT_MODE;
// character in the double quotation string. e.g. "a"
fragment
Expand Down
7 changes: 7 additions & 0 deletions modules/compiler/src/main/antlr/ScriptParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ stringLiteral
gstring
: GStringBegin gstringDqPart* GStringEnd
| TdqGStringBegin gstringTdqPart* TdqGStringEnd
| SlashyGStringBegin gstringSlashyPart* SlashyGStringEnd
;

gstringDqPart
Expand All @@ -536,6 +537,12 @@ gstringTdqPart
| TdqGStringExprStart expression RBRACE #gstringTdqExprAlt
;

gstringSlashyPart
: SlashyGStringText #gstringSlashyTextAlt
| SlashyGStringPath #gstringSlashyPathAlt
| SlashyGStringExprStart expression RBRACE #gstringSlashyExprAlt
;

// -- constructor method call
creator
: createdName arguments
Expand Down
16 changes: 13 additions & 3 deletions modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1222,9 +1222,6 @@ private String stringLiteral(String text) {
text = StringUtils.trimQuotations(text, 3);
}
else if( text.startsWith(SQ_STR) || text.startsWith(DQ_STR) || startsWithSlash ) {
// the slashy string can span rows, so we have to remove CR for it
if( startsWithSlash )
text = StringUtils.removeCR(text);
text = StringUtils.trimQuotations(text, 1);
}

Expand Down Expand Up @@ -1264,6 +1261,17 @@ private Expression gstring(GstringContext ctx) {
values.add(expression(eac.expression()));
}

for( var part : ctx.gstringSlashyPart() ) {
if( part instanceof GstringSlashyTextAltContext tac )
strings.add(ast( gstringText(tac, beginQuotation), tac ));

if( part instanceof GstringSlashyPathAltContext pac )
values.add(ast( gstringPath(pac), pac ));

if( part instanceof GstringSlashyExprAltContext eac )
values.add(expression(eac.expression()));
}

var result = new GStringExpression(verbatimText, strings, values);
result.putNodeMetaData(QUOTE_CHAR, beginQuotation);
return result;
Expand All @@ -1274,6 +1282,8 @@ private String beginQuotation(String text) {
return TDQ_STR;
if( text.startsWith(DQ_STR) )
return DQ_STR;
if( text.startsWith(SLASH_STR) )
return SLASH_STR;

throw new IllegalStateException();
}
Expand Down