Coverage Report - com.sun.tools.javafx.antlr.AbstractGeneratedTreeParser
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractGeneratedTreeParser
60%
29/48
69%
18/26
0
 
 1  
 /*
 2  
  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
 3  
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  
  *
 5  
  * This code is free software; you can redistribute it and/or modify it
 6  
  * under the terms of the GNU General Public License version 2 only, as
 7  
  * published by the Free Software Foundation.  Sun designates this
 8  
  * particular file as subject to the "Classpath" exception as provided
 9  
  * by Sun in the LICENSE file that accompanied this code.
 10  
  *
 11  
  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  
  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  
  * version 2 for more details (a copy is included in the LICENSE file that
 15  
  * accompanied this code).
 16  
  *
 17  
  * You should have received a copy of the GNU General Public License version
 18  
  * 2 along with this work; if not, write to the Free Software Foundation,
 19  
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  
  *
 21  
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 22  
  * CA 95054 USA or visit www.sun.com if you need additional information or
 23  
  * have any questions.
 24  
  */
 25  
 
 26  
 package com.sun.tools.javafx.antlr;
 27  
 
 28  
 import com.sun.tools.javafx.tree.JavafxTreeMaker;
 29  
 import com.sun.tools.javac.tree.JCTree;
 30  
 import com.sun.tools.javac.tree.JCTree.*;
 31  
 
 32  
 import com.sun.tools.javac.code.*;
 33  
 import com.sun.tools.javac.util.*;
 34  
 import com.sun.tools.javafx.tree.JFXInterpolateValue;
 35  
 import com.sun.tools.javafx.util.MsgSym;
 36  
 import java.util.HashMap;
 37  
 import javax.tools.DiagnosticListener;
 38  
 import static com.sun.tools.javac.util.ListBuffer.lb;
 39  
 
 40  
 import org.antlr.runtime.*;
 41  
 import org.antlr.runtime.tree.*;
 42  
 
 43  
 /**
 44  
  * Base class for ANTLR generated parsers 
 45  
  * 
 46  
  * @author Robert Field
 47  
  */
 48  
 public abstract class AbstractGeneratedTreeParser extends TreeParser {
 49  
     
 50  
     /** The factory to be used for abstract syntax tree construction.
 51  
      */
 52  
     protected JavafxTreeMaker F;
 53  
     
 54  
     /** The log to be used for error diagnostics.
 55  
      */
 56  
     protected Log log;
 57  
     
 58  
     /** The Source language setting. */
 59  
     protected Source source;
 60  
     
 61  
     /** The name table. */
 62  
     protected Name.Table names;
 63  
     
 64  
     /** should parser generate an end positions map? */
 65  
     protected boolean genEndPos;
 66  
     
 67  
     /** The end positions map. */
 68  
     HashMap<JCTree,Integer> endPositions;
 69  
     
 70  
     /** The doc comments map */
 71  
     HashMap<JCTree,String> docComments;
 72  
 
 73  
     /** the token id for white space */
 74  
     protected int whiteSpaceToken;
 75  
     
 76  
     /* ---------- error recovery -------------- */
 77  
     
 78  
     protected JCErroneous errorTree;
 79  
     
 80  
     /** initializes a new instance of GeneratedParser */
 81  
     protected void initialize(Context context) {
 82  397
         this.F = (JavafxTreeMaker)JavafxTreeMaker.instance(context);
 83  397
         this.log = Log.instance(context);
 84  397
         this.names = Name.Table.instance(context);
 85  397
         this.source = Source.instance(context);
 86  397
         Options options = Options.instance(context);
 87  397
         this.genEndPos = options.get("-Xjcov") != null ||
 88  
                          context.get(DiagnosticListener.class) != null ||
 89  
                          Boolean.getBoolean("JavafxModuleBuilder.debugBadPositions");
 90  397
     }
 91  
     
 92  
     protected AbstractGeneratedTreeParser(TreeNodeStream input) {
 93  0
         super(input);
 94  0
     }
 95  
     
 96  
     protected AbstractGeneratedTreeParser(TreeNodeStream input, RecognizerSharedState state) {
 97  397
         super(input, state);
 98  397
     }
 99  
     
 100  
     public String getErrorMessage(RecognitionException e, String[] tokenNames) {
 101  
         //java.util.List stack = getRuleInvocationStack(e, this.getClass().getName());
 102  0
         String msg = null;
 103  0
         if (e instanceof NoViableAltException) {
 104  0
             NoViableAltException nvae = (NoViableAltException) e;
 105  0
             msg = "Trying to understand your program, I'm confused at " + getRuleInvocationStack(e, this.getClass().getName());
 106  
             //msg = " no viable alt; token=" + e.token + " (decision=" 
 107  
             //        + nvae.decisionNumber + " state " + nvae.stateNumber + ")" 
 108  
             //        + " decision=<<" + nvae.grammarDecisionDescription + ">>";
 109  0
         } else {
 110  0
             msg = super.getErrorMessage(e, tokenNames);
 111  
         }
 112  0
         return msg;
 113  
     }
 114  
 
 115  
 /**
 116  
     public String getTokenErrorDisplay(Token t) {
 117  
         return t.toString();
 118  
     }
 119  
 **/
 120  
     
 121  
     /** What is the error header, normally line/character position information? */
 122  
     @Override
 123  
     public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
 124  0
         int pos = ((CommonToken)(e.token)).getStartIndex();
 125  0
         String msg = getErrorMessage(e, tokenNames);
 126  
         //        System.err.println("ERROR: " + msg);
 127  0
         log.error(pos, MsgSym.MESSAGE_JAVAFX_GENERALERROR, msg);
 128  0
     }
 129  
     
 130  
     protected int pos(CommonTree tree) {
 131  
         //System.out.println("TOKEN: line: " + tok.getLine() + " char: " + tok.getCharPositionInLine() + " pos: " + ((CommonToken)tok).getStartIndex());
 132  51550
         return ((CommonToken)tree.getToken()).getStartIndex();
 133  
     }
 134  
     
 135  
     protected List noJCTrees() {
 136  0
         return List.<JCTree>nil();
 137  
     }
 138  
     
 139  
     protected List<JCAnnotation> noJCAnnotations() {
 140  397
         return List.<JCAnnotation>nil();
 141  
     }
 142  
 
 143  
     void setDocComment(JCTree tree, CommonTree comment) {
 144  3216
         if (comment != null) {
 145  140
             if (docComments == null)
 146  84
                 docComments = new HashMap<JCTree,String>();
 147  140
             docComments.put(tree, comment.getText());
 148  
         }
 149  3216
     }
 150  
 
 151  
     void endPos(JCTree tree, CommonTree node) {
 152  36073
         int endIndex = node.getTokenStopIndex();
 153  36073
         if (genEndPos && endIndex != -1) { // -1 means no such token
 154  706
             TokenStream src = input.getTokenStream();
 155  706
             CommonToken endToken = (CommonToken)src.get(endIndex);
 156  
             // backtrack over WS and imaginary tokens
 157  717
             while (endToken.getType() == whiteSpaceToken || endToken.getCharPositionInLine() == -1) { 
 158  11
                 if (--endIndex < 0)
 159  0
                     return;
 160  11
                 endToken = (CommonToken)src.get(endIndex);
 161  
             }
 162  706
             int endPos = endToken.getStopIndex();
 163  706
             endPos(tree, endPos+1);
 164  
         }
 165  36073
     }
 166  
 
 167  
     void endPos(JCTree tree, com.sun.tools.javac.util.List<JFXInterpolateValue> list) {
 168  0
         if (genEndPos) {
 169  0
             int endLast = endPositions.get(list.last());
 170  0
             endPositions.put(tree, endLast);
 171  
         }
 172  0
     }
 173  
 
 174  
     void endPos(JCTree tree, int end) {
 175  13555
         if (genEndPos)
 176  878
             endPositions.put(tree, end);
 177  13555
     }
 178  
 }