From 720a8f250b8278fb604c2e6fecc33f9f16481dcb Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 17:18:51 +0400 Subject: [PATCH] KT-2864 --- .../js/JsToStringGenerationVisitor.java | 87 +- .../compiler/backend/js/ast/JsDocComment.java | 48 +- .../compiler/backend/js/ast/JsVisitor.java | 770 +++++++++--------- 3 files changed, 500 insertions(+), 405 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 49b0c2bd610..62905cb51db 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -222,11 +222,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { } private void printExpressions(List expressions) { - boolean sep = false; - for (JsExpression arg : expressions) { - sep = sepCommaOptSpace(sep); - boolean isEnclosed = parenPushIfCommaExpression(arg); - accept(arg); + boolean notFirst = false; + for (JsExpression expression : expressions) { + notFirst = sepCommaOptSpace(notFirst) && !(expression instanceof JsDocComment); + boolean isEnclosed = parenPushIfCommaExpression(expression); + accept(expression); if (isEnclosed) { rightParen(); } @@ -556,10 +556,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { } leftParen(); - boolean sep = false; + boolean notFirst = false; for (Object element : x.getParameters()) { JsParameter param = (JsParameter) element; - sep = sepCommaOptSpace(sep); + notFirst = sepCommaOptSpace(notFirst); accept(param); } rightParen(); @@ -610,8 +610,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsInvocation x, JsContext ctx) { - JsExpression qualifier = x.getQualifier(); - printPair(x, qualifier); + printPair(x, x.getQualifier()); leftParen(); printExpressions(x.getArguments()); @@ -697,20 +696,20 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.indentIn(); } - boolean isNotFirst = false; + boolean notFirst = false; for (JsPropertyInitializer item : objectLiteral.getPropertyInitializers()) { - if (isNotFirst) { + if (notFirst) { p.print(','); } if (objectLiteral.isMultiline()) { newlineOpt(); } - else if (isNotFirst) { + else if (notFirst) { spaceOpt(); } - isNotFirst = true; + notFirst = true; JsExpression labelExpr = item.getLabelExpr(); // labels can be either string, integral, or decimal literals @@ -907,7 +906,65 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } - protected void _newline() { + @Override + public boolean visit(JsDocComment comment, JsContext context) { + boolean asSingleLine = comment.getTags().size() == 1; + if (!asSingleLine) { + newlineOpt(); + } + p.print("/**"); + if (asSingleLine) { + space(); + } + else { + newline(); + } + + boolean notFirst = false; + for (Map.Entry entry : comment.getTags().entrySet()) { + if (notFirst) { + newline(); + p.print(' '); + p.print('*'); + } + else { + notFirst = true; + } + + p.print('@'); + p.print(entry.getKey()); + Object value = entry.getValue(); + if (value != null) { + space(); + if (value instanceof CharSequence) { + p.print((CharSequence) value); + } + else { + visit((JsNameRef) value, context); + } + } + + if (!asSingleLine) { + newline(); + } + } + + if (asSingleLine) { + space(); + } + else { + newlineOpt(); + } + + p.print('*'); + p.print('/'); + if (asSingleLine) { + spaceOpt(); + } + return false; + } + + protected void newline() { p.newline(); } @@ -977,7 +1034,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } else { - _newline(); + newline(); } } else { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index e66ea3eb2f0..ffc8867edae 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -1,13 +1,49 @@ package com.google.dart.compiler.backend.js.ast; -public class JsDocComment extends JsNodeImpl implements JsStatement { - @Override - public NodeKind getKind() { - return null; //To change body of implemented methods use File | Settings | File Templates. +import java.util.Collections; +import java.util.Map; + +public class JsDocComment extends JsExpressionImpl implements JsExpression { + private final Map tags; + + public JsDocComment(Map tags) { + this.tags = tags; + } + + public Map getTags() { + return tags; + } + + public JsDocComment(String tagName, JsNameRef tagValue) { + tags = Collections.singletonMap(tagName, tagValue); + } + + public JsDocComment(String tagName, String tagValue) { + tags = Collections.singletonMap(tagName, tagValue); } @Override - public void traverse(JsVisitor visitor, JsContext context) { - //To change body of implemented methods use File | Settings | File Templates. + public NodeKind getKind() { + throw new UnsupportedOperationException(); + } + + @Override + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public boolean hasSideEffects() { + return false; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 814ae92385b..a7575cf0766 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -12,420 +12,422 @@ import java.util.List; * Implemented by nodes that will visit child nodes. */ public class JsVisitor { + protected static final JsContext LVALUE_CONTEXT = new JsContext() { + @Override + public boolean canInsert() { + return false; + } - protected static final JsContext LVALUE_CONTEXT = new JsContext() { + @Override + public boolean canRemove() { + return false; + } - @Override - public boolean canInsert() { - return false; + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return true; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { + + @Override + public boolean canInsert() { + return false; + } + + @Override + public boolean canRemove() { + return false; + } + + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return false; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + public final T accept(T node) { + return doAccept(node); } - @Override - public boolean canRemove() { - return false; + public final void acceptList(List collection) { + doAcceptList(collection); } - @Override - public void insertAfter(JsVisitable node) { - throw new UnsupportedOperationException(); + public JsExpression acceptLvalue(JsExpression expr) { + return doAcceptLvalue(expr); } - @Override - public void insertBefore(JsVisitable node) { - throw new UnsupportedOperationException(); + public final void acceptWithInsertRemove(List collection) { + doAcceptWithInsertRemove(collection); } - @Override - public boolean isLvalue() { - return true; + public void endVisit(JsArrayAccess x, JsContext ctx) { } - @Override - public void removeMe() { - throw new UnsupportedOperationException(); + public void endVisit(JsArrayLiteral x, JsContext ctx) { } - @Override - public void replaceMe(JsVisitable node) { - throw new UnsupportedOperationException(); - } - }; - - protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { - - @Override - public boolean canInsert() { - return false; + public void endVisit(JsBinaryOperation x, JsContext ctx) { } - @Override - public boolean canRemove() { - return false; + public void endVisit(JsBlock x, JsContext ctx) { } - @Override - public void insertAfter(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { } - @Override - public void insertBefore(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsBreak x, JsContext ctx) { } - @Override - public boolean isLvalue() { - return false; + public void endVisit(JsCase x, JsContext ctx) { } - @Override - public void removeMe() { - throw new UnsupportedOperationException(); + public void endVisit(JsCatch x, JsContext ctx) { } - @Override - public void replaceMe(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsConditional x, JsContext ctx) { } - }; - public final T accept(T node) { - return doAccept(node); - } - - public final void acceptList(List collection) { - doAcceptList(collection); - } - - public JsExpression acceptLvalue(JsExpression expr) { - return doAcceptLvalue(expr); - } - - public final void acceptWithInsertRemove(List collection) { - doAcceptWithInsertRemove(collection); - } - - public void endVisit(JsArrayAccess x, JsContext ctx) { - } - - public void endVisit(JsArrayLiteral x, JsContext ctx) { - } - - public void endVisit(JsBinaryOperation x, JsContext ctx) { - } - - public void endVisit(JsBlock x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - } - - public void endVisit(JsBreak x, JsContext ctx) { - } - - public void endVisit(JsCase x, JsContext ctx) { - } - - public void endVisit(JsCatch x, JsContext ctx) { - } - - public void endVisit(JsConditional x, JsContext ctx) { - } - - public void endVisit(JsContinue x, JsContext ctx) { - } - - public void endVisit(JsDebugger x, JsContext ctx) { - } - - public void endVisit(JsDefault x, JsContext ctx) { - } - - public void endVisit(JsDoWhile x, JsContext ctx) { - } - - public void endVisit(JsEmpty x, JsContext ctx) { - } - - public void endVisit(JsExprStmt x, JsContext ctx) { - } - - public void endVisit(JsFor x, JsContext ctx) { - } - - public void endVisit(JsForIn x, JsContext ctx) { - } - - public void endVisit(JsFunction x, JsContext ctx) { - } - - public void endVisit(JsIf x, JsContext ctx) { - } - - public void endVisit(JsInvocation x, JsContext ctx) { - } - - public void endVisit(JsLabel x, JsContext ctx) { - } - - public void endVisit(JsNameRef x, JsContext ctx) { - } - - public void endVisit(JsNew x, JsContext ctx) { - } - - public void endVisit(JsNullLiteral x, JsContext ctx) { - } - - public void endVisit(JsNumberLiteral x, JsContext ctx) { - } - - public void endVisit(JsObjectLiteral x, JsContext ctx) { - } - - public void endVisit(JsParameter x, JsContext ctx) { - } - - public void endVisit(JsPostfixOperation x, JsContext ctx) { - } - - public void endVisit(JsPrefixOperation x, JsContext ctx) { - } - - public void endVisit(JsProgram x, JsContext ctx) { - } - - public void endVisit(JsProgramFragment x, JsContext ctx) { - } - - public void endVisit(JsPropertyInitializer x, JsContext ctx) { - } - - public void endVisit(JsRegExp x, JsContext ctx) { - } - - public void endVisit(JsReturn x, JsContext ctx) { - } - - public void endVisit(JsStringLiteral x, JsContext ctx) { - } - - public void endVisit(JsSwitch x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { - } - - public void endVisit(JsThrow x, JsContext ctx) { - } - - public void endVisit(JsTry x, JsContext ctx) { - } - - public void endVisit(JsVar x, JsContext ctx) { - } - - public void endVisit(JsVars x, JsContext ctx) { - } - - public void endVisit(JsWhile x, JsContext ctx) { - } - - public boolean visit(JsArrayAccess x, JsContext ctx) { - return true; - } - - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsBinaryOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsBlock x, JsContext ctx) { - return true; - } - - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsBreak x, JsContext ctx) { - return true; - } - - public boolean visit(JsCase x, JsContext ctx) { - return true; - } - - public boolean visit(JsCatch x, JsContext ctx) { - return true; - } - - public boolean visit(JsConditional x, JsContext ctx) { - return true; - } - - public boolean visit(JsContinue x, JsContext ctx) { - return true; - } - - public boolean visit(JsDebugger x, JsContext ctx) { - return true; - } - - public boolean visit(JsDefault x, JsContext ctx) { - return true; - } - - public boolean visit(JsDoWhile x, JsContext ctx) { - return true; - } - - public boolean visit(JsEmpty x, JsContext ctx) { - return true; - } - - public boolean visit(JsExprStmt x, JsContext ctx) { - return true; - } - - public boolean visit(JsFor x, JsContext ctx) { - return true; - } - - public boolean visit(JsForIn x, JsContext ctx) { - return true; - } - - public boolean visit(JsFunction x, JsContext ctx) { - return true; - } - - public boolean visit(JsIf x, JsContext ctx) { - return true; - } - - public boolean visit(JsInvocation x, JsContext ctx) { - return true; - } - - public boolean visit(JsLabel x, JsContext ctx) { - return true; - } - - public boolean visit(JsNameRef x, JsContext ctx) { - return true; - } - - public boolean visit(JsNew x, JsContext ctx) { - return true; - } - - public boolean visit(JsNullLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsObjectLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsParameter x, JsContext ctx) { - return true; - } - - public boolean visit(JsPostfixOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsPrefixOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsProgram x, JsContext ctx) { - return true; - } - - public boolean visit(JsProgramFragment x, JsContext ctx) { - return true; - } - - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - return true; - } - - public boolean visit(JsRegExp x, JsContext ctx) { - return true; - } - - public boolean visit(JsReturn x, JsContext ctx) { - return true; - } - - public boolean visit(JsStringLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsSwitch x, JsContext ctx) { - return true; - } - - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { - return true; - } - - public boolean visit(JsThrow x, JsContext ctx) { - return true; - } - - public boolean visit(JsTry x, JsContext ctx) { - return true; - } - - public boolean visit(JsVar x, JsContext ctx) { - return true; - } - - public boolean visit(JsVars x, JsContext ctx) { - return true; - } - - public boolean visit(JsWhile x, JsContext ctx) { - return true; - } - - protected T doAccept(T node) { - doTraverse(node, UNMODIFIABLE_CONTEXT); - return node; - } - - protected void doAcceptList(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + public void endVisit(JsContinue x, JsContext ctx) { } - } - protected JsExpression doAcceptLvalue(JsExpression expr) { - doTraverse(expr, LVALUE_CONTEXT); - return expr; - } - - protected void doAcceptWithInsertRemove(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + public void endVisit(JsDebugger x, JsContext ctx) { } - } - protected void doTraverse(JsVisitable node, JsContext ctx) { - node.traverse(this, ctx); - } + public void endVisit(JsDefault x, JsContext ctx) { + } + + public void endVisit(JsDoWhile x, JsContext ctx) { + } + + public void endVisit(JsEmpty x, JsContext ctx) { + } + + public void endVisit(JsExprStmt x, JsContext ctx) { + } + + public void endVisit(JsFor x, JsContext ctx) { + } + + public void endVisit(JsForIn x, JsContext ctx) { + } + + public void endVisit(JsFunction x, JsContext ctx) { + } + + public void endVisit(JsIf x, JsContext ctx) { + } + + public void endVisit(JsInvocation x, JsContext ctx) { + } + + public void endVisit(JsLabel x, JsContext ctx) { + } + + public void endVisit(JsNameRef x, JsContext ctx) { + } + + public void endVisit(JsNew x, JsContext ctx) { + } + + public void endVisit(JsNullLiteral x, JsContext ctx) { + } + + public void endVisit(JsNumberLiteral x, JsContext ctx) { + } + + public void endVisit(JsObjectLiteral x, JsContext ctx) { + } + + public void endVisit(JsParameter x, JsContext ctx) { + } + + public void endVisit(JsPostfixOperation x, JsContext ctx) { + } + + public void endVisit(JsPrefixOperation x, JsContext ctx) { + } + + public void endVisit(JsProgram x, JsContext ctx) { + } + + public void endVisit(JsProgramFragment x, JsContext ctx) { + } + + public void endVisit(JsPropertyInitializer x, JsContext ctx) { + } + + public void endVisit(JsRegExp x, JsContext ctx) { + } + + public void endVisit(JsReturn x, JsContext ctx) { + } + + public void endVisit(JsStringLiteral x, JsContext ctx) { + } + + public void endVisit(JsSwitch x, JsContext ctx) { + } + + public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { + } + + public void endVisit(JsThrow x, JsContext ctx) { + } + + public void endVisit(JsTry x, JsContext ctx) { + } + + public void endVisit(JsVar x, JsContext ctx) { + } + + public void endVisit(JsVars x, JsContext ctx) { + } + + public void endVisit(JsWhile x, JsContext ctx) { + } + + public boolean visit(JsArrayAccess x, JsContext ctx) { + return true; + } + + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBinaryOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsBlock x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBreak x, JsContext ctx) { + return true; + } + + public boolean visit(JsCase x, JsContext ctx) { + return true; + } + + public boolean visit(JsCatch x, JsContext ctx) { + return true; + } + + public boolean visit(JsConditional x, JsContext ctx) { + return true; + } + + public boolean visit(JsContinue x, JsContext ctx) { + return true; + } + + public boolean visit(JsDebugger x, JsContext ctx) { + return true; + } + + public boolean visit(JsDefault x, JsContext ctx) { + return true; + } + + public boolean visit(JsDoWhile x, JsContext ctx) { + return true; + } + + public boolean visit(JsEmpty x, JsContext ctx) { + return true; + } + + public boolean visit(JsExprStmt x, JsContext ctx) { + return true; + } + + public boolean visit(JsFor x, JsContext ctx) { + return true; + } + + public boolean visit(JsForIn x, JsContext ctx) { + return true; + } + + public boolean visit(JsFunction x, JsContext ctx) { + return true; + } + + public boolean visit(JsIf x, JsContext ctx) { + return true; + } + + public boolean visit(JsInvocation x, JsContext ctx) { + return true; + } + + public boolean visit(JsLabel x, JsContext ctx) { + return true; + } + + public boolean visit(JsNameRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsNew x, JsContext ctx) { + return true; + } + + public boolean visit(JsNullLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsObjectLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsParameter x, JsContext ctx) { + return true; + } + + public boolean visit(JsPostfixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsPrefixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgram x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgramFragment x, JsContext ctx) { + return true; + } + + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + return true; + } + + public boolean visit(JsRegExp x, JsContext ctx) { + return true; + } + + public boolean visit(JsReturn x, JsContext ctx) { + return true; + } + + public boolean visit(JsStringLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsSwitch x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsThrow x, JsContext ctx) { + return true; + } + + public boolean visit(JsTry x, JsContext ctx) { + return true; + } + + public boolean visit(JsVar x, JsContext ctx) { + return true; + } + + public boolean visit(JsVars x, JsContext ctx) { + return true; + } + + public boolean visit(JsWhile x, JsContext ctx) { + return true; + } + + public boolean visit(JsDocComment x, JsContext context) { + return true; + } + + protected T doAccept(T node) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + return node; + } + + protected void doAcceptList(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected JsExpression doAcceptLvalue(JsExpression expr) { + doTraverse(expr, LVALUE_CONTEXT); + return expr; + } + + protected void doAcceptWithInsertRemove(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected void doTraverse(JsVisitable node, JsContext ctx) { + node.traverse(this, ctx); + } }