Skip to content

Commit 7f12b3d

Browse files
Add getLine/column to TokenMgrError+ParseException
This change adds getLine() and getColumn() methods to TokenMgrError and ParseException. (When integrating style-element checking into the HTML checker, those methods were found be useful for identifying the right line and column numbers for parse errors.)
1 parent a0bb2f8 commit 7f12b3d

File tree

5 files changed

+105
-13
lines changed

5 files changed

+105
-13
lines changed

org/w3c/css/parser/CssParseException.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,41 @@ public class CssParseException extends ParseException {
4545

4646
private boolean error;
4747

48+
/**
49+
* Line number of the error.
50+
*/
51+
int errorLine;
52+
53+
/**
54+
* Column number of the error.
55+
*/
56+
int errorColumn;
57+
58+
/**
59+
* Gets the line number of the error.
60+
*/
61+
public int getLine() {
62+
return this.errorLine;
63+
}
64+
65+
/**
66+
* Gets the column number of the error.
67+
*/
68+
public int getColumn() {
69+
return this.errorColumn;
70+
}
71+
4872
/**
4973
* Create a new CssParseException
5074
*/
5175
public CssParseException(Exception exc) {
76+
this.errorLine = -1;
77+
this.errorColumn = -1;
5278
parseException = exc;
5379
if (parseException instanceof ParseException) {
5480
ParseException e = (ParseException) exc;
81+
this.errorLine = e.getLine();
82+
this.errorColumn = e.getColumn();
5583
error = (e.currentToken != null
5684
&& e.expectedTokenSequences != null
5785
&& e.tokenImage != null);

org/w3c/css/parser/analyzer/CssParser.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,8 @@ private static String addOperator(char operator, String value) {
547547
afterImportDeclaration();
548548
jj_consume_token(0);
549549
} catch (TokenMgrError err) {
550-
addError (new ParseException(ac.getMsg().getString("generator.unrecognize")),
550+
addError (new ParseException(ac.getMsg().getString("generator.unrecognize"),
551+
err.getLine(), err.getColumn()),
551552
err.getMessage());
552553
}
553554
}
@@ -5294,14 +5295,6 @@ private boolean jj_2_5(int xla)
52945295
finally { jj_save(4, xla); }
52955296
}
52965297

5297-
private boolean jj_3R_138()
5298-
{
5299-
Token xsp;
5300-
xsp = jj_scanpos;
5301-
if (jj_3R_177()) jj_scanpos = xsp;
5302-
return false;
5303-
}
5304-
53055298
private boolean jj_3R_126()
53065299
{
53075300
if (jj_scan_token(S)) return true;
@@ -5911,6 +5904,14 @@ private boolean jj_3R_129()
59115904
return false;
59125905
}
59135906

5907+
private boolean jj_3R_138()
5908+
{
5909+
Token xsp;
5910+
xsp = jj_scanpos;
5911+
if (jj_3R_177()) jj_scanpos = xsp;
5912+
return false;
5913+
}
5914+
59145915
/** Generated Token Manager. */
59155916
public CssParserTokenManager token_source;
59165917
SimpleCharStream jj_input_stream;

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,8 @@ void parserUnit() :
716716
afterImportDeclaration()
717717
<EOF>
718718
} catch (TokenMgrError err) {
719-
addError (new ParseException(ac.getMsg().getString("generator.unrecognize")),
719+
addError (new ParseException(ac.getMsg().getString("generator.unrecognize"),
720+
err.getLine(), err.getColumn()),
720721
err.getMessage());
721722
}
722723
}

org/w3c/css/parser/analyzer/ParseException.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public ParseException(Token currentTokenVal,
3535
currentToken = currentTokenVal;
3636
expectedTokenSequences = expectedTokenSequencesVal;
3737
tokenImage = tokenImageVal;
38+
this.errorLine = -1;
39+
this.errorColumn = -1;
3840
}
3941

4042
/**
@@ -49,13 +51,47 @@ public ParseException(Token currentTokenVal,
4951

5052
public ParseException() {
5153
super();
54+
this.errorLine = -1;
55+
this.errorColumn = -1;
5256
}
5357

5458
/** Constructor with message. */
5559
public ParseException(String message) {
5660
super(message);
61+
this.errorLine = -1;
62+
this.errorColumn = -1;
5763
}
5864

65+
/** Constructor with message, line number, and column number */
66+
public ParseException(String message, int errorLine, int errorColumn) {
67+
super(message);
68+
this.errorLine = errorLine;
69+
this.errorColumn = errorColumn;
70+
}
71+
72+
/**
73+
* Line number of the error.
74+
*/
75+
int errorLine;
76+
77+
/**
78+
* Column number of the error.
79+
*/
80+
int errorColumn;
81+
82+
/**
83+
* Gets the line number of the error.
84+
*/
85+
public int getLine() {
86+
return this.errorLine;
87+
}
88+
89+
/**
90+
* Gets the column number of the error.
91+
*/
92+
public int getColumn() {
93+
return this.errorColumn;
94+
}
5995

6096
/**
6197
* This is the last token that has been consumed successfully. If

org/w3c/css/parser/analyzer/TokenMgrError.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ public class TokenMgrError extends Error
4343
*/
4444
int errorCode;
4545

46+
/**
47+
* Line number of the error.
48+
*/
49+
int errorLine;
50+
51+
/**
52+
* Column number of the error.
53+
*/
54+
int errorColumn;
55+
56+
/**
57+
* Gets the line number of the error.
58+
*/
59+
public int getLine() {
60+
return this.errorLine;
61+
}
62+
63+
/**
64+
* Gets the column number of the error.
65+
*/
66+
public int getColumn() {
67+
return this.errorColumn;
68+
}
69+
4670
/**
4771
* Replaces unprintable characters by their escaped (or unicode escaped)
4872
* equivalents in the given string
@@ -104,7 +128,7 @@ protected static final String addEscapes(String str) {
104128
* curchar : the offending character
105129
* Note: You can customize the lexical error message by modifying this method.
106130
*/
107-
protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
131+
protected static String LexicalErr(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, int curChar) {
108132
return("Lexical error at line " +
109133
errorLine + ", column " +
110134
errorColumn + ". Encountered: " +
@@ -140,8 +164,10 @@ public TokenMgrError(String message, int reason) {
140164
}
141165

142166
/** Full Constructor. */
143-
public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
144-
this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
167+
public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, int curChar, int reason) {
168+
this(LexicalErr(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
169+
this.errorLine = errorLine;
170+
this.errorColumn = errorColumn;
145171
}
146172
}
147173
/* JavaCC - OriginalChecksum=a0e8995d1ac9564645c0d60018a41934 (do not edit this line) */

0 commit comments

Comments
 (0)