/** * This constructor is used by the method "generateParseException" * in the generated parser. Calling this constructor generates * a new object of this type with the fields "currentToken", * "expectedTokenSequences", and "tokenImage" set. */ public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, string[] tokenImageVal ) : base(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)) { currentToken = currentTokenVal; expectedTokenSequences = expectedTokenSequencesVal; tokenImage = tokenImageVal; }
/** * It uses "currentToken" and "expectedTokenSequences" to generate a parse * error message and returns it. If this object has been created * due to a parse error, and you do not catch it (it gets thrown * from the parser) the correct error message * gets displayed. */ private static string initialise(Token currentToken, int[][] expectedTokenSequences, string[] tokenImage) { string eol = System.getProperty("line.separator", "\n"); StringBuilder expected = new StringBuilder(); int maxSize = 0; for (int i = 0; i < expectedTokenSequences.Length; i++) { if (maxSize < expectedTokenSequences[i].Length) { maxSize = expectedTokenSequences[i].Length; } for (int j = 0; j < expectedTokenSequences[i].Length; j++) { expected.Append(tokenImage[expectedTokenSequences[i][j]]).Append(' '); } if (expectedTokenSequences[i][expectedTokenSequences[i].Length - 1] != 0) { expected.Append("..."); } expected.Append(eol).Append(" "); } String retval = "Encountered \""; Token tok = currentToken.next; for (int i = 0; i < maxSize; i++) { if (i != 0) retval += " "; if (tok.kind == 0) { retval += tokenImage[0]; break; } retval += " " + tokenImage[tok.kind]; retval += " \""; retval += add_escapes(tok.image); retval += " \""; tok = tok.next; } retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn; retval += "." + eol; if (expectedTokenSequences.Length == 1) { retval += "Was expecting:" + eol + " "; } else { retval += "Was expecting one of:" + eol + " "; } retval += expected.ToString(); return retval; }