Coverage Report - com.sun.tools.javafx.antlr.JavafxSyntacticAnalysis
 
Classes in this File Line Coverage Branch Coverage Complexity
JavafxSyntacticAnalysis
76%
34/45
50%
5/10
0
 
 1  
 /*
 2  
  * To change this template, choose Tools | Templates
 3  
  * and open the template in the editor.
 4  
  */
 5  
 package com.sun.tools.javafx.antlr;
 6  
 
 7  
 import com.sun.tools.javac.tree.JCTree.*;
 8  
 
 9  
 import org.antlr.runtime.*;
 10  
 import org.antlr.runtime.tree.*;
 11  
 import com.sun.tools.javac.util.*;
 12  
 
 13  
 /**
 14  
  *
 15  
  * @author Robert Field
 16  
  */
 17  
 public class JavafxSyntacticAnalysis {
 18  
 
 19  
     /** The context key for the parser. */
 20  12
     protected static final Context.Key<JavafxSyntacticAnalysis> syntaxKey =
 21  
             new Context.Key<JavafxSyntacticAnalysis>();
 22  
     
 23  
     protected Context context;
 24  
     /** Command line options
 25  
      */
 26  
     protected Options options;
 27  
     /** The name table.
 28  
      */
 29  
     protected Name.Table names;
 30  
     
 31  
     /** The log to be used for error reporting.
 32  
      */
 33  
     public Log log;
 34  
 
 35  
     public static JavafxSyntacticAnalysis instance(Context context) {
 36  399
         JavafxSyntacticAnalysis instance = context.get(syntaxKey);
 37  399
         if (instance == null)
 38  399
             instance = new JavafxSyntacticAnalysis(context);
 39  399
         return instance;
 40  
     }
 41  
     
 42  399
     protected JavafxSyntacticAnalysis(final Context context) {
 43  399
         this.context = context;
 44  399
         context.put(syntaxKey, this);
 45  
 
 46  399
         names = Name.Table.instance(context);
 47  399
         options = Options.instance(context);
 48  399
         log = Log.instance(context);
 49  399
     }
 50  
 
 51  
     public JCCompilationUnit parse(CharSequence content) {
 52  407
         JCCompilationUnit unit = null;
 53  407
         String parserChoice = options.get("parser");
 54  407
         if (parserChoice == null) {
 55  407
             parserChoice = "v3"; // default
 56  
         }
 57  
         {
 58  
             try {
 59  
                 // Create input stream from standard input
 60  407
                 ANTLRStringStream input = new ANTLRStringStream(content.toString());
 61  
                 // Create a lexer attached to that input stream
 62  407
                 v3Lexer lexer = new v3Lexer(context, input);
 63  
                 // Create a stream of tokens pulled from the lexer
 64  407
                 CommonTokenStream tokens = new CommonTokenStream(lexer);
 65  
                 // Create a parser attached to the token stream
 66  407
                 v3Parser parser = new v3Parser(tokens);
 67  
                 // Set the context
 68  407
                 parser.initialize(context);
 69  
                 // Invoke the module rule in get return value
 70  407
                 v3Parser.module_return comReturn = parser.module();
 71  407
                 CommonTree comTree = (CommonTree) comReturn.getTree();
 72  407
                 if (errorCount() == 0) {
 73  
                     // Walk resulting tree; create treenode stream first
 74  397
                     CommonTreeNodeStream nodes = new CommonTreeNodeStream(comTree);
 75  
                     // AST nodes have payloads that point into token stream
 76  397
                     nodes.setTokenStream(tokens);
 77  
                     // Create a tree Walker attached to the nodes stream
 78  397
                     v3Walker walker = new v3Walker(nodes);
 79  
                     // Set the context
 80  397
                     walker.initialize(context);
 81  
                     // Invoke the start symbol, rule module
 82  397
                     unit = walker.module();                   
 83  
                 }
 84  407
                 String treeChoice = options.get("tree");
 85  407
                 if (treeChoice != null) {
 86  0
                     printTree(comTree, "---");
 87  
                 }            
 88  0
             } catch (Throwable thr) {
 89  0
                 System.err.println("Error in syntactic analysis:");
 90  0
                 thr.printStackTrace(System.err);
 91  407
             }
 92  
         }
 93  407
         return unit;
 94  
     }
 95  
 
 96  
     /** The number of errors reported so far.
 97  
      */
 98  
     public int errorCount() {
 99  407
         return log.nerrors;
 100  
     }
 101  
 
 102  
     private void printTree(Tree tree, String prefix) {
 103  0
         CommonToken token = (CommonToken)((CommonTree)tree).getToken();
 104  0
         System.out.println(prefix + tree + "  (" + token.getStartIndex() + ")" + token.getLine());
 105  0
         int n = tree.getChildCount();
 106  0
         String nextPrefix = prefix + "   ";
 107  0
         for (int i = 0; i < n; ++i) {
 108  0
             printTree(tree.getChild(i), nextPrefix);
 109  
         }
 110  0
     }
 111  
 }
 112