Search in sources :

Example 6 with JooSymbol

use of net.jangaroo.jooc.JooSymbol in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitCatch.

@Override
public void visitCatch(Catch aCatch) throws IOException {
    List<Catch> catches = aCatch.getParentTryStatement().getCatches();
    Catch firstCatch = catches.get(0);
    boolean isFirst = aCatch.equals(firstCatch);
    boolean isLast = aCatch.equals(catches.get(catches.size() - 1));
    TypeRelation typeRelation = aCatch.getParam().getOptTypeRelation();
    boolean hasCondition = aCatch.hasCondition();
    if (!hasCondition && !isLast) {
        throw Jooc.error(aCatch.getRParen(), "Only last catch clause may be untyped.");
    }
    final JooSymbol errorVar = firstCatch.getParam().getIde().getIde();
    final JooSymbol localErrorVar = aCatch.getParam().getIde().getIde();
    // in the following, always take care to write whitespace only once!
    out.writeSymbolWhitespace(aCatch.getSymKeyword());
    if (isFirst) {
        // "catch"
        out.writeSymbolToken(aCatch.getSymKeyword());
        // "(localErrorVar)":
        out.writeSymbol(aCatch.getLParen(), !hasCondition);
        out.writeSymbol(errorVar, !hasCondition);
        if (!hasCondition && typeRelation != null) {
            // can only be ": *", add as comment:
            typeRelation.visit(this);
        }
        out.writeSymbol(aCatch.getRParen(), !hasCondition);
        if (hasCondition || !isLast) {
            // a catch block always needs a brace, so generate one for conditions:
            out.writeToken("{");
        }
    } else {
        // transform catch(ide:Type){...} into else if is(e,Type)){var ide=e;...}
        out.writeToken("else");
    }
    if (hasCondition) {
        out.writeToken("if(is");
        out.writeSymbol(aCatch.getLParen());
        out.writeSymbolWhitespace(localErrorVar);
        out.writeSymbolToken(errorVar);
        out.writeSymbolWhitespace(typeRelation.getSymRelation());
        out.writeToken(",");
        Ide typeIde = typeRelation.getType().getIde();
        out.writeSymbolWhitespace(typeIde.getIde());
        out.writeToken(typeIde.getDeclaration().getQualifiedNameStr());
        out.writeSymbol(aCatch.getRParen());
        out.writeToken(")");
    }
    if (!localErrorVar.getText().equals(errorVar.getText())) {
        aCatch.getBlock().addBlockStartCodeGenerator(new VarCodeGenerator(localErrorVar, errorVar));
    }
    aCatch.getBlock().visit(this);
    if (isLast) {
        if (hasCondition) {
            out.writeToken("else throw");
            out.writeSymbolToken(errorVar);
            out.writeToken(";");
        }
        if (!(isFirst && !hasCondition)) {
            // last catch clause closes the JS catch block:
            out.writeToken("}");
        }
    }
}
Also used : TypeRelation(net.jangaroo.jooc.ast.TypeRelation) Catch(net.jangaroo.jooc.ast.Catch) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) JooSymbol(net.jangaroo.jooc.JooSymbol)

Example 7 with JooSymbol

use of net.jangaroo.jooc.JooSymbol in project jangaroo-tools by CoreMedia.

the class AnnotationParameter method analyze.

public void analyze(AstNode parentNode) {
    super.analyze(parentNode);
    if (getValue() != null) {
        getValue().analyze(this);
        String metaName = parentAnnotation.getMetaName();
        if ("Embed".equals(metaName) && getOptName() != null && "source".equals(getOptName().getName())) {
            JooSymbol valueSymbol = getValue().getSymbol();
            if (valueSymbol.sym != sym.STRING_LITERAL) {
                throw new CompilerError(valueSymbol, "The source parameter of an [Embed] annotation must be a string literal");
            }
            String text = valueSymbol.getText();
            String quote = text.substring(0, 1);
            String source = (String) valueSymbol.getJooValue();
            String absoluteSource = compilationUnit.addResourceDependency(source);
            getValue().setValue(new JooSymbol(valueSymbol.sym, valueSymbol.getFileName(), valueSymbol.getLine(), valueSymbol.getColumn(), valueSymbol.getWhitespace(), quote + absoluteSource + quote, absoluteSource));
        }
    }
}
Also used : CompilerError(net.jangaroo.jooc.CompilerError) JooSymbol(net.jangaroo.jooc.JooSymbol)

Aggregations

JooSymbol (net.jangaroo.jooc.JooSymbol)7 File (java.io.File)1 CompilerError (net.jangaroo.jooc.CompilerError)1 Catch (net.jangaroo.jooc.ast.Catch)1 Ide (net.jangaroo.jooc.ast.Ide)1 IdeDeclaration (net.jangaroo.jooc.ast.IdeDeclaration)1 IdeExpr (net.jangaroo.jooc.ast.IdeExpr)1 NamespacedIde (net.jangaroo.jooc.ast.NamespacedIde)1 QualifiedIde (net.jangaroo.jooc.ast.QualifiedIde)1 TypeRelation (net.jangaroo.jooc.ast.TypeRelation)1 TypedIdeDeclaration (net.jangaroo.jooc.ast.TypedIdeDeclaration)1