microsoft / Kusto-Query-Language

Kusto Query Language is a simple and productive language for querying Big Data.
Apache License 2.0
510 stars 97 forks source link

Multiple commands grammar #57

Closed micekdan closed 2 years ago

micekdan commented 2 years ago

Hello, in our team we use single script file for multiple ingest commands like

.set-or-replace A <| Cook_A()

.set-or-replace B <| Cook_B()

image

This file however cannot be parsed correctly even if the commands are ended by a semicolon.

.set-or-replace A <| Cook_A();

.set-or-replace B <| Cook_B();

image

I think the culprit is in CommandGrammar.cs:172

var commandBlock =
    Rule(
        SeparatedList(
            commandStatement, // first one is a command statement
            SyntaxKind.SemicolonToken,
            q.Statement,      // all others elements are query statements               <- ##### when this line is deleted the second case works but still the first one won't
            MissingCommandStatementNode,
            endOfList: EndOfText,
            oneOrMore: true,
            allowTrailingSeparator: true),
        Optional(skippedTokens), // consumes all remaining tokens (no diagnostic)
        Optional(Token(SyntaxKind.EndOfTextToken)),
        (cmd, skipped, end) =>
            new CommandBlock(cmd, skipped, end));

return new CommandGrammar(commandBlock);
mattwar commented 2 years ago

You cannot parse a document with multiple commands/queries in it that are separated by a blank line. These are considered entirely separate commands/queries that are just located in the same file. The query language grammar does not understand this concept. What does support it is the editor, Kusto Explorer, ADX, etc.

You can use the Editor.CodeScript class to break the document down into separate parts and it will parse each piece separately when you access each block's Service property. There is no easy way to get at the syntax model of each piece this way though, as this is meant as an abstraction for the editor. You can, instead, just use the CodeScript class to break the document down and then invoke the KustoCode.ParseAndAnalyze method for each block manually.