Search in sources :

Example 1 with StringValue

use of dyvilx.tools.compiler.ast.expression.constant.StringValue in project Dyvil by Dyvil.

the class ProcessedText method resolve.

@Override
public IValue resolve(MarkerList markers, IContext context) {
    final int startLine = this.position.startLine();
    final int startColumn = this.position.startColumn();
    final StringInterpolationExpr parts = new StringInterpolationExpr();
    final String text = this.text;
    final int length = text.length();
    int prev = 0;
    for (int startIndex = 0; startIndex < length; ) {
        final int c = text.codePointAt(startIndex);
        // advance to an identifier character
        if (!Character.isJavaIdentifierStart(c)) {
            startIndex += Character.charCount(c);
            continue;
        }
        final int endIndex = identifierEnd(text, startIndex + 1, length);
        final String key = text.substring(startIndex, endIndex);
        final IDataMember field = context.resolveField(Name.fromRaw(key));
        if (field != null && (field.isLocal() || field.hasModifier(Modifiers.PUBLIC))) {
            // append contents before this identifier
            parts.append(new StringValue(text.substring(prev, startIndex)));
            final SourcePosition position = SourcePosition.apply(startLine, startColumn + startIndex, startColumn + endIndex);
            parts.append(new FieldAccess(position, null, field));
            // advance to the end of the identifier
            prev = endIndex;
            startIndex = endIndex;
            continue;
        }
        startIndex += Character.charCount(c);
    }
    if (prev != length) {
        parts.append(new StringValue(text.substring(prev, length)));
    }
    return new WriteCall(parts.resolve(markers, context));
}
Also used : SourcePosition(dyvil.source.position.SourcePosition) StringInterpolationExpr(dyvilx.tools.compiler.ast.expression.StringInterpolationExpr) StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue) FieldAccess(dyvilx.tools.compiler.ast.expression.access.FieldAccess) IDataMember(dyvilx.tools.compiler.ast.field.IDataMember)

Example 2 with StringValue

use of dyvilx.tools.compiler.ast.expression.constant.StringValue in project Dyvil by Dyvil.

the class StingInterpolationParser method parse.

@Override
public void parse(IParserManager pm, IToken token) {
    final int type = token.type();
    switch(type) {
        case Tokens.STRING_START:
        case Tokens.STRING_PART:
            {
                int nextType = token.next().type();
                if (nextType == Tokens.STRING_PART || nextType == Tokens.STRING_END) {
                    pm.report(token.next(), "stringinterpolation.expression");
                    return;
                }
                this.value.append(new StringValue(token.raw(), token.stringValue()));
                pm.pushParser(new ExpressionParser(this));
                return;
            }
        case Tokens.STRING_END:
            this.value.append(new StringValue(token.raw(), token.stringValue()));
            pm.popParser();
            return;
    }
    pm.reparse();
    pm.report(token, "stringinterpolation.part");
}
Also used : StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue)

Example 3 with StringValue

use of dyvilx.tools.compiler.ast.expression.constant.StringValue in project Dyvil by Dyvil.

the class Template method resolveTypes.

@Override
public void resolveTypes() {
    this.makeGenerateSpecMethod();
    this.makeMainMethod();
    // automatically infer package declaration
    this.packageDeclaration = new PackageDeclaration(null, this.getPackage().getFullName());
    this.templateClass.setSuperType(LazyTypes.Template);
    this.templateClass.setSuperConstructorArguments(new ArgumentList(new StringValue(this.getTemplateName())));
    super.resolveTypes();
}
Also used : ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue) PackageDeclaration(dyvilx.tools.compiler.ast.header.PackageDeclaration)

Example 4 with StringValue

use of dyvilx.tools.compiler.ast.expression.constant.StringValue in project Dyvil by Dyvil.

the class StringInterpolationExpr method moveString.

private static void moveString(StringBuilder builder, ArgumentList newValues) {
    if (builder.length() > 0) {
        newValues.add(new StringValue(builder.toString()));
        builder.delete(0, builder.length());
    }
}
Also used : StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue)

Example 5 with StringValue

use of dyvilx.tools.compiler.ast.expression.constant.StringValue in project Dyvil by Dyvil.

the class StringInterpolationExpr method foldConstants.

@Override
public IValue foldConstants() {
    this.values.foldConstants();
    // Merges adjacent String constants into one
    final int size = this.values.size();
    final ArgumentList newValues = new ArgumentList(size);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < size; i++) {
        final IValue value = this.values.get(i);
        if (value.toStringBuilder(builder)) {
            continue;
        }
        moveString(builder, newValues);
        newValues.add(value);
    }
    moveString(builder, newValues);
    if (newValues.isEmpty()) {
        return new StringValue("");
    }
    // assert newValues.size() >= 1
    final IValue first = newValues.getFirst();
    if (newValues.size() == 1 && isString(first)) {
        return first;
    }
    this.values = newValues;
    return this;
}
Also used : ArgumentList(dyvilx.tools.compiler.ast.parameter.ArgumentList) StringValue(dyvilx.tools.compiler.ast.expression.constant.StringValue)

Aggregations

StringValue (dyvilx.tools.compiler.ast.expression.constant.StringValue)6 ArgumentList (dyvilx.tools.compiler.ast.parameter.ArgumentList)3 SourcePosition (dyvil.source.position.SourcePosition)1 StringInterpolationExpr (dyvilx.tools.compiler.ast.expression.StringInterpolationExpr)1 ConstructorCall (dyvilx.tools.compiler.ast.expression.access.ConstructorCall)1 FieldAccess (dyvilx.tools.compiler.ast.expression.access.FieldAccess)1 IntValue (dyvilx.tools.compiler.ast.expression.constant.IntValue)1 IDataMember (dyvilx.tools.compiler.ast.field.IDataMember)1 PackageDeclaration (dyvilx.tools.compiler.ast.header.PackageDeclaration)1