aboutsummaryrefslogtreecommitdiffstats
path: root/infrastructure/rhino1_7R1/src/org/mozilla/javascript/InformativeParser.java
blob: c73db34e72fd437e4e9de4b4c8dad8c29f06d998 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package org.mozilla.javascript;

import java.io.IOException;

/**
 *  Subclass of Rhino's Parser that saves information about the token stream
 *  and error message to allow more helpful error messages.
 *
 * @author David Greenspan for AppJet
 */

/*
  This class is written with speed in mind, to some extent.  Rhino's tokenizer
  is pretty efficient, and we wouldn't want to slow it down by, for example,
  creating a TokenInfo object on the heap for every token seen.
 */

/*APPJET*/
public class InformativeParser extends Parser {

    public static class InformativeEvaluatorException extends EvaluatorException {
	final ParseErrorInfo pei;
	
	InformativeEvaluatorException(String errorMessage,
				      String sourceName, int lineNumber,
				      String lineSource, int columnNumber,
				      ParseErrorInfo peInfo) {
	    super(errorMessage, sourceName,
		  lineNumber, lineSource, columnNumber);
	    pei = peInfo;
	}

	public ParseErrorInfo getParseErrorInfo() {
	    return pei;
	}
    }

    public static class ParseErrorInfo {
	ParseErrorInfo() {}

	String messageId = null;
	String messageArg = null;
	
	final int tokenMaxHistory = 10;
	// ring buffers
	final int[] tokenTypes = new int[tokenMaxHistory];
	final String[] tokenStrings = new String[tokenMaxHistory];
	final double[] tokenNumbers = new double[tokenMaxHistory];
	final int[] tokenLineNumbers = new int[tokenMaxHistory];
	final int[] tokenLineOffsets = new int[tokenMaxHistory];
	int nextBufPos = 0;
	int historyLength = 0;
	boolean tokenPeeking = false;
	int peekSlot;

	void reportPeekToken(int type, String str, double num, int lineno,
			     int lineOffset) {
	    if (! tokenPeeking) {
		peekSlot = nextBufPos;
		tokenTypes[nextBufPos] = type;
		tokenStrings[nextBufPos] = str;
		tokenNumbers[nextBufPos] = num;
		tokenLineNumbers[nextBufPos] = lineno;
		tokenLineOffsets[nextBufPos] = lineOffset;
		
		nextBufPos++;
		if (nextBufPos == tokenMaxHistory) nextBufPos = 0;
		if (historyLength < tokenMaxHistory) historyLength++;
		tokenPeeking = true;
	    }
	}

	void reportConsumeToken() {
	    tokenPeeking = false;
	}

	private TokenInfo backToken(int n) {
	    // 0 is most recent token added to history
	    if (n >= historyLength) return null;
	    int i = (nextBufPos - 1 - n);
	    while (i < 0) i += tokenMaxHistory;
	    return new TokenInfo(tokenTypes[i], tokenStrings[i],
				 tokenNumbers[i], tokenLineNumbers[i],
				 tokenLineOffsets[i]);
	}
	
	public String getMessageId() { return messageId; }
	public String getMessageArg() { return messageArg; }
	public TokenInfo getPeekToken() {
	    if (tokenPeeking) return backToken(0);
	    return null;
	}
	public TokenInfo getPrevToken(int n) {
	    // 1 = last non-peek token seen, 2 = before that, etc.
	    if (! tokenPeeking) n--;
	    return backToken(n);
	}
	public TokenInfo getPrevToken() {
	    return getPrevToken(1);
	}
    }

    public static class TokenInfo {
	private int type, lineno, lineOffset;
	private String str;
	private double num;
	TokenInfo(int type, String str, double num, int lineno,
		  int lineOffset) {
	    this.type = type; this.str = str; this.num = num;
	    this.lineno = lineno; this.lineOffset = lineOffset;
	}
	public int getType() { return type; }
	public int getLineNumber() { return lineno; }
	public int getLineOffset() { return lineOffset; }
	public double getNumber() { return num; }
	public String getString() { return str; }
    }
    
    ParseErrorInfo info = new ParseErrorInfo();

    void doErrorReporterError(String message, String sourceURI, int line,
			      String lineText, int lineOffset) {
	
	throw new InformativeEvaluatorException(message, sourceURI, line,
						lineText, lineOffset, info);
	
    }
    
    public InformativeParser(CompilerEnvirons compilerEnv) {
	// we override most calls to the parent's ErrorReporter anyway
	super(compilerEnv, DefaultErrorReporter.instance);
    }

    @Override int peekToken() throws IOException {
	int tt = super.peekToken();
	info.reportPeekToken(tt, ts.getString(), ts.getNumber(),
			     ts.getLineno(), ts.getOffset());
	return tt;
    }
    @Override void consumeToken() {
	super.consumeToken();
	info.reportConsumeToken();
    }

    @Override void addWarning(String messageId, String messageArg)
    {
	info.messageId = messageId;
	info.messageArg = messageArg;
	
        String message = ScriptRuntime.getMessage1(messageId, messageArg);
        if (compilerEnv.reportWarningAsError()) {
            ++syntaxErrorCount;
            doErrorReporterError(message, sourceURI, ts.getLineno(),
				 ts.getLine(), ts.getOffset());
        }
	else { /* don't report */ }
    }
    
    @Override void addError(String messageId)
    {
	info.messageId = messageId;
	
	++syntaxErrorCount;
        String message = ScriptRuntime.getMessage0(messageId);
        doErrorReporterError(message, sourceURI, ts.getLineno(),
			     ts.getLine(), ts.getOffset());
    }

    @Override void addError(String messageId, String messageArg)
    {
	info.messageId = messageId;
	info.messageArg = messageArg;
	
	++syntaxErrorCount;
        String message = ScriptRuntime.getMessage1(messageId, messageArg);
        doErrorReporterError(message, sourceURI, ts.getLineno(),
			     ts.getLine(), ts.getOffset());
    }

    @Override protected Decompiler createDecompiler(CompilerEnvirons env) {
	return new MyDecompiler();
    }
    
    public static final ErrorReporter THROW_INFORMATIVE_ERRORS
	= new ErrorReporter() {
		public void warning(String message, String sourceURI, int line,
				    String lineText, int lineOffset) {
		    DefaultErrorReporter.instance.warning
			(message, sourceURI, line, lineText, lineOffset);
		}
		public void error(String message, String sourceURI, int line,
				  String lineText, int lineOffset) {
		    DefaultErrorReporter.instance.error
			(message, sourceURI, line, lineText, lineOffset);
		}
		public EvaluatorException runtimeError(String message,
						       String sourceURI,
						       int line, String lineText,
						       int lineOffset) {
		    return DefaultErrorReporter.instance.runtimeError
			(message, sourceURI, line, lineText, lineOffset);
		}
		
	    };
    
    public static Parser makeParser(CompilerEnvirons compilerEnv,
				    ErrorReporter errorReporter) {
	if (errorReporter == THROW_INFORMATIVE_ERRORS) {
	    return new InformativeParser(compilerEnv);
	}
	else {
	    return new Parser(compilerEnv, errorReporter);
	}
    }

    private class MyDecompiler extends Decompiler {
	@Override void addRegexp(String regexp, String flags) {
	    super.addRegexp(regexp, flags);
	    String str = '/'+regexp+'/'+flags;
	    info.reportPeekToken(Token.REGEXP, str, ts.getNumber(),
				 ts.getLineno(), ts.getOffset());
	    info.reportConsumeToken();
	}
    }
}