Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context extraction #43

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
77 changes: 76 additions & 1 deletion src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@
/**
* Takes any number of Contexts and merges them into a single context.
*
* @param { ...VariableContext } contexts
* @param { ...ContextValue } contexts
* @returns { VariableContext }
*/
static of(...contexts) {
Expand Down Expand Up @@ -1026,6 +1026,11 @@
if (name?.raw === 'get value') {
variables = getContextValue(variables, args);
}

// construct context type through context(entries) utility
if (name?.raw === 'context') {
variables = getContextFromEntries(variables, args);
}
}

const start = contextStarts[term];
Expand Down Expand Up @@ -1246,6 +1251,76 @@
});
}

function getContextFromEntries(variables, args) {

const VariableContext = /** @type { typeof VariableContext } */ (

Check failure on line 1256 in src/tokens.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 20)

'VariableContext' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 1256 in src/tokens.js

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, 20)

'VariableContext' is assigned a value but never used. Allowed unused vars must match /^_/u
variables.context.constructor
);

if (!args.length) {
return variables.assign({
value: null
});
}

if (args[0].name === 'Name') {
args = extractNamedArgs(args, [ 'entries' ]);
}

if (args.length !== 1) {
return variables.assign({
value: null
});
}

const [
key
] = args;

console.log('\n\n\n------------');

const keyValue = key?.computedValue();

console.log('%o', {
keyValue,
children: keyValue?.children
});

const entryContexts = keyValue?.get('$$entries')?.value || (keyValue && [ keyValue ]) || [];

const context = reduce(entryContexts, (context, entryContext) => {

console.log('%o', { entryContext });

const keyValue = entryContext?.get('key');
const valueValue = entryContext?.get('value');

console.log('%o', {
keyValue,
valueValue
});

const value = entryContext?.value;

if (!value) {
return context;
}

if (has(value, 'key') && has(value, 'value')) {
return {
...context,
[ value.key ]: value.value
};
}

return context;
}, {});

return variables.assign({
value: context
});
}

function extractNamedArgs(args, argNames) {

const context = {};
Expand Down
193 changes: 188 additions & 5 deletions test/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ Expression(
)


# Built in (get value) { "top": "Expressions" }
# Built-in (get value) { "top": "Expressions" }

get value();
get value(null, 1);
Expand All @@ -222,7 +222,7 @@ Expressions(
)


# Built in (get value, literal key) { "context": { "x": { "a+b": { "c+": 1 } } } }
# Built-in (get value, literal key) { "context": { "x": { "a+b": { "c+": 1 } } } }

get value(x, "a+b").c+

Expand All @@ -242,7 +242,7 @@ Expression(
)


# Built in (get value, literal key, named args) { "context": { "x": { "a+b": { "c+": 1 } } } }
# Built-in (get value, literal key, named args) { "context": { "x": { "a+b": { "c+": 1 } } } }

get value(key: "a+b", m: x).c+;

Expand All @@ -260,7 +260,7 @@ Expression(
)


# Built in (get value, special key)
# Built-in (get value, special key)

get value({
foo-bar: {
Expand Down Expand Up @@ -295,7 +295,7 @@ Expression(
)


# Built in (get value, special key, named args)
# Built-in (get value, special key, named args)

get value(key: "foo-bar", m: {
foo-bar: {
Expand Down Expand Up @@ -329,6 +329,175 @@ Expression(
)


# Built-in (context)

context([
{ key: "b+", value: { c-: 1} }
]).b+.c-

==>

Expression(...)


# Built-in (context, referenced)

{
entries: [
{
"key": "a + b",
"value": 1
},
{
"key": "b--",
"value": 2
}
],
ctx: context(entries),
value: ctx.a + b + ctx.b--
}

==>

Expression(
Context("{",
ContextEntry(
Key(...),List(...)
),
ContextEntry(
Key(...),FunctionInvocation(...)
),
ContextEntry(
Key(...),ArithmeticExpression(
PathExpression(
VariableName(...),
VariableName(...)
),
ArithOp,
PathExpression(
VariableName(...),
VariableName(...)
)
)
),
"}")
)


# Built-in (context, dynamic entry)

{
dynamicEntry: {
"key": "a + b",
"value": {
d-e: 1000
}
},
entries: [
dynamicEntry
],
ctx: context(entries),
value: ctx.a+b.d-e
}

==>

Expression(
Context("{",
ContextEntry(
Key(...),Context(...)
),
ContextEntry(
Key(...),List("[",
VariableName(...),
"]")
),
ContextEntry(
Key(...),FunctionInvocation(...)
),
ContextEntry(
Key(Name(...)),PathExpression(
PathExpression(
VariableName(...),
VariableName(...)
),
VariableName(...)
)
),
"}")
)


# Built-in (context, dynamic key/value)

{
dynamicKey: "a + b",
dynamicValue: {
d-e: 1000
},
entries: [
{
"key": dynamicKey,
"value": dynamicValue
}
],
ctx: context(entries),
value: ctx.a + b.d-e
}

==>

Expression(
Context("{",
ContextEntry(
Key(...),StringLiteral
),
ContextEntry(
Key(...),Context(...)
),
ContextEntry(
Key(...),List("[",
Context("{",
ContextEntry(...),
ContextEntry(...),
"}")
"]")
),
ContextEntry(
Key(...),FunctionInvocation(...)
),
ContextEntry(
Key(Name(...)),PathExpression(
PathExpression(
VariableName(...),
VariableName(...)
),
VariableName(...)
)
),
"}")
)


# Built-in (context, error)

[
context(),
context(null),
context([]),
context([ { foo: bar }, 1 ]),
context([ nope ]),
context([ { key: nope, value: 100 } ]),
context([ { key: 'abc' } ])
]

==>

Expression(
List(...)
)


# null { "top": "Expressions" }

null[1];
Expand Down Expand Up @@ -2075,6 +2244,20 @@ Expression(
)


# Context (simple)

{
a: 1,
b: "BAR"
}

==>

Expression(
Context(...)
)


# Context (variable scopes)

{
Expand Down
Loading