3838import com .dashjoin .jsonata .Jsonata .Frame ;
3939import com .dashjoin .jsonata .Tokenizer .Token ;
4040import com .dashjoin .jsonata .utils .Signature ;
41+ import org .slf4j .Logger ;
42+ import org .slf4j .LoggerFactory ;
4143
4244//var parseSignature = require('./signature');
4345@ SuppressWarnings ({"unchecked" })
4446public class Parser implements Serializable {
4547
46- boolean dbg = false ;
48+ private static final Logger log = LoggerFactory . getLogger ( Parser . class ) ;
4749
4850 // This parser implements the 'Top down operator precedence' algorithm developed by Vaughan R Pratt; http://dl.acm.org/citation.cfm?id=512931.
4951 // and builds on the Javascript framework described by Douglas Crockford at http://javascript.crockford.com/tdop/tdop.html
@@ -212,11 +214,11 @@ public Symbol create() {
212214 // We want a shallow clone (do not duplicate outer class!)
213215 try {
214216 Symbol cl = (Symbol ) this .clone ();
215- //System.err.println ("cloning "+this+" clone="+ cl);
217+ //log.info ("cloning {} clone={}", this, cl);
216218 return cl ;
217219 } catch (CloneNotSupportedException e ) {
218220 // never reached
219- if ( dbg ) e . printStackTrace ( );
221+ log . debug ( "Error when clone object" , e );
220222 return null ;
221223 }
222224 }
@@ -232,10 +234,10 @@ void register(Symbol t) {
232234
233235 Symbol s = symbolTable .get (t .id );
234236 if (s != null ) {
235- if ( dbg ) System . out . println ("Symbol in table " + t .id + " " + s .getClass ().getName ()+ " -> " + t .getClass ().getName ());
237+ log . debug ("Symbol in table {} {} -> {}" , t .id , s .getClass ().getName (), t .getClass ().getName ());
236238 //symbolTable.put(t.id, t);
237239 if (t .bp >= s .lbp ) {
238- if ( dbg ) System . out . println ("Symbol in table " + t .id + " lbp=" + s .lbp + " -> " + t .bp );
240+ log . debug ("Symbol in table {} lbp={} -> {}" , t .id , s .lbp , t .bp );
239241 s .lbp = t .bp ;
240242 }
241243 } else {
@@ -281,7 +283,7 @@ Symbol advance(String id, boolean infix) {
281283 return handleError (err );
282284 }
283285 Token next_token = lexer .next (infix );
284- if ( dbg ) System . out . println ("nextToken " +( next_token !=null ? next_token .type : null ) );
286+ log . debug ("nextToken {}" , next_token !=null ? next_token .type : null );
285287 if (next_token == null ) {
286288 node = symbolTable .get ("(end)" );
287289 node .position = source .length ();
@@ -322,7 +324,7 @@ Symbol advance(String id, boolean infix) {
322324 node .value = value ;
323325 node .type = type ;
324326 node .position = next_token .position ;
325- if ( dbg ) System . out . println ("advance " + node );
327+ log . debug ("advance {}" , node );
326328 return node ;
327329 }
328330
@@ -335,7 +337,7 @@ Symbol expression(int rbp) {
335337 while (rbp < node .lbp ) {
336338 t = node ;
337339 advance (null , false );
338- if ( dbg ) System . out . println ("t=" + t + " , left=" + left .type );
340+ log . debug ("t={} , left={}" , t , left .type );
339341 left = t .led (left );
340342 }
341343 return left ;
@@ -618,7 +620,7 @@ Symbol led(Symbol left) {
618620 //register(new Prefix("(") {
619621
620622 @ Override Symbol nud () {
621- if ( dbg ) System . out . println ("Prefix (" );
623+ log . debug ("Prefix (" );
622624 List <Symbol > expressions = new ArrayList <>();
623625 while (!node .id .equals (")" )) {
624626 expressions .add (Parser .this .expression (0 ));
@@ -981,7 +983,7 @@ void resolveAncestry(Symbol path) {
981983 Symbol processAST (Symbol expr ) {
982984 Symbol result = expr ;
983985 if (expr ==null ) return null ;
984- if ( dbg ) System . out . println (" > processAST type=" + expr . type + " value='" + expr .value + "'" );
986+ log . debug (" > processAST type={} value='{}'" , expr .type , expr . value );
985987 switch (expr .type != null ? expr .type : "(null)" ) {
986988 case "binary" : {
987989 switch ("" +expr .value ) {
@@ -1028,11 +1030,11 @@ Symbol processAST(Symbol expr) {
10281030 step .value
10291031 );
10301032 }
1031- //System.out.println ("step "+step+" type="+ step.type);
1033+ //log.info ("step {} type={}", step, step.type);
10321034 if (step .type .equals ("string" ))
10331035 step .type = "name" ;
10341036 // for (var lit : step.steps) {
1035- // System.out.println ("step2 "+lit+" type="+ lit.type);
1037+ // log.info ("step2 {} type={}", lit, lit.type);
10361038 // lit.type = "name";
10371039 // }
10381040 }
@@ -1056,7 +1058,7 @@ Symbol processAST(Symbol expr) {
10561058 resolveAncestry (result );
10571059 break ;
10581060 case "[" :
1059- if ( dbg ) System . out . println ("binary [" );
1061+ log . debug ("binary [" );
10601062 // predicated step
10611063 // LHS is a step or a predicated step
10621064 // RHS is the predicate expr
@@ -1236,7 +1238,7 @@ Symbol processAST(Symbol expr) {
12361238 // expr.value might be Character!
12371239 String exprValue = "" +expr .value ;
12381240 if (exprValue .equals ("[" )) {
1239- if ( dbg ) System . out . println ("unary [ " + result );
1241+ log . debug ("unary [ {}" , result );
12401242 // array constructor - process each item
12411243 final Symbol _result = result ;
12421244 result .expressions = expr .expressions .stream ().map (item -> {
@@ -1263,7 +1265,7 @@ Symbol processAST(Symbol expr) {
12631265 if (exprValue .equals ("-" ) && result .expression .type .equals ("number" )) {
12641266 result = result .expression ;
12651267 result .value = Utils .convertNumber ( -((Number )result .value ).doubleValue () );
1266- if ( dbg ) System . out . println ("unary - value=" + result .value );
1268+ log . debug ("unary - value={}" , result .value );
12671269 } else {
12681270 pushAncestry (result , result .expression );
12691271 }
0 commit comments