Skip to content

Commit e0e2452

Browse files
author
James Roseborough
committed
Improve processing of first-line comments by LuaParser
1 parent 6ac7e6e commit e0e2452

File tree

5 files changed

+53
-103
lines changed

5 files changed

+53
-103
lines changed

grammar/Lua52.jj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ TOKEN :
133133
| < #QUOTED: <DECIMAL> | <UNICODE> | <CHAR> >
134134
| < #DECIMAL: "\\" ["0"-"9"] (["0"-"9"])? (["0"-"9"])? >
135135
| < DBCOLON: "::" >
136-
| < SHEBANG: "#" ["!"," "] (~["\n","\r"])* (<LF>)? >
137136
| < #UNICODE: "\\" "u" <HEXDIGIT> <HEXDIGIT> <HEXDIGIT> <HEXDIGIT> >
138137
| < #CHAR: "\\" (~[]) >
139138
| < #LF: ("\n" | "\r" | "\r\n") >
@@ -143,7 +142,7 @@ TOKEN :
143142
void Chunk():
144143
{}
145144
{
146-
( <SHEBANG> )? Block() <EOF>
145+
( "#" { token_source.SwitchTo(IN_COMMENT); } )? Block() <EOF>
147146
}
148147

149148
void Block():

grammar/LuaParser.jj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ TOKEN :
157157
| < #QUOTED: <DECIMAL> | <UNICODE> | <CHAR> >
158158
| < #DECIMAL: "\\" ["0"-"9"] (["0"-"9"])? (["0"-"9"])? >
159159
| < DBCOLON: "::" >
160-
| < SHEBANG: "#" ["!"," "] (~["\n","\r"])* (<LF>)? >
161160
| < #UNICODE: "\\" "u" <HEXDIGIT> <HEXDIGIT> <HEXDIGIT> <HEXDIGIT> >
162161
| < #CHAR: "\\" (~[]) >
163162
| < #LF: ("\n" | "\r" | "\r\n") >
@@ -169,7 +168,7 @@ Chunk Chunk():
169168
Block b;
170169
}
171170
{
172-
( <SHEBANG> )? b=Block() <EOF> { return new Chunk(b); }
171+
( "#" { token_source.SwitchTo(IN_COMMENT); } )? b=Block() <EOF> { return new Chunk(b); }
173172
}
174173

175174
Block Block():

src/jse/org/luaj/vm2/parser/LuaParser.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ private static Exp.FuncCall assertfunccall(Exp.PrimaryExp pe) throws ParseExcept
2727
final public Chunk Chunk() throws ParseException {
2828
Block b;
2929
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
30-
case SHEBANG:
31-
jj_consume_token(SHEBANG);
30+
case 69:
31+
jj_consume_token(69);
32+
token_source.SwitchTo(IN_COMMENT);
3233
break;
3334
default:
3435
;
3536
}
3637
b = Block();
3738
jj_consume_token(0);
38-
{if (true) return new Chunk(b);}
39+
{if (true) return new Chunk(b);}
3940
throw new Error("Missing return statement in function");
4041
}
4142

@@ -264,11 +265,11 @@ final public Stat ReturnStat() throws ParseException {
264265
case NUMBER:
265266
case STRING:
266267
case CHARSTRING:
268+
case 69:
267269
case 75:
268270
case 79:
269271
case 80:
270272
case 83:
271-
case 95:
272273
el = ExpList();
273274
break;
274275
default:
@@ -472,11 +473,11 @@ final public FuncArgs FuncArgs() throws ParseException {
472473
case NUMBER:
473474
case STRING:
474475
case CHARSTRING:
476+
case 69:
475477
case 75:
476478
case 79:
477479
case 80:
478480
case 83:
479-
case 95:
480481
el = ExpList();
481482
break;
482483
default:
@@ -665,8 +666,8 @@ final public Exp Exp() throws ParseException {
665666
e = SimpleExp();
666667
break;
667668
case NOT:
669+
case 69:
668670
case 83:
669-
case 95:
670671
op = Unop();
671672
s = Exp();
672673
e=Exp.unaryexp(op,s);
@@ -764,12 +765,12 @@ final public TableConstructor TableConstructor() throws ParseException {
764765
case NUMBER:
765766
case STRING:
766767
case CHARSTRING:
768+
case 69:
767769
case 75:
768770
case 77:
769771
case 79:
770772
case 80:
771773
case 83:
772-
case 95:
773774
fl = FieldList();
774775
tc.fields=fl;
775776
break;
@@ -843,11 +844,11 @@ final public TableField Field() throws ParseException {
843844
case NUMBER:
844845
case STRING:
845846
case CHARSTRING:
847+
case 69:
846848
case 75:
847849
case 79:
848850
case 80:
849851
case 83:
850-
case 95:
851852
rhs = Exp();
852853
{if (true) return TableField.listField(rhs);}
853854
break;
@@ -953,8 +954,8 @@ final public int Unop() throws ParseException {
953954
jj_consume_token(NOT);
954955
{if (true) return Lua.OP_NOT;}
955956
break;
956-
case 95:
957-
jj_consume_token(95);
957+
case 69:
958+
jj_consume_token(69);
958959
{if (true) return Lua.OP_LEN;}
959960
break;
960961
default:
@@ -1006,6 +1007,11 @@ private boolean jj_2_7(int xla) {
10061007
catch(LookaheadSuccess ls) { return true; }
10071008
}
10081009

1010+
private boolean jj_3R_28() {
1011+
if (jj_scan_token(91)) return true;
1012+
return false;
1013+
}
1014+
10091015
private boolean jj_3R_27() {
10101016
if (jj_scan_token(90)) return true;
10111017
return false;
@@ -1396,7 +1402,7 @@ private boolean jj_3R_12() {
13961402
}
13971403

13981404
private boolean jj_3R_55() {
1399-
if (jj_scan_token(95)) return true;
1405+
if (jj_scan_token(69)) return true;
14001406
return false;
14011407
}
14021408

@@ -1514,11 +1520,6 @@ private boolean jj_3R_29() {
15141520
return false;
15151521
}
15161522

1517-
private boolean jj_3R_28() {
1518-
if (jj_scan_token(91)) return true;
1519-
return false;
1520-
}
1521-
15221523
/** Generated Token Manager. */
15231524
public LuaParserTokenManager token_source;
15241525
SimpleCharStream jj_input_stream;

src/jse/org/luaj/vm2/parser/LuaParserConstants.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,11 @@ public interface LuaParserConstants {
107107
/** RegularExpression Id. */
108108
int DBCOLON = 65;
109109
/** RegularExpression Id. */
110-
int SHEBANG = 66;
110+
int UNICODE = 66;
111111
/** RegularExpression Id. */
112-
int UNICODE = 67;
112+
int CHAR = 67;
113113
/** RegularExpression Id. */
114-
int CHAR = 68;
115-
/** RegularExpression Id. */
116-
int LF = 69;
114+
int LF = 68;
117115

118116
/** Lexical state. */
119117
int DEFAULT = 0;
@@ -208,10 +206,10 @@ public interface LuaParserConstants {
208206
"<QUOTED>",
209207
"<DECIMAL>",
210208
"\"::\"",
211-
"<SHEBANG>",
212209
"<UNICODE>",
213210
"<CHAR>",
214211
"<LF>",
212+
"\"#\"",
215213
"\";\"",
216214
"\"=\"",
217215
"\",\"",
@@ -237,7 +235,6 @@ public interface LuaParserConstants {
237235
"\">=\"",
238236
"\"==\"",
239237
"\"~=\"",
240-
"\"#\"",
241238
};
242239

243240
}

0 commit comments

Comments
 (0)