Skip to content

Commit 9bfbb8e

Browse files
committed
add slf4j
1 parent bf8ec3a commit 9bfbb8e

5 files changed

Lines changed: 44 additions & 33 deletions

File tree

pom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
</scm>
3838

3939
<dependencies>
40+
<dependency>
41+
<groupId>org.slf4j</groupId>
42+
<artifactId>slf4j-api</artifactId>
43+
<version>2.0.18</version>
44+
</dependency>
4045
<dependency>
4146
<groupId>org.junit.jupiter</groupId>
4247
<artifactId>junit-jupiter</artifactId>

src/main/java/com/dashjoin/jsonata/Functions.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ public static List<RegexpMatch> evaluateMatcher(Pattern matcher, String str) {
666666
while (m.find()) {
667667
RegexpMatch rm = new RegexpMatch();
668668

669-
//System.out.println("grc="+m.groupCount()+" "+m.group(1));
669+
//log.info("grc={} {}", m.groupCount(), m.group(1));
670670

671671
rm.index = m.start();
672672
rm.match = m.group();
@@ -700,7 +700,7 @@ public static Boolean contains(String str, Object token) {
700700
result = (str.indexOf((String)token) != -1);
701701
} else if (token instanceof Pattern) {
702702
var matches = evaluateMatcher((Pattern)token, str);
703-
//if (dbg) System.out.println("match = "+matches);
703+
//log.debug("match = {}", matches);
704704
//result = (typeof matches !== 'undefined');
705705
//throw new Error("regexp not impl"); //result = false;
706706
result = !matches.isEmpty();
@@ -1134,7 +1134,7 @@ public static String formatNumber(Number value, String picture, Map options) {
11341134
fixedPicture = fixedPicture.replace("e", "E");
11351135
littleE = true;
11361136
}
1137-
//System.out.println("picture "+fixedPicture);
1137+
//log.info("picture {}", fixedPicture);
11381138
formatter.applyLocalizedPattern(fixedPicture);
11391139
String result = formatter.format(value);
11401140

@@ -2145,8 +2145,8 @@ public static Method getFunction(Class clz, String name) {
21452145
Method[] methods = clz.getMethods();
21462146
for (Method m : methods) {
21472147
// if (m.getModifiers() == (Modifier.STATIC | Modifier.PUBLIC) ) {
2148-
// System.out.println(m.getName());
2149-
// System.out.println(m.getParameterTypes());
2148+
// log.info(m.getName());
2149+
// log.info(m.getParameterTypes());
21502150
// }
21512151
if (m.getName().equals(name)) {
21522152
return m;
@@ -2176,7 +2176,7 @@ public static Object call(Object instance, Method m, List<Object> args) throws T
21762176
if (arg1!=null) {
21772177
List wrap = new ArrayList<>(); wrap.add(arg1);
21782178
callArgs.set(0, wrap);
2179-
//System.err.println("wrapped "+arg1+" as "+wrap);
2179+
//log.error("wrapped {} as {}"+, arg1, wrap);
21802180
}
21812181
}
21822182

@@ -2344,8 +2344,7 @@ public static Number parseInteger(String value, String picture) throws ParseExce
23442344
throw new ParseException("Formatting or parsing an integer as a sequence starting with \""+ picture +"\" is not supported by this implementation", 0);
23452345
} catch (Exception ex) {
23462346
// Ignore the exception, return null
2347-
// System.err.println("Exception in parseInteger (returning null): " + ex);
2348-
// ex.printStackTrace();
2347+
// log.error("Exception in parseInteger (returning null): ", ex);
23492348
return null;
23502349
}
23512350
}

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@
4545
import com.dashjoin.jsonata.Parser.Symbol;
4646
import com.dashjoin.jsonata.Utils.JList;
4747
import com.dashjoin.jsonata.utils.Signature;
48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
4850

4951
/**
5052
* @module JSONata
5153
* @description JSON query and transformation language
5254
*/
5355
@SuppressWarnings({"rawtypes", "unchecked"})
5456
public class Jsonata {
57+
58+
private static final Logger log = LoggerFactory.getLogger(Jsonata.class);
5559

5660
// Start of Evaluator code
5761

@@ -150,7 +154,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) {
150154
this.input = input;
151155
this.environment = environment;
152156

153-
if (parser.dbg) System.out.println("eval expr="+expr+" type="+expr.type);//+" input="+input);
157+
log.debug("eval expr={} type={}", expr, expr.type);//+" input="+input);
154158

155159
var entryCallback = environment.lookup("__evaluate_entry");
156160
if(entryCallback!=null) {
@@ -170,7 +174,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) {
170174
break;
171175
case "name":
172176
result = evaluateName(expr, input, environment);
173-
if (parser.dbg) System.out.println("evalName "+result);
177+
log.debug("evalName {}");
174178
break;
175179
case "string":
176180
case "number":
@@ -676,7 +680,7 @@ public Object call() throws Exception {
676680
if(expr.consarray) {
677681
if (!(result instanceof JList))
678682
result = new JList((List)result);
679-
//System.out.println("const "+result);
683+
//log.info("const {}", result);
680684
((JList)result).cons = true;
681685
}
682686
break;
@@ -837,7 +841,7 @@ Object evaluateNumericExpression(Object _lhs, Object _rhs, String op) {
837841
return null;
838842
}
839843

840-
//System.out.println("op22 "+op+" "+_lhs+" "+_rhs);
844+
//log.info("op22 {} {} {}", op, _lhs, _rhs);
841845
double lhs = ((Number)_lhs).doubleValue();
842846
double rhs = ((Number)_rhs).doubleValue();
843847

@@ -1303,7 +1307,7 @@ Object evaluateVariable(Symbol expr, Object input, Frame environment) {
13031307
result = input instanceof JList && ((JList)input).outerWrapper ? ((JList)input).get(0) : input;
13041308
} else {
13051309
result = environment.lookup((String)expr.value);
1306-
if (parser.dbg) System.out.println("variable name="+expr.value+" val="+result);
1310+
log.debug("variable name={} val={}", expr.value, result);
13071311
}
13081312
return result;
13091313
}
@@ -1757,7 +1761,7 @@ Jsonata getPerThreadInstance() {
17571761
// result = /* await */ result;
17581762
// }
17591763
} else if (proc instanceof JLambda) {
1760-
// System.err.println("Lambda "+proc);
1764+
// log.error("Lambda {}", proc);
17611765
List _args = (List)validatedArgs;
17621766
if (proc instanceof Fn0) {
17631767
result = ((Fn0)proc).get();
@@ -1769,7 +1773,7 @@ Jsonata getPerThreadInstance() {
17691773
} else if (proc instanceof Pattern) {
17701774
List _res = new ArrayList<>();
17711775
for (Object s : (List)validatedArgs) {
1772-
//System.err.println("PAT "+proc+" input "+s);
1776+
//log.error("PAT {} input {}", proc, s);
17731777
if (s instanceof String) {
17741778
Matcher matcher = ((Pattern) proc).matcher((String) s);
17751779
_res.add(regexClosure(matcher));
@@ -1781,7 +1785,7 @@ Jsonata getPerThreadInstance() {
17811785
result = _res;
17821786
}
17831787
} else {
1784-
System.out.println("Proc not found "+proc);
1788+
log.warn("Proc not found {}", proc);
17851789
throw new JException(
17861790
"T1006", 0
17871791
//stack: (new Error()).stack
@@ -1989,7 +1993,7 @@ Object partialApplyNativeFunction(JFunction _native, List args) {
19891993
var body = "function(" + String.join(", ", sigArgs) + "){";
19901994
body += "$"+_native.functionName+"("+String.join(", ", sigArgs) + ") }";
19911995

1992-
if (parser.dbg) System.out.println("partial trampoline = "+body);
1996+
log.debug("partial trampoline = {}", body);
19931997

19941998
// var sigArgs = getNativeFunctionArguments(_native);
19951999
// sigArgs = sigArgs.stream().map(sigArg -> {
@@ -2232,7 +2236,7 @@ public JFunction(String functionName, String signature, Class clz, Object instan
22322236
this.method = Functions.getFunction(clz, implMethodName);
22332237
this.methodInstance = instance;
22342238
if (method==null) {
2235-
System.err.println("Function not implemented: "+functionName+" impl="+implMethodName);
2239+
log.error("Function not implemented: {} impl={}", functionName, implMethodName);
22362240
}
22372241
}
22382242

src/main/java/com/dashjoin/jsonata/Parser.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838
import com.dashjoin.jsonata.Jsonata.Frame;
3939
import com.dashjoin.jsonata.Tokenizer.Token;
4040
import com.dashjoin.jsonata.utils.Signature;
41+
import org.slf4j.Logger;
42+
import org.slf4j.LoggerFactory;
4143

4244
//var parseSignature = require('./signature');
4345
@SuppressWarnings({"unchecked"})
4446
public 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
}

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module com.dashjoin.jsonata {
22
requires java.compiler;
33
requires java.management;
4+
requires org.slf4j;
45

56
exports com.dashjoin.jsonata;
67
exports com.dashjoin.jsonata.json;

0 commit comments

Comments
 (0)