001/* 002 * Copyright (c) 2012-2017 Institut National des Sciences Appliquées de Lyon (INSA-Lyon) 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 */ 009package org.eclipse.golo.compiler.parser; 010 011import static gololang.Messages.message; 012 013/** 014 * This exception is thrown when parse errors are encountered. 015 * You can explicitly create objects of this exception type by 016 * calling the method generateParseException in the generated 017 * parser. 018 * 019 * You can modify this class to customize your error reporting 020 * mechanisms so long as you retain the public fields. 021 */ 022public class ParseException extends Exception { 023 024 /** 025 * The version identifier for this Serializable class. 026 * Increment only if the <i>serialized</i> form of the 027 * class changes. 028 */ 029 private static final long serialVersionUID = 1L; 030 031 /** 032 * The end of line string for this machine. 033 */ 034 protected static String EOL = System.getProperty("line.separator", "\n"); 035 036 /** 037 * This constructor is used by the method "generateParseException" 038 * in the generated parser. Calling this constructor generates 039 * a new object of this type with the fields "currentToken", 040 * "expectedTokenSequences", and "tokenImage" set. 041 */ 042 public ParseException(Token currentTokenVal, 043 int[][] expectedTokenSequencesVal, 044 String[] tokenImageVal 045 ) { 046 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); 047 currentToken = currentTokenVal; 048 expectedTokenSequences = expectedTokenSequencesVal; 049 tokenImage = tokenImageVal; 050 } 051 052 /** 053 * The following constructors are for use by you for whatever 054 * purpose you can think of. Constructing the exception in this 055 * manner makes the exception behave in the normal way - i.e., as 056 * documented in the class "Throwable". The fields "errorToken", 057 * "expectedTokenSequences", and "tokenImage" do not contain 058 * relevant information. The JavaCC generated code does not use 059 * these constructors. 060 */ 061 062 public ParseException() { 063 super(); 064 } 065 066 /** 067 * Constructor with message. 068 */ 069 public ParseException(String message) { 070 super(message); 071 } 072 073 074 /** 075 * This is the last token that has been consumed successfully. If 076 * this object has been created due to a parse error, the token 077 * followng this token will (therefore) be the first error token. 078 */ 079 public Token currentToken; 080 081 /** 082 * Each entry in this array is an array of integers. Each array 083 * of integers represents a sequence of tokens (by their ordinal 084 * values) that is expected at this point of the parse. 085 */ 086 public int[][] expectedTokenSequences; 087 088 /** 089 * This is a reference to the "tokenImage" array of the generated 090 * parser within which the parse error occurred. This array is 091 * defined in the generated ...Constants interface. 092 */ 093 public String[] tokenImage; 094 095 /** 096 * It uses "currentToken" and "expectedTokenSequences" to generate a parse 097 * error message and returns it. If this object has been created 098 * due to a parse error, and you do not catch it (it gets thrown 099 * from the parser) the correct error message 100 * gets displayed. 101 */ 102 private static String initialise(Token currentToken, 103 int[][] expectedTokenSequences, 104 String[] tokenImage) { 105 106 StringBuffer expected = new StringBuffer(); 107 int maxSize = 0; 108 for (int i = 0; i < expectedTokenSequences.length; i++) { 109 if (maxSize < expectedTokenSequences[i].length) { 110 maxSize = expectedTokenSequences[i].length; 111 } 112 for (int j = 0; j < expectedTokenSequences[i].length; j++) { 113 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' '); 114 } 115 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) { 116 expected.append("..."); 117 } 118 expected.append(EOL).append(" "); 119 } 120 String retval = message("unexpected_token"); 121 Token tok = currentToken.next; 122 for (int i = 0; i < maxSize; i++) { 123 if (i != 0) retval += " "; 124 if (tok.kind == 0) { 125 retval += tokenImage[0]; 126 break; 127 } 128 retval += " " + tokenImage[tok.kind]; 129 retval += " `"; 130 retval += add_escapes(tok.image); 131 retval += "` "; 132 tok = tok.next; 133 } 134 retval += message("source_position", currentToken.next.beginLine, currentToken.next.beginColumn); 135 136 return retval; 137 } 138 139 /** 140 * Used to convert raw characters to their escaped version 141 * when these raw version cannot be used as part of an ASCII 142 * string literal. 143 */ 144 static String add_escapes(String str) { 145 StringBuffer retval = new StringBuffer(); 146 char ch; 147 for (int i = 0; i < str.length(); i++) { 148 switch (str.charAt(i)) { 149 case '\b': 150 retval.append("\\b"); 151 continue; 152 case '\t': 153 retval.append("\\t"); 154 continue; 155 case '\n': 156 retval.append("\\n"); 157 continue; 158 case '\f': 159 retval.append("\\f"); 160 continue; 161 case '\r': 162 retval.append("\\r"); 163 continue; 164 case '\"': 165 retval.append("\\\""); 166 continue; 167 case '\'': 168 retval.append("\\\'"); 169 continue; 170 case '\\': 171 retval.append("\\\\"); 172 continue; 173 default: 174 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) { 175 String s = "0000" + Integer.toString(ch, 16); 176 retval.append("\\u" + s.substring(s.length() - 4, s.length())); 177 } else { 178 retval.append(ch); 179 } 180 continue; 181 } 182 } 183 return retval.toString(); 184 } 185 186}