Finished migrating parser to be object orientation free. All tests pass
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
|
||||
import AccessorNode from '/imports/parser/parseTree/AccessorNode.js';
|
||||
import findAncestorByType from '/imports/api/engine/computation/utility/findAncestorByType.js';
|
||||
import { traverse } from '/imports/parser/resolve.js';
|
||||
|
||||
export default function linkCalculationDependencies(dependencyGraph, prop, {propsById}){
|
||||
prop._computationDetails.calculations.forEach(calcObj => {
|
||||
@@ -9,9 +8,9 @@ export default function linkCalculationDependencies(dependencyGraph, prop, {prop
|
||||
// ancestors: {} //this gets added if there are resolved ancestors
|
||||
};
|
||||
// Traverse the parsed calculation looking for variable names
|
||||
calcObj._parsedCalculation.traverse(node => {
|
||||
traverse(calcObj._parsedCalculation, node => {
|
||||
// Skip nodes that aren't symbols or accessors
|
||||
if (!(node instanceof SymbolNode || node instanceof AccessorNode)) return;
|
||||
if (node.parseType !== 'symbol' && node.parseType !== 'accessor') return;
|
||||
// Link ancestor references as direct property dependencies
|
||||
if (node.name[0] === '#'){
|
||||
let ancestorProp = getAncestorProp(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import INLINE_CALCULATION_REGEX from '/imports/constants/INLINE_CALCULTION_REGEX.js';
|
||||
import { prettifyParseError, parse } from '/imports/parser/parser.js';
|
||||
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
|
||||
import applyFnToKey from '/imports/api/engine/computation/utility/applyFnToKey.js';
|
||||
import { get, unset } from 'lodash';
|
||||
import { get } from 'lodash';
|
||||
import errorNode from '/imports/parser/parseTree/error.js'
|
||||
|
||||
export default function parseCalculationFields(prop, schemas){
|
||||
discoverInlineCalculationFields(prop, schemas);
|
||||
@@ -66,6 +66,6 @@ function parseCalculation(calcObj){
|
||||
calcObj.errors ?
|
||||
calcObj.errors.push(error) :
|
||||
calcObj.errors = [error];
|
||||
calcObj._parsedCalculation = new ErrorNode({error});
|
||||
calcObj._parsedCalculation = errorNode.create({error});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { CompilationContext } from '/imports/parser/parser.js';
|
||||
import resolve, { toString } from '/imports/parser/resolve.js';
|
||||
import INLINE_CALCULATION_REGEX from '/imports/constants/INLINE_CALCULTION_REGEX.js';
|
||||
import ConstantNode from '/imports/parser/parseTree/ConstantNode.js';
|
||||
import ErrorNode from '/imports/parser/parseTree/ErrorNode.js';
|
||||
|
||||
export default function computeCalculations(computation, node){
|
||||
if (!node.data) return;
|
||||
@@ -15,17 +13,16 @@ export default function computeCalculations(computation, node){
|
||||
}
|
||||
|
||||
function evaluateCalculation(calculation, scope){
|
||||
const context = new CompilationContext();
|
||||
const parseNode = calculation._parsedCalculation;
|
||||
const fn = calculation._parseLevel;
|
||||
const calculationScope = {...calculation._localScope, ...scope};
|
||||
const resultNode = parseNode[fn](calculationScope, context);
|
||||
if (resultNode instanceof ConstantNode){
|
||||
const {result: resultNode, context} = resolve(fn, parseNode, calculationScope);
|
||||
if (resultNode.parseType === 'constant'){
|
||||
calculation.value = resultNode.value;
|
||||
} else if (resultNode instanceof ErrorNode){
|
||||
} else if (resultNode.parseType === 'error'){
|
||||
calculation.value = null;
|
||||
} else {
|
||||
calculation.value = resultNode.toString();
|
||||
calculation.value = toString(resultNode);
|
||||
}
|
||||
if (calculation.errors){
|
||||
calculation.errors = [...calculation.errors, ...context.errors]
|
||||
|
||||
@@ -3,12 +3,10 @@ import VARIABLE_NAME_REGEX from '/imports/constants/VARIABLE_NAME_REGEX.js';
|
||||
import ErrorSchema from '/imports/api/properties/subSchemas/ErrorSchema.js';
|
||||
import {
|
||||
parse,
|
||||
CompilationContext,
|
||||
prettifyParseError,
|
||||
} from '/imports/parser/parser.js';
|
||||
import AccessorNode from '/imports/parser/parseTree/AccessorNode.js';
|
||||
import SymbolNode from '/imports/parser/parseTree/SymbolNode.js';
|
||||
import STORAGE_LIMITS from '/imports/constants/STORAGE_LIMITS.js';
|
||||
import resolve, { Context, traverse } from '/imports/parser/resolve.js';
|
||||
|
||||
/*
|
||||
* Constants are primitive values that can be used elsewhere in computations
|
||||
@@ -50,12 +48,9 @@ let ConstantSchema = new SimpleSchema({
|
||||
// Any existing errors will result in an early failure
|
||||
if (context && context.errors.length) return context.errors;
|
||||
// Ban variables in constants if necessary
|
||||
result && result.traverse(node => {
|
||||
if (node instanceof SymbolNode || node instanceof AccessorNode){
|
||||
context.storeError({
|
||||
type: 'error',
|
||||
message: 'Variables can\'t be used to define a constant'
|
||||
});
|
||||
result && traverse(result, node => {
|
||||
if (node.parseType === 'symbol' || node.parseType === 'accessor'){
|
||||
context.error('Variables can\'t be used to define a constant');
|
||||
}
|
||||
});
|
||||
return context && context.errors || [];
|
||||
@@ -67,7 +62,7 @@ let ConstantSchema = new SimpleSchema({
|
||||
});
|
||||
|
||||
function parseString(string, fn = 'compile'){
|
||||
let context = new CompilationContext();
|
||||
let context = new Context();
|
||||
if (!string){
|
||||
return {result: string, context};
|
||||
}
|
||||
@@ -78,10 +73,11 @@ function parseString(string, fn = 'compile'){
|
||||
node = parse(string);
|
||||
} catch (e) {
|
||||
let message = prettifyParseError(e);
|
||||
context.storeError({type: 'error', message});
|
||||
context.error(message);
|
||||
return {context};
|
||||
}
|
||||
let result = node[fn]({/*empty scope*/}, context);
|
||||
if (!node) return {context};
|
||||
let {result} = resolve(fn, node, {/*empty scope*/}, context);
|
||||
return {result, context}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user