Skip to content

Commit 51506e0

Browse files
tcoxonncannasse
authored andcommitted
Add filename / line number error reporting with -D hscriptPos (#57)
1 parent 4a1432b commit 51506e0

File tree

5 files changed

+87
-22
lines changed

5 files changed

+87
-22
lines changed

hscript/Bytes.hx

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ class Bytes {
7474
return strings[id];
7575
}
7676

77+
function doEncodeInt(v: Int) {
78+
bout.addByte(v & 0xFF);
79+
bout.addByte((v >> 8) & 0xFF);
80+
bout.addByte((v >> 16) & 0xFF);
81+
bout.addByte(v >>> 24);
82+
}
83+
7784
function doEncodeConst( c : Const ) {
7885
switch( c ) {
7986
case CInt(v):
@@ -82,10 +89,7 @@ class Bytes {
8289
bout.addByte(v);
8390
} else {
8491
bout.addByte(1);
85-
bout.addByte(v & 0xFF);
86-
bout.addByte((v >> 8) & 0xFF);
87-
bout.addByte((v >> 16) & 0xFF);
88-
bout.addByte(v >>> 24);
92+
doEncodeInt(v);
8993
}
9094
#if !haxe3
9195
case CInt32(v):
@@ -105,13 +109,18 @@ class Bytes {
105109
}
106110
}
107111

112+
function doDecodeInt() {
113+
var i = bin.get(pin) | (bin.get(pin+1) << 8) | (bin.get(pin+2) << 16) | (bin.get(pin+3) << 24);
114+
pin += 4;
115+
return i;
116+
}
117+
108118
function doDecodeConst() {
109119
return switch( bin.get(pin++) ) {
110120
case 0:
111121
CInt(bin.get(pin++));
112122
case 1:
113-
var i = bin.get(pin) | (bin.get(pin+1) << 8) | (bin.get(pin+2) << 16) | (bin.get(pin+3) << 24);
114-
pin += 4;
123+
var i = doDecodeInt();
115124
CInt(i);
116125
case 2:
117126
CFloat( Std.parseFloat(doDecodeString()) );
@@ -131,6 +140,8 @@ class Bytes {
131140

132141
function doEncode( e : Expr ) {
133142
#if hscriptPos
143+
doEncodeString(e.origin);
144+
doEncodeInt(e.line);
134145
var e = e.e;
135146
#end
136147
bout.addByte(Type.enumIndex(e));
@@ -241,7 +252,9 @@ class Bytes {
241252

242253
function doDecode() : Expr {
243254
#if hscriptPos
244-
return { e : _doDecode(), pmin : 0, pmax : 0 };
255+
var origin = doDecodeString();
256+
var line = doDecodeInt();
257+
return { e : _doDecode(), pmin : 0, pmax : 0, origin : origin, line : line };
245258
}
246259
function _doDecode() : ExprDef {
247260
#end
@@ -371,4 +384,4 @@ class Bytes {
371384
return b.doDecode();
372385
}
373386

374-
}
387+
}

hscript/Expr.hx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ typedef Expr = {
3838
var e : ExprDef;
3939
var pmin : Int;
4040
var pmax : Int;
41+
var origin : String;
42+
var line : Int;
4143
}
4244
enum ExprDef {
4345
#else
@@ -84,10 +86,48 @@ class Error {
8486
public var e : ErrorDef;
8587
public var pmin : Int;
8688
public var pmax : Int;
87-
public function new(e, pmin, pmax) {
89+
public var origin : String;
90+
public var line : Int;
91+
public function new(e, pmin, pmax, origin, line) {
8892
this.e = e;
8993
this.pmin = pmin;
9094
this.pmax = pmax;
95+
this.origin = origin;
96+
this.line = line;
97+
}
98+
99+
private function errorDefToString(): String {
100+
switch (e) {
101+
case EInvalidChar(c):
102+
return "Invalid character: '"+c+"'";
103+
104+
case EUnexpected(s):
105+
return "Unexpected token: \""+s+"\"";
106+
107+
case EUnterminatedString:
108+
return "Unterminated string";
109+
110+
case EUnterminatedComment:
111+
return "Unterminated comment";
112+
113+
case EUnknownVariable(v):
114+
return "Unknown variable: "+v;
115+
116+
case EInvalidIterator(v):
117+
return "Invalid iterator: "+v;
118+
119+
case EInvalidOp(op):
120+
return "Invalid operator: "+op;
121+
122+
case EInvalidAccess(f):
123+
return "Invalid access to field "+f;
124+
}
125+
}
126+
127+
public function toString(): String {
128+
var message = errorDefToString();
129+
message = origin + ":" + line + ": " + message;
130+
return message;
91131
}
92132
}
93133
enum ErrorDef {

hscript/Interp.hx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* DAMAGE.
2424
*/
2525
package hscript;
26+
import haxe.PosInfos;
2627
import hscript.Expr;
2728

2829
private enum Stop {
@@ -63,10 +64,18 @@ class Interp {
6364
variables.set("null",null);
6465
variables.set("true",true);
6566
variables.set("false",false);
66-
variables.set("trace",function(e) haxe.Log.trace(Std.string(e),cast { fileName : "hscript", lineNumber : 0 }));
67+
variables.set("trace",function(e) haxe.Log.trace(Std.string(e), posInfos()));
6768
initOps();
6869
}
6970

71+
public function posInfos(): PosInfos {
72+
#if hscriptPos
73+
if (curExpr != null)
74+
return cast { fileName : curExpr.origin, lineNumber : curExpr.line };
75+
#end
76+
return cast { fileName : "hscript", lineNumber : 0 };
77+
}
78+
7079
function initOps() {
7180
var me = this;
7281
#if haxe3
@@ -275,7 +284,7 @@ class Interp {
275284

276285
inline function error(e : #if hscriptPos ErrorDef #else Error #end ) : Dynamic {
277286
#if hscriptPos
278-
throw new Error(e, curExpr.pmin, curExpr.pmax);
287+
throw new Error(e, curExpr.pmin, curExpr.pmax, curExpr.origin, curExpr.line);
279288
#else
280289
throw e;
281290
#end
@@ -447,16 +456,16 @@ class Interp {
447456
}
448457
return f;
449458
case EArrayDecl(arr):
450-
if (arr.length > 0 && arr[0].match(Expr.EBinop("=>", _))) {
459+
if (arr.length > 0 && edef(arr[0]).match(EBinop("=>", _))) {
451460
var isAllString:Bool = true;
452461
var isAllInt:Bool = true;
453462
var isAllObject:Bool = true;
454463
var isAllEnum:Bool = true;
455464
var keys:Array<Dynamic> = [];
456465
var values:Array<Dynamic> = [];
457466
for (e in arr) {
458-
switch(e) {
459-
case Expr.EBinop("=>", eKey, eValue): {
467+
switch(edef(e)) {
468+
case EBinop("=>", eKey, eValue): {
460469
var key:Dynamic = expr(eKey);
461470
var value:Dynamic = expr(eValue);
462471
isAllString = isAllString && Std.is(key, String);

hscript/Macro.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,4 @@ class Macro {
234234
}, pos : #if hscriptPos { file : p.file, min : e.pmin, max : e.pmax } #else p #end }
235235
}
236236

237-
}
237+
}

hscript/Parser.hx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Parser {
7777
var uid : Int = 0;
7878

7979
#if hscriptPos
80+
var origin : String;
8081
var readPos : Int;
8182
var tokenMin : Int;
8283
var tokenMax : Int;
@@ -133,7 +134,7 @@ class Parser {
133134

134135
public inline function error( err, pmin, pmax ) {
135136
#if hscriptPos
136-
throw new Error(err, pmin, pmax);
137+
throw new Error(err, pmin, pmax, origin, line);
137138
#else
138139
throw err;
139140
#end
@@ -143,14 +144,15 @@ class Parser {
143144
error(EInvalidChar(c), readPos, readPos);
144145
}
145146

146-
public function parseString( s : String ) {
147-
line = 1;
147+
public function parseString( s : String, ?origin : String = "hscript" ) {
148148
uid = 0;
149-
return parse( new haxe.io.StringInput(s) );
149+
return parse( new haxe.io.StringInput(s), origin );
150150
}
151151

152-
public function parse( s : haxe.io.Input ) {
152+
public function parse( s : haxe.io.Input, ?origin : String = "hscript" ) {
153+
line = 1;
153154
#if hscriptPos
155+
this.origin = origin;
154156
readPos = 0;
155157
tokenMin = oldTokenMin = 0;
156158
tokenMax = oldTokenMax = 0;
@@ -224,9 +226,10 @@ class Parser {
224226

225227
inline function mk(e,?pmin,?pmax) : Expr {
226228
#if hscriptPos
229+
if( e == null ) return null;
227230
if( pmin == null ) pmin = tokenMin;
228231
if( pmax == null ) pmax = tokenMax;
229-
return { e : e, pmin : pmin, pmax : pmax };
232+
return { e : e, pmin : pmin, pmax : pmax, origin : origin, line : line };
230233
#else
231234
return e;
232235
#end
@@ -480,7 +483,7 @@ class Parser {
480483
var e = parseExpr();
481484
mk(EWhile(econd,e),p1,pmax(e));
482485
case "do":
483-
var e = parseExpr();
486+
var e = parseExpr();
484487
var tk = token();
485488
switch(tk)
486489
{

0 commit comments

Comments
 (0)