You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by er...@apache.org on 2013/01/17 21:37:38 UTC
svn commit: r1434902 - in
/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler: as/codegen/
internal/as/codegen/ internal/js/codegen/ internal/js/codegen/amd/
internal/js/codegen/goog/
Author: erikdebruin
Date: Thu Jan 17 20:37:37 2013
New Revision: 1434902
URL: http://svn.apache.org/viewvc?rev=1434902&view=rev
Log:
- From Wiki (ToDo): "Get all Strings in the emitters to use constants either the keyword enums or another constant class IE for ", brackets, parenthesis etc."... Done!
Modified:
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/as/codegen/IASEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASAfterNodeStrategy.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASBeforeNodeStrategy.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSDocEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java
flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogEmitter.java
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/as/codegen/IASEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/as/codegen/IASEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/as/codegen/IASEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/as/codegen/IASEmitter.java Thu Jan 17 20:37:37 2013
@@ -93,16 +93,18 @@ public interface IASEmitter
void indentPush();
/**
- * Writes the block closing character(s)
- */
- void writeBlockClose();
-
- /**
* Pops an indent from the emitter so after newlines are emitted, the output
* is correctly formatted.
*/
void indentPop();
+ /**
+ * Writes newline character(s)
+ */
+ void writeNewline();
+
+
+
void emitPackageHeader(IPackageNode node);
void emitPackageHeaderContents(IPackageNode node);
@@ -111,6 +113,8 @@ public interface IASEmitter
void emitPackageFooter(IPackageNode node);
+
+
/**
* Emit a Class.
*
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASAfterNodeStrategy.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASAfterNodeStrategy.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASAfterNodeStrategy.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASAfterNodeStrategy.java Thu Jan 17 20:37:37 2013
@@ -58,11 +58,11 @@ public class ASAfterNodeStrategy impleme
if (node.getChildCount() != 0)
{
emitter.indentPop();
- emitter.write("\n");
+ emitter.writeNewline();
}
// (erikdebruin) moved this to utility method to allow overriding
- emitter.writeBlockClose();
+ emitter.write(ASEmitter.CURLYBRACE_CLOSE);
}
else if (type == ContainerType.IMPLICIT
|| type == ContainerType.SYNTHESIZED)
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASBeforeNodeStrategy.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASBeforeNodeStrategy.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASBeforeNodeStrategy.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASBeforeNodeStrategy.java Thu Jan 17 20:37:37 2013
@@ -60,12 +60,12 @@ public class ASBeforeNodeStrategy implem
if (type != ContainerType.IMPLICIT
&& type != ContainerType.SYNTHESIZED)
{
- emitter.write("{");
+ emitter.write(ASEmitter.CURLYBRACE_OPEN);
}
if (parent.getNodeID() != ASTNodeID.LabledStatementID)
{
- emitter.write("\n");
+ emitter.writeNewline();
}
}
}
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/as/codegen/ASEmitter.java Thu Jan 17 20:37:37 2013
@@ -29,6 +29,7 @@ import org.apache.flex.compiler.as.codeg
import org.apache.flex.compiler.common.ASModifier;
import org.apache.flex.compiler.common.ModifiersSet;
import org.apache.flex.compiler.constants.IASKeywordConstants;
+import org.apache.flex.compiler.constants.IASLanguageConstants;
import org.apache.flex.compiler.definitions.IDefinition;
import org.apache.flex.compiler.definitions.IFunctionDefinition;
import org.apache.flex.compiler.definitions.IVariableDefinition;
@@ -47,6 +48,7 @@ import org.apache.flex.compiler.tree.as.
import org.apache.flex.compiler.tree.as.IClassNode;
import org.apache.flex.compiler.tree.as.IConditionalNode;
import org.apache.flex.compiler.tree.as.IContainerNode;
+import org.apache.flex.compiler.tree.as.IContainerNode.ContainerType;
import org.apache.flex.compiler.tree.as.IDefinitionNode;
import org.apache.flex.compiler.tree.as.IDynamicAccessNode;
import org.apache.flex.compiler.tree.as.IExpressionNode;
@@ -80,7 +82,6 @@ import org.apache.flex.compiler.tree.as.
import org.apache.flex.compiler.tree.as.ITypedExpressionNode;
import org.apache.flex.compiler.tree.as.IUnaryOperatorNode;
import org.apache.flex.compiler.tree.as.IVariableNode;
-import org.apache.flex.compiler.tree.as.IContainerNode.ContainerType;
import org.apache.flex.compiler.tree.as.IWhileLoopNode;
import org.apache.flex.compiler.tree.as.IWithNode;
import org.apache.flex.compiler.visitor.IASBlockWalker;
@@ -92,14 +93,28 @@ import org.apache.flex.compiler.visitor.
*/
public class ASEmitter implements IASEmitter
{
- private static final String SPACE = " ";
-
- protected static final String NL = "\n";
-
- protected static final String INDENT_STRING = "\t";
-
private final FilterWriter out;
+ public static final String AS3 = "__AS3__";
+ public static final String COLON = ":";
+ public static final String COMMA = ",";
+ public static final String CURLYBRACE_CLOSE = "}";
+ public static final String CURLYBRACE_OPEN = "{";
+ public static final String EQUALS = ASTNodeID.AssignmentExpressionID.getParaphrase();
+ public static final String FUNCTION = IASKeywordConstants.FUNCTION.toLowerCase();
+ public static final String INDENT = "\t";
+ public static final String LENGTH = "length";
+ public static final String LESS_THEN = ASTNodeID.Op_LessThanID.getParaphrase();
+ public static final String NL = "\n";
+ public static final String PARENTHESES_CLOSE = ")";
+ public static final String PARENTHESES_OPEN = "(";
+ public static final String PERIOD = ".";
+ public static final String SEMICOLON = ";";
+ public static final String SINGLE_QUOTE = "'";
+ public static final String SPACE = " ";
+ public static final String SQUAREBRACKETS_CLOSE = "]";
+ public static final String SQUAREBRACKETS_OPEN = "[";
+
List<ICompilerProblem> problems;
// (mschmalle) think about how this should be implemented, we can add our
@@ -147,24 +162,16 @@ public class ASEmitter implements IASEmi
public ASEmitter(FilterWriter out)
{
this.out = out;
+
problems = new ArrayList<ICompilerProblem>();
}
@Override
public void write(String value)
{
- try
- {
- out.write(value);
-
- final StringBuilder sb = new StringBuilder();
- if (value.indexOf(NL) != -1)
- {
- for (int i = 0; i < currentIndent; i++)
- sb.append(INDENT_STRING);
-
- out.write(sb.toString());
- }
+ try
+ {
+ out.write(value);
}
catch (IOException e)
{
@@ -176,7 +183,7 @@ public class ASEmitter implements IASEmi
{
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < numIndent; i++)
- sb.append(INDENT_STRING);
+ sb.append(INDENT);
return sb.toString();
}
@@ -191,36 +198,20 @@ public class ASEmitter implements IASEmi
{
currentIndent--;
}
-
- // (erikdebruin) I needed a way to add a semi-colon after the closing curly
- // bracket of a block in the 'goog'-ified output. Instead of
- // subclassing 'ASAfterNodeStrategy' and copying
- // the entire function body, I thought I might use this little
- // utility method and override that. Am I doing it right?
- public void writeBlockClose()
- {
- write("}");
- }
-
- public void writeIndent()
- {
- String indent = "";
- for (int i = 0; i < currentIndent; i++)
- indent += INDENT_STRING;
- write(indent);
- }
-
+
+ @Override
public void writeNewline()
{
write(NL);
+ write(getIndent(currentIndent));
}
- public void writeToken(String value)
+ public void writeSymbol(String value)
{
write(value);
}
- public void writeSymbol(String value)
+ public void writeToken(String value)
{
write(value);
}
@@ -237,12 +228,12 @@ public class ASEmitter implements IASEmi
String name = node.getQualifiedName();
if (name != null && !name.equals(""))
{
- write(SPACE);
+ write(SPACE);
getWalker().walk(node.getNameExpressionNode());
}
write(SPACE);
- write("{");
+ write(CURLYBRACE_OPEN);
}
@Override
@@ -257,7 +248,7 @@ public class ASEmitter implements IASEmi
if (tnode != null)
{
indentPush();
- write(NL);
+ writeNewline();
getWalker().walk(tnode); // IClassNode | IInterfaceNode
}
}
@@ -266,8 +257,8 @@ public class ASEmitter implements IASEmi
public void emitPackageFooter(IPackageNode node)
{
indentPop();
- write(NL);
- write("}");
+ writeNewline();
+ write(CURLYBRACE_CLOSE);
}
//--------------------------------------------------------------------------
@@ -316,21 +307,21 @@ public class ASEmitter implements IASEmi
getWalker().walk(inodes[i]);
if (i < ilen - 1)
{
- write(",");
+ write(COMMA);
write(SPACE);
}
}
write(SPACE);
}
- write("{");
+ write(CURLYBRACE_OPEN);
// fields, methods, namespaces
final IDefinitionNode[] members = node.getAllMemberNodes();
if (members.length > 0)
{
indentPush();
- write(NL);
+ writeNewline();
final int len = members.length;
int i = 0;
@@ -339,20 +330,20 @@ public class ASEmitter implements IASEmi
getWalker().walk(mnode);
if (mnode.getNodeID() == ASTNodeID.VariableID)
{
- write(";");
+ write(SEMICOLON);
if (i < len - 1)
- write(NL);
+ writeNewline();
}
else if (mnode.getNodeID() == ASTNodeID.FunctionID)
{
if (i < len - 1)
- write(NL);
+ writeNewline();
}
else if (mnode.getNodeID() == ASTNodeID.GetterID
|| mnode.getNodeID() == ASTNodeID.SetterID)
{
if (i < len - 1)
- write(NL);
+ writeNewline();
}
i++;
}
@@ -360,8 +351,8 @@ public class ASEmitter implements IASEmi
indentPop();
}
- write(NL);
- write("}");
+ writeNewline();
+ write(CURLYBRACE_CLOSE);
}
@Override
@@ -386,37 +377,37 @@ public class ASEmitter implements IASEmi
getWalker().walk(inodes[i]);
if (i < ilen - 1)
{
- write(",");
+ write(COMMA);
write(SPACE);
}
}
write(SPACE);
}
- write("{");
+ write(CURLYBRACE_OPEN);
final IDefinitionNode[] members = node.getAllMemberDefinitionNodes();
if (members.length > 0)
{
indentPush();
- write(NL);
+ writeNewline();
final int len = members.length;
int i = 0;
for (IDefinitionNode mnode : members)
{
getWalker().walk(mnode);
- write(";");
+ write(SEMICOLON);
if (i < len - 1)
- write(NL);
+ writeNewline();
i++;
}
indentPop();
}
- write(NL);
- write("}");
+ writeNewline();
+ write(CURLYBRACE_CLOSE);
}
//--------------------------------------------------------------------------
@@ -444,7 +435,7 @@ public class ASEmitter implements IASEmi
IASNode child = node.getChild(i);
if (child instanceof ChainedVariableNode)
{
- write(",");
+ write(COMMA);
write(SPACE);
emitVarDeclaration((IVariableNode) child);
}
@@ -492,7 +483,7 @@ public class ASEmitter implements IASEmi
IASNode child = node.getChild(i);
if (child instanceof ChainedVariableNode)
{
- write(",");
+ write(COMMA);
write(SPACE);
emitField((IVariableNode) child);
}
@@ -589,7 +580,7 @@ public class ASEmitter implements IASEmi
{
FunctionObjectNode f = (FunctionObjectNode) node;
FunctionNode fnode = f.getFunctionNode();
- writeToken(IASKeywordConstants.FUNCTION);
+ writeToken(FUNCTION);
emitParamters(fnode.getParameterNodes());
emitType(fnode.getTypeNode());
emitFunctionScope(fnode.getScopedNode());
@@ -607,7 +598,7 @@ public class ASEmitter implements IASEmi
write(SPACE);
emitMemberName(node);
write(SPACE);
- write("=");
+ write(EQUALS);
write(SPACE);
getWalker().walk(node.getNamespaceURINode());
}
@@ -644,12 +635,14 @@ public class ASEmitter implements IASEmi
{
if (node instanceof IFunctionNode)
{
- writeToken(IASKeywordConstants.FUNCTION);
+ writeToken(FUNCTION);
write(SPACE);
}
else if (node instanceof IVariableNode)
{
- write(((IVariableNode) node).isConst() ? "const" : "var");
+ write(((IVariableNode) node).isConst() ?
+ IASKeywordConstants.CONST :
+ IASKeywordConstants.VAR);
write(SPACE);
}
}
@@ -666,29 +659,33 @@ public class ASEmitter implements IASEmi
protected void emitParamters(IParameterNode[] nodes)
{
- write("(");
+ write(PARENTHESES_OPEN);
int len = nodes.length;
for (int i = 0; i < len; i++)
{
IParameterNode node = nodes[i];
- // this will call emitParameter(node)
- getWalker().walk(node);
+ getWalker().walk(node); //emitParameter
if (i < len - 1)
- write(", ");
+ {
+ write(COMMA);
+ write(SPACE);
+ }
}
- write(")");
+ write(PARENTHESES_CLOSE);
}
@Override
public void emitParameter(IParameterNode node)
{
getWalker().walk(node.getNameExpressionNode());
- write(":");
+ write(COLON);
getWalker().walk(node.getVariableTypeNode());
IExpressionNode anode = node.getAssignedValueNode();
if (anode != null)
{
- write(" = ");
+ write(SPACE);
+ write(EQUALS);
+ write(SPACE);
getWalker().walk(anode);
}
}
@@ -699,7 +696,7 @@ public class ASEmitter implements IASEmi
// or node.getReturnTypeNode()
if (node != null)
{
- write(":");
+ write(COLON);
getWalker().walk(node);
}
}
@@ -708,8 +705,8 @@ public class ASEmitter implements IASEmi
{
if (node != null)
{
- write(SPACE);
- write("=");
+ write(SPACE);
+ write(EQUALS);
write(SPACE);
getWalker().walk(node);
}
@@ -723,7 +720,7 @@ public class ASEmitter implements IASEmi
protected void emitMethodScope(IScopedNode node)
{
- write(SPACE);
+ write(SPACE);
getWalker().walk(node);
}
@@ -751,11 +748,11 @@ public class ASEmitter implements IASEmi
if (node.getParent().getNodeID() != ASTNodeID.LabledStatementID
&& !(node instanceof IStatementNode))
{
- write(";");
+ write(SEMICOLON);
}
if (!isLastStatement(node))
- write("\n");
+ writeNewline();
}
@Override
@@ -766,13 +763,13 @@ public class ASEmitter implements IASEmi
IContainerNode xnode = (IContainerNode) conditional
.getStatementContentsNode();
- write("if");
- write(" ");
- write("(");
+ write(IASKeywordConstants.IF);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(conditional.getChild(0)); // conditional expression
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit(xnode))
- write(" ");
+ write(SPACE);
getWalker().walk(conditional.getChild(1)); // BlockNode
IConditionalNode[] nodes = node.getElseIfNodes();
@@ -786,17 +783,19 @@ public class ASEmitter implements IASEmi
final boolean isImplicit = isImplicit(snode);
if (isImplicit)
- write("\n");
+ writeNewline();
else
- write(" ");
+ write(SPACE);
- write("else if");
- write(" ");
- write("(");
+ write(IASKeywordConstants.ELSE);
+ write(SPACE);
+ write(IASKeywordConstants.IF);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(enode.getChild(0));
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit)
- write(" ");
+ write(SPACE);
getWalker().walk(enode.getChild(1)); // ConditionalNode
}
@@ -809,12 +808,12 @@ public class ASEmitter implements IASEmi
// if an implicit if, add a newline with no space
final boolean isImplicit = isImplicit(cnode);
if (isImplicit)
- write("\n");
+ writeNewline();
else
- write(" ");
- write("else");
+ write(SPACE);
+ write(IASKeywordConstants.ELSE);
if (!isImplicit)
- write(" ");
+ write(SPACE);
getWalker().walk(elseNode); // TerminalNode
}
@@ -824,18 +823,18 @@ public class ASEmitter implements IASEmi
public void emitForEachLoop(IForLoopNode node)
{
IContainerNode xnode = (IContainerNode) node.getChild(1);
- write("for");
- write(" ");
- write("each");
- write(" ");
- write("(");
+ write(IASKeywordConstants.FOR);
+ write(SPACE);
+ write(IASKeywordConstants.EACH);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
IContainerNode cnode = node.getConditionalsContainerNode();
getWalker().walk(cnode.getChild(0));
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit(xnode))
- write(" ");
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
}
@@ -845,9 +844,9 @@ public class ASEmitter implements IASEmi
{
IContainerNode xnode = (IContainerNode) node.getChild(1);
- write("for");
- write(" ");
- write("(");
+ write(IASKeywordConstants.FOR);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
IContainerNode cnode = node.getConditionalsContainerNode();
final IASNode node0 = cnode.getChild(0);
@@ -860,9 +859,9 @@ public class ASEmitter implements IASEmi
visitForBody(cnode);
}
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit(xnode))
- write(" ");
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
}
@@ -870,14 +869,15 @@ public class ASEmitter implements IASEmi
@Override
public void emitSwitch(ISwitchNode node)
{
- write("switch");
- write(" ");
- write("(");
+ write(IASKeywordConstants.SWITCH);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getChild(0));
- write(")");
- write(" {");
+ write(PARENTHESES_CLOSE);
+ write(SPACE);
+ write(CURLYBRACE_OPEN);
indentPush();
- write("\n");
+ writeNewline();
IConditionalNode[] cnodes = getCaseNodes(node);
ITerminalNode dnode = getDefaultNode(node);
@@ -886,46 +886,46 @@ public class ASEmitter implements IASEmi
{
IConditionalNode casen = cnodes[i];
IContainerNode cnode = (IContainerNode) casen.getChild(1);
- write("case");
- write(" ");
+ write(IASKeywordConstants.CASE);
+ write(SPACE);
getWalker().walk(casen.getConditionalExpressionNode());
- write(":");
+ write(COLON);
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
getWalker().walk(casen.getStatementContentsNode());
if (i == cnodes.length - 1 && dnode == null)
{
indentPop();
- write("\n");
+ writeNewline();
}
else
- write("\n");
+ writeNewline();
}
if (dnode != null)
{
IContainerNode cnode = (IContainerNode) dnode.getChild(0);
- write("default");
- write(":");
+ write(IASKeywordConstants.DEFAULT);
+ write(COLON);
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
getWalker().walk(dnode);
indentPop();
- write("\n");
+ writeNewline();
}
- write("}");
+ write(CURLYBRACE_CLOSE);
}
@Override
public void emitWhileLoop(IWhileLoopNode node)
{
IContainerNode cnode = (IContainerNode) node.getChild(1);
- write("while");
- write(" ");
- write("(");
+ write(IASKeywordConstants.WHILE);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getConditionalExpressionNode());
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
}
@@ -933,48 +933,49 @@ public class ASEmitter implements IASEmi
public void emitDoLoop(IWhileLoopNode node)
{
IContainerNode cnode = (IContainerNode) node.getChild(0);
- write("do");
+ write(IASKeywordConstants.DO);
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
else
- write("\n"); // TODO (mschmalle) there is something wrong here, block should NL
- write("while");
- write(" ");
- write("(");
+ writeNewline(); // TODO (mschmalle) there is something wrong here, block should NL
+ write(IASKeywordConstants.WHILE);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getConditionalExpressionNode());
- write(");");
+ write(PARENTHESES_CLOSE);
+ write(SEMICOLON);
}
@Override
public void emitWith(IWithNode node)
{
IContainerNode cnode = (IContainerNode) node.getChild(1);
- write("with");
- write(" ");
- write("(");
+ write(IASKeywordConstants.WITH);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getTargetNode());
- write(")");
+ write(PARENTHESES_CLOSE);
if (!isImplicit(cnode))
- write(" ");
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
}
@Override
public void emitThrow(IThrowNode node)
{
- write("throw");
- write(" ");
+ write(IASKeywordConstants.THROW);
+ write(SPACE);
getWalker().walk(node.getThrownExpressionNode());
}
@Override
public void emitTry(ITryNode node)
{
- write("try");
- write(" ");
+ write(IASKeywordConstants.TRY);
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
for (int i = 0; i < node.getCatchNodeCount(); i++)
{
@@ -983,9 +984,9 @@ public class ASEmitter implements IASEmi
ITerminalNode fnode = node.getFinallyNode();
if (fnode != null)
{
- write(" ");
- write("finally");
- write(" ");
+ write(SPACE);
+ write(IASKeywordConstants.FINALLY);
+ write(SPACE);
getWalker().walk(fnode);
}
}
@@ -993,24 +994,24 @@ public class ASEmitter implements IASEmi
@Override
public void emitCatch(ICatchNode node)
{
- write(" ");
- write("catch");
- write(" ");
- write("(");
+ write(SPACE);
+ write(IASKeywordConstants.CATCH);
+ write(SPACE);
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getCatchParameterNode());
- write(")");
- write(" ");
+ write(PARENTHESES_CLOSE);
+ write(SPACE);
getWalker().walk(node.getStatementContentsNode());
}
@Override
public void emitReturn(IReturnNode node)
{
- write("return");
+ write(IASKeywordConstants.RETURN);
IExpressionNode rnode = node.getReturnValueNode();
if (rnode != null && rnode.getNodeID() != ASTNodeID.NilID)
{
- write(" ");
+ write(SPACE);
getWalker().walk(rnode);
}
}
@@ -1024,15 +1025,15 @@ public class ASEmitter implements IASEmi
{
if (node.isNewExpression())
{
- write("new");
- write(" ");
+ write(IASKeywordConstants.NEW);
+ write(SPACE);
}
getWalker().walk(node.getNameNode());
- write("(");
+ write(PARENTHESES_OPEN);
walkArguments(node.getArgumentNodes());
- write(")");
+ write(PARENTHESES_CLOSE);
}
//--------------------------------------------------------------------------
@@ -1044,7 +1045,7 @@ public class ASEmitter implements IASEmi
{
getWalker().walk(node.getLeftOperandNode());
if (node.getNodeID() != ASTNodeID.Op_CommaID)
- write(SPACE);
+ write(SPACE);
write(node.getOperator().getOperatorText());
write(SPACE);
getWalker().walk(node.getRightOperandNode());
@@ -1074,7 +1075,10 @@ public class ASEmitter implements IASEmi
IExpressionNode node = nodes[i];
getWalker().walk(node);
if (i < len - 1)
- write(", ");
+ {
+ write(COMMA);
+ write(SPACE);
+ }
}
}
@@ -1082,32 +1086,6 @@ public class ASEmitter implements IASEmi
// Static Utility
//--------------------------------------------------------------------------
- private static String toPrefix(ContainerType type)
- {
- if (type == ContainerType.BRACES)
- return "{";
- else if (type == ContainerType.BRACKETS)
- return "[";
- else if (type == ContainerType.IMPLICIT)
- return "";
- else if (type == ContainerType.PARENTHESIS)
- return "(";
- return null;
- }
-
- private static String toPostfix(ContainerType type)
- {
- if (type == ContainerType.BRACES)
- return "}";
- else if (type == ContainerType.BRACKETS)
- return "]";
- else if (type == ContainerType.IMPLICIT)
- return "";
- else if (type == ContainerType.PARENTHESIS)
- return ")";
- return null;
- }
-
protected static IFunctionNode getConstructor(IDefinitionNode[] members)
{
for (IDefinitionNode node : members)
@@ -1156,17 +1134,17 @@ public class ASEmitter implements IASEmi
if (node0 != null)
{
getWalker().walk(node0);
- write(";");
+ write(SEMICOLON);
if (node1.getNodeID() != ASTNodeID.NilID)
- write(" ");
+ write(SPACE);
}
// condition or target
if (node1 != null)
{
getWalker().walk(node1);
- write(";");
+ write(SEMICOLON);
if (node2.getNodeID() != ASTNodeID.NilID)
- write(" ");
+ write(SPACE);
}
// iterator
if (node2 != null)
@@ -1221,16 +1199,40 @@ public class ASEmitter implements IASEmi
@Override
public void emitLiteralContainer(IContainerNode node)
{
- write(toPrefix(node.getContainerType()));
+ ContainerType type = node.getContainerType();
+ String postFix = "";
+
+ if (type == ContainerType.BRACES)
+ {
+ write(CURLYBRACE_OPEN);
+ postFix = CURLYBRACE_CLOSE;
+ }
+ else if (type == ContainerType.BRACKETS)
+ {
+ write(SQUAREBRACKETS_OPEN);
+ postFix = SQUAREBRACKETS_CLOSE;
+ }
+ else if (type == ContainerType.IMPLICIT)
+ {
+ // nothing to write, move along
+ }
+ else if (type == ContainerType.PARENTHESIS)
+ {
+ write(PARENTHESES_OPEN);
+ postFix = PARENTHESES_CLOSE;
+ }
+
final int len = node.getChildCount();
for (int i = 0; i < len; i++)
{
IASNode child = node.getChild(i);
getWalker().walk(child);
if (i < len - 1)
- write(",");
+ write(COMMA);
}
- write(toPostfix(node.getContainerType()));
+
+ if (postFix != "")
+ write(postFix);
}
@Override
@@ -1258,7 +1260,7 @@ public class ASEmitter implements IASEmi
IIdentifierNode lnode = node.getLabelNode();
if (lnode != null)
{
- write(" ");
+ write(SPACE);
getWalker().walk(lnode);
}
}
@@ -1275,31 +1277,32 @@ public class ASEmitter implements IASEmi
public void emitDynamicAccess(IDynamicAccessNode node)
{
getWalker().walk(node.getLeftOperandNode());
- write("[");
+ write(SQUAREBRACKETS_OPEN);
getWalker().walk(node.getRightOperandNode());
- write("]");
+ write(SQUAREBRACKETS_CLOSE);
}
@Override
public void emitTypedExpression(ITypedExpressionNode node)
{
getWalker().walk(node.getCollectionNode());
- write(".<");
+ write(PERIOD);
+ write(ASTNodeID.Op_LessThanID.getParaphrase());
getWalker().walk(node.getTypeNode());
- write(">");
+ write(ASTNodeID.Op_GreaterThanID.getParaphrase());
}
@Override
public void emitTernaryOperator(ITernaryOperatorNode node)
{
getWalker().walk(node.getConditionalNode());
- write(" ");
- write("?");
- write(" ");
+ write(SPACE);
+ write(ASTNodeID.TernaryExpressionID.getParaphrase());
+ write(SPACE);
getWalker().walk(node.getLeftOperandNode());
- write(" ");
- write(":");
- write(" ");
+ write(SPACE);
+ write(COLON);
+ write(SPACE);
getWalker().walk(node.getRightOperandNode());
}
@@ -1307,7 +1310,7 @@ public class ASEmitter implements IASEmi
public void emitObjectLiteralValuePair(IObjectLiteralValuePairNode node)
{
getWalker().walk(node.getNameNode());
- write(":");
+ write(COLON);
getWalker().walk(node.getValueNode());
}
@@ -1315,9 +1318,9 @@ public class ASEmitter implements IASEmi
public void emitLabelStatement(LabeledStatementNode node)
{
write(node.getLabel());
- write(" ");
- write(":");
- write(" ");
+ write(SPACE);
+ write(COLON);
+ write(SPACE);
getWalker().walk(node.getLabeledStatement());
}
@@ -1353,15 +1356,15 @@ public class ASEmitter implements IASEmi
|| node.getNodeID() == ASTNodeID.Op_VoidID)
{
write(node.getOperator().getOperatorText());
- write(" ");
+ write(SPACE);
getWalker().walk(node.getOperandNode());
}
else if (node.getNodeID() == ASTNodeID.Op_TypeOfID)
{
write(node.getOperator().getOperatorText());
- write("(");
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getOperandNode());
- write(")");
+ write(PARENTHESES_CLOSE);
}
}
@@ -1370,23 +1373,23 @@ public class ASEmitter implements IASEmi
{
if (node.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.ANY_TYPE)
{
- write("*");
+ write(IASLanguageConstants.ANY_TYPE);
}
else if (node.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.REST)
{
- write("...");
+ write(IASLanguageConstants.REST);
}
else if (node.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.SUPER)
{
- write("super");
+ write(IASKeywordConstants.SUPER);
}
else if (node.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.THIS)
{
- write("this");
+ write(IASKeywordConstants.THIS);
}
else if (node.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.VOID)
{
- write("void");
+ write(IASKeywordConstants.VOID);
}
}
}
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSDocEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSDocEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSDocEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSDocEmitter.java Thu Jan 17 20:37:37 2013
@@ -19,16 +19,40 @@
package org.apache.flex.compiler.internal.js.codegen;
+import org.apache.flex.compiler.internal.as.codegen.ASEmitter;
import org.apache.flex.compiler.js.codegen.IJSDocEmitter;
import org.apache.flex.compiler.js.codegen.IJSEmitter;
public class JSDocEmitter implements IJSDocEmitter
{
+ public static final String JSDOC_CLOSE = "*/";
+ public static final String JSDOC_OPEN = "/**";
+
public void write(String value)
{
emitter.write(value);
}
+ public void writeBlockClose()
+ {
+ emitter.write(ASEmitter.CURLYBRACE_CLOSE);
+ }
+
+ public void writeBlockOpen()
+ {
+ emitter.write(ASEmitter.CURLYBRACE_OPEN);
+ }
+
+ public void writeNewline()
+ {
+ emitter.writeNewline();
+ }
+
+ public void writeSpace()
+ {
+ emitter.write(ASEmitter.SPACE);
+ }
+
private IJSEmitter emitter;
public JSDocEmitter(IJSEmitter emitter)
@@ -39,13 +63,16 @@ public class JSDocEmitter implements IJS
@Override
public void begin()
{
- write("/**\n");
+ write(JSDOC_OPEN);
+ writeNewline();
}
@Override
public void end()
{
- write(" */\n");
+ writeSpace();
+ write(JSDOC_CLOSE);
+ writeNewline();
}
}
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/JSEmitter.java Thu Jan 17 20:37:37 2013
@@ -33,6 +33,13 @@ import org.apache.flex.compiler.tree.as.
public class JSEmitter extends ASEmitter implements IJSEmitter
{
+ public static final String CALL = "call";
+ public static final String CONFIGURABLE = "configurable";
+ public static final String CONSTRUCTOR = "constructor";
+ public static final String DEFINE_PROPERTY = "defineProperty";
+ public static final String PROTOTYPE = "prototype";
+ public static final String SLICE = "slice";
+
public JSEmitter(FilterWriter out)
{
super(out);
@@ -45,7 +52,7 @@ public class JSEmitter extends ASEmitter
FunctionNode fnode = f.getFunctionNode();
- write("function");
+ write(FUNCTION);
emitParamters(fnode.getParameterNodes());
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/amd/JSAMDEmitter.java Thu Jan 17 20:37:37 2013
@@ -27,6 +27,8 @@ import java.util.List;
import java.util.Map;
import org.apache.flex.compiler.common.ASModifier;
+import org.apache.flex.compiler.constants.IASKeywordConstants;
+import org.apache.flex.compiler.constants.IASLanguageConstants;
import org.apache.flex.compiler.definitions.IClassDefinition;
import org.apache.flex.compiler.definitions.ITypeDefinition;
import org.apache.flex.compiler.internal.js.codegen.JSEmitter;
@@ -38,7 +40,6 @@ import org.apache.flex.compiler.tree.as.
import org.apache.flex.compiler.tree.as.IDefinitionNode;
import org.apache.flex.compiler.tree.as.IFunctionNode;
import org.apache.flex.compiler.tree.as.IParameterNode;
-import org.apache.flex.compiler.tree.as.IScopedNode;
import org.apache.flex.compiler.tree.as.ITypeNode;
/**
@@ -69,10 +70,10 @@ public class JSAMDEmitter extends JSEmit
String qname = definition.getQualifiedName();
write(qname);
- write(" ");
- write("=");
- write(" ");
- write("function");
+ write(SPACE);
+ write(EQUALS);
+ write(SPACE);
+ write(FUNCTION);
emitParamters(node.getParameterNodes());
emitMethodScope(node.getScopedNode());
@@ -86,16 +87,19 @@ public class JSAMDEmitter extends JSEmit
if (qname != null && !qname.equals(""))
{
write(qname);
- write(".");
+ write(PERIOD);
if (!fn.hasModifier(ASModifier.STATIC))
- write("prototype.");
+ {
+ write(PROTOTYPE);
+ write(PERIOD);
+ }
}
emitMemberName(node);
- write(" ");
- write("=");
- write(" ");
- write("function");
+ write(SPACE);
+ write(EQUALS);
+ write(SPACE);
+ write(FUNCTION);
emitParamters(node.getParameterNodes());
emitMethodScope(node.getScopedNode());
}
@@ -115,62 +119,86 @@ public class JSAMDEmitter extends JSEmit
if (pnodes.length == 0)
return;
- final StringBuilder code = new StringBuilder();
if (defaults != null)
{
+ boolean hasBody = node.getScopedNode().getChildCount() > 0;
+
+ if (!hasBody)
+ {
+ indentPush();
+ write(INDENT);
+ }
+
+ final StringBuilder code = new StringBuilder();
+
List<IParameterNode> parameters = new ArrayList<IParameterNode>(
defaults.values());
Collections.reverse(parameters);
int len = defaults.size();
- int numDefaults = 0;
// make the header in reverse order
for (IParameterNode pnode : parameters)
{
if (pnode != null)
{
- code.append(getIndent(numDefaults));
- code.append("if (arguments.length < " + len + ") {\n");
- numDefaults++;
+ code.setLength(0);
+
+ code.append(IASKeywordConstants.IF);
+ code.append(SPACE);
+ code.append(PARENTHESES_OPEN);
+ code.append(IASLanguageConstants.arguments);
+ code.append(PERIOD);
+ code.append(LENGTH);
+ code.append(SPACE);
+ code.append(LESS_THEN);
+ code.append(SPACE);
+ code.append(len);
+ code.append(PARENTHESES_CLOSE);
+ code.append(SPACE);
+ code.append(CURLYBRACE_OPEN);
+
+ write(code.toString());
+
+ indentPush();
+ writeNewline();
}
len--;
}
Collections.reverse(parameters);
- for (IParameterNode pnode : parameters)
+ for (int i = 0, n = parameters.size(); i < n; i++)
{
+ IParameterNode pnode = parameters.get(i);
+
if (pnode != null)
{
- code.append(getIndent(numDefaults));
+ code.setLength(0);
+
code.append(pnode.getName());
- code.append(" = ");
+ code.append(SPACE);
+ code.append(EQUALS);
+ code.append(SPACE);
code.append(pnode.getDefaultValue());
- code.append(";\n");
- code.append(getIndent(numDefaults - 1));
- code.append("}");
- if (numDefaults > 1)
- code.append("\n");
- numDefaults--;
+ code.append(SEMICOLON);
+ write(code.toString());
+
+ indentPop();
+ writeNewline();
+
+ write(CURLYBRACE_CLOSE);
+
+ if (i == n - 1 && !hasBody)
+ indentPop();
+
+ writeNewline();
}
}
- IScopedNode sbn = node.getScopedNode();
- boolean hasBody = sbn.getChildCount() > 0;
- // adds the current block indent to the generated code
- String indent = getIndent(getCurrentIndent() + (!hasBody ? 1 : 0));
- String result = code.toString().replaceAll("\n", "\n" + indent);
- // if the block dosn't have a body (children), need to add indent to head
- if (!hasBody)
- result = indent + result;
- // have to add newline after the replace or we get an extra indent
- result += "\n";
- write(result);
}
}
@Override
public void emitParameter(IParameterNode node)
{
- // only the name gets output in JS
getWalker().walk(node.getNameExpressionNode());
}
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogDocEmitter.java Thu Jan 17 20:37:37 2013
@@ -20,15 +20,19 @@
package org.apache.flex.compiler.internal.js.codegen.goog;
import org.apache.flex.compiler.common.ASModifier;
+import org.apache.flex.compiler.constants.IASKeywordConstants;
+import org.apache.flex.compiler.constants.IASLanguageConstants;
import org.apache.flex.compiler.definitions.IClassDefinition;
import org.apache.flex.compiler.definitions.ITypeDefinition;
import org.apache.flex.compiler.definitions.references.IReference;
+import org.apache.flex.compiler.internal.as.codegen.ASEmitter;
import org.apache.flex.compiler.internal.js.codegen.JSDocEmitter;
import org.apache.flex.compiler.internal.js.codegen.JSSharedData;
import org.apache.flex.compiler.internal.semantics.SemanticUtils;
import org.apache.flex.compiler.js.codegen.IJSEmitter;
import org.apache.flex.compiler.js.codegen.goog.IJSGoogDocEmitter;
import org.apache.flex.compiler.projects.ICompilerProject;
+import org.apache.flex.compiler.tree.ASTNodeID;
import org.apache.flex.compiler.tree.as.IASNode;
import org.apache.flex.compiler.tree.as.IClassNode;
import org.apache.flex.compiler.tree.as.IFunctionNode;
@@ -38,6 +42,10 @@ import org.apache.flex.compiler.tree.as.
public class JSGoogDocEmitter extends JSDocEmitter implements IJSGoogDocEmitter
{
+ public static final String AT = "@";
+ public static final String PARAM = "param";
+ public static final String STAR = "*";
+ public static final String TYPE = "type";
public JSGoogDocEmitter(IJSEmitter emitter)
{
@@ -50,11 +58,11 @@ public class JSGoogDocEmitter extends JS
begin();
String ns = node.getNamespace();
- if (ns == "private")
+ if (ns == IASKeywordConstants.PRIVATE)
{
emitPrivate(node);
}
- else if (ns == "protected")
+ else if (ns == IASKeywordConstants.PROTECTED)
{
emitProtected(node);
}
@@ -82,13 +90,13 @@ public class JSGoogDocEmitter extends JS
begin();
hasDoc = true;
- write(" * @constructor\n");
+ emitJSDocLine(JSGoogEmitter.CONSTRUCTOR);
IClassDefinition parent = (IClassDefinition) node.getDefinition().getParent();
IClassDefinition superClass = parent.resolveBaseClass(project);
String qname = superClass.getQualifiedName();
- if (superClass != null && !qname.equals("Object"))
+ if (superClass != null && !qname.equals(IASLanguageConstants.Object))
emitExtends(superClass);
IReference[] interfaceReferences = classDefinition.getImplementedInterfaceReferences();
@@ -126,7 +134,7 @@ public class JSGoogDocEmitter extends JS
// @return
String returnType = node.getReturnType();
- if (returnType != "" && returnType != "void")
+ if (returnType != "" && returnType != ASTNodeID.LiteralVoidID.getParaphrase())
{
if (!hasDoc)
{
@@ -165,7 +173,7 @@ public class JSGoogDocEmitter extends JS
}
else
{
- write("\n"); // TODO (erikdebruin) check if this is needed
+ writeNewline(); // TODO (erikdebruin) check if this is needed
begin();
emitConst(node);
emitType(node);
@@ -176,7 +184,7 @@ public class JSGoogDocEmitter extends JS
@Override
public void emitConst(IVariableNode node)
{
- write(" * @const\n");
+ emitJSDocLine(IASKeywordConstants.CONST);
}
@Override
@@ -203,14 +211,14 @@ public class JSGoogDocEmitter extends JS
@Override
public void emitExtends(IClassDefinition superDefinition)
{
- write(" * @extends {" + superDefinition.getQualifiedName() + "}\n");
+ emitJSDocLine(IASKeywordConstants.EXTENDS, superDefinition.getQualifiedName());
}
@Override
public void emitImplements(IReference reference)
{
// TODO (erikdebruin) we need to get the fully qualified name...
- write(" * @implements {" + reference.getName() + "}\n");
+ emitJSDocLine(IASKeywordConstants.IMPLEMENTS, reference.getName());
}
@Override
@@ -230,33 +238,33 @@ public class JSGoogDocEmitter extends JS
@Override
public void emitOverride(IFunctionNode node)
{
- write(" * @override\n");
+ emitJSDocLine(IASKeywordConstants.OVERRIDE);
}
@Override
public void emitParam(IParameterNode node)
{
- String postfix = (node.getDefaultValue() == null) ? "" : "=";
+ String postfix = (node.getDefaultValue() == null) ? "" : ASEmitter.EQUALS;
String paramType = "";
if (node.isRest())
- paramType = "...";
+ paramType = IASLanguageConstants.REST;
else
paramType = convertASTypeToJS(node.getVariableType());
- write(" * @param {" + paramType + postfix + "} " + node.getName() + "\n");
+ emitJSDocLine(PARAM, paramType + postfix, node.getName());
}
@Override
public void emitPrivate(IASNode node)
{
- write(" * @private\n");
+ emitJSDocLine(IASKeywordConstants.PRIVATE);
}
@Override
public void emitProtected(IASNode node)
{
- write(" * @protected\n");
+ emitJSDocLine(IASKeywordConstants.PROTECTED);
}
@Override
@@ -264,26 +272,38 @@ public class JSGoogDocEmitter extends JS
{
String rtype = node.getReturnType();
if (rtype != null)
- write(" * @return {" + convertASTypeToJS(rtype) + "}\n");
+ {
+ emitJSDocLine(IASKeywordConstants.RETURN, convertASTypeToJS(rtype));
+ }
}
@Override
public void emitThis(ITypeDefinition type)
{
- write(" * @this {" + type.getQualifiedName() + "}\n");
+ emitJSDocLine(IASKeywordConstants.THIS, type.getQualifiedName());
}
@Override
public void emitType(IASNode node)
{
- String type = ((IVariableNode) node).getVariableType();
- write(" * @type {" + convertASTypeToJS(type) + "}\n");
+ String type = ((IVariableNode) node).getVariableType();
+ emitJSDocLine(TYPE, convertASTypeToJS(type));
}
public void emitTypeShort(IASNode node)
{
String type = ((IVariableNode) node).getVariableType();
- write("/** @type {" + convertASTypeToJS(type) + "} */ ");
+ write(JSDOC_OPEN);
+ writeSpace();
+ write(AT);
+ write(TYPE);
+ writeSpace();
+ writeBlockOpen();
+ write(convertASTypeToJS(type));
+ writeBlockClose();
+ writeSpace();
+ write(JSDOC_CLOSE);
+ writeSpace();
}
@Override
@@ -298,12 +318,47 @@ public class JSGoogDocEmitter extends JS
public void emmitPackageHeader(IPackageNode node)
{
begin();
- write(" * " + JSSharedData.getTimeStampString());
+ write(ASEmitter.SPACE);
+ write(STAR);
+ write(ASEmitter.SPACE);
+ write(JSSharedData.getTimeStampString());
end();
}
//--------------------------------------------------------------------------
+ private void emitJSDocLine(String name)
+ {
+ emitJSDocLine(name, "");
+ }
+
+ private void emitJSDocLine(String name, String type)
+ {
+ emitJSDocLine(name, type, "");
+ }
+
+ private void emitJSDocLine(String name, String type, String param)
+ {
+ writeSpace();
+ write(STAR);
+ writeSpace();
+ write(AT);
+ write(name);
+ if (type != "")
+ {
+ writeSpace();
+ writeBlockOpen();
+ write(type);
+ writeBlockClose();
+ }
+ if (param != "")
+ {
+ writeSpace();
+ write(param);
+ }
+ writeNewline();
+ }
+
private boolean containsThisReference(IASNode node)
{
final int len = node.getChildCount();
@@ -329,14 +384,17 @@ public class JSGoogDocEmitter extends JS
String result = name;
if (name.equals(""))
- result = "*";
- else if (name.equals("Boolean") || name.equals("String") || name.equals("Number"))
+ result = IASLanguageConstants.ANY_TYPE;
+ else if (name.equals(IASLanguageConstants.Boolean) ||
+ name.equals(IASLanguageConstants.String) ||
+ name.equals(IASLanguageConstants.Number))
result = result.toLowerCase();
- else if (name.equals("int") || name.equals("uint"))
- result = "number";
+ else if (name.equals(IASLanguageConstants._int) ||
+ name.equals(IASLanguageConstants.uint))
+ result = IASLanguageConstants.Number.toLowerCase();
else if (name.matches("Vector.<.*>"))
// TODO (erikdebruin) will this work with nested Vector declarations?
- result = name.replace("Vector", "Array");
+ result = name.replace(IASLanguageConstants.Vector, IASLanguageConstants.Array);
return result;
}
Modified: flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogEmitter.java
URL: http://svn.apache.org/viewvc/flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogEmitter.java?rev=1434902&r1=1434901&r2=1434902&view=diff
==============================================================================
--- flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogEmitter.java (original)
+++ flex/falcon/trunk/compiler.jx/src/org/apache/flex/compiler/internal/js/codegen/goog/JSGoogEmitter.java Thu Jan 17 20:37:37 2013
@@ -26,6 +26,8 @@ import java.util.List;
import java.util.Map;
import org.apache.flex.compiler.common.ASModifier;
+import org.apache.flex.compiler.constants.IASKeywordConstants;
+import org.apache.flex.compiler.constants.IASLanguageConstants;
import org.apache.flex.compiler.definitions.IClassDefinition;
import org.apache.flex.compiler.definitions.IFunctionDefinition;
import org.apache.flex.compiler.definitions.IPackageDefinition;
@@ -61,6 +63,11 @@ import org.apache.flex.compiler.tree.as.
*/
public class JSGoogEmitter extends JSEmitter implements IJSGoogEmitter
{
+ public static final String GOOG_BASE = "goog.base";
+ public static final String GOOG_INHERITS = "goog.inherits";
+ public static final String GOOG_PROVIDE = "goog.provide";
+ public static final String GOOG_REQUIRE = "goog.require";
+
IJSGoogDocEmitter getDoc()
{
return (IJSGoogDocEmitter) getDocEmitter();
@@ -77,9 +84,16 @@ public class JSGoogEmitter extends JSEmi
if (type == null)
return;
- write("goog.provide('" + type.getQualifiedName() + "');");
- write("\n");
- write("\n");
+ /* goog.provide('x');\n\n */
+ write(GOOG_PROVIDE);
+ write(PARENTHESES_OPEN);
+ write(SINGLE_QUOTE);
+ write(type.getQualifiedName());
+ write(SINGLE_QUOTE);
+ write(PARENTHESES_CLOSE);
+ write(SEMICOLON);
+ writeNewline();
+ writeNewline();
}
@Override
@@ -94,18 +108,26 @@ public class JSGoogEmitter extends JSEmi
parent.getContainedScope().getScopeNode().getAllImports(list);
for (String imp : list)
{
- if (imp.indexOf("__AS3__") != -1)
+ if (imp.indexOf(AS3) != -1)
continue;
- write("goog.require('" + imp + "');");
- write("\n");
+
+ /* goog.require('x');\n */
+ write(GOOG_REQUIRE);
+ write(PARENTHESES_OPEN);
+ write(SINGLE_QUOTE);
+ write(imp);
+ write(SINGLE_QUOTE);
+ write(PARENTHESES_CLOSE);
+ write(SEMICOLON);
+ writeNewline();
}
// (erikdebruin) only write 'closing' line break when there are
// actually imports...
if (list.size() > 1 ||
- (list.size() == 1 && list.get(0).indexOf("__AS3__") == -1))
+ (list.size() == 1 && list.get(0).indexOf(AS3) == -1))
{
- write("\n");
+ writeNewline();
}
}
@@ -137,16 +159,17 @@ public class JSGoogEmitter extends JSEmi
// constructor
emitMethod((IFunctionNode) definition.getConstructor().getNode());
- write(";");
+ write(SEMICOLON);
IDefinitionNode[] members = node.getAllMemberNodes();
for (IDefinitionNode dnode : members)
{
if (dnode instanceof IVariableNode)
{
- write("\n\n");
+ writeNewline();
+ writeNewline();
emitField((IVariableNode) dnode);
- write(";");
+ write(SEMICOLON);
}
}
@@ -156,9 +179,10 @@ public class JSGoogEmitter extends JSEmi
{
if (!((IFunctionNode) dnode).isConstructor())
{
- write("\n\n");
+ writeNewline();
+ writeNewline();
emitMethod((IFunctionNode) dnode);
- write(";");
+ write(SEMICOLON);
}
}
}
@@ -253,15 +277,22 @@ public class JSGoogEmitter extends JSEmi
getDoc().emitFieldDoc(node);
+ /* x.prototype.y = z */
+
String root = "";
if (!node.isConst())
- root = "prototype.";
- write(definition.getQualifiedName() + "." + root + node.getName());
+ {
+ root = PROTOTYPE;
+ root += PERIOD;
+ }
+ write(definition.getQualifiedName() + PERIOD + root + node.getName());
IExpressionNode vnode = node.getAssignedValueNode();
if (vnode != null)
{
- write(" = ");
+ write(SPACE);
+ write(EQUALS);
+ write(SPACE);
getWalker().walk(vnode);
}
@@ -273,8 +304,9 @@ public class JSGoogEmitter extends JSEmi
IASNode child = node.getChild(i);
if (child instanceof ChainedVariableNode)
{
- write(";");
- write("\n\n");
+ write(SEMICOLON);
+ writeNewline();
+ writeNewline();
emitField((IVariableNode) child);
}
}
@@ -314,8 +346,8 @@ public class JSGoogEmitter extends JSEmi
IASNode child = node.getChild(i);
if (child instanceof ChainedVariableNode)
{
- write(",");
- write(" ");
+ write(COMMA);
+ write(SPACE);
emitVarDeclaration((IVariableNode) child);
}
}
@@ -352,11 +384,11 @@ public class JSGoogEmitter extends JSEmi
write(qname);
if (!isConstructor)
{
- write(".");
+ write(PERIOD);
if (!fn.hasModifier(ASModifier.STATIC))
{
- write("prototype");
- write(".");
+ write(PROTOTYPE);
+ write(PERIOD);
}
}
}
@@ -364,10 +396,10 @@ public class JSGoogEmitter extends JSEmi
if (!isConstructor)
emitMemberName(node);
- write(" ");
- write("=");
- write(" ");
- write("function");
+ write(SPACE);
+ write(EQUALS);
+ write(SPACE);
+ write(FUNCTION);
emitParamters(node.getParameterNodes());
@@ -376,18 +408,29 @@ public class JSGoogEmitter extends JSEmi
boolean hasSuperClass = hasSuperClass(node, project);
if (node.getScopedNode().getChildCount() > 0 || hasSuperClass)
+ {
emitMethodScope(node.getScopedNode());
+ }
else
- write(" {\n}");
+ {
+ write(SPACE);
+ write(CURLYBRACE_OPEN);
+ writeNewline();
+ write(CURLYBRACE_CLOSE);
+ }
if (hasSuperClass)
{
- write("\ngoog.inherits(");
+ /* \ngoog.inherits(x, y) */
+ writeNewline();
+ write(GOOG_INHERITS);
+ write(PARENTHESES_OPEN);
write(qname);
- write(", ");
+ write(COMMA);
+ write(SPACE);
String sname = getSuperClassDefinition(node, project).getQualifiedName();
write(sname);
- write(")");
+ write(PARENTHESES_CLOSE);
}
return;
@@ -414,10 +457,15 @@ public class JSGoogEmitter extends JSEmi
if (bnode != null && node.isConstructor())
{
if (!hasBody(node))
- write("\t");
+ write(INDENT);
// TODO (erikdebruin) handle arguments when calling super
- write("goog.base(this);\n");
+ write(GOOG_BASE);
+ write(PARENTHESES_OPEN);
+ write(IASKeywordConstants.THIS);
+ write(PARENTHESES_CLOSE);
+ write(SEMICOLON);
+ writeNewline();
}
}
@@ -429,40 +477,59 @@ public class JSGoogEmitter extends JSEmi
Map<Integer, IParameterNode> defaults = getDefaults(pnodes);
- final StringBuilder code = new StringBuilder();
-
if (defaults != null)
{
+ final StringBuilder code = new StringBuilder();
+
if (!hasBody(node))
{
indentPush();
- write("\t");
+ write(INDENT);
}
List<IParameterNode> parameters = new ArrayList<IParameterNode>(
defaults.values());
- int numDefaults = 0;
- for (IParameterNode pnode : parameters)
+ for (int i = 0, n = parameters.size(); i < n; i++)
{
+ IParameterNode pnode = parameters.get(i);
+
if (pnode != null)
{
- if (numDefaults > 0)
- code.append(getIndent(getCurrentIndent()));
-
- code.append(pnode.getName() + " = typeof "
- + pnode.getName() + " !== 'undefined' ? "
- + pnode.getName() + " : " + pnode.getDefaultValue()
- + ";\n");
-
- numDefaults++;
+ code.setLength(0);
+
+ /* x = typeof y !== 'undefined' ? y : z;\n */
+ code.append(pnode.getName());
+ code.append(SPACE);
+ code.append(EQUALS);
+ code.append(SPACE);
+ code.append(IASKeywordConstants.TYPEOF);
+ code.append(SPACE);
+ code.append(pnode.getName());
+ code.append(SPACE);
+ code.append(ASTNodeID.Op_StrictNotEqualID.getParaphrase());
+ code.append(SPACE);
+ code.append(SINGLE_QUOTE);
+ code.append(IASLanguageConstants.UNDEFINED);
+ code.append(SINGLE_QUOTE);
+ code.append(SPACE);
+ code.append(ASTNodeID.TernaryExpressionID.getParaphrase());
+ code.append(SPACE);
+ code.append(pnode.getName());
+ code.append(SPACE);
+ code.append(COLON);
+ code.append(SPACE);
+ code.append(pnode.getDefaultValue());
+ code.append(SEMICOLON);
+
+ write(code.toString());
+
+ if (i == n - 1 && !hasBody(node))
+ indentPop();
+
+ writeNewline();
}
}
-
- if (!hasBody(node))
- indentPop();
-
- write(code.toString());
}
}
@@ -475,18 +542,35 @@ public class JSGoogEmitter extends JSEmi
{
final StringBuilder code = new StringBuilder();
- code.append(rest.getName() + " = "
- + "Array.prototype.slice.call(arguments, "
- + (pnodes.length - 1) + ");\n");
+ /* x = Array.prototype.slice.call(arguments, y);\n */
+ code.append(rest.getName());
+ code.append(SPACE);
+ code.append(EQUALS);
+ code.append(SPACE);
+ code.append(IASLanguageConstants.Array);
+ code.append(PERIOD);
+ code.append(PROTOTYPE);
+ code.append(PERIOD);
+ code.append(SLICE);
+ code.append(PERIOD);
+ code.append(CALL);
+ code.append(PARENTHESES_OPEN);
+ code.append(IASLanguageConstants.arguments);
+ code.append(COMMA);
+ code.append(SPACE);
+ code.append(String.valueOf(pnodes.length - 1));
+ code.append(PARENTHESES_CLOSE);
+ code.append(SEMICOLON);
write(code.toString());
+
+ writeNewline();
}
}
@Override
public void emitParameter(IParameterNode node)
{
- // only the name gets output in JS
getWalker().walk(node.getNameExpressionNode());
}
@@ -554,7 +638,7 @@ public class JSGoogEmitter extends JSEmi
{
IClassDefinition superClassDefinition = getSuperClassDefinition(node, project);
String qname = superClassDefinition.getQualifiedName();
- return superClassDefinition != null && !qname.equals("Object");
+ return superClassDefinition != null && !qname.equals(IASLanguageConstants.Object);
}
private static boolean hasBody(IFunctionNode node)
@@ -574,44 +658,55 @@ public class JSGoogEmitter extends JSEmi
);
*/
// head
- write("Object.defineProperty(");
+ write(IASLanguageConstants.Object);
+ write(PERIOD);
+ write(DEFINE_PROPERTY);
+ write(PARENTHESES_OPEN);
indentPush();
- write("\n");
+ writeNewline();
// Type
IFunctionDefinition definition = node.getDefinition();
ITypeDefinition type = (ITypeDefinition) definition.getParent();
write(type.getQualifiedName());
- write(".");
- write("prototype");
- write(", ");
- write("\n");
+ write(PERIOD);
+ write(PROTOTYPE);
+ write(COMMA);
+ write(SPACE);
+ writeNewline();
// name
- write("'");
+ write(SINGLE_QUOTE);
write(definition.getBaseName());
- write("'");
- write(", ");
- write("\n");
+ write(SINGLE_QUOTE);
+ write(COMMA);
+ write(SPACE);
+ writeNewline();
// info object
// declaration
- write("{");
- write(node.getNodeID() == ASTNodeID.GetterID ? "get" : "set");
- write(":");
- write("function");
+ write(CURLYBRACE_OPEN);
+ write(node.getNodeID() ==
+ ASTNodeID.GetterID ?
+ IASKeywordConstants.GET :
+ IASKeywordConstants.SET);
+ write(COLON);
+ write(FUNCTION);
emitParamters(node.getParameterNodes());
emitMethodScope(node.getScopedNode());
- write(", ");
- write("configurable:true");
- write("}");
+ write(COMMA);
+ write(SPACE);
+ write(CONFIGURABLE);
+ write(COLON);
+ write(IASKeywordConstants.TRUE);
+ write(CURLYBRACE_CLOSE);
indentPop();
- write("\n");
+ writeNewline();
// tail, no colon; parent container will add it
- write(")");
+ write(PARENTHESES_CLOSE);
}
//--------------------------------------------------------------------------
@@ -627,36 +722,40 @@ public class JSGoogEmitter extends JSEmi
{
// TODO (erikdebruin) replace: this is a placeholder for the
// eventual implementation
- write((id == ASTNodeID.Op_AsID) ? "as(" : "is(");
+ write((id == ASTNodeID.Op_AsID) ? "as" : "is");
+ write(PARENTHESES_OPEN);
getWalker().walk(node.getLeftOperandNode());
- write(", ");
+ write(COMMA);
+ write(SPACE);
getWalker().walk(node.getRightOperandNode());
- write(")");
+ write(PARENTHESES_CLOSE);
}
else
{
getWalker().walk(node.getLeftOperandNode());
if (id != ASTNodeID.Op_CommaID)
- write(" ");
+ write(SPACE);
// (erikdebruin) rewrite 'a &&= b' to 'a = a && b'
if (id == ASTNodeID.Op_LogicalAndAssignID || id == ASTNodeID.Op_LogicalOrAssignID)
{
IIdentifierNode lnode = (IIdentifierNode) node.getLeftOperandNode();
- write("=");
- write(" ");
+ write(EQUALS);
+ write(SPACE);
write(lnode.getName());
- write(" ");
- write((id == ASTNodeID.Op_LogicalAndAssignID) ? ASTNodeID.Op_LogicalAndID.getParaphrase() : ASTNodeID.Op_LogicalOrID.getParaphrase());
+ write(SPACE);
+ write((id == ASTNodeID.Op_LogicalAndAssignID) ?
+ ASTNodeID.Op_LogicalAndID.getParaphrase() :
+ ASTNodeID.Op_LogicalOrID.getParaphrase());
}
else
{
write(node.getOperator().getOperatorText());
}
- write(" ");
+ write(SPACE);
getWalker().walk(node.getRightOperandNode());
}