use of org.apache.groovy.parser.antlr4.GroovyParser.GstringContext in project groovy by apache.
the class AstBuilder method visitGstring.
// } literal ---------------------------------------------------------------
// gstring { ---------------------------------------------------------------
@Override
public GStringExpression visitGstring(final GstringContext ctx) {
final List<ConstantExpression> stringLiteralList = new LinkedList<>();
final String begin = ctx.GStringBegin().getText();
final String beginQuotation = beginQuotation(begin);
stringLiteralList.add(configureAST(new ConstantExpression(parseGStringBegin(ctx, beginQuotation)), ctx.GStringBegin()));
List<ConstantExpression> partStrings = ctx.GStringPart().stream().map(e -> configureAST(new ConstantExpression(parseGStringPart(e, beginQuotation)), e)).collect(Collectors.toList());
stringLiteralList.addAll(partStrings);
stringLiteralList.add(configureAST(new ConstantExpression(parseGStringEnd(ctx, beginQuotation)), ctx.GStringEnd()));
List<Expression> values = ctx.gstringValue().stream().map(this::visitGstringValue).collect(Collectors.toList());
StringBuilder verbatimText = new StringBuilder(ctx.getText().length());
for (int i = 0, n = stringLiteralList.size(), s = values.size(); i < n; i += 1) {
verbatimText.append(stringLiteralList.get(i).getValue());
if (i == s) {
continue;
}
Expression value = values.get(i);
if (!asBoolean(value)) {
continue;
}
boolean isVariableExpression = value instanceof VariableExpression;
verbatimText.append(DOLLAR_STR);
if (!isVariableExpression)
verbatimText.append('{');
verbatimText.append(value.getText());
if (!isVariableExpression)
verbatimText.append('}');
}
return configureAST(new GStringExpression(verbatimText.toString(), stringLiteralList, values), ctx);
}
Aggregations