0001: //
0002: // Generated by JTB 1.2.1
0003: //
0004: package oscript.visitor;
0005:
0006: import oscript.syntaxtree.*;
0007: import java.util.*;
0008:
0009: /**
0010: * A skeleton output formatter for your language grammar. Using the
0011: * add() method along with force(), indent(), and outdent(), you can
0012: * easily specify how this visitor will format the given syntax tree.
0013: * See the JTB documentation for more details.
0014: *
0015: * Pass your syntax tree to this visitor, and then to the TreeDumper
0016: * visitor in order to "pretty print" your tree.
0017: */
0018: public class TreeFormatter extends DepthFirstVisitor {
0019: private Vector cmdQueue = new Vector();
0020: private boolean lineWrap;
0021: private int wrapWidth;
0022: private int indentAmt;
0023: private int curLine = 1;
0024: private int curColumn = 1;
0025: private int curIndent = 0;
0026:
0027: /**
0028: * The default constructor assumes an indentation amount of 3 spaces
0029: * and no line-wrap. You may alternately use the other constructor to
0030: * specify your own indentation amount and line width.
0031: */
0032: public TreeFormatter() {
0033: this (3, 0);
0034: }
0035:
0036: /**
0037: * This constructor accepts an indent amount and a line width which is
0038: * used to wrap long lines. If a token's beginColumn value is greater
0039: * than the specified wrapWidth, it will be moved to the next line and
0040: * indented one extra level. To turn off line-wrapping, specify a
0041: * wrapWidth of 0.
0042: *
0043: * @param indentAmt Amount of spaces per indentation level.
0044: * @param wrapWidth Wrap lines longer than wrapWidth. 0 for no wrap.
0045: */
0046: public TreeFormatter(int indentAmt, int wrapWidth) {
0047: this .indentAmt = indentAmt;
0048: this .wrapWidth = wrapWidth;
0049:
0050: if (wrapWidth > 0)
0051: lineWrap = true;
0052: else
0053: lineWrap = false;
0054: }
0055:
0056: /**
0057: * Accepts a NodeListInterface object and performs an optional format
0058: * command between each node in the list (but not after the last node).
0059: */
0060: protected void processList(NodeListInterface n) {
0061: processList(n, null);
0062: }
0063:
0064: protected void processList(NodeListInterface n, FormatCommand cmd) {
0065: for (Enumeration e = n.elements(); e.hasMoreElements();) {
0066: ((Node) e.nextElement()).accept(this );
0067: if (cmd != null && e.hasMoreElements())
0068: cmdQueue.addElement(cmd);
0069: }
0070: }
0071:
0072: /**
0073: * A Force command inserts a line break and indents the next line to
0074: * the current indentation level. Use "add(force());".
0075: */
0076: protected FormatCommand force() {
0077: return force(1);
0078: }
0079:
0080: protected FormatCommand force(int i) {
0081: return new FormatCommand(FormatCommand.FORCE, i);
0082: }
0083:
0084: /**
0085: * An Indent command increases the indentation level by one (or a
0086: * user-specified amount). Use "add(indent());".
0087: */
0088: protected FormatCommand indent() {
0089: return indent(1);
0090: }
0091:
0092: protected FormatCommand indent(int i) {
0093: return new FormatCommand(FormatCommand.INDENT, i);
0094: }
0095:
0096: /**
0097: * An Outdent command is the reverse of the Indent command: it reduces
0098: * the indentation level. Use "add(outdent());".
0099: */
0100: protected FormatCommand outdent() {
0101: return outdent(1);
0102: }
0103:
0104: protected FormatCommand outdent(int i) {
0105: return new FormatCommand(FormatCommand.OUTDENT, i);
0106: }
0107:
0108: /**
0109: * A Space command simply adds one or a user-specified number of
0110: * spaces between tokens. Use "add(space());".
0111: */
0112: protected FormatCommand space() {
0113: return space(1);
0114: }
0115:
0116: protected FormatCommand space(int i) {
0117: return new FormatCommand(FormatCommand.SPACE, i);
0118: }
0119:
0120: /**
0121: * Use this method to add FormatCommands to the command queue to be
0122: * executed when the next token in the tree is visited.
0123: */
0124: protected void add(FormatCommand cmd) {
0125: cmdQueue.addElement(cmd);
0126: }
0127:
0128: /**
0129: * Executes the commands waiting in the command queue, then inserts the
0130: * proper location information into the current NodeToken.
0131: *
0132: * If there are any special tokens preceding this token, they will be
0133: * given the current location information. The token will follow on
0134: * the next line, at the proper indentation level. If this is not the
0135: * behavior you want from special tokens, feel free to modify this
0136: * method.
0137: */
0138: public void visit(NodeToken n) {
0139: for (Enumeration e = cmdQueue.elements(); e.hasMoreElements();) {
0140: FormatCommand cmd = (FormatCommand) e.nextElement();
0141: switch (cmd.getCommand()) {
0142: case FormatCommand.FORCE:
0143: curLine += cmd.getNumCommands();
0144: curColumn = curIndent + 1;
0145: break;
0146: case FormatCommand.INDENT:
0147: curIndent += indentAmt * cmd.getNumCommands();
0148: break;
0149: case FormatCommand.OUTDENT:
0150: if (curIndent >= indentAmt)
0151: curIndent -= indentAmt * cmd.getNumCommands();
0152: break;
0153: case FormatCommand.SPACE:
0154: curColumn += cmd.getNumCommands();
0155: break;
0156: default:
0157: throw new TreeFormatterException(
0158: "Invalid value in command queue.");
0159: }
0160: }
0161:
0162: cmdQueue.removeAllElements();
0163:
0164: //
0165: // Handle all special tokens preceding this NodeToken
0166: //
0167: if (n.numSpecials() > 0)
0168: for (Enumeration e = n.specialTokens.elements(); e
0169: .hasMoreElements();) {
0170: NodeToken special = (NodeToken) e.nextElement();
0171:
0172: //
0173: // -Place the token.
0174: // -Move cursor to next line after the special token.
0175: // -Don't update curColumn--want to keep current indent level.
0176: //
0177: placeToken(special, curLine, curColumn);
0178: curLine = special.endLine + 1;
0179: }
0180:
0181: placeToken(n, curLine, curColumn);
0182: curLine = n.endLine;
0183: curColumn = n.endColumn;
0184: }
0185:
0186: /**
0187: * Inserts token location (beginLine, beginColumn, endLine, endColumn)
0188: * information into the NodeToken. Takes into account line-wrap.
0189: * Does not update curLine and curColumn.
0190: */
0191: private void placeToken(NodeToken n, int line, int column) {
0192: int length = n.tokenImage.length();
0193:
0194: //
0195: // Find beginning of token. Only line-wrap for single-line tokens
0196: //
0197: if (!lineWrap || n.tokenImage.indexOf('\n') != -1
0198: || column + length <= wrapWidth)
0199: n.beginColumn = column;
0200: else {
0201: ++line;
0202: column = curIndent + indentAmt + 1;
0203: n.beginColumn = column;
0204: }
0205:
0206: n.beginLine = line;
0207:
0208: //
0209: // Find end of token; don't count \n if it's the last character
0210: //
0211: for (int i = 0; i < length; ++i) {
0212: if (n.tokenImage.charAt(i) == '\n' && i < length - 1) {
0213: ++line;
0214: column = 1;
0215: } else
0216: ++column;
0217: }
0218:
0219: n.endLine = line;
0220: n.endColumn = column;
0221: }
0222:
0223: //
0224: // User-generated visitor methods below
0225: //
0226:
0227: /**
0228: * <PRE>
0229: * f0 -> ( <UNIX_SELF_EXECUTABLE_COMMENT> )?
0230: * f1 -> Program(false)
0231: * f2 -> <EOF>
0232: * </PRE>
0233: */
0234: public void visit(ProgramFile n) {
0235: if (n.f0.present()) {
0236: n.f0.accept(this );
0237: }
0238: n.f1.accept(this );
0239: n.f2.accept(this );
0240: }
0241:
0242: /**
0243: * <PRE>
0244: * f0 -> ( EvaluationUnit() )*
0245: * </PRE>
0246: */
0247: public void visit(Program n) {
0248: if (n.f0.present()) {
0249: processList(n.f0);
0250: }
0251: }
0252:
0253: /**
0254: * <PRE>
0255: * f0 -> ScopeBlock()
0256: * | VariableDeclarationBlock()
0257: * | FunctionDeclaration()
0258: * | TryStatement()
0259: * | ForLoopStatement()
0260: * | CollectionForLoopStatement()
0261: * | WhileLoopStatement()
0262: * | ConditionalStatement()
0263: * | SynchronizedStatement()
0264: * | ReturnStatement()
0265: * | BreakStatement()
0266: * | ContinueStatement()
0267: * | ExpressionBlock()
0268: * | ThrowBlock()
0269: * | ImportBlock()
0270: * | MixinBlock()
0271: * | EvalBlock()
0272: * </PRE>
0273: */
0274: public void visit(EvaluationUnit n) {
0275: n.f0.accept(this );
0276: }
0277:
0278: /**
0279: * <PRE>
0280: * f0 -> "{"
0281: * f1 -> Program(false)
0282: * f2 -> "}"
0283: * </PRE>
0284: */
0285: public void visit(ScopeBlock n) {
0286: n.f0.accept(this );
0287: n.f1.accept(this );
0288: n.f2.accept(this );
0289: }
0290:
0291: /**
0292: * <PRE>
0293: * f0 -> VariableDeclaration()
0294: * f1 -> ";"
0295: * </PRE>
0296: */
0297: public void visit(VariableDeclarationBlock n) {
0298: n.f0.accept(this );
0299: n.f1.accept(this );
0300: }
0301:
0302: /**
0303: * <PRE>
0304: * f0 -> Expression()
0305: * f1 -> ";"
0306: * </PRE>
0307: */
0308: public void visit(ExpressionBlock n) {
0309: n.f0.accept(this );
0310: n.f1.accept(this );
0311: }
0312:
0313: /**
0314: * <PRE>
0315: * f0 -> "throw"
0316: * f1 -> Expression()
0317: * f2 -> ";"
0318: * </PRE>
0319: */
0320: public void visit(ThrowBlock n) {
0321: n.f0.accept(this );
0322: n.f1.accept(this );
0323: n.f2.accept(this );
0324: }
0325:
0326: /**
0327: * <PRE>
0328:
0329: * f0 -> "import"
0330: * f1 -> Expression()
0331: * f2 -> ";"
0332: * </PRE>
0333: */
0334: public void visit(ImportBlock n) {
0335: n.f0.accept(this );
0336: n.f1.accept(this );
0337: n.f2.accept(this );
0338: }
0339:
0340: /**
0341: * <PRE>
0342:
0343: * f0 -> "mixin"
0344: * f1 -> Expression()
0345: * f2 -> ";"
0346: * </PRE>
0347: */
0348: public void visit(MixinBlock n) {
0349: n.f0.accept(this );
0350: n.f1.accept(this );
0351: n.f2.accept(this );
0352: }
0353:
0354: /**
0355: * <PRE>
0356:
0357: * f0 -> "eval"
0358: * f1 -> Expression()
0359: * f2 -> ";"
0360: * </PRE>
0361: */
0362: public void visit(EvalBlock n) {
0363: n.f0.accept(this );
0364: n.f1.accept(this );
0365: n.f2.accept(this );
0366: }
0367:
0368: /**
0369: * <PRE>
0370:
0371: * f0 -> Permissions(true)
0372: * f1 -> "var"
0373: * f2 -> <IDENTIFIER>
0374: * f3 -> ( "=" Expression() )?
0375: * </PRE>
0376: */
0377: public void visit(VariableDeclaration n) {
0378: n.f0.accept(this );
0379: n.f1.accept(this );
0380: n.f2.accept(this );
0381: if (n.f3.present()) {
0382: n.f3.accept(this );
0383: }
0384: }
0385:
0386: /**
0387: * <PRE>
0388:
0389: * f0 -> Permissions(true)
0390: * f1 -> "function"
0391: * f2 -> <IDENTIFIER>
0392: * f3 -> "("
0393: * f4 -> ( Arglist() )?
0394: * f5 -> ")"
0395: * f6 -> ( "extends" PrimaryExpressionWithTrailingFxnCallExpList() FunctionCallExpressionList() )?
0396: * f7 -> "{"
0397: * f8 -> Program(true)
0398: * f9 -> "}"
0399: * </PRE>
0400: */
0401: public void visit(FunctionDeclaration n) {
0402: n.f0.accept(this );
0403: n.f1.accept(this );
0404: n.f2.accept(this );
0405: n.f3.accept(this );
0406: if (n.f4.present()) {
0407: n.f4.accept(this );
0408: }
0409: n.f5.accept(this );
0410: if (n.f6.present()) {
0411: n.f6.accept(this );
0412: }
0413: n.f7.accept(this );
0414: n.f8.accept(this );
0415: n.f9.accept(this );
0416: }
0417:
0418: /**
0419: * <PRE>
0420: * f0 -> Permissions(false)
0421: * f1 -> <IDENTIFIER>
0422: * f2 -> ( "," Permissions(false) <IDENTIFIER> )*
0423: * f3 -> ( "..." )?
0424: * </PRE>
0425: */
0426: public void visit(Arglist n) {
0427: n.f0.accept(this );
0428: n.f1.accept(this );
0429: if (n.f2.present()) {
0430: processList(n.f2);
0431: }
0432: if (n.f3.present()) {
0433: n.f3.accept(this );
0434: }
0435: }
0436:
0437: /**
0438: * <PRE>
0439: * f0 -> "try"
0440: * f1 -> EvaluationUnit()
0441: * f2 -> ( "catch" "(" Expression() <IDENTIFIER> ")" EvaluationUnit() )*
0442: * f3 -> ( "catch" "(" <IDENTIFIER> ")" EvaluationUnit() )?
0443: * f4 -> ( "finally" EvaluationUnit() )?
0444: * </PRE>
0445: */
0446: public void visit(TryStatement n) {
0447: n.f0.accept(this );
0448: n.f1.accept(this );
0449: if (n.f2.present()) {
0450: processList(n.f2);
0451: }
0452: if (n.f3.present()) {
0453: n.f3.accept(this );
0454: }
0455: if (n.f4.present()) {
0456: n.f4.accept(this );
0457: }
0458: }
0459:
0460: /**
0461: * <PRE>
0462:
0463: * f0 -> "for"
0464: * f1 -> "("
0465: * f2 -> ( PreLoopStatement() )?
0466: * f3 -> ";"
0467: * f4 -> ( Expression() )?
0468: * f5 -> ";"
0469: * f6 -> ( Expression() )?
0470: * f7 -> ")"
0471: * f8 -> EvaluationUnit()
0472: * </PRE>
0473: */
0474: public void visit(ForLoopStatement n) {
0475: n.f0.accept(this );
0476: n.f1.accept(this );
0477: if (n.f2.present()) {
0478: n.f2.accept(this );
0479: }
0480: n.f3.accept(this );
0481: if (n.f4.present()) {
0482: n.f4.accept(this );
0483: }
0484: n.f5.accept(this );
0485: if (n.f6.present()) {
0486: n.f6.accept(this );
0487: }
0488: n.f7.accept(this );
0489: n.f8.accept(this );
0490: }
0491:
0492: /**
0493: * <PRE>
0494:
0495: * f0 -> "for"
0496: * f1 -> "("
0497: * f2 -> PreLoopStatement()
0498: * f3 -> ":"
0499: * f4 -> Expression()
0500: * f5 -> ")"
0501: * f6 -> EvaluationUnit()
0502: * </PRE>
0503: */
0504: public void visit(CollectionForLoopStatement n) {
0505: n.f0.accept(this );
0506: n.f1.accept(this );
0507: n.f2.accept(this );
0508: n.f3.accept(this );
0509: n.f4.accept(this );
0510: n.f5.accept(this );
0511: n.f6.accept(this );
0512: }
0513:
0514: /**
0515: * <PRE>
0516: * f0 -> VariableDeclaration()
0517: * | Expression()
0518: * </PRE>
0519: */
0520: public void visit(PreLoopStatement n) {
0521: n.f0.accept(this );
0522: }
0523:
0524: /**
0525: * <PRE>
0526: * f0 -> "while"
0527: * f1 -> "("
0528: * f2 -> Expression()
0529: * f3 -> ")"
0530: * f4 -> EvaluationUnit()
0531: * </PRE>
0532: */
0533: public void visit(WhileLoopStatement n) {
0534: n.f0.accept(this );
0535: n.f1.accept(this );
0536: n.f2.accept(this );
0537: n.f3.accept(this );
0538: n.f4.accept(this );
0539: }
0540:
0541: /**
0542: * <PRE>
0543: * f0 -> "if"
0544: * f1 -> "("
0545: * f2 -> Expression()
0546: * f3 -> ")"
0547: * f4 -> EvaluationUnit()
0548: * f5 -> ( "else" EvaluationUnit() )?
0549: * </PRE>
0550: */
0551: public void visit(ConditionalStatement n) {
0552: n.f0.accept(this );
0553: n.f1.accept(this );
0554: n.f2.accept(this );
0555: n.f3.accept(this );
0556: n.f4.accept(this );
0557: if (n.f5.present()) {
0558: n.f5.accept(this );
0559: }
0560: }
0561:
0562: /**
0563: * <PRE>
0564: * f0 -> "synchronized"
0565: * f1 -> "("
0566: * f2 -> Expression()
0567: * f3 -> ")"
0568: * f4 -> EvaluationUnit()
0569: * </PRE>
0570: */
0571: public void visit(SynchronizedStatement n) {
0572: n.f0.accept(this );
0573: n.f1.accept(this );
0574: n.f2.accept(this );
0575: n.f3.accept(this );
0576: n.f4.accept(this );
0577: }
0578:
0579: /**
0580: * <PRE>
0581: * f0 -> "return"
0582: * f1 -> ( Expression() )?
0583: * f2 -> ";"
0584: * </PRE>
0585: */
0586: public void visit(ReturnStatement n) {
0587: n.f0.accept(this );
0588: if (n.f1.present()) {
0589: n.f1.accept(this );
0590: }
0591: n.f2.accept(this );
0592: }
0593:
0594: /**
0595: * <PRE>
0596: * f0 -> "break"
0597: * f1 -> ";"
0598: * </PRE>
0599: */
0600: public void visit(BreakStatement n) {
0601: n.f0.accept(this );
0602: n.f1.accept(this );
0603: }
0604:
0605: /**
0606: * <PRE>
0607: * f0 -> "continue"
0608: * f1 -> ";"
0609: * </PRE>
0610: */
0611: public void visit(ContinueStatement n) {
0612: n.f0.accept(this );
0613: n.f1.accept(this );
0614: }
0615:
0616: /**
0617: * <PRE>
0618: * f0 -> AssignmentExpression()
0619: * f1 -> ( "," AssignmentExpression() )*
0620: * </PRE>
0621: */
0622: public void visit(Expression n) {
0623: n.f0.accept(this );
0624: if (n.f1.present()) {
0625: processList(n.f1);
0626: }
0627: }
0628:
0629: /**
0630: * <PRE>
0631: * f0 -> "("
0632: * f1 -> ( FunctionCallExpressionListBody() )?
0633: * f2 -> ")"
0634: * </PRE>
0635: */
0636: public void visit(FunctionCallExpressionList n) {
0637: n.f0.accept(this );
0638: if (n.f1.present()) {
0639: n.f1.accept(this );
0640: }
0641: n.f2.accept(this );
0642: }
0643:
0644: /**
0645: * <PRE>
0646: * f0 -> AssignmentExpression()
0647: * f1 -> ( "," AssignmentExpression() )*
0648: * </PRE>
0649: */
0650: public void visit(FunctionCallExpressionListBody n) {
0651: n.f0.accept(this );
0652: if (n.f1.present()) {
0653: processList(n.f1);
0654: }
0655: }
0656:
0657: /**
0658: * <PRE>
0659: * f0 -> ConditionalExpression()
0660: * f1 -> ( ( "=" | "+=" | "-=" | "*=" | "/=" | "%=" | ">>=" | "<<=" | ">>>=" | "&=" | "^=" | "|=" ) ConditionalExpression() )*
0661: * </PRE>
0662: */
0663: public void visit(AssignmentExpression n) {
0664: n.f0.accept(this );
0665: if (n.f1.present()) {
0666: processList(n.f1);
0667: }
0668: }
0669:
0670: /**
0671: * <PRE>
0672: * f0 -> LogicalOrExpression()
0673: * f1 -> ( "?" LogicalOrExpression() ":" LogicalOrExpression() )?
0674: * </PRE>
0675: */
0676: public void visit(ConditionalExpression n) {
0677: n.f0.accept(this );
0678: if (n.f1.present()) {
0679: n.f1.accept(this );
0680: }
0681: }
0682:
0683: /**
0684: * <PRE>
0685: * f0 -> LogicalAndExpression()
0686: * f1 -> ( "||" LogicalAndExpression() )*
0687: * </PRE>
0688: */
0689: public void visit(LogicalOrExpression n) {
0690: n.f0.accept(this );
0691: if (n.f1.present()) {
0692: processList(n.f1);
0693: }
0694: }
0695:
0696: /**
0697: * <PRE>
0698: * f0 -> BitwiseOrExpression()
0699: * f1 -> ( "&&" BitwiseOrExpression() )*
0700: * </PRE>
0701: */
0702: public void visit(LogicalAndExpression n) {
0703: n.f0.accept(this );
0704: if (n.f1.present()) {
0705: processList(n.f1);
0706: }
0707: }
0708:
0709: /**
0710: * <PRE>
0711: * f0 -> BitwiseXorExpression()
0712: * f1 -> ( "|" BitwiseXorExpression() )*
0713: * </PRE>
0714: */
0715: public void visit(BitwiseOrExpression n) {
0716: n.f0.accept(this );
0717: if (n.f1.present()) {
0718: processList(n.f1);
0719: }
0720: }
0721:
0722: /**
0723: * <PRE>
0724: * f0 -> BitwiseAndExpression()
0725: * f1 -> ( "^" BitwiseAndExpression() )*
0726: * </PRE>
0727: */
0728: public void visit(BitwiseXorExpression n) {
0729: n.f0.accept(this );
0730: if (n.f1.present()) {
0731: processList(n.f1);
0732: }
0733: }
0734:
0735: /**
0736: * <PRE>
0737: * f0 -> EqualityExpression()
0738: * f1 -> ( "&" EqualityExpression() )*
0739: * </PRE>
0740: */
0741: public void visit(BitwiseAndExpression n) {
0742: n.f0.accept(this );
0743: if (n.f1.present()) {
0744: processList(n.f1);
0745: }
0746: }
0747:
0748: /**
0749: * <PRE>
0750: * f0 -> RelationalExpression()
0751: * f1 -> ( ( "==" | "!=" ) RelationalExpression() )*
0752: * </PRE>
0753: */
0754: public void visit(EqualityExpression n) {
0755: n.f0.accept(this );
0756: if (n.f1.present()) {
0757: processList(n.f1);
0758: }
0759: }
0760:
0761: /**
0762: * <PRE>
0763: * f0 -> ShiftExpression()
0764: * f1 -> ( ( "<" | ">" | ">=" | "<=" | "instanceof" ) ShiftExpression() )*
0765: * </PRE>
0766: */
0767: public void visit(RelationalExpression n) {
0768: n.f0.accept(this );
0769: if (n.f1.present()) {
0770: processList(n.f1);
0771: }
0772: }
0773:
0774: /**
0775: * <PRE>
0776: * f0 -> AdditiveExpression()
0777: * f1 -> ( ( "<<" | ">>" | ">>>" ) AdditiveExpression() )*
0778: * </PRE>
0779: */
0780: public void visit(ShiftExpression n) {
0781: n.f0.accept(this );
0782: if (n.f1.present()) {
0783: processList(n.f1);
0784: }
0785: }
0786:
0787: /**
0788: * <PRE>
0789: * f0 -> MultiplicativeExpression()
0790: * f1 -> ( ( "+" | "-" ) MultiplicativeExpression() )*
0791: * </PRE>
0792: */
0793: public void visit(AdditiveExpression n) {
0794: n.f0.accept(this );
0795: if (n.f1.present()) {
0796: processList(n.f1);
0797: }
0798: }
0799:
0800: /**
0801: * <PRE>
0802: * f0 -> UnaryExpression()
0803: * f1 -> ( ( "*" | "/" | "%" ) UnaryExpression() )*
0804: * </PRE>
0805: */
0806: public void visit(MultiplicativeExpression n) {
0807: n.f0.accept(this );
0808: if (n.f1.present()) {
0809: processList(n.f1);
0810: }
0811: }
0812:
0813: /**
0814: * <PRE>
0815: * f0 -> ( ( "++" | "--" | "+" | "-" | "~" | "!" ) )?
0816: * f1 -> PostfixExpression()
0817: * </PRE>
0818: */
0819: public void visit(UnaryExpression n) {
0820: if (n.f0.present()) {
0821: n.f0.accept(this );
0822: }
0823: n.f1.accept(this );
0824: }
0825:
0826: /**
0827: * <PRE>
0828: * f0 -> TypeExpression()
0829: * f1 -> ( "++" | "--" )?
0830: * </PRE>
0831: */
0832: public void visit(PostfixExpression n) {
0833: n.f0.accept(this );
0834: if (n.f1.present()) {
0835: n.f1.accept(this );
0836: }
0837: }
0838:
0839: /**
0840: * <PRE>
0841: * f0 -> AllocationExpression()
0842: * | CastExpression()
0843: * | PrimaryExpression()
0844: * </PRE>
0845: */
0846: public void visit(TypeExpression n) {
0847: n.f0.accept(this );
0848: }
0849:
0850: /**
0851: * <PRE>
0852: * f0 -> "new"
0853: * f1 -> PrimaryExpressionWithTrailingFxnCallExpList()
0854: * f2 -> FunctionCallExpressionList()
0855: * </PRE>
0856: */
0857: public void visit(AllocationExpression n) {
0858: n.f0.accept(this );
0859: n.f1.accept(this );
0860: n.f2.accept(this );
0861: }
0862:
0863: /**
0864: * <PRE>
0865: * f0 -> "("
0866: * f1 -> PrimaryExpressionNotFunction()
0867: * f2 -> ")"
0868: * f3 -> PrimaryExpression()
0869: * </PRE>
0870: */
0871: public void visit(CastExpression n) {
0872: n.f0.accept(this );
0873: n.f1.accept(this );
0874: n.f2.accept(this );
0875: n.f3.accept(this );
0876: }
0877:
0878: /**
0879: * <PRE>
0880: * f0 -> PrimaryPrefix()
0881: * f1 -> ( PrimaryPostfix() )*
0882: * </PRE>
0883: */
0884: public void visit(PrimaryExpression n) {
0885: n.f0.accept(this );
0886: if (n.f1.present()) {
0887: processList(n.f1);
0888: }
0889: }
0890:
0891: /**
0892: * <PRE>
0893: * f0 -> PrimaryPrefixNotFunction()
0894: * f1 -> ( PrimaryPostfix() )*
0895: * </PRE>
0896: */
0897: public void visit(PrimaryExpressionNotFunction n) {
0898: n.f0.accept(this );
0899: if (n.f1.present()) {
0900: processList(n.f1);
0901: }
0902: }
0903:
0904: /**
0905: * <PRE>
0906: * f0 -> PrimaryPrefix()
0907: * f1 -> ( PrimaryPostfixWithTrailingFxnCallExpList() )*
0908: * </PRE>
0909: */
0910: public void visit(PrimaryExpressionWithTrailingFxnCallExpList n) {
0911: n.f0.accept(this );
0912: if (n.f1.present()) {
0913: processList(n.f1);
0914: }
0915: }
0916:
0917: /**
0918: * <PRE>
0919: * f0 -> PrimaryPrefixNotFunction()
0920: * | FunctionPrimaryPrefix()
0921: * | ShorthandFunctionPrimaryPrefix()
0922: * </PRE>
0923: */
0924: public void visit(PrimaryPrefix n) {
0925: n.f0.accept(this );
0926: }
0927:
0928: /**
0929: * <PRE>
0930: * f0 -> ThisPrimaryPrefix()
0931: * | SuperPrimaryPrefix()
0932: * | CalleePrimaryPrefix()
0933: * | ArrayDeclarationPrimaryPrefix()
0934: * | IdentifierPrimaryPrefix()
0935: * | ParenPrimaryPrefix()
0936: * | Literal()
0937: * </PRE>
0938: */
0939: public void visit(PrimaryPrefixNotFunction n) {
0940: n.f0.accept(this );
0941: }
0942:
0943: /**
0944: * <PRE>
0945: * f0 -> "this"
0946: * </PRE>
0947: */
0948: public void visit(ThisPrimaryPrefix n) {
0949: n.f0.accept(this );
0950: }
0951:
0952: /**
0953: * <PRE>
0954: * f0 -> "super"
0955: * </PRE>
0956: */
0957: public void visit(SuperPrimaryPrefix n) {
0958: n.f0.accept(this );
0959: }
0960:
0961: /**
0962: * <PRE>
0963:
0964: * f0 -> "callee"
0965: * </PRE>
0966: */
0967: public void visit(CalleePrimaryPrefix n) {
0968: n.f0.accept(this );
0969: }
0970:
0971: /**
0972: * <PRE>
0973: * f0 -> <IDENTIFIER>
0974: * </PRE>
0975: */
0976: public void visit(IdentifierPrimaryPrefix n) {
0977: n.f0.accept(this );
0978: }
0979:
0980: /**
0981: * <PRE>
0982: * f0 -> "("
0983: * f1 -> Expression()
0984: * f2 -> ")"
0985: * </PRE>
0986: */
0987: public void visit(ParenPrimaryPrefix n) {
0988: n.f0.accept(this );
0989: n.f1.accept(this );
0990: n.f2.accept(this );
0991: }
0992:
0993: /**
0994: * <PRE>
0995:
0996: * f0 -> "function"
0997: * f1 -> "("
0998: * f2 -> ( Arglist() )?
0999: * f3 -> ")"
1000: * f4 -> ( "extends" PrimaryExpressionWithTrailingFxnCallExpList() FunctionCallExpressionList() )?
1001: * f5 -> "{"
1002: * f6 -> Program(true)
1003: * f7 -> "}"
1004: * </PRE>
1005: */
1006: public void visit(FunctionPrimaryPrefix n) {
1007: n.f0.accept(this );
1008: n.f1.accept(this );
1009: if (n.f2.present()) {
1010: n.f2.accept(this );
1011: }
1012: n.f3.accept(this );
1013: if (n.f4.present()) {
1014: n.f4.accept(this );
1015: }
1016: n.f5.accept(this );
1017: n.f6.accept(this );
1018: n.f7.accept(this );
1019: }
1020:
1021: /**
1022: * <PRE>
1023:
1024: * f0 -> "'{"
1025: * f1 -> Program(true)
1026: * f2 -> "}"
1027: * </PRE>
1028: */
1029: public void visit(ShorthandFunctionPrimaryPrefix n) {
1030: n.f0.accept(this );
1031: n.f1.accept(this );
1032: n.f2.accept(this );
1033: }
1034:
1035: /**
1036: * <PRE>
1037: * f0 -> "["
1038: * f1 -> ( FunctionCallExpressionListBody() )?
1039: * f2 -> "]"
1040: * </PRE>
1041: */
1042: public void visit(ArrayDeclarationPrimaryPrefix n) {
1043: n.f0.accept(this );
1044: if (n.f1.present()) {
1045: n.f1.accept(this );
1046: }
1047: n.f2.accept(this );
1048: }
1049:
1050: /**
1051: * <PRE>
1052: * f0 -> FunctionCallPrimaryPostfix()
1053: * | ArraySubscriptPrimaryPostfix()
1054: * | ThisScopeQualifierPrimaryPostfix()
1055: * | PropertyIdentifierPrimaryPostfix()
1056: * </PRE>
1057: */
1058: public void visit(PrimaryPostfix n) {
1059: n.f0.accept(this );
1060: }
1061:
1062: /**
1063: * <PRE>
1064: * f0 -> ArraySubscriptPrimaryPostfix()
1065: * | ThisScopeQualifierPrimaryPostfix()
1066: * | PropertyIdentifierPrimaryPostfix()
1067: * </PRE>
1068: */
1069: public void visit(PrimaryPostfixWithTrailingFxnCallExpList n) {
1070: n.f0.accept(this );
1071: }
1072:
1073: /**
1074: * <PRE>
1075: * f0 -> FunctionCallExpressionList()
1076: * </PRE>
1077: */
1078: public void visit(FunctionCallPrimaryPostfix n) {
1079: n.f0.accept(this );
1080: }
1081:
1082: /**
1083: * <PRE>
1084: * f0 -> "["
1085: * f1 -> Expression()
1086: * f2 -> ( ".." Expression() )?
1087: * f3 -> "]"
1088: * </PRE>
1089: */
1090: public void visit(ArraySubscriptPrimaryPostfix n) {
1091: n.f0.accept(this );
1092: n.f1.accept(this );
1093: if (n.f2.present()) {
1094: n.f2.accept(this );
1095: }
1096: n.f3.accept(this );
1097: }
1098:
1099: /**
1100: * <PRE>
1101: * f0 -> "."
1102: * f1 -> <IDENTIFIER>
1103: * </PRE>
1104: */
1105: public void visit(PropertyIdentifierPrimaryPostfix n) {
1106: n.f0.accept(this );
1107: n.f1.accept(this );
1108: }
1109:
1110: /**
1111: * <PRE>
1112: * f0 -> "."
1113: * f1 -> "this"
1114: * </PRE>
1115: */
1116: public void visit(ThisScopeQualifierPrimaryPostfix n) {
1117: n.f0.accept(this );
1118: n.f1.accept(this );
1119: }
1120:
1121: /**
1122: * <PRE>
1123: * f0 -> <INTEGER_LITERAL>
1124: * | <FLOATING_POINT_LITERAL>
1125: * | <STRING_LITERAL>
1126: * | <REGEXP_LITERAL>
1127: * | "true"
1128: * | "false"
1129: * | "null"
1130: * | "undefined"
1131: * </PRE>
1132: */
1133: public void visit(Literal n) {
1134: n.f0.accept(this );
1135: }
1136:
1137: /**
1138: * <PRE>
1139:
1140: * f0 -> ( "static" | "const" | "private" | "protected" | "public" )*
1141: * </PRE>
1142: */
1143: public void visit(Permissions n) {
1144: if (n.f0.present()) {
1145: processList(n.f0);
1146: }
1147: }
1148:
1149: }
1150:
1151: class FormatCommand {
1152: public static final int FORCE = 0;
1153: public static final int INDENT = 1;
1154: public static final int OUTDENT = 2;
1155: public static final int SPACE = 3;
1156:
1157: private int command;
1158: private int numCommands;
1159:
1160: FormatCommand(int command, int numCommands) {
1161: this .command = command;
1162: this .numCommands = numCommands;
1163: }
1164:
1165: public int getCommand() {
1166: return command;
1167: }
1168:
1169: public int getNumCommands() {
1170: return numCommands;
1171: }
1172:
1173: public void setCommand(int i) {
1174: command = i;
1175: }
1176:
1177: public void setNumCommands(int i) {
1178: numCommands = i;
1179: }
1180: }
1181:
1182: class TreeFormatterException extends RuntimeException {
1183: TreeFormatterException() {
1184: super ();
1185: }
1186:
1187: TreeFormatterException(String s) {
1188: super(s);
1189: }
1190: }
|