use of net.jangaroo.jooc.ast.Ide in project jangaroo-tools by CoreMedia.
the class DeclarationScope method declareIde.
@Override
public IdeDeclaration declareIde(IdeDeclaration decl) {
final Ide ide = decl.getIde();
final String name = ide.getName();
if (importsByName.containsKey(name)) {
throw new CompilerError(ide.getSymbol(), "attempt to redefine an imported identifier " + name);
}
if (AUX_VAR_NAME_PATTERN.matcher(name).matches()) {
DeclarationScope packageDeclarationScope = getPackageDeclarationScope();
if (packageDeclarationScope != null && packageDeclarationScope != this) {
// also declare local auxiliary vars in package scope to reserve them so they are not used for package names:
new VariableDeclaration(null, new Ide(name), null).scope(packageDeclarationScope);
}
}
return ides.put(name, decl);
}
use of net.jangaroo.jooc.ast.Ide in project jangaroo-tools by CoreMedia.
the class JsCodeGenerator method generateSuperConstructorCallCode.
private void generateSuperConstructorCallCode(ClassDeclaration classDeclaration, ParenthesizedExpr<CommaSeparatedList<Expr>> args) throws IOException {
String superClassQName = classDeclaration.getSuperTypeDeclaration().getQualifiedNameStr();
if ("Error".equals(superClassQName)) {
// built-in Error constructor called as function unfortunately always creates a new Error object, so we have to use emulation provided by Jangaroo Runtime:
out.write("joo.Error");
} else {
Ide superClassIde = classDeclaration.getSuperType().getIde();
out.writeSymbolWhitespace(superClassIde.getSymbol());
IdeDeclaration superClassDeclaration = superClassIde.getDeclaration();
String packageName = superClassDeclaration.getPackageDeclaration().getQualifiedNameStr();
String qName = superClassDeclaration.getName();
if (packageName.length() > 0) {
String packageAuxVar = compilationUnit.getAuxVarForPackage(packageName);
qName = CompilerUtils.qName(packageAuxVar, qName);
}
out.write(qName);
}
out.writeToken(".call");
if (args == null) {
out.writeToken("(this)");
} else {
out.writeSymbol(args.getLParen());
out.writeToken("this");
CommaSeparatedList<Expr> arguments = args.getExpr();
if (arguments != null) {
if (arguments.getHead() != null) {
out.writeToken(",");
}
arguments.visit(this);
}
out.writeSymbol(args.getRParen());
}
}
use of net.jangaroo.jooc.ast.Ide 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("}");
}
}
}
use of net.jangaroo.jooc.ast.Ide in project jangaroo-tools by CoreMedia.
the class JsCodeGenerator method visitForInStatement.
@Override
public void visitForInStatement(final ForInStatement forInStatement) throws IOException {
final Ide exprAuxIde = forInStatement.getExprAuxIde();
IdeDeclaration exprType = forInStatement.getExpr().getType();
String exprTypeName = exprType != null ? exprType.getQualifiedNameStr() : "";
boolean iterateArrayMode = "Array".equals(exprTypeName) || "Vector$object".equals(exprTypeName);
if (exprAuxIde != null && !iterateArrayMode) {
new SemicolonTerminatedStatement(new VariableDeclaration(SYM_VAR, exprAuxIde, null, null), SYM_SEMICOLON).visit(this);
}
out.writeSymbol(forInStatement.getSymKeyword());
final boolean isForEach = forInStatement.getSymEach() != null;
if (isForEach) {
out.beginComment();
out.writeSymbol(forInStatement.getSymEach());
out.endComment();
}
out.writeSymbol(forInStatement.getLParen());
if (isForEach || iterateArrayMode) {
new VariableDeclaration(SYM_VAR, forInStatement.getAuxIde(), null, null).visit(this);
} else {
if (forInStatement.getDecl() != null) {
forInStatement.getDecl().visit(this);
} else {
forInStatement.getLValue().visit(this);
}
}
if (iterateArrayMode) {
String indexVarName = forInStatement.getAuxIde().getName();
out.write("=0");
if (exprAuxIde != null) {
out.write(",");
out.writeToken(exprAuxIde.getName());
out.writeToken("=");
out.beginComment();
out.writeSymbol(forInStatement.getSymIn());
out.endComment();
forInStatement.getExpr().visit(this);
}
out.write(";");
out.write(indexVarName);
out.write("<");
if (exprAuxIde != null) {
out.writeToken(exprAuxIde.getName());
} else {
out.beginComment();
out.writeSymbol(forInStatement.getSymIn());
out.endComment();
forInStatement.getExpr().visit(this);
}
out.write(".length;");
out.write("++" + indexVarName);
} else {
out.writeSymbol(forInStatement.getSymIn());
if (exprAuxIde != null) {
// assign the expression value to the auxiliary expression value variable once:
out.writeToken(exprAuxIde.getName());
out.writeToken("=");
}
forInStatement.getExpr().visit(this);
}
out.writeSymbol(forInStatement.getRParen());
if (isForEach || iterateArrayMode) {
// inject synthesized statement into loop body:
if (!(forInStatement.getBody() instanceof BlockStatement)) {
forInStatement.setBody(new BlockStatement(SYM_LBRACE, Arrays.<Directive>asList(forInStatement.getBody()), SYM_RBRACE));
}
((BlockStatement) forInStatement.getBody()).addBlockStartCodeGenerator(new CodeGenerator() {
@Override
public void generate(JsWriter out, boolean first) throws IOException {
// synthesize assigning the correct index to the variable given in the original for each statement:
if (forInStatement.getDecl() != null) {
forInStatement.getDecl().visit(JsCodeGenerator.this);
} else {
forInStatement.getLValue().visit(JsCodeGenerator.this);
}
out.writeToken("=");
if (!isForEach) {
out.write("String(" + forInStatement.getAuxIde().getName() + ")");
} else {
if (exprAuxIde == null) {
forInStatement.getExpr().visit(JsCodeGenerator.this);
} else {
out.write(exprAuxIde.getName());
}
out.write("[" + forInStatement.getAuxIde().getName() + "]");
}
out.write(";");
}
});
}
forInStatement.getBody().visit(this);
}
use of net.jangaroo.jooc.ast.Ide in project jangaroo-tools by CoreMedia.
the class JangarooParser method declareValues.
protected static void declareValues(Scope scope, String[] identifiers) {
for (String identifier : identifiers) {
Ide ide = new Ide(new JooSymbol(identifier));
IdeDeclaration decl = new VariableDeclaration(new JooSymbol("var"), ide, null, null);
decl.scope(scope);
}
}
Aggregations