@jsonic/expr
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -23,3 +23,3 @@ import { Plugin, Rule, Context, Token } from 'jsonic'; | ||
}; | ||
evaluate?: typeof evaluate; | ||
evaluate?: typeof evaluation; | ||
}; | ||
@@ -53,7 +53,7 @@ type Op = { | ||
}; | ||
type Resolve = (rule: Rule, ctx: Context, op: Op, ...terms: any) => any; | ||
type Evaluate = (rule: Rule, ctx: Context, op: Op, ...terms: any) => any; | ||
declare const OP_MARK: {}; | ||
declare let Expr: Plugin; | ||
declare function prattify(expr: any, op?: Op): any[]; | ||
declare function evaluate(rule: Rule, ctx: Context, expr: any, resolve: Resolve): any; | ||
declare function evaluation(rule: Rule, ctx: Context, expr: any, evaluate: Evaluate): any; | ||
declare const testing: { | ||
@@ -63,3 +63,3 @@ prattify: typeof prattify; | ||
}; | ||
export { Expr, evaluate, testing }; | ||
export type { ExprOptions, OpDef, Op, Resolve }; | ||
export { Expr, evaluation, testing }; | ||
export type { ExprOptions, OpDef, Op, Evaluate }; |
36
expr.js
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.testing = exports.Expr = void 0; | ||
exports.evaluate = evaluate; | ||
exports.evaluation = evaluation; | ||
// This algorithm is based on Pratt parsing, and draws heavily from | ||
@@ -55,2 +55,3 @@ // the explanation written by Aleksey Kladov here: | ||
// } | ||
// console.log('EXPR', options) | ||
let token = jsonic.token.bind(jsonic); | ||
@@ -60,2 +61,8 @@ let fixed = jsonic.fixed.bind(jsonic); | ||
let optop = options.op || {}; | ||
// Delete operations marked null. | ||
for (let opname in optop) { | ||
if (null === optop[opname]) { | ||
delete optop[opname]; | ||
} | ||
} | ||
const prefixTM = makeOpMap(token, fixed, optop, 'prefix'); | ||
@@ -143,3 +150,4 @@ const suffixTM = makeOpMap(token, fixed, optop, 'suffix'); | ||
: NONE, | ||
// An opening parenthesis of an expression. | ||
// An opening parenthesis. | ||
// NOTE: this can happen outside an expression. | ||
hasParen | ||
@@ -317,2 +325,10 @@ ? { | ||
rs.open([ | ||
// An opening parenthesis of an expression. | ||
hasParen | ||
? { | ||
s: [OP], | ||
p: 'val', | ||
g: 'expr,expr-paren,expr-start', | ||
} | ||
: NONE, | ||
hasPrefix | ||
@@ -462,5 +478,6 @@ ? { | ||
// Only evaluate at root of expr (where r.n.expr === 0) | ||
// console.log('EXPR AC', r) | ||
if (options.evaluate && 0 === r.n.expr) { | ||
// The parent node will contain the root of the expr tree | ||
r.parent.node = evaluate(r.parent, ctx, r.parent.node, options.evaluate); | ||
r.parent.node = evaluation(r.parent, ctx, r.parent.node, options.evaluate); | ||
} | ||
@@ -514,3 +531,9 @@ }); | ||
: NONE, | ||
]); | ||
]) | ||
.ac((r, ctx) => { | ||
// A Paren can occur outside an expression | ||
if (options.evaluate && 0 === r.n.expr) { | ||
r.node = evaluation(r.child, ctx, r.child.node, options.evaluate); | ||
} | ||
}); | ||
}); | ||
@@ -949,3 +972,4 @@ // Ternary operators are like fancy parens. | ||
} | ||
function evaluate(rule, ctx, expr, resolve) { | ||
function evaluation(rule, ctx, expr, evaluate) { | ||
// console.log('EXPR-EVAL', expr, resolve) | ||
if (null == expr) { | ||
@@ -955,3 +979,3 @@ return expr; | ||
if (isOp(expr)) { | ||
return resolve(rule, ctx, expr[0], expr.slice(1).map((term) => evaluate(rule, ctx, term, resolve))); | ||
return evaluate(rule, ctx, expr[0], expr.slice(1).map((term) => evaluation(rule, ctx, term, evaluate))); | ||
} | ||
@@ -958,0 +982,0 @@ return expr; |
50
expr.ts
@@ -75,3 +75,3 @@ /* Copyright (c) 2021-2025 Richard Rodger, MIT License */ | ||
// TODO: define Evalute type | ||
evaluate?: typeof evaluate | ||
evaluate?: typeof evaluation | ||
} | ||
@@ -112,3 +112,3 @@ | ||
// Resolve the value of an operartion | ||
type Resolve = (rule: Rule, ctx: Context, op: Op, ...terms: any) => any | ||
type Evaluate = (rule: Rule, ctx: Context, op: Op, ...terms: any) => any | ||
@@ -132,2 +132,4 @@ // Mark Operator objects as owned by this plugin. | ||
// console.log('EXPR', options) | ||
let token = jsonic.token.bind(jsonic) as any | ||
@@ -138,2 +140,10 @@ let fixed = jsonic.fixed.bind(jsonic) as any | ||
let optop = options.op || {} | ||
// Delete operations marked null. | ||
for (let opname in optop) { | ||
if (null === optop[opname]) { | ||
delete optop[opname] | ||
} | ||
} | ||
const prefixTM: OpMap = makeOpMap(token, fixed, optop, 'prefix') | ||
@@ -240,3 +250,4 @@ const suffixTM: OpMap = makeOpMap(token, fixed, optop, 'suffix') | ||
// An opening parenthesis of an expression. | ||
// An opening parenthesis. | ||
// NOTE: this can happen outside an expression. | ||
hasParen | ||
@@ -433,2 +444,11 @@ ? { | ||
rs.open([ | ||
// An opening parenthesis of an expression. | ||
hasParen | ||
? { | ||
s: [OP], | ||
p: 'val', | ||
g: 'expr,expr-paren,expr-start', | ||
} | ||
: NONE, | ||
hasPrefix | ||
@@ -593,5 +613,8 @@ ? { | ||
// Only evaluate at root of expr (where r.n.expr === 0) | ||
// console.log('EXPR AC', r) | ||
if (options.evaluate && 0 === r.n.expr) { | ||
// The parent node will contain the root of the expr tree | ||
r.parent.node = evaluate( | ||
r.parent.node = evaluation( | ||
r.parent, | ||
@@ -656,2 +679,9 @@ ctx, | ||
]) | ||
.ac((r: Rule, ctx: Context) => { | ||
// A Paren can occur outside an expression | ||
if (options.evaluate && 0 === r.n.expr) { | ||
r.node = evaluation(r.child, ctx, r.child.node, options.evaluate) | ||
} | ||
}) | ||
}) | ||
@@ -1154,3 +1184,5 @@ | ||
function evaluate(rule: Rule, ctx: Context, expr: any, resolve: Resolve) { | ||
function evaluation(rule: Rule, ctx: Context, expr: any, evaluate: Evaluate) { | ||
// console.log('EXPR-EVAL', expr, resolve) | ||
if (null == expr) { | ||
@@ -1161,7 +1193,7 @@ return expr | ||
if (isOp(expr)) { | ||
return resolve( | ||
return evaluate( | ||
rule, | ||
ctx, | ||
expr[0], | ||
expr.slice(1).map((term: any) => evaluate(rule, ctx, term, resolve)), | ||
expr.slice(1).map((term: any) => evaluation(rule, ctx, term, evaluate)), | ||
) | ||
@@ -1178,4 +1210,4 @@ } | ||
export { Expr, evaluate, testing } | ||
export { Expr, evaluation, testing } | ||
export type { ExprOptions, OpDef, Op, Resolve } | ||
export type { ExprOptions, OpDef, Op, Evaluate } |
{ | ||
"name": "@jsonic/expr", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "This plugin allows the [Jsonic](https://jsonic.senecajs.org) JSON parser to support expression syntax.", | ||
@@ -5,0 +5,0 @@ "main": "expr.js", |
Sorry, the diff of this file is not supported yet
102306
2115