Пример #1
0
 public KustoCache WithGlobals(GlobalState globals)
 {
     if (this.Globals == globals)
     {
         return(this);
     }
     else
     {
         return(new KustoCache(globals));
     }
 }
Пример #2
0
        /// <summary>
        /// Create a new <see cref="KustoCode"/> instance from the text and globals and performs semantic analysis.
        /// </summary>
        /// <param name="text">The code text</param>
        /// <param name="globals">The globals to use for parsing and semantic analysis. Defaults to <see cref="GlobalState.Default"/></param>.
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel parsing and semantic analysis.</param>
        public static KustoCode ParseAndAnalyze(string text, GlobalState globals = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (text == null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            var tokens = TokenParser.Default.ParseTokens(text, alwaysProduceEndToken: true);
            var starts = GetTokenStarts(tokens);

            return(Create(text, globals, tokens, starts, analyze: true, cancellationToken: cancellationToken));
        }
Пример #3
0
        /// <summary>
        /// Creates a new <see cref="KustoCode"/> form the already parsed lexical tokens.
        /// </summary>
        private static KustoCode Create(string text, GlobalState globals, LexicalToken[] tokens, List <int> tokenStarts, bool analyze, CancellationToken cancellationToken)
        {
            Parser <LexicalToken> grammar;
            SyntaxNode            syntax;

            globals = globals ?? GlobalState.Default;

            var kind = GetKind(text);

            switch (kind)
            {
            case CodeKinds.Command:
                var commandBlock = CommandGrammar.From(globals).CommandBlock;
                grammar = commandBlock;
                syntax  = commandBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Directive:
                grammar = DirectiveGrammar.DirectiveBlock;
                syntax  = DirectiveGrammar.DirectiveBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Query:
            default:
                var queryBlock = QueryGrammar.From(globals).QueryBlock;
                grammar = queryBlock;
                syntax  = queryBlock.ParseFirst(tokens);
                break;
            }

            var tree = new SyntaxTree(syntax);

            syntax.InitializeTriviaStarts();

            LocalBindingCache localCache = null;
            TypeSymbol        resultType = null;
            var analyzed = false;

            if (analyze)
            {
                cancellationToken.ThrowIfCancellationRequested();
                analyzed = true;

                localCache = new LocalBindingCache();
                if (Binder.TryBind(tree, globals, localCache, null, cancellationToken))
                {
                    resultType = DetermineResultType(syntax);
                }
            }

            return(new KustoCode(text, kind, globals, grammar, tree, analyzed, resultType, tokens, tokenStarts, localCache));
        }
Пример #4
0
        /// <summary>
        /// Creates a new <see cref="KustoCode"/> form the already parsed lexical tokens.
        /// </summary>
        private static KustoCode Create(string text, GlobalState globals, LexicalToken[] tokens, bool analyze, CancellationToken cancellationToken)
        {
            Parser <LexicalToken> grammar;
            SyntaxNode            syntax;

            var kind = GetKind(text);

            switch (kind)
            {
            case CodeKinds.Command:
                var commandBlock = CommandGrammar.From(globals).CommandBlock;
                grammar = commandBlock;
                syntax  = commandBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Directive:
                grammar = DirectiveGrammar.DirectiveBlock;
                syntax  = DirectiveGrammar.DirectiveBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Query:
            default:
                var queryBlock = QueryGrammar.From(globals).QueryBlock;
                grammar = queryBlock;
                syntax  = queryBlock.ParseFirst(tokens);
                break;
            }

            var maxDepth     = ComputeMaxDepth(syntax);
            var isAnalyzable = maxDepth <= MaxAnalyzableSyntaxDepth;

            syntax.InitializeTriviaStarts();

            LocalBindingCache localCache = null;

            TypeSymbol resultType = null;

            if (analyze && isAnalyzable)
            {
                cancellationToken.ThrowIfCancellationRequested();
                localCache = new LocalBindingCache();
                if (Binder.TryBind(syntax, globals, localCache, null, cancellationToken))
                {
                    resultType = DetermineResultType(syntax);
                }
            }

            return(new KustoCode(text, kind, globals, grammar, syntax, analyze && isAnalyzable, resultType, tokens, localCache, maxDepth));
        }
Пример #5
0
        /// <summary>
        /// Returns a new <see cref="KustoCode"/> with semantic analysis performed
        /// or the current instance if semantic analysis has already been performed.
        /// </summary>
        public KustoCode Analyze(GlobalState globals = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (globals == null)
            {
                globals = this.Globals;
            }

            if (this.HasSemantics && this.Globals == globals)
            {
                return(this);
            }
            else
            {
                return(Create(this.Text, this.Globals, this.lexerTokens, analyze: true, cancellationToken));
            }
        }
Пример #6
0
 private KustoCode(
     string text,
     string kind,
     GlobalState globals,
     Parser <LexicalToken> grammar,
     SyntaxNode syntax,
     bool hasSemantics,
     LexicalToken[] lexerTokens,
     LocalBindingCache localCache,
     int maxDepth)
 {
     this.Text         = text;
     this.Kind         = kind;
     this.Globals      = globals;
     this.Grammar      = grammar;
     this.Syntax       = syntax;
     this.HasSemantics = hasSemantics;
     this.lexerTokens  = lexerTokens;
     this.localCache   = localCache;
     this.MaxDepth     = maxDepth;
 }
Пример #7
0
 private KustoCode(
     string text,
     string kind,
     GlobalState globals,
     Parser <LexicalToken> grammar,
     SyntaxTree tree,
     bool hasSemantics,
     TypeSymbol resultType,
     LexicalToken[] lexerTokens,
     LocalBindingCache localCache)
 {
     this.Text         = text;
     this.Kind         = kind;
     this.Globals      = globals;
     this.Grammar      = grammar;
     this.Tree         = tree;
     this.HasSemantics = hasSemantics;
     this.ResultType   = resultType;
     this.lexerTokens  = lexerTokens;
     this.localCache   = localCache;
 }
Пример #8
0
        /// <summary>
        /// Creates a new <see cref="KustoCode"/> form the already parsed lexical tokens.
        /// </summary>
        private static KustoCode Create(string text, GlobalState globals, LexicalToken[] tokens, bool analyze, CancellationToken cancellationToken)
        {
            Parser <LexicalToken> grammar;
            SyntaxNode            syntax;

            var kind = GetKind(text);

            switch (kind)
            {
            case CodeKinds.Command:
                var commandBlock = CommandGrammar.From(globals).CommandBlock;
                grammar = commandBlock;
                syntax  = commandBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Directive:
                grammar = DirectiveGrammar.DirectiveBlock;
                syntax  = DirectiveGrammar.DirectiveBlock.ParseFirst(tokens);
                break;

            case CodeKinds.Query:
            default:
                var queryBlock = QueryGrammar.From(globals).QueryBlock;
                grammar = queryBlock;
                syntax  = queryBlock.ParseFirst(tokens);
                break;
            }

            LocalBindingCache localCache = null;

            if (analyze)
            {
                cancellationToken.ThrowIfCancellationRequested();
                localCache = new LocalBindingCache();
                Binder.Bind(syntax, globals, localCache, null, cancellationToken);
            }

            return(new KustoCode(text, kind, globals, grammar, syntax, analyze, tokens, localCache));
        }
Пример #9
0
        public static void Bind(SyntaxNode syntax, GlobalState globals)
        {
            var tree = new SyntaxTree(syntax);

            Binder.TryBind(tree, globals);
        }
Пример #10
0
 public static void Bind(SyntaxNode syntax, GlobalState globals)
 {
     Binder.TryBind(syntax, globals);
 }
Пример #11
0
 public KustoCache(GlobalState globals)
 {
     this.Globals = globals;
 }