Exemplo n.º 1
0
        /// <summary>
        /// Compiles the IDL contained in the input stream.
        /// </summary>
        /// <param name="inputStream">The stream containing the text to parse.</param>
        /// <returns>The result of the compile operation.</returns>
        public CompilationResult Compile(Stream inputStream)
        {
            var parserErrorListener = new CollectingErrorListener();
            var parser = ThriftParserFactory.Create(inputStream, parserErrorListener);

            var documentNode = parser.document();
            var document     = BinderProvider.Instance
                               .GetBinder(documentNode)
                               .Bind <IDocument>(documentNode, null);

            var visitor = new CompilationVisitor();

            document.Accept(visitor);

            var combinedMessages = visitor.Messages
                                   .Union(parserErrorListener.Messages)
                                   .ToList();

            return(new CompilationResult(document, combinedMessages));
        }
        /// <summary>
        /// Creates a new parser from the specified input stream.
        /// </summary>
        /// <param name="inputStream">The stream to parse.</param>
        /// <param name="errorListener">
        /// Used to collect any errors encountered during lexing or parsing. If
        /// no listener is provided, the default Antlr listeners that output
        /// messages to the console will be used.
        /// </param>
        /// <returns>The parser.</returns>
        public static ThriftParser Create(
            Stream inputStream,
            CollectingErrorListener errorListener = null)
        {
            var charStream  = new AntlrInputStream(inputStream);
            var lexer       = new ThriftLexer(charStream);
            var tokenStream = new CommonTokenStream(lexer);

            ThriftParser parser = new ThriftParser(tokenStream);

            if (errorListener != null)
            {
                lexer.RemoveErrorListeners();
                lexer.AddErrorListener(errorListener);

                parser.RemoveErrorListeners();
                parser.AddErrorListener(errorListener);
            }

            return(parser);
        }