-
Notifications
You must be signed in to change notification settings - Fork 204
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
Metabase Expressions Grammar Assistance Thread #327
Comments
Cool @tlrobinson I did not know there was Open Source BI & analytics software. 👍 I see you have an expression language which you compile to some data structure. The Problem:Is that during your parsing flow you lose the vast majority of the syntactic information examples:
|
A Possible Solution:Basically instead of building an Abstract Syntax Tree which does not contain the entire syntactic information. The Parser should create a more low level data structure: A Concrete Syntax Tree Example building Language Services:
For your use case to support syntactic highlights depending on grammar context, you would visit all the AST nodes, find the Identifier tokens in each Node and decide on the color do highlight depending on current parent AstNode. This may be over-designed for a small expression language, not all these steps are necessary for all use cases... |
The "Separate the Grammar from the Grammar Actions" issue when implemented will provide some It will not build any AST, but it will create some low level Concrete Syntax Tree and provide the capability to iterate over it and invoke actions. So something like grammar context dependent syntactic highlights would be trivial to implement using that future Chevrotain functionality. But this issue won't help with semantic related capabilities like "Semantic Checks". |
@bd82 Thanks for all your help. I've got my parser outputting a syntax tree, however the last wrinkle I forgot about is my lexer skips whitespace, which obviously leaves gaps in my syntax tree. For my purposes I could just assume any gaps are spaces (don't care about tabs/newlines) but is there another way to get the full syntax tree without adding |
You are welcome @tlrobinson. For handling whitespace you could use tokenGroups Currently you define the whitespace tokens as "skipped" Which means they are both ignored and sent into oblivion. If you define their group as some custom name, for example "whitespace". So if you have all the whitespace tokens available(including the distinction between tabs/newlines). |
@bd82 Thanks, that makes sense, I'll give that a try. Another question, (sorry if this isn't the best place to ask), my expression language has "aggregations" (like I'd like for identifiers and string literals to parse as a "fields" inside aggregations, and "metrics" (basically a user-defined aggregation) outside aggregations, e.x. I tried this: $.RULE("atomicExpression", (outsideAggregation) => {
return $.OR([
// aggregations and metrics are not allowed inside other aggregations
{GATE: () => outsideAggregation, ALT: () => $.SUBRULE($.aggregationExpression, [false]) },
{GATE: () => outsideAggregation, ALT: () => $.SUBRULE($.metricExpression) },
// fields are not allowed outside aggregations
{GATE: () => !outsideAggregation, ALT: () => $.SUBRULE($.fieldExpression) },
{ALT: () => $.SUBRULE($.parenthesisExpression, [outsideAggregation]) },
{ALT: () => $.SUBRULE($.numberLiteral) }
]);
}); (both But now I'm getting this:
Is there a way to resolve this without duplicating all the rules that can appear inside and outside of aggregations? (e.x. |
Github issues seem more convenient than google groups, Perhaps you are familiar with a better solution for handling these discussions? I know gitter but this use case is not a live chat... Perhaps I will rename this issue to "Metabase Expressions Grammar Assistance" 😄 Back to the Problem:The problem is that the errors are on a pure grammar level (design time) and the gates only exist at runtime.
But I am not certain that really is needed. Can you link a fuller example? |
Currently
|
@bd82 I tried disabling the error and everything seems to work correctly, thanks! |
While this works it just hides the symptom. Note that both the metricExpression and the fieldExpression have exactly the same syntax. You can create a single rule which combines them both. What about something like: $.RULE("metricOrFieldExpression", (isFieldContext) => {
const metricOrFieldName = $.OR([
{ALT: () => $.SUBRULE($.stringLiteral) },
{ALT: () => $.SUBRULE($.identifier) }
]);
let result
if (isFieldContext) {
// do something and create a Field
result = ...
}
else {
// do something and create a Metric
result = ...
}
return result;
}); |
Closing this issue for now as it seems the MetaBase expressions parser using Chevrotain was successfully merged. |
Moved Discussion from:#215 (comment) to here:
@tlrobinson Wrote:
The text was updated successfully, but these errors were encountered: