Coverage Report - com.sun.tools.javafx.antlr.Lexer
 
Classes in this File Line Coverage Branch Coverage Complexity
Lexer
94%
63/67
94%
15/16
0
Lexer$1
N/A
N/A
0
Lexer$BraceQuoteTracker
96%
23/24
82%
23/28
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 static com.sun.tools.javac.util.ListBuffer.lb;
 35  
 import com.sun.tools.javac.util.Position;
 36  
 
 37  
 import com.sun.tools.javafx.util.MsgSym;
 38  
 
 39  
 import org.antlr.runtime.*;
 40  
 
 41  
 import java.util.List;
 42  
 import java.util.ArrayList;
 43  
 
 44  
 /**
 45  
  * Base class for ANTLR generated parsers 
 46  
  * 
 47  
  * @author Robert Field
 48  
  * @author Zhiqun Chen
 49  
  */
 50  141604
 public abstract class Lexer extends org.antlr.runtime.Lexer {
 51  
     
 52  
     /** The log to be used for error diagnostics.
 53  
      */
 54  
     protected Log log;
 55  407
     protected int previousTokenType = -1;
 56  407
     List<Token> tokens = new ArrayList<Token>();
 57  407
     private final BraceQuoteTracker NULL_BQT = new BraceQuoteTracker(null, '\'', false);
 58  407
     private BraceQuoteTracker quoteStack = NULL_BQT;
 59  
       
 60  0
     protected Lexer(){};
 61  
     
 62  
     protected Lexer (CharStream input) {
 63  0
         super(input);
 64  0
     }
 65  
     
 66  
     protected Lexer (CharStream input, RecognizerSharedState state) {
 67  407
         super(input, state);
 68  407
     }
 69  
     
 70  
     /** Allow emitting more than one token from a lexer rule
 71  
      */
 72  
     public void emit(Token token) {
 73  138897
         int ttype = token.getType();
 74  
         //System.err.println("::: " + ttype + " --- " + token.getText());
 75  138897
         if (verifyPreviousType(ttype, previousTokenType)) {
 76  1755
             Token syntheticSemi = new CommonToken(token);
 77  1755
             syntheticSemi.setType(getSyntheticSemiType());
 78  1755
             syntheticSemi.setText("beginning of new statement");
 79  1755
             state.token = syntheticSemi;
 80  1755
             tokens.add(syntheticSemi);
 81  
             //System.err.println("INSERTING in front of: " + ttype);
 82  1755
         } else {
 83  137142
             state.token = token;
 84  
         }
 85  138897
         tokens.add(token);
 86  
 
 87  138897
         if (verifyCurrentType(ttype)) {
 88  78910
             previousTokenType = ttype;
 89  
         }
 90  138897
     }
 91  
 
 92  
     protected abstract int getSyntheticSemiType();
 93  
 
 94  
     protected abstract boolean verifyCurrentType(int ttype);
 95  
 
 96  
     protected abstract boolean verifyPreviousType(int ttype, int previousTokenType);
 97  
 
 98  
     public Token nextToken() {
 99  140652
         if ( tokens.size() > 0 ) {
 100  2002
             return tokens.remove(0);
 101  
         }
 102  138650
         super.nextToken();
 103  138650
         if ( tokens.size()==0 ) {
 104  407
             emit(Token.EOF_TOKEN);
 105  
         }
 106  138650
         return tokens.remove(0);
 107  
     }
 108  
 
 109  
     void processString() {
 110  3470
         setText(StringLiteralProcessor.convert( log, getCharIndex(), getText() ));
 111  3470
     }
 112  
 
 113  
     void processFormatString() {
 114  
         // Add quote characters and adjust the index to invoke StringLiteralProcessor.convert().
 115  40
         StringBuilder sb = new StringBuilder();
 116  40
         sb.append('"').append(getText()).append('"');
 117  40
         setText(StringLiteralProcessor.convert(log, getCharIndex() + 1, sb.toString()));
 118  40
     }
 119  
 
 120  
     void processTranslationKey() {
 121  28
         String text = getText().substring(2); // remove '##'
 122  28
         if (text.length() > 0) {
 123  4
             text = StringLiteralProcessor.convert( log, getCharIndex(), text );
 124  
         }
 125  28
         setText(text);
 126  28
     }
 127  
 
 128  
 
 129  
     protected void enterBrace(int quote, boolean nextIsPercent) {
 130  3798
         quoteStack.enterBrace(quote, nextIsPercent);
 131  3798
     }
 132  
 
 133  
     protected void leaveQuote() {
 134  724
         quoteStack.leaveQuote();
 135  724
     }
 136  
 
 137  
     protected boolean rightBraceLikeQuote(int quote) {
 138  37624
         return quoteStack.rightBraceLikeQuote(quote);
 139  
     }
 140  
 
 141  
     protected void leaveBrace() {
 142  3795
         quoteStack.leaveBrace();
 143  3795
     }
 144  
 
 145  
     protected boolean percentIsFormat() {
 146  112
         return quoteStack.percentIsFormat();
 147  
     }
 148  
 
 149  
     protected void resetPercentIsFormat() {
 150  40
         quoteStack.resetPercentIsFormat();
 151  40
     }
 152  
 
 153  
     
 154  
     
 155  
     public String getErrorMessage(RecognitionException e, String[] tokenNames) {
 156  
         
 157  4
         StringBuffer mb = new StringBuffer();
 158  4
         if (e instanceof NoViableAltException) {
 159  3
             NoViableAltException nvae = (NoViableAltException) e;
 160  3
             if (e.c == Token.EOF) {
 161  2
                 mb.append("Sorry, I reached to the end of file. ");
 162  2
                 mb.append("Perhaps you are having a mismatched " + "'" + "\"" + "' or '{'");
 163  
             } else {
 164  1
                 mb.append("Sorry, " + getCharErrorDisplay(e.c));          
 165  1
                 mb.append(" is not supported in JavaFX");
 166  
             }
 167  3
         } else if (e instanceof FailedPredicateException) {
 168  1
              mb.append("Sorry, I was trying to understand a " + getCharErrorDisplay(e.c) + ". ");
 169  1
              mb.append("Perhaps you are having a mismatched " + "'" + "\"" + "' or '{'");
 170  1
             recover(e);
 171  
         } else {
 172  0
             mb.append( super.getErrorMessage(e, tokenNames) );
 173  
         }
 174  
       
 175  4
         return  mb.toString();
 176  
     }
 177  
     
 178  
 
 179  
     @Override
 180  
     public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
 181  
    
 182  4
         String msg = getErrorMessage(e, tokenNames);
 183  
 //        log.error(Position.NOPOS, "javafx.generalerror", msg);
 184  4
         log.error(getCharIndex(), MsgSym.MESSAGE_JAVAFX_GENERALERROR, msg);
 185  4
     }
 186  
     
 187  
     
 188  
      /** Track "He{"l{"l"}o"} world" quotes
 189  
      */
 190  419
     protected class BraceQuoteTracker {
 191  
         private int braceDepth;
 192  
         private char quote;
 193  
         private boolean percentIsFormat;
 194  
         private BraceQuoteTracker next;
 195  1132
         private BraceQuoteTracker(BraceQuoteTracker prev, char quote, boolean percentIsFormat) {
 196  1132
             this.quote = quote;
 197  1132
             this.percentIsFormat = percentIsFormat;
 198  1132
             this.braceDepth = 1;
 199  1132
             this.next = prev;
 200  1132
         }
 201  
 
 202  
         void enterBrace(int quote, boolean percentIsFormat) {
 203  3798
             if (quote == 0) {  // exisiting string expression or non string expression
 204  3073
                 if (quoteStack != NULL_BQT) {
 205  460
                     ++quoteStack.braceDepth;
 206  460
                     quoteStack.percentIsFormat = percentIsFormat;
 207  
                 }
 208  
             } else {
 209  725
                 quoteStack = new BraceQuoteTracker(quoteStack, (char)quote, percentIsFormat); // push
 210  
             }
 211  3798
         }
 212  
         /**
 213  
          * Return quote kind if we are reentering a quote
 214  
          *
 215  
          * @return leaving quote
 216  
          */
 217  
         char leaveBrace() {
 218  3795
             if (quoteStack != NULL_BQT && --quoteStack.braceDepth == 0) {
 219  1179
                 return quoteStack.quote;
 220  
             }
 221  2616
             return 0;
 222  
         }
 223  
 
 224  
         boolean rightBraceLikeQuote(int quote) {
 225  37624
             return quoteStack != NULL_BQT && quoteStack.braceDepth == 1 && (quote == 0 || quoteStack.quote == (char)quote);
 226  
         }
 227  
 
 228  
         void leaveQuote() {
 229  724
             assert (quoteStack != NULL_BQT && quoteStack.braceDepth == 0);
 230  724
             quoteStack = quoteStack.next; // pop
 231  724
         }
 232  
 
 233  
         boolean percentIsFormat() {
 234  112
             return quoteStack != NULL_BQT && quoteStack.percentIsFormat;
 235  
         }
 236  
 
 237  
         void resetPercentIsFormat() {
 238  40
             quoteStack.percentIsFormat = false;
 239  40
         }
 240  
 
 241  
         boolean inBraceQuote() {
 242  0
             return quoteStack != NULL_BQT;
 243  
         }
 244  
     }
 245  
 }