@@ -22,20 +22,11 @@ BasicFormatter.prototype = {
22
22
23
23
this . lexer = this . options . lexer ;
24
24
this . parser = this . options . parser ;
25
- this . iLine = 0 ; // current line (label)
25
+ this . sLine = "" ; // current line (label) for error messages
26
26
} ,
27
27
28
- /*
29
- reset: function () {
30
- this.iLine = 0; // current line (label)
31
- },
32
- */
33
-
34
- composeError : function ( ) { // varargs
35
- var aArgs = Array . prototype . slice . call ( arguments ) ;
36
-
37
- aArgs . unshift ( "BasicFormatter" ) ;
38
- return Utils . composeError . apply ( null , aArgs ) ;
28
+ composeError : function ( oError , message , value , pos ) {
29
+ return Utils . composeError ( "BasicFormatter" , oError , message , value , pos , this . sLine ) ;
39
30
} ,
40
31
41
32
fnRenumber : function ( sInput , aParseTree , iNew , iOld , iStep , iKeep ) {
@@ -45,17 +36,16 @@ BasicFormatter.prototype = {
45
36
oChanges = { } ,
46
37
47
38
fnCreateLineNumbersMap = function ( ) { // create line numbers map
48
- var iLastLine = 0 ,
39
+ var iLastLine = - 1 ,
49
40
i , oNode , sLine , iLine ;
50
41
51
- oLines [ 0 ] = { // dummy line 0 for: on error goto 0
52
- value : 0
53
- } ;
54
42
for ( i = 0 ; i < aParseTree . length ; i += 1 ) {
55
43
oNode = aParseTree [ i ] ;
56
44
if ( oNode . type === "label" ) {
57
45
sLine = oNode . value ;
58
- iLine = Number ( oNode . value ) ;
46
+ that . sLine = sLine ;
47
+
48
+ iLine = Number ( sLine ) ;
59
49
if ( sLine in oLines ) {
60
50
throw that . composeError ( Error ( ) , "Duplicate line number" , sLine , oNode . pos ) ;
61
51
}
@@ -65,56 +55,23 @@ BasicFormatter.prototype = {
65
55
if ( iLine < 1 || iLine > 65535 ) {
66
56
throw that . composeError ( Error ( ) , "Line number overflow" , sLine , oNode . pos ) ;
67
57
}
68
- oLines [ oNode . value ] = {
58
+ oLines [ sLine ] = {
69
59
value : iLine ,
70
60
pos : oNode . pos ,
71
- len : String ( oNode . orig || oNode . value ) . length
61
+ len : ( oNode . orig || sLine ) . length
72
62
} ;
73
63
iLastLine = iLine ;
74
64
}
75
65
}
76
66
} ,
77
67
78
- /*
79
- fnAddReferences = function (aNodes) {
80
- var i, oNode;
81
-
82
- for (i = 0; i < aNodes.length; i += 1) {
83
- oNode = aNodes[i];
84
- if (oNode.type === "linenumber") {
85
- if (oNode.value in oLines) {
86
- aRefs.push({
87
- value: Number(oNode.value),
88
- pos: oNode.pos,
89
- len: String(oNode.orig || oNode.value).length
90
- });
91
- } else {
92
- throw that.composeError(Error(), "Line does not exist", oNode.value, oNode.pos);
93
- }
94
- }
95
- if (oNode.left) {
96
- fnAddReferences(oNode.left);
97
- }
98
- if (oNode.right) {
99
- fnAddReferences(oNode.right);
100
- }
101
- if (oNode.third) {
102
- fnAddReferences(oNode.third);
103
- }
104
- if (oNode.args) {
105
- fnAddReferences(oNode.args);
106
- }
107
- }
108
- },
109
- */
110
-
111
68
fnAddSingleReference = function ( oNode ) {
112
69
if ( oNode . type === "linenumber" ) {
113
70
if ( oNode . value in oLines ) {
114
71
aRefs . push ( {
115
- value : Number ( oNode . value ) ,
72
+ value : oNode . value ,
116
73
pos : oNode . pos ,
117
- len : String ( oNode . orig || oNode . value ) . length
74
+ len : ( oNode . orig || oNode . value ) . length
118
75
} ) ;
119
76
} else {
120
77
throw that . composeError ( Error ( ) , "Line does not exist" , oNode . value , oNode . pos ) ;
@@ -129,20 +86,23 @@ BasicFormatter.prototype = {
129
86
oNode = aNodes [ i ] ;
130
87
131
88
if ( oNode . type === "label" ) {
132
- //TTT this.iLine = Number( oNode.value); // for error messages
89
+ that . sLine = oNode . value ;
133
90
} else {
134
91
fnAddSingleReference ( oNode ) ;
135
92
}
136
93
137
94
if ( oNode . left ) {
138
95
fnAddSingleReference ( oNode . left ) ;
139
- //fnAddReferences(oNode.left); // recursive for e.g. lineRange ?
140
96
}
141
97
if ( oNode . right ) {
142
98
fnAddSingleReference ( oNode . right ) ;
143
99
}
144
100
if ( oNode . args ) {
145
- fnAddReferences ( oNode . args ) ; // recursive
101
+ if ( oNode . type === "onErrorGoto" && oNode . args . length === 1 && oNode . args [ 0 ] . value === "0" ) {
102
+ // ignore "on error goto 0"
103
+ } else {
104
+ fnAddReferences ( oNode . args ) ; // recursive
105
+ }
146
106
}
147
107
if ( oNode . args2 ) { // for "ELSE"
148
108
fnAddReferences ( oNode . args2 ) ; // recursive
@@ -152,11 +112,12 @@ BasicFormatter.prototype = {
152
112
153
113
fnRenumberLines = function ( ) {
154
114
var aKeys = Object . keys ( oLines ) ,
155
- i , oLine , oRef ;
115
+ i , oLine , iLine , oRef , sLine ;
156
116
157
117
for ( i = 0 ; i < aKeys . length ; i += 1 ) {
158
118
oLine = oLines [ aKeys [ i ] ] ;
159
- if ( oLine . value >= iOld && oLine . value < iKeep ) {
119
+ iLine = Number ( oLine . value ) ;
120
+ if ( iLine >= iOld && iLine < iKeep ) {
160
121
if ( iNew > 65535 ) {
161
122
throw that . composeError ( Error ( ) , "Line number overflow" , oLine . value , oLine . pos ) ;
162
123
}
@@ -168,9 +129,11 @@ BasicFormatter.prototype = {
168
129
169
130
for ( i = 0 ; i < aRefs . length ; i += 1 ) {
170
131
oRef = aRefs [ i ] ;
171
- if ( oRef . value >= iOld && oRef . value < iKeep ) {
172
- if ( oRef . value !== oLines [ oRef . value ] . newLine ) {
173
- oRef . newLine = oLines [ oRef . value ] . newLine ;
132
+ sLine = oRef . value ;
133
+ iLine = Number ( sLine ) ;
134
+ if ( iLine >= iOld && iLine < iKeep ) {
135
+ if ( iLine !== oLines [ sLine ] . newLine ) {
136
+ oRef . newLine = oLines [ sLine ] . newLine ;
174
137
oChanges [ oRef . pos ] = oRef ;
175
138
}
176
139
}
@@ -210,6 +173,7 @@ BasicFormatter.prototype = {
210
173
} ,
211
174
aTokens , aParseTree , sOutput ;
212
175
176
+ this . sLine = "" ; // current line (label)
213
177
try {
214
178
aTokens = this . lexer . lex ( sInput ) ;
215
179
aParseTree = this . parser . parse ( aTokens ) ;
0 commit comments