JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/ParseException.java
1/* Generated by: ParserGeneratorCC: Do not edit this line. ParseException.java Version 1.1 */
2/* ParserGeneratorCCOptions:KEEP_LINE_COLUMN=true */
3/*
4 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
5 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
6 *
7 * This file is part of JavaParser.
8 *
9 * JavaParser can be used either under the terms of
10 * a) the GNU Lesser General Public License as published by
11 *     the Free Software Foundation, either version 3 of the License, or
12 *     (at your option) any later version.
13 * b) the terms of the Apache License
14 *
15 * You should have received a copy of both licenses in LICENCE.LGPL and
16 * LICENCE.APACHE. Please refer to those files for details.
17 *
18 * JavaParser is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU Lesser General Public License for more details.
22 */
23package com.github.javaparser;
24
25/**
26 * This exception is thrown when parse errors are encountered.
27 * You can explicitly create objects of this exception type by
28 * calling the method generateParseException in the generated
29 * parser.
30 *
31 * You can modify this class to customize your error reporting
32 * mechanisms so long as you retain the public fields.
33 */
34public class ParseException extends Exception {
35  private static final String INDENT = "    ";
36  
37  /**
38   * The end of line string (we do not use System.getProperty("") so that we are compatible with Android/GWT);
39   */
40  protected static final String EOL = "\n";
41
42  
43  public ParseException(final Token currentTokenVal,
44                        final int[][] expectedTokenSequencesVal,
45                        final String[] tokenImageVal)
46  {
47    this (currentTokenValexpectedTokenSequencesValtokenImageValnull);
48  }
49  
50  /**
51   * This constructor is used by the method "generateParseException"
52   * in the generated parser.  Calling this constructor generates
53   * a new object of this type with the fields "currentToken",
54   * "expectedTokenSequences", and "tokenImage" set.
55   */
56  public ParseException(final Token currentTokenVal,
57                        final int[][] expectedTokenSequencesVal,
58                        final String[] tokenImageVal,
59                        final String lexicalStateName)
60  {
61    super(_initialise(currentTokenValexpectedTokenSequencesValtokenImageVallexicalStateName));
62    currentToken = currentTokenVal;
63    expectedTokenSequences = expectedTokenSequencesVal;
64    tokenImage = tokenImageVal;
65  }
66
67  /**
68   * The following constructors are for use by you for whatever
69   * purpose you can think of.  Constructing the exception in this
70   * manner makes the exception behave in the normal way - i.e., as
71   * documented in the class "Throwable".  The fields "errorToken",
72   * "expectedTokenSequences", and "tokenImage" do not contain
73   * relevant information.  The JavaCC generated code does not use
74   * these constructors.
75   */
76
77  public ParseException() {
78    super();
79  }
80
81  /** Constructor with message. */
82  public ParseException(String message) {
83    super(message);
84  }
85
86
87  /**
88   * This is the last token that has been consumed successfully.  If
89   * this object has been created due to a parse error, the token
90   * followng this token will (therefore) be the first error token.
91   */
92  public Token currentToken;
93
94  /**
95   * Each entry in this array is an array of integers.  Each array
96   * of integers represents a sequence of tokens (by their ordinal
97   * values) that is expected at this point of the parse.
98   */
99  public int[][] expectedTokenSequences;
100
101  /**
102   * This is a reference to the "tokenImage" array of the generated
103   * parser within which the parse error occurred.  This array is
104   * defined in the generated ...Constants interface.
105   */
106  public String[] tokenImage;
107
108  /**
109   * It uses "currentToken" and "expectedTokenSequences" to generate a parse
110   * error message and returns it.  If this object has been created
111   * due to a parse error, and you do not catch it (it gets thrown
112   * from the parser) the correct error message
113   * gets displayed.
114   */
115  private static String _initialise(final Token currentToken,
116                                    final int[][] expectedTokenSequences,
117                                    final String[] tokenImage,
118                                    final String lexicalStateName)
119  {
120    StringBuilder expected = new StringBuilder();
121    
122    int maxSize = 0;
123    java.util.TreeSet<StringsortedOptions = new java.util.TreeSet<String>();
124    for (int i = 0i < expectedTokenSequences.lengthi++) {
125      if (maxSize < expectedTokenSequences[i].length)
126        maxSize = expectedTokenSequences[i].length;
127      for (int j = 0j < expectedTokenSequences[i].lengthj++)
128        sortedOptions.add(tokenImage[expectedTokenSequences[i][j]]);
129    }
130    
131    for (String option : sortedOptions)
132      expected.append(INDENT).append(option).append(EOL);
133    
134    StringBuilder sb = new StringBuilder();
135    sb.append ("Encountered unexpected token:");
136
137    Token tok = currentToken.next;
138    for (int i = 0i < maxSizei++) {
139      String tokenText = tok.image;
140        String escapedTokenText = add_escapes(tokenText);
141      if (i != 0)
142          sb.append (' ');
143      if (tok.kind == 0) {
144          sb.append(tokenImage[0]);
145        break;
146      }
147      sb.append(" \"");
148      sb.append(escapedTokenText);
149      sb.append("\"");
150      sb.append(" " + tokenImage[tok.kind]);
151      tok = tok.next;
152    }
153    sb.append (EOL)
154      .append (INDENT)
155      .append ("at line ")
156      .append (currentToken.next.beginLine)
157      .append (", column ")
158      .append (currentToken.next.beginColumn);
159    sb.append(".").append(EOL);
160    
161    if (expectedTokenSequences.length == 0) {
162      // Nothing to add here
163    } else {
164      sb.append (EOL)
165        .append ("Was expecting")
166        .append (expectedTokenSequences.length == 1 ? ":" : " one of:")
167        .append (EOL)
168        .append (EOL)
169        .append (expected);
170    }
171    // 2013/07/30 --> Seems to be inaccurate as represents the readahead state, not the lexical state BEFORE the unknown token
172//    if (lexicalStateName != null) {
173//        sb.append(EOL).append("** Lexical State : ").append(lexicalStateName).append(EOL).append(EOL);
174//    }
175    
176    return sb.toString();
177  }
178
179
180  /**
181   * Used to convert raw characters to their escaped version
182   * when these raw version cannot be used as part of an ASCII
183   * string literal.
184   */
185  static String add_escapes(String str) {
186      StringBuilder retval = new StringBuilder();
187      for (int i = 0i < str.length(); i++) {
188        final char ch = str.charAt(i);
189        switch (ch)
190        {
191           case '\b':
192              retval.append("\\b");
193              continue;
194           case '\t':
195              retval.append("\\t");
196              continue;
197           case '\n':
198              retval.append("\\n");
199              continue;
200           case '\f':
201              retval.append("\\f");
202              continue;
203           case '\r':
204              retval.append("\\r");
205              continue;
206           case '\"':
207              retval.append("\\\"");
208              continue;
209           case '\'':
210              retval.append("\\\'");
211              continue;
212           case '\\':
213              retval.append("\\\\");
214              continue;
215           default:
216              if (ch < 0x20 || ch > 0x7e) {
217                 String s = "0000" + Integer.toString(ch16);
218                 retval.append("\\u" + s.substring(s.length() - 4s.length()));
219              } else {
220                 retval.append(ch);
221              }
222              continue;
223        }
224      }
225      return retval.toString();
226   }
227}
228/* ParserGeneratorCC - OriginalChecksum=50aabe88cfb586d9a299a44032d0c1f7 (do not edit this line) */
229
MembersX
ParseException:ParseException
ParseException:_initialise:Block:Block:tokenText
ParseException:add_escapes:Block:retval
ParseException:_initialise:Block:sb
ParseException:_initialise:Block:sortedOptions
ParseException:_initialise
ParseException:add_escapes:Block:Block:ch
ParseException:_initialise:Block:Block:escapedTokenText
ParseException:tokenImage
ParseException:_initialise:Block:tok
ParseException:_initialise:Block:maxSize
ParseException:add_escapes:Block:Block:Block:s
ParseException:EOL
ParseException:currentToken
ParseException:_initialise:Block:expected
ParseException:expectedTokenSequences
ParseException:INDENT
ParseException:add_escapes
Members
X