Пример #1
0
        private List <GrammarReference> FindGrammars([NotNull] string path, string name = null)
        {
            var di      = new DirectoryInfo(path);
            var files   = di.GetFiles("*.dll");
            var results = new List <GrammarReference>();

            foreach (var file in files)
            {
                try
                {
                    var assembly = Assembly.Load(File.ReadAllBytes(file.FullName));

                    // We make parsers an explicit list to avoid multiple enumerations
                    var parsers = FindGrammarParsersInAssembly(assembly).ToList();
                    var lexers  = FindGrammarLexersInAssembly(assembly);
                    IEnumerable <LexerType> matches;
                    if (!string.IsNullOrEmpty(name))
                    {
                        matches = from lexer in lexers
                                  where lexer.GrammarName == name
                                  select lexer;
                    }
                    else
                    {
                        matches = lexers;
                    }

                    foreach (var lexer in matches)
                    {
                        var parser = (from candidate in parsers
                                      where candidate.GrammarName == lexer.GrammarName
                                      select candidate).FirstOrDefault();

                        var rules = parser.ActualType == null
                                 ? new List <string>()
                                 : GetParserRulesForGrammarParser(parser.ActualType).ToList();
                        var grammarRef = new GrammarReference(
                            file,
                            lexer.GrammarName,
                            lexer.ActualType,
                            parser.ActualType,
                            rules);
                        results.Add(grammarRef);
                    }
                }
                catch (FileLoadException)
                {
                    // do nothing for now, perhaps log later
                }
                catch (BadImageFormatException)
                {
                    // do nothing for now, perhaps log later
                }
            }

            return(results);
        }
Пример #2
0
        /// <summary>
        /// Builds a new parser for the specified grammar and input using the supplied rules.
        /// </summary>
        /// <param name="grammar">The grammar to use.</param>
        /// <param name="inputText">The input text to use.</param>
        /// <param name="option">The parsing options to use.</param>
        /// <returns>A new <see cref="Parser"/> instance.</returns>
        /// <exception cref="GrammarException">No parser found for specified grammar.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="grammar"/> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="inputText"/> is <see langword="null" />.</exception>
        public Parser BuildParserWithOptions([NotNull] GrammarReference grammar, [NotNull] string inputText, ParseOption option)
        {
            if (grammar is null)
            {
                throw new ArgumentNullException(nameof(grammar));
            }
            if (inputText is null)
            {
                throw new ArgumentNullException(nameof(inputText));
            }
            if (grammar.Parser == null)
            {
                throw new GrammarException($"No parser found for grammar \"{grammar.GrammarName}\"");
            }

            var loader            = new Grammar.Loader();
            var inputStream       = new AntlrInputStream(inputText);
            var lexer             = loader.LoadLexer(grammar, inputStream);
            var commonTokenStream = new CommonTokenStream(lexer);

            commonTokenStream.Fill();
            Tokens       = commonTokenStream.GetTokens();
            SyntaxTokens = ConvertTokensToSyntaxTokens(lexer, Tokens);

            if (option.HasFlag(ParseOption.Tokens))
            {
                foreach (var token in Tokens)
                {
                    Console.WriteLine(token.ToString());
                }
            }

            var parser = loader.LoadParser(grammar, commonTokenStream);

            // Handle Tree parsing option
            parser.BuildParseTree = option.HasFlag(ParseOption.Tree);

            // Handle Diagnostics parsing option
            if (option.HasFlag(ParseOption.Diagnostics))
            {
                parser.AddErrorListener(new DiagnosticErrorListener());
                parser.Interpreter.PredictionMode = Antlr4.Runtime.Atn.PredictionMode.LlExactAmbigDetection;
            }

            // Handle Sll parsing option
            if (option.HasFlag(ParseOption.Sll))
            {
                parser.Interpreter.PredictionMode = Antlr4.Runtime.Atn.PredictionMode.Sll;
            }

            // Handle Trace parsing option
            parser.Trace = option.HasFlag(ParseOption.Trace);

            return(parser);
        }
Пример #3
0
        /// <summary>
        /// Loads a Parser instance for the referenced grammar.
        /// </summary>
        /// <param name="reference">The grammar reference.</param>
        /// <param name="stream">The token stream.</param>
        /// <returns>A new <see cref="Antlr4.Runtime.Parser" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="reference"/> or <paramref name="stream"/> are <see langword="null"/></exception>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="reference" /> is <see langword="null" /></exception>
        /// <exception cref="T:System.IO.FileLoadException">A file that was found could not be loaded.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">The assembly path is an empty string ("") or does not exist.</exception>
        /// <exception cref="T:System.BadImageFormatException">The assembly path is not a valid assembly.</exception>
        public Antlr4.Runtime.Parser LoadParser([NotNull] GrammarReference reference, [NotNull] ITokenStream stream)
        {
            if (reference is null)
            {
                throw new ArgumentNullException(nameof(reference));
            }
            if (stream is null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            Assembly.Load(File.ReadAllBytes(reference.AssemblyPath));
            var lexer = Activator.CreateInstance(reference.Parser, stream) as Parser;

            return(lexer);
        }
Пример #4
0
        private List <GrammarReference> FindGrammarsInFile([NotNull] string filePath, string name = null)
        {
            var file    = new FileInfo(filePath);
            var results = new List <GrammarReference>();

            var assembly = Assembly.Load(File.ReadAllBytes(file.FullName));

            // We make parsers an explicit list to avoid multiple enumerations
            var parsers = FindGrammarParsersInAssembly(assembly).ToList();
            var lexers  = FindGrammarLexersInAssembly(assembly);
            IEnumerable <LexerType> matches;

            if (!string.IsNullOrEmpty(name))
            {
                matches = from lexer in lexers
                          where lexer.GrammarName == name
                          select lexer;
            }
            else
            {
                matches = lexers;
            }

            foreach (var lexer in matches)
            {
                ParserType parser;
                parser = (from candidate in parsers
                          where candidate.GrammarName == lexer.GrammarName
                          select candidate).FirstOrDefault();

                var rules = parser.ActualType == null
                              ? new List <string>()
                              : GetParserRulesForGrammarParser(parser.ActualType).ToList();
                var grammarRef = new GrammarReference(
                    file,
                    lexer.GrammarName,
                    lexer.ActualType,
                    parser.ActualType,
                    rules);
                results.Add(grammarRef);
            }

            return(results);
        }
Пример #5
0
        /// <summary>
        /// Tokenizes the grammar text and returns the tokens.
        /// </summary>
        /// <param name="grammar">The grammar.</param>
        /// <param name="inputText">The input text.</param>
        /// <returns>A new <see cref="IList{IToken}" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="grammar"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="inputText"/> is <see langword="null"/> or empty.</exception>
        /// <exception cref="T:System.IO.FileLoadException">Grammar assembly could not be loaded.</exception>
        /// <exception cref="T:System.IO.FileNotFoundException">The grammar assembly path is an empty string ("") or does not exist.</exception>
        /// <exception cref="T:System.BadImageFormatException">The grammar assembly path is not a valid assembly.</exception>
        public IList <IToken> Tokenize([NotNull] GrammarReference grammar, [NotNull] string inputText)
        {
            if (grammar is null)
            {
                throw new ArgumentNullException(nameof(grammar));
            }
            if (inputText is null)
            {
                throw new ArgumentNullException(nameof(inputText));
            }

            var loader      = new Grammar.Loader();
            var inputStream = new AntlrInputStream(inputText);
            var lexer       = loader.LoadLexer(grammar, inputStream);

            lexer.RemoveErrorListeners();
            var commonTokenStream = new CommonTokenStream(lexer);

            commonTokenStream.Fill();
            Tokens       = commonTokenStream.GetTokens();
            SyntaxTokens = ConvertTokensToSyntaxTokens(lexer, Tokens);
            return(Tokens);
        }