/**
  * Constructor.
  *
  * @param pTree  The tree node representing a class declaration.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2AnnotationDeclaration(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.ANNOTATION_DECLARATION, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.AT) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
Example #2
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing an <code>throws</code> clause.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ThrowsClause(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.THROWS_CLAUSE, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.THROWS_CLAUSE)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The root node of a parenthesized expression.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ParenthesizedExpression(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ExpressionType.PARENTHESIZED_EXPRESSION, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.PARENTESIZED_EXPR)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a variable declarator
  *               identifier.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2VariableDeclaratorIdentifier(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.VARIABLE_DECLARATOR_IDENTIFIER,pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.IDENT)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
Example #5
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing the literal.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2Identifier(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ExpressionType.IDENTIFIER, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.IDENT)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>continue</code>
  *               statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ContinueStatement(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ElementType.CONTINUE_STATEMENT,pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.CONTINUE) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
     if (pTree.ChildCount == 1) {
     mHasLabelIdentifier = true;
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>break</code> statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2BreakStatement(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ElementType.BREAK_STATEMENT, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.BREAK)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
     if (pTree.ChildCount == 1)
     {
         mLabelNode = (AST2JSOMTree)pTree.GetChild(0);
     }
 }
Example #8
0
 public static void Main(string[] args)
 {
     if (args.Length > 0)
     {
         string inputFileName = args[0];
         if (!Path.IsPathRooted(inputFileName))
         {
             inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
         }
         ICharStream input = new ANTLRFileStream(inputFileName);
         TLexer lex = new TLexer(input);
         ITokenStream tokens = new TokenRewriteStream(lex);
         TParser parser = new TParser(tokens);
         parser.program();
         Console.Out.WriteLine(tokens);
     }
     else
         Console.Error.WriteLine("Usage: tweak <input-file>");
 }
Example #9
0
        static string createRewriter(string file, Script script)
        {
            ANTLRFileStream stream = new ANTLRFileStream(file);
            SourceExprLexer lex = new SourceExprLexer(stream);
            // create a buffer of tokens pulled from the lexer
            // Must use TokenRewriteStream not CommonTokenStream!
            TokenRewriteStream tokens = new TokenRewriteStream(lex);
            SourceExprParser parser = new SourceExprParser(tokens);
            SourceExprParser.prog_return r = parser.prog(script);

            // WALK TREE AND REWRITE TOKEN BUFFER
            CommonTree t = (CommonTree)r.Tree; // get tree from parser
            // create a stream of tree nodes from AST built by parser
            CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
            // tell it where it can find the token objects
            nodes.TokenStream = tokens;
            //SourceRewrite rewriter = new SourceRewrite(nodes);
            //rewriter.prog(script);
            return tokens.ToString(); // return tweaked token buffer
        }
Example #10
0
 public ReplaceOp( TokenRewriteStream stream, int from, int to, object text )
     : base( stream, from, text )
 {
     lastIndex = to;
 }
 public ReplaceOp(int from, int to, object text, TokenRewriteStream parent)
     : base(from, text, parent)
 {
     lastIndex = to;
 }
Example #12
0
 public InsertBeforeOp( TokenRewriteStream stream, int index, object text ) :
     base( stream, index, text )
 {
 }
 public ReplaceOp(TokenRewriteStream stream, int from, int to, object text)
     : base(stream, from, text)
 {
     lastIndex = to;
 }
Example #14
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>synchronized</code>
  *               statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2Catch(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.STATEMENT_BLOCK_ELEMENT_HELPER,pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.CATCH)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type,
                         "pTree"));
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a type.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ExplicitConstructorCall(
     AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ExpressionType.EXPLICIT_CONSTRUCTOR_CALL,pTokenRewriteStream)
 {
     if (pTree.Type == JavaTreeParser.SUPER_CONSTRUCTOR_CALL)
     {
         mIsSuperConstructorCall = true;
     }
     else if (pTree.Type != JavaTreeParser.THIS_CONSTRUCTOR_CALL)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
     int offset = 0;
     AST2JSOMTree child = (AST2JSOMTree)pTree.GetChild(offset);
     // 'super' constructor calls may be preceded by a primary expression
     // stating the constructor's owner.
     if (mIsSuperConstructorCall
         && child.Type != JavaTreeParser.GENERIC_TYPE_ARG_LIST
         && child.Type != JavaTreeParser.ARGUMENT_LIST)
     {
         // There must be a primary expression tree.
         mPrimaryExprTree = child;
         offset++;
         child = (AST2JSOMTree)pTree.GetChild(offset);
     }
     // Check if there is a generic type argument list.
     if (child.Type == JavaTreeParser.GENERIC_TYPE_ARG_LIST)
     {
         mGenericTypeArgumentListTree = child;
         offset++;
         child = (AST2JSOMTree)pTree.GetChild(offset);
     }
     // Get the optional arguments.
     if (child.ChildCount > 0)
     {
         mArgumentListTree = child;
     }
 }
 protected RewriteOperation(TokenRewriteStream stream, int index, object text)
 {
     this.index  = index;
     this.text   = text;
     this.stream = stream;
 }
Example #17
0
 /**
  * Constructor.
  * <p>
  * Creates a new instance for a certain literal type.
  *
  * @param pTree  The tree node representing the literal.
  * @param pLiteralType  One of the 'LITERAL_???' constants defined by the
  *                      interface 'Literal'. Note that it's up to the caller
  *                      to ensure that the literal type matches the token
  *                      type.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2Literal(AST2JSOMTree pTree, LiteralType pLiteralType,
     TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ExpressionType.LITERAL, pTokenRewriteStream)
 {
     mLiteralType = pLiteralType;
 }
 protected internal RewriteOperation(int index, object text, TokenRewriteStream parent)
 {
     this.index  = index;
     this.text   = text;
     this.parent = parent;
 }
Example #19
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a modifier list.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ModifierList(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.MODIFIER_LIST, pTokenRewriteStream)
 {
     if (pTree.Type == JavaTreeParser.MODIFIER_LIST) {
     mIsLocalModifier = false;
     } else if (pTree.Type == JavaTreeParser.LOCAL_MODIFIER_LIST) {
     mIsLocalModifier = true;
     } else {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
     resolveModifierList(pTree);
 }
 /**
  * Constructor.
  * <p>
  * If classes derived from this class should be recognized by another JSOM
  * type than <code>JSOMType.VARIABLE_DECLARATION</code> they must call
  * this constructor via the explicit super constructor call.
  *
  * @param pTree  The tree node representing a variable declaration.
  * @param pJSOMType  The JSOM type of the new instance.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 protected AST2CommonVariableDeclaration(
     AST2JSOMTree pTree, JSOMType pJSOMType,
     TokenRewriteStream pTokenRewriteStream)
     : base(pTree, pJSOMType, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.VAR_DECLARATION) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
     if (pTree.GetChild(0).ChildCount == 0) {
     mHasModifier = false;
     }
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>forEach</code>
  *               statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ForEachStatement(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ElementType.FOR_EACH_STATEMENT,pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.FOR_EACH) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
     if (pTree.GetChild(0).ChildCount > 0) {
     mHasModifier = true;
     }
     mIdentifierTree = (AST2JSOMTree)pTree.GetChild(2);
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing local class declaration.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2LocalClassDeclaration(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.STATEMENT_BLOCK_ELEMENT, pTokenRewriteStream)
 {
 }
 public DeleteOp(int from, int to, TokenRewriteStream parent)
     : base(from, to, null, parent)
 {
 }
Example #24
0
 public DeleteOp( TokenRewriteStream stream, int from, int to ) :
     base( stream, from, to, null )
 {
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a formal parameter declaration.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  *
  */
 public AST2FormalParameterDeclaration(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.FORMAL_PARAMETER_DECLARATION, pTokenRewriteStream)
 {
     if (pTree.Type == JavaTreeParser.FORMAL_PARAM_VARARG_DECL)
     {
         mIsVarArgParam = true;
     }
     else if (pTree.Type != JavaTreeParser.FORMAL_PARAM_STD_DECL)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
     mHasModifier = pTree.GetChild(0).ChildCount > 0;
 }
Example #26
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>throw</code> statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2ThrowStatement(
         AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ElementType.THROW_STATEMENT, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.THROW)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
Example #27
0
 public DeleteOp(TokenRewriteStream stream, int from, int to) :
     base(stream, from, to, null)
 {
 }
 protected RewriteOperation(TokenRewriteStream stream, int index)
 {
     this.stream = stream;
     this.index  = index;
 }
 public ReplaceOp(int from, int to, object text, TokenRewriteStream parent)
     : base(from, text, parent)
 {
     lastIndex = to;
 }
 public InsertBeforeOp(TokenRewriteStream stream, int index, object text) :
     base(stream, index, text)
 {
 }
 protected internal RewriteOperation(int index, object text, TokenRewriteStream parent)
 {
     this.index = index;
     this.text = text;
     this.parent = parent;
 }
Example #32
0
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a <code>switch</code> statement.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2TryStatement(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, ElementType.TRY_STATEMENT,pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.TRY)
     {
         throw new ArgumentException(
                 CommonErrorMessages.getInvalidArgumentValueMessage(
                         "pTree.Type == " + pTree.Type, "pTree"));
     }
     if (pTree.ChildCount > 1)
     {
         // The next child may be an 'catch' clause list or a 'finally'
         // clause.
         AST2JSOMTree child = (AST2JSOMTree)pTree.GetChild(1);
         if (child.Type == JavaTreeParser.CATCH_CLAUSE_LIST)
         {
             if (child.ChildCount > 0)
             {
                 mCatchClauseListTree = child;
             }
             if (pTree.ChildCount == 3)
             {
                 // Switch forward to the 'finally' clause.
                 child = (AST2JSOMTree)pTree.GetChild(2);
             }
             else
             {
                 child = null;
             }
         }
         if (child != null)
         {
             mFinallyBlockScopeTree = child;
         }
     }
 }
Example #33
0
 protected RewriteOperation( TokenRewriteStream stream, int index, object text )
 {
     this.index = index;
     this.text = text;
     this.stream = stream;
 }
        public virtual void Instrument(StreamReader inputStream, string inputFilename, StreamWriter outputStream)
        {
            var testTemplate = LoadStringTemplateGroup();

            var inputPath = Path.GetFullPath(inputFilename);
            var inputFile = Path.GetFileName(inputFilename);

            //get headerTemplate for file header
            var headerTemplate = testTemplate.GetInstanceOf("file_header");
            headerTemplate.SetAttribute("src", inputFile);
            headerTemplate.SetAttribute("path", inputPath.Replace("\\", "\\\\"));

            //read lines for later usage
            var codeLines = new StringBuilder();
            var code = new StringBuilder();

            codeLines.Append("_yuitest_coverage[\"");
            codeLines.Append(inputFile);
            codeLines.Append("\"].code=[");

            string line;

            while ((line = inputStream.ReadLine()) != null)
            {
                //build up array of lines
                codeLines.Append("\"");
                codeLines.Append(line.Replace(@"\", @"\\").Replace("\"", "\\\""));
                codeLines.Append("\",");

                //build up source code
                code.AppendLine(line);
            }

            var index = codeLines.Length - 1;
            switch (codeLines[index])
            {
                case ',': //if there's a dangling comma, replace it
                    codeLines.Remove(index, 1);
                    codeLines.Insert(index, ']');
                    break;
                case '[': //empty file
                    codeLines.Append("]");
                    break;
                    //no default
            }
            codeLines.Append(";");

            //output full path

            //setup parser
            var antlrStream = new ANTLRReaderStream(new StringReader(code.ToString()));
            var lexer = new ES3YUITestLexer(antlrStream);
            var tokens = new TokenRewriteStream(lexer);
            var parser = new ES3YUITestParser(tokens)
                             {
                                 TemplateGroup = testTemplate,
                                 SourceFileName = inputFile
                             };

            var result = string.Empty;

            //an empty string will cause the parser to explode
            if (code.ToString().Trim().Length > 0)
            {
                parser.program();
                result = tokens.ToString();
            }

            //output the resulting file
            outputStream.Write(headerTemplate.ToString());
            outputStream.WriteLine();
            outputStream.Write(codeLines.ToString());
            outputStream.WriteLine();
            outputStream.Write(result);
            outputStream.Flush();
        }
 public InsertBeforeOp(int index, object text, TokenRewriteStream parent)
     : base(index, text, parent)
 {
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing an annotation identifier. This
  *               may be a root node of type <code>IDENT</code> for named
  *               initializers or any start/root node that matches an
  *               annotation element value.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2AnnotationInitializer(AST2JSOMTree pTree, TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.ANNOTATION_INITIALIZER, pTokenRewriteStream)
 {
     int type = pTree.Type;
     if (type == JavaTreeParser.IDENT) {
     mIsDefaultInitializer = false;
     } else if (   type != JavaTreeParser.ANNOTATION_INIT_ARRAY_ELEMENT
            && type != JavaTreeParser.AT
            && type != JavaTreeParser.EXPR) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
 }
 public DeleteOp(int from, int to, TokenRewriteStream parent)
     : base(from, to, null, parent)
 {
 }
 /**
  * Constructor.
  *
  * @param pTree  The tree node representing a type.
  * @param pOwner  The annotation declaration the new object belongs to.
  * @param pTokenRewriteStream  The token stream the token of the stated
  *                             tree node belongs to.
  */
 public AST2AnnotationTopLevelScope(
     AST2JSOMTree pTree, AST2AnnotationDeclaration pOwner, 
     TokenRewriteStream pTokenRewriteStream)
     : base(pTree, JSOMType.ANNOTATION_TOP_LEVEL_SCOPE, pTokenRewriteStream)
 {
     if (pTree.Type != JavaTreeParser.ANNOTATION_TOP_LEVEL_SCOPE) {
     throw new ArgumentException(
             CommonErrorMessages.getInvalidArgumentValueMessage(
                     "pTree.Type == " + pTree.Type, "pTree"));
     }
     mOwner = pOwner;
 }
 public InsertBeforeOp(int index, object text, TokenRewriteStream parent)
     : base(index, text, parent)
 {
 }