@@ -67,10 +67,7 @@ function lex(text, parseStringsForObjects){
6767 tokens . push ( { index :index , text :ch , fn :fn , json : was ( '[,:' ) && is ( '+-' ) } ) ;
6868 index += 1 ;
6969 } else {
70- throw "Lexer Error: Unexpected next character [" +
71- text . substring ( index ) +
72- "] in expression '" + text +
73- "' at column '" + ( index + 1 ) + "'." ;
70+ throwError ( "Unexpected next character " , index , index + 1 ) ;
7471 }
7572 }
7673 lastCh = ch ;
@@ -103,6 +100,16 @@ function lex(text, parseStringsForObjects){
103100 function isExpOperator ( ch ) {
104101 return ch == '-' || ch == '+' || isNumber ( ch ) ;
105102 }
103+
104+ function throwError ( error , start , end ) {
105+ end = end || index ;
106+ throw Error ( "Lexer Error: " + error + " at column" +
107+ ( isDefined ( start ) ?
108+ "s " + start + "-" + index + " [" + text . substring ( start , end ) + "]" :
109+ " " + end ) +
110+ " in expression [" + text + "]." ) ;
111+ }
112+
106113 function readNumber ( ) {
107114 var number = "" ;
108115 var start = index ;
@@ -121,7 +128,7 @@ function lex(text, parseStringsForObjects){
121128 } else if ( isExpOperator ( ch ) &&
122129 ( ! peekCh || ! isNumber ( peekCh ) ) &&
123130 number . charAt ( number . length - 1 ) == 'e' ) {
124- throw 'Lexer found invalid exponential value "' + text + '"' ;
131+ throwError ( 'Invalid exponent' ) ;
125132 } else {
126133 break ;
127134 }
@@ -151,7 +158,7 @@ function lex(text, parseStringsForObjects){
151158 }
152159 tokens . push ( { index :start , text :ident , fn :fn , json : OPERATORS [ ident ] } ) ;
153160 }
154-
161+
155162 function readString ( quote ) {
156163 var start = index ;
157164 index ++ ;
@@ -165,9 +172,7 @@ function lex(text, parseStringsForObjects){
165172 if ( ch == 'u' ) {
166173 var hex = text . substring ( index + 1 , index + 5 ) ;
167174 if ( ! hex . match ( / [ \d a - f ] { 4 } / i) )
168- throw "Lexer Error: Invalid unicode escape [\\u" +
169- hex + "] starting at column '" +
170- start + "' in expression '" + text + "'." ;
175+ throwError ( "Invalid unicode escape [\\u" + hex + "]" ) ;
171176 index += 4 ;
172177 string += String . fromCharCode ( parseInt ( hex , 16 ) ) ;
173178 } else {
@@ -194,9 +199,7 @@ function lex(text, parseStringsForObjects){
194199 }
195200 index ++ ;
196201 }
197- throw "Lexer Error: Unterminated quote [" +
198- text . substring ( start ) + "] starting at column '" +
199- ( start + 1 ) + "' in expression '" + text + "'." ;
202+ throwError ( "Unterminated quote" , start ) ;
200203 }
201204 function readRegexp ( quote ) {
202205 var start = index ;
@@ -227,9 +230,7 @@ function lex(text, parseStringsForObjects){
227230 }
228231 index ++ ;
229232 }
230- throw "Lexer Error: Unterminated RegExp [" +
231- text . substring ( start ) + "] starting at column '" +
232- ( start + 1 ) + "' in expression '" + text + "'." ;
233+ throwError ( "Unterminated RegExp" , start ) ;
233234 }
234235}
235236
@@ -249,17 +250,16 @@ function parser(text, json){
249250 } ;
250251
251252 ///////////////////////////////////
252-
253- function error ( msg , token ) {
254- throw "Token '" + token . text +
255- "' is " + msg + " at column='" +
256- ( token . index + 1 ) + "' of expression '" +
257- text + "' starting at '" + text . substring ( token . index ) + "'." ;
253+ function throwError ( msg , token ) {
254+ throw Error ( "Parse Error: Token '" + token . text +
255+ "' " + msg + " at column " +
256+ ( token . index + 1 ) + " of expression [" +
257+ text + "] starting at [" + text . substring ( token . index ) + "]." ) ;
258258 }
259259
260260 function peekToken ( ) {
261261 if ( tokens . length === 0 )
262- throw "Unexpected end of expression: " + text ;
262+ throw Error ( "Unexpected end of expression: " + text ) ;
263263 return tokens [ 0 ] ;
264264 }
265265
@@ -280,10 +280,7 @@ function parser(text, json){
280280 if ( token ) {
281281 if ( json && ! token . json ) {
282282 index = token . index ;
283- throw "Expression at column='" +
284- token . index + "' of expression '" +
285- text + "' starting at '" + text . substring ( token . index ) +
286- "' is not valid json." ;
283+ throwError ( "is not valid json" , token ) ;
287284 }
288285 tokens . shift ( ) ;
289286 this . currentToken = token ;
@@ -294,11 +291,7 @@ function parser(text, json){
294291
295292 function consume ( e1 ) {
296293 if ( ! expect ( e1 ) ) {
297- var token = peek ( ) ;
298- throw "Expecting '" + e1 + "' at column '" +
299- ( token . index + 1 ) + "' in '" +
300- text + "' got '" +
301- text . substring ( token . index ) + "'." ;
294+ throwError ( "is unexpected, expecting [" + e1 + "]" , peek ( ) ) ;
302295 }
303296 }
304297
@@ -320,8 +313,7 @@ function parser(text, json){
320313
321314 function assertAllConsumed ( ) {
322315 if ( tokens . length !== 0 ) {
323- throw "Did not understand '" + text . substring ( tokens [ 0 ] . index ) +
324- "' while evaluating '" + text + "'." ;
316+ throwError ( "is extra token not part of expression" , tokens [ 0 ] ) ;
325317 }
326318 }
327319
@@ -387,28 +379,16 @@ function parser(text, json){
387379 }
388380
389381 function expression ( ) {
390- return throwStmt ( ) ;
391- }
392-
393- function throwStmt ( ) {
394- if ( expect ( 'throw' ) ) {
395- var throwExp = assignment ( ) ;
396- return function ( self ) {
397- throw throwExp ( self ) ;
398- } ;
399- } else {
400- return assignment ( ) ;
401- }
382+ return assignment ( ) ;
402383 }
403384
404385 function assignment ( ) {
405386 var left = logicalOR ( ) ;
406387 var token ;
407388 if ( token = expect ( '=' ) ) {
408389 if ( ! left . isAssignable ) {
409- throw "Left hand side '" +
410- text . substring ( 0 , token . index ) + "' of assignment '" +
411- text . substring ( token . index ) + "' is not assignable." ;
390+ throwError ( "implies assignment but [" +
391+ text . substring ( 0 , token . index ) + "] can not be assigned to" , token ) ;
412392 }
413393 var ident = function ( ) { return left . isAssignable ; } ;
414394 return binaryFn ( ident , token . fn , logicalOR ( ) ) ;
@@ -498,8 +478,7 @@ function parser(text, json){
498478 instance = instance [ key ] ;
499479 }
500480 if ( typeof instance != $function ) {
501- throw "Function '" + token . text + "' at column '" +
502- ( token . index + 1 ) + "' in '" + text + "' is not defined." ;
481+ throwError ( "should be a function" , token ) ;
503482 }
504483 return instance ;
505484 }
@@ -518,7 +497,7 @@ function parser(text, json){
518497 var token = expect ( ) ;
519498 primary = token . fn ;
520499 if ( ! primary ) {
521- error ( "not a primary expression" , token ) ;
500+ throwError ( "not a primary expression" , token ) ;
522501 }
523502 }
524503 var next ;
@@ -530,7 +509,7 @@ function parser(text, json){
530509 } else if ( next . text === '.' ) {
531510 primary = fieldAccess ( primary ) ;
532511 } else {
533- throw "IMPOSSIBLE" ;
512+ throwError ( "IMPOSSIBLE" ) ;
534513 }
535514 }
536515 return primary ;
0 commit comments