001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019
020package lucee.runtime.sql.old;
021
022public final class ParseException extends Exception
023{
024
025 public ParseException(Token token, int ai[][], String as[])
026 {
027     super("");
028     eol = System.getProperty("line.separator", "\n");
029     specialConstructor = true;
030     currentToken = token;
031     expectedTokenSequences = ai;
032     tokenImage = as;
033 }
034
035 public ParseException()
036 {
037     eol = System.getProperty("line.separator", "\n");
038     specialConstructor = false;
039 }
040
041 public ParseException(String s)
042 {
043     super(s);
044     eol = System.getProperty("line.separator", "\n");
045     specialConstructor = false;
046 }
047
048 public String getMessage()
049 {
050     if(!specialConstructor)
051         return super.getMessage();
052     String s = "";
053     int i = 0;
054     for(int j = 0; j < expectedTokenSequences.length; j++)
055     {
056         if(i < expectedTokenSequences[j].length)
057             i = expectedTokenSequences[j].length;
058         for(int k = 0; k < expectedTokenSequences[j].length; k++)
059             s = s + tokenImage[expectedTokenSequences[j][k]] + " ";
060
061         if(expectedTokenSequences[j][expectedTokenSequences[j].length - 1] != 0)
062             s = s + "...";
063         s = s + eol + "    ";
064     }
065
066     String s1 = "Encountered \"";
067     Token token = currentToken.next;
068     for(int l = 0; l < i; l++)
069     {
070         if(l != 0)
071             s1 = s1 + " ";
072         if(token.kind == 0)
073         {
074             s1 = s1 + tokenImage[0];
075             break;
076         }
077         s1 = s1 + add_escapes(token.image);
078         token = token.next;
079     }
080
081     s1 = s1 + "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
082     s1 = s1 + "." + eol;
083     if(expectedTokenSequences.length == 1)
084         s1 = s1 + "Was expecting:" + eol + "    ";
085     else
086         s1 = s1 + "Was expecting one of:" + eol + "    ";
087     s1 = s1 + s;
088     return s1;
089 }
090
091 protected String add_escapes(String s)
092 {
093     StringBuffer stringbuffer = new StringBuffer();
094     for(int i = 0; i < s.length(); i++)
095     {
096         char c;
097         switch(s.charAt(i))
098         {
099         case 0: // '\0'
100             break;
101
102         case 8: // '\b'
103             stringbuffer.append("\\b");
104             break;
105
106         case 9: // '\t'
107             stringbuffer.append("\\t");
108             break;
109
110         case 10: // '\n'
111             stringbuffer.append("\\n");
112             break;
113
114         case 12: // '\f'
115             stringbuffer.append("\\f");
116             break;
117
118         case 13: // '\r'
119             stringbuffer.append("\\r");
120             break;
121
122         case 34: // '"'
123             stringbuffer.append("\\\"");
124             break;
125
126         case 39: // '\''
127             stringbuffer.append("\\'");
128             break;
129
130         case 92: // '\\'
131             stringbuffer.append("\\\\");
132             break;
133
134         default:
135             if((c = s.charAt(i)) < ' ' || c > '~')
136             {
137                 String s1 = "0000" + Integer.toString(c, 16);
138                 stringbuffer.append("\\u" + s1.substring(s1.length() - 4, s1.length()));
139             } else
140             {
141                 stringbuffer.append(c);
142             }
143             break;
144         }
145     }
146
147     return stringbuffer.toString();
148 }
149
150 protected boolean specialConstructor;
151 public Token currentToken;
152 public int expectedTokenSequences[][];
153 public String tokenImage[];
154 protected String eol;
155}