Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
</scm>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.18</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/dashjoin/jsonata/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ public static List<RegexpMatch> evaluateMatcher(Pattern matcher, String str) {
while (m.find()) {
RegexpMatch rm = new RegexpMatch();

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

rm.index = m.start();
rm.match = m.group();
Expand Down Expand Up @@ -700,7 +700,7 @@ public static Boolean contains(String str, Object token) {
result = (str.indexOf((String)token) != -1);
} else if (token instanceof Pattern) {
var matches = evaluateMatcher((Pattern)token, str);
//if (dbg) System.out.println("match = "+matches);
//log.debug("match = {}", matches);
//result = (typeof matches !== 'undefined');
//throw new Error("regexp not impl"); //result = false;
result = !matches.isEmpty();
Expand Down Expand Up @@ -1134,7 +1134,7 @@ public static String formatNumber(Number value, String picture, Map options) {
fixedPicture = fixedPicture.replace("e", "E");
littleE = true;
}
//System.out.println("picture "+fixedPicture);
//log.info("picture {}", fixedPicture);
formatter.applyLocalizedPattern(fixedPicture);
String result = formatter.format(value);

Expand Down Expand Up @@ -2145,8 +2145,8 @@ public static Method getFunction(Class clz, String name) {
Method[] methods = clz.getMethods();
for (Method m : methods) {
// if (m.getModifiers() == (Modifier.STATIC | Modifier.PUBLIC) ) {
// System.out.println(m.getName());
// System.out.println(m.getParameterTypes());
// log.info(m.getName());
// log.info(m.getParameterTypes());
// }
if (m.getName().equals(name)) {
return m;
Expand Down Expand Up @@ -2176,7 +2176,7 @@ public static Object call(Object instance, Method m, List<Object> args) throws T
if (arg1!=null) {
List wrap = new ArrayList<>(); wrap.add(arg1);
callArgs.set(0, wrap);
//System.err.println("wrapped "+arg1+" as "+wrap);
//log.error("wrapped {} as {}"+, arg1, wrap);
}
}

Expand Down Expand Up @@ -2344,8 +2344,7 @@ public static Number parseInteger(String value, String picture) throws ParseExce
throw new ParseException("Formatting or parsing an integer as a sequence starting with \""+ picture +"\" is not supported by this implementation", 0);
} catch (Exception ex) {
// Ignore the exception, return null
// System.err.println("Exception in parseInteger (returning null): " + ex);
// ex.printStackTrace();
// log.error("Exception in parseInteger (returning null): ", ex);
return null;
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/dashjoin/jsonata/Jsonata.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@
import com.dashjoin.jsonata.Parser.Symbol;
import com.dashjoin.jsonata.Utils.JList;
import com.dashjoin.jsonata.utils.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @module JSONata
* @description JSON query and transformation language
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public class Jsonata {

private static final Logger log = LoggerFactory.getLogger(Jsonata.class);

// Start of Evaluator code

Expand Down Expand Up @@ -150,7 +154,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) {
this.input = input;
this.environment = environment;

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

var entryCallback = environment.lookup("__evaluate_entry");
if(entryCallback!=null) {
Expand All @@ -170,7 +174,7 @@ Object _evaluate(Symbol expr, Object input, Frame environment) {
break;
case "name":
result = evaluateName(expr, input, environment);
if (parser.dbg) System.out.println("evalName "+result);
log.debug("evalName {}");
break;
case "string":
case "number":
Expand Down Expand Up @@ -676,7 +680,7 @@ public Object call() throws Exception {
if(expr.consarray) {
if (!(result instanceof JList))
result = new JList((List)result);
//System.out.println("const "+result);
//log.info("const {}", result);
((JList)result).cons = true;
}
break;
Expand Down Expand Up @@ -837,7 +841,7 @@ Object evaluateNumericExpression(Object _lhs, Object _rhs, String op) {
return null;
}

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

Expand Down Expand Up @@ -1303,7 +1307,7 @@ Object evaluateVariable(Symbol expr, Object input, Frame environment) {
result = input instanceof JList && ((JList)input).outerWrapper ? ((JList)input).get(0) : input;
} else {
result = environment.lookup((String)expr.value);
if (parser.dbg) System.out.println("variable name="+expr.value+" val="+result);
log.debug("variable name={} val={}", expr.value, result);
}
return result;
}
Expand Down Expand Up @@ -1757,7 +1761,7 @@ Jsonata getPerThreadInstance() {
// result = /* await */ result;
// }
} else if (proc instanceof JLambda) {
// System.err.println("Lambda "+proc);
// log.error("Lambda {}", proc);
List _args = (List)validatedArgs;
if (proc instanceof Fn0) {
result = ((Fn0)proc).get();
Expand All @@ -1769,7 +1773,7 @@ Jsonata getPerThreadInstance() {
} else if (proc instanceof Pattern) {
List _res = new ArrayList<>();
for (Object s : (List)validatedArgs) {
//System.err.println("PAT "+proc+" input "+s);
//log.error("PAT {} input {}", proc, s);
if (s instanceof String) {
Matcher matcher = ((Pattern) proc).matcher((String) s);
_res.add(regexClosure(matcher));
Expand All @@ -1781,7 +1785,7 @@ Jsonata getPerThreadInstance() {
result = _res;
}
} else {
System.out.println("Proc not found "+proc);
log.warn("Proc not found {}", proc);
throw new JException(
"T1006", 0
//stack: (new Error()).stack
Expand Down Expand Up @@ -1989,7 +1993,7 @@ Object partialApplyNativeFunction(JFunction _native, List args) {
var body = "function(" + String.join(", ", sigArgs) + "){";
body += "$"+_native.functionName+"("+String.join(", ", sigArgs) + ") }";

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

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

Expand Down
32 changes: 17 additions & 15 deletions src/main/java/com/dashjoin/jsonata/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
import com.dashjoin.jsonata.Jsonata.Frame;
import com.dashjoin.jsonata.Tokenizer.Token;
import com.dashjoin.jsonata.utils.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//var parseSignature = require('./signature');
@SuppressWarnings({"unchecked"})
public class Parser implements Serializable {

boolean dbg = false;
private static final Logger log = LoggerFactory.getLogger(Parser.class);

// This parser implements the 'Top down operator precedence' algorithm developed by Vaughan R Pratt; http://dl.acm.org/citation.cfm?id=512931.
// and builds on the Javascript framework described by Douglas Crockford at http://javascript.crockford.com/tdop/tdop.html
Expand Down Expand Up @@ -212,11 +214,11 @@ public Symbol create() {
// We want a shallow clone (do not duplicate outer class!)
try {
Symbol cl = (Symbol) this.clone();
//System.err.println("cloning "+this+" clone="+cl);
//log.info("cloning {} clone={}", this, cl);
return cl;
} catch (CloneNotSupportedException e) {
// never reached
if (dbg) e.printStackTrace();
log.debug("Error when clone object", e);
return null;
}
}
Expand All @@ -232,10 +234,10 @@ void register(Symbol t) {

Symbol s = symbolTable.get(t.id);
if (s != null) {
if (dbg) System.out.println("Symbol in table "+t.id+" "+s.getClass().getName()+" -> "+ t.getClass().getName());
log.debug("Symbol in table {} {} -> {}", t.id, s.getClass().getName(), t.getClass().getName());
//symbolTable.put(t.id, t);
if (t.bp >= s.lbp) {
if (dbg) System.out.println("Symbol in table "+t.id+" lbp="+s.lbp+" -> "+t.bp);
log.debug("Symbol in table {} lbp={} -> {}", t.id, s.lbp, t.bp);
s.lbp = t.bp;
}
} else {
Expand Down Expand Up @@ -281,7 +283,7 @@ Symbol advance(String id, boolean infix) {
return handleError(err);
}
Token next_token = lexer.next(infix);
if (dbg) System.out.println("nextToken "+(next_token!=null ? next_token.type : null));
log.debug("nextToken {}", next_token!=null ? next_token.type : null);
if (next_token == null) {
node = symbolTable.get("(end)");
node.position = source.length();
Expand Down Expand Up @@ -322,7 +324,7 @@ Symbol advance(String id, boolean infix) {
node.value = value;
node.type = type;
node.position = next_token.position;
if (dbg) System.out.println("advance "+node);
log.debug("advance {}", node);
return node;
}

Expand All @@ -335,7 +337,7 @@ Symbol expression(int rbp) {
while (rbp < node.lbp) {
t = node;
advance(null, false);
if (dbg) System.out.println("t="+t+", left="+left.type);
log.debug("t={}, left={}", t, left.type);
left = t.led(left);
}
return left;
Expand Down Expand Up @@ -618,7 +620,7 @@ Symbol led(Symbol left) {
//register(new Prefix("(") {

@Override Symbol nud() {
if (dbg) System.out.println("Prefix (");
log.debug("Prefix (");
List<Symbol> expressions = new ArrayList<>();
while (!node.id.equals(")")) {
expressions.add(Parser.this.expression(0));
Expand Down Expand Up @@ -981,7 +983,7 @@ void resolveAncestry(Symbol path) {
Symbol processAST(Symbol expr) {
Symbol result = expr;
if (expr==null) return null;
if (dbg) System.out.println(" > processAST type="+expr.type+" value='"+expr.value+"'");
log.debug(" > processAST type={} value='{}'", expr.type, expr.value);
switch (expr.type != null ? expr.type : "(null)") {
case "binary": {
switch (""+expr.value) {
Expand Down Expand Up @@ -1028,11 +1030,11 @@ Symbol processAST(Symbol expr) {
step.value
);
}
//System.out.println("step "+step+" type="+step.type);
//log.info("step {} type={}", step, step.type);
if (step.type.equals("string"))
step.type = "name";
// for (var lit : step.steps) {
// System.out.println("step2 "+lit+" type="+lit.type);
// log.info("step2 {} type={}", lit, lit.type);
// lit.type = "name";
// }
}
Expand All @@ -1056,7 +1058,7 @@ Symbol processAST(Symbol expr) {
resolveAncestry(result);
break;
case "[":
if (dbg) System.out.println("binary [");
log.debug("binary [");
// predicated step
// LHS is a step or a predicated step
// RHS is the predicate expr
Expand Down Expand Up @@ -1236,7 +1238,7 @@ Symbol processAST(Symbol expr) {
// expr.value might be Character!
String exprValue = ""+expr.value;
if (exprValue.equals("[")) {
if (dbg) System.out.println("unary [ "+result);
log.debug("unary [ {}", result);
// array constructor - process each item
final Symbol _result = result;
result.expressions = expr.expressions.stream().map(item -> {
Expand All @@ -1263,7 +1265,7 @@ Symbol processAST(Symbol expr) {
if (exprValue.equals("-") && result.expression.type.equals("number")) {
result = result.expression;
result.value = Utils.convertNumber( -((Number)result.value).doubleValue() );
if (dbg) System.out.println("unary - value="+result.value);
log.debug("unary - value={}", result.value);
} else {
pushAncestry(result, result.expression);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module com.dashjoin.jsonata {
requires java.compiler;
requires java.management;
requires org.slf4j;

exports com.dashjoin.jsonata;
exports com.dashjoin.jsonata.json;
Expand Down
Loading