Skip to content

Commit

Permalink
works with modified lezer-feel
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm committed Apr 16, 2024
1 parent 7ea4dfc commit 6da2cf2
Show file tree
Hide file tree
Showing 4 changed files with 389 additions and 32 deletions.
8 changes: 7 additions & 1 deletion lib/zeebe/util/VariableContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export class EntriesContext extends VariableContext {
constructor(value = { entries: {} }) {
super(value);

this.value.id = this.value.id || crypto.randomUUID();
this.value.entries = this.value.entries || {};

const context = this.value;
Expand Down Expand Up @@ -72,6 +73,7 @@ export class EntriesContext extends VariableContext {

const {
entries = {},
used = [],
...rest
} = unwrap(context);

Expand All @@ -81,7 +83,11 @@ export class EntriesContext extends VariableContext {
entries: {
...merged.entries,
...entries
}
},
used: [
...(merged.used || []),
...used
]
};
}, {});

Expand Down
80 changes: 77 additions & 3 deletions lib/zeebe/util/feelUtility.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,16 @@ function resolveReferences(variablesToResolve, allVariables) {
sortedVariables.forEach(({ variable, expression }) => {
const resultContext = getResultContext(expression, filterForScope(rootContext, variable));

findUnresolvedVariables(resultContext, true);

// console.log('annotated', annotateUsedVariables(resultContext));
addUsedInfo(rootContext, resultContext, variable.origin[0]);
debugger;

let computedResult = resultContext.computedValue();

console.log('computedResult', resultContext, computedResult, rootContext);

// Wrap primitive values in an EntriesContext
if (!(computedResult instanceof EntriesContext)) {
computedResult = EntriesContext.of(computedResult);
Expand Down Expand Up @@ -114,6 +122,49 @@ function resolveReferences(variablesToResolve, allVariables) {
return result;
}

function findUsageNode(name, variables) {
console.log('findUsageNode', name, variables);
if (!variables) {
return [];
}

return (variables.children || []).flatMap(child => {
console.log(name, child);

if (child.context.value.name === name) {
return child;
}

return findUsageNode(name, child);
}).filter(i => i);
}

function addUsedInfo(rootContext, variables, scope) {

// iterate over the result context and find used names
const context = variables.context;
const usedNames = context.value.used; // .map(variableName => {
// return context.value.entries[variableName]?.value?.id
// }).filter(i => i);

// Add usage info to root context
const rootContextValues = Object.values(rootContext.entries);
rootContextValues.filter(v => usedNames.includes(v.name)).forEach(
v => {
v.usedIn = v.usedIn || [];
v.usedIn.push(scope);

// Recursively find places the variable was used
console.log(findUsageNode(v.name, variables));
findUsageNode(v.name, variables).forEach(node => {
addUsedInfo(v, node, scope);
console.log('node', node);
});

// Find recursive used variables
}
);
}

// helpers //////////////////////

Expand All @@ -135,6 +186,9 @@ export function getResultContext(expression, variables = {}) {
start: contextTracker.start,
reduce(...args) {
const result = contextTracker.reduce(...args);

console.log('reduce', result, ...args);

latestVariables = result;
return result;
}
Expand Down Expand Up @@ -186,7 +240,9 @@ function getExpressionDetails(variable, origin) {

const result = getResultContext(expression);

const unresolved = findUnresolvedVariables(result) ;
const unresolved = findUnresolvedVariables(result);

// console.log('unresolved', unresolved);

return { expression, unresolved };
}
Expand All @@ -197,10 +253,14 @@ function getExpressionDetails(variable, origin) {
* @param {Object} node
* @returns {Array<String>}
*/
function findUnresolvedVariables(node) {
function findUnresolvedVariables(node, log) {
const results = [];

results.push(...(node.children.flatMap(findUnresolvedVariables)));
results.push(...(node.children.flatMap(v => findUnresolvedVariables(v, log))));

// if (log && node.name === 'VariableName') {
// console.log('node', node);
// }

if (node.name === 'VariableName' && !node.value) {
results.push(node.raw);
Expand All @@ -210,6 +270,20 @@ function findUnresolvedVariables(node) {
}


function annotateUsedVariables(node) {
node.children.forEach(v => annotateUsedVariables(v));

// console.log('node', node);

if (node.name === 'VariableName') {
node.context.value.usedIn = node.context.value.used || [];
node.context.value.usedIn.push(node.raw);
}

return node;
}


/**
* Transforms the entries of a variable from an array to an object.
* This allows faster lookup times during parsing.
Expand Down
32 changes: 32 additions & 0 deletions lib/zeebe/util/variable-resolver.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"folders": [
{
"path": "../../.."
},
{
"path": "../../../../lezer-feel"
}
],
"settings": {
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#7586eb",
"activityBar.background": "#7586eb",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#f7c7ce",
"activityBarBadge.foreground": "#15202b",
"commandCenter.border": "#e7e7e799",
"sash.hoverBorder": "#7586eb",
"statusBar.background": "#485fe5",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#7586eb",
"statusBarItem.remoteBackground": "#485fe5",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#485fe5",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#485fe599",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.remoteColor": "#485fe5"
}
}
Loading

0 comments on commit 6da2cf2

Please sign in to comment.