Пример #1
0
        public VisualizerData(object o, Config config)
        {
            if (!(o is IParseTree tree))
            {
                throw new ArgumentException("Unhandled type.");
            }

            if (!config.RootNodePath.IsNullOrWhitespace())
            {
                var pathParts = config.RootNodePath.Split('.').Select(x =>
                                                                      int.TryParse(x, out var ret) ?
                                                                      ret :
                                                                      -1
                                                                      ).ToArray();
                foreach (var pathPart in pathParts)
                {
                    var nextTree = tree.GetChild(pathPart);
                    if (nextTree == null)
                    {
                        break;
                    }
                    tree = tree.GetChild(pathPart);
                }
            }

            Config = config;
            Source = tree.GetText();

            var parserType       = tree.GetType().DeclaringType;
            var vocabulary       = parserType.GetField("DefaultVocabulary").GetValue(null) as IVocabulary;
            var tokenTypeMapping = Range(1, vocabulary.MaxTokenType).ToDictionary(x => (x, vocabulary.GetSymbolicName(x)));

            var ruleNames = parserType.GetField("ruleNames").GetValue(null) as string[];

            var rulenameMapping = new Dictionary <Type, (string name, int index)>();
            var actualRoot      = new ParseTreeNode(tree, Tokens, ruleNames, tokenTypeMapping, config, rulenameMapping, Config.RootNodePath);

            if (config.RootNodePath.IsNullOrWhitespace())
            {
                Root = actualRoot;
            }
            else
            {
                Root         = ParseTreeNode.GetPlaceholder(actualRoot);
                SourceOffset = actualRoot.CharSpan.startChar;
            }

            #region Load debuggee state
            TokenTypeMapping = tokenTypeMapping;

            UsedRuleContexts = rulenameMapping.Keys
                               .Select(x => {
                rulenameMapping.TryGetValue(x, out var y);
                return(new ClassInfo(x, y.name, y.index));
            })
                               .OrderBy(x => x.Name)
                               .ToList();

            {
                // load available parsers and lexers

                var baseTypes = new[] { typeof(Parser), typeof(Lexer) };
                var types     = AppDomain.CurrentDomain.GetAssemblies()
                                .Where(x => x != GetType().Assembly)
                                .SelectMany(x => {
                    try {
                        return(x.GetTypes());
                    } catch {
                        AssemblyLoadErrors.Add(x.FullName);
                        return(Empty <Type>());
                    }
                })
                                .Where(x => !x.IsAbstract);
                foreach (var t in types)
                {
                    var dest =
                        t.InheritsFromOrImplements <Parser>() ? AvailableParsers :
                        t.InheritsFromOrImplements <Lexer>() ? AvailableLexers :
                        null;
                    dest?.Add(new ClassInfo(t));
                }

                Config.SelectedParserName = checkSelection(AvailableParsers, Config.SelectedParserName);
                Config.SelectedLexerName  = checkSelection(AvailableLexers, Config.SelectedLexerName);
            }

            #endregion
        }
Пример #2
0
        public VisualizerData(object o, Config config)
        {
            if (config is null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            Config = config;

            Type[] types;
            T createInstance <T>(string typename, object[]?args = null) =>
            types.Single(x => x.FullName == typename).CreateInstance <T>(args);

            {
                var baseTypes = new[] { typeof(Parser), typeof(Lexer) };
                types = AppDomain.CurrentDomain.GetAssemblies()
                        .Where(x => x != GetType().Assembly)
                        .SelectMany(x => {
                    var ret = Empty <Type>();
                    if (!x.FullName.StartsWithAny(ignnoreLoadErrors))
                    {
                        try {
                            ret = x.GetTypes();
#pragma warning disable CA1031 // Do not catch general exception types
                        } catch {
#pragma warning restore CA1031 // Do not catch general exception types
                            AssemblyLoadErrors.Add(x.FullName);
                        }
                    }
                    return(ret);
                })
                        .Where(x => !x.IsAbstract)
                        .ToArray();
                foreach (var t in types)
                {
                    if (t.InheritsFromOrImplements <Parser>())
                    {
                        AvailableParsers.Add(new ClassInfo(t, null, null, true));
                    }
                    else if (t.InheritsFromOrImplements <Lexer>())
                    {
                        AvailableLexers.Add(new ClassInfo(t));
                    }
                }

                Config.SelectedLexerName  = checkSelection(AvailableLexers, Config.SelectedLexerName);
                Config.SelectedParserName = checkSelection(AvailableParsers, Config.SelectedParserName);
                if (!Config.SelectedParserName.IsNullOrWhitespace())
                {
                    var parserInfo = AvailableParsers.OneOrDefault(x => x.FullName == Config.SelectedParserName);
                    if (parserInfo is null)
                    {
                        Config.ParseTokensWithRule = null;
                    }
                    else
                    {
                        if (Config.ParseTokensWithRule.NotIn(parserInfo.MethodNames))
                        {
                            Config.ParseTokensWithRule = null;
                        }
                        if (Config.ParseTokensWithRule is null)
                        {
                            Config.ParseTokensWithRule = parserInfo.MethodNames.OneOrDefault();
                        }
                    }
                }
            }

            string?source = null;
            BufferedTokenStream?tokenStream = null;
            IParseTree?         tree        = null;
            IVocabulary?        vocabulary  = null;

            // these three are mutually exclusive
            switch (o)
            {
            case string source1:
                source          = source1;
                CanSelectLexer  = true;
                CanSelectParser = true;
                Source          = source;
                break;

            case BufferedTokenStream tokenStream1:
                tokenStream     = tokenStream1;
                CanSelectParser = true;
                Source          = tokenStream.TokenSource.InputStream.ToString();
                break;

            case IParseTree tree1:
                tree   = tree1;
                Source = tree.GetPositionedText();
                break;

            default:
                throw new ArgumentException("Unhandled type");
            }

            if (source != null && !Config.SelectedLexerName.IsNullOrWhitespace())
            {
                var input = new AntlrInputStream(source);
                var lexer = createInstance <Lexer>(Config.SelectedLexerName !, new[] { input });
                tokenStream = new CommonTokenStream(lexer);
                vocabulary  = lexer.Vocabulary;
            }

            if (
                tokenStream != null &&
                !Config.SelectedParserName.IsNullOrWhitespace() &&
                !Config.ParseTokensWithRule.IsNullOrWhitespace()
                )
            {
                var parser = createInstance <Parser>(Config.SelectedParserName, new[] { tokenStream });
                tree       = (IParseTree)parser.GetType().GetMethod(Config.ParseTokensWithRule)?.Invoke(parser, Array.Empty <object>()) !;
                vocabulary = parser.Vocabulary;
            }

            if (tree is null && tokenStream is null)
            {
                return;
            }

            if (tree is null)
            {
                tokenStream !.Fill();
                Tokens = tokenStream.GetTokens()
                         .Select(token => new Token(token, getTokenTypeMapping()))
                         .Where(token => token.ShowToken(config))
                         .ToList();
                return;
            }

            if (!config.RootNodePath.IsNullOrWhitespace())
            {
                var pathParts = config.RootNodePath.Split('.').Select(x =>
                                                                      int.TryParse(x, out var ret) ?
                                                                      ret :
                                                                      -1
                                                                      ).ToArray();
                foreach (var pathPart in pathParts)
                {
                    var nextTree = tree.GetChild(pathPart);
                    if (nextTree == null)
                    {
                        break;
                    }
                    tree = tree.GetChild(pathPart);
                }
            }

            var parserType = tree.GetType().DeclaringType;
            Config.SelectedParserName = parserType.FullName;
            if (vocabulary is null)
            {
                vocabulary = parserType.GetField("DefaultVocabulary").GetValue(null) as IVocabulary;
            }

            var tokenTypeMapping = getTokenTypeMapping() !;

            var ruleNames       = (parserType.GetField("ruleNames").GetValue(null) as string[]) !;
            var rulenameMapping = new Dictionary <Type, (string?name, int?index)>();
            Tokens = new List <Token>();
            var actualRoot = new ParseTreeNode(tree, Tokens, ruleNames, tokenTypeMapping, config, rulenameMapping, Config.RootNodePath);
            if (config.RootNodePath.IsNullOrWhitespace())
            {
                Root = actualRoot;
            }
            else
            {
                Root         = ParseTreeNode.GetPlaceholder(actualRoot);
                SourceOffset = actualRoot.CharSpan.startChar;
            }

            UsedRuleContexts = rulenameMapping.Keys
                               .Select(x => {
                rulenameMapping.TryGetValue(x, out var y);
                return(new ClassInfo(x, y.name, y.index));
            })
                               .OrderBy(x => x.Name)
                               .ToList();

            Dictionary <int, string>?getTokenTypeMapping()
            {
                if (vocabulary is null)
                {
                    return(null);
                }
#if ANTLR_RUNTIME
                int maxTokenType = vocabulary.MaxTokenType;
#else
                int maxTokenType = (vocabulary as Vocabulary) !.getMaxTokenType();
#endif
                TokenTypeMapping = Range(1, maxTokenType).ToDictionary(x => (x, vocabulary.GetSymbolicName(x)));
                return(TokenTypeMapping);
            }
        }