Example #1
0
        public T Parse(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner = m_scanner;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            Lexeme r;

            do
            {
                r = scanner.Read();

                engine.Input(r);
            } while (!r.IsEndOfStream);

            object result = engine.GetResult(0, m_errorManager);

            return((T)result);
        }
Example #2
0
        public T Parse(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner = m_scanner;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            Lexeme r;

            do
            {
                r = scanner.Read();

                engine.Input(r);
            } while (!r.IsEndOfStream);

            if (engine.AcceptedCount == 0)
            {
                throw new ParsingFailureException("There's no parsing result");
            }

            if (engine.AcceptedCount > 1 && engine.GetResultInfo(0).ErrorCount == 0)
            {
                throw new ParsingFailureException("Multiple parsing results are found. There's ambiguity in your grammar");
            }

            object result = engine.GetResult(0, m_errorManager);

            return((T)result);
        }
Example #3
0
        public T Parse(SourceReader source)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner = m_scanner;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            Lexeme r = scanner.Read();

            while (true)
            {
                try
                {
                    engine.Input(r);
                }
                catch (PanicRecoverException prex)
                {
                    var follow = prex.PossibleFollow;

                    HashSet <int> validTokens = new HashSet <int>(follow.Select(p =>
                    {
                        Terminal t = p as Terminal;
                        if (t != null)
                        {
                            return(t.Token.Index);
                        }
                        else
                        {
                            return(m_scannerInfo.EndOfStreamTokenIndex);
                        }
                    }));

                    while (!validTokens.Contains(r.TokenIndex) && !r.IsEndOfStream)
                    {
                        r = scanner.Read();
                    }

                    continue;
                }

                if (r.IsEndOfStream)
                {
                    break;
                }

                r = scanner.Read();
            }

            if (engine.AcceptedCount == 0)
            {
                throw new ParsingFailureException("There's no parsing result");
            }

            if (engine.AcceptedCount > 1 && engine.GetResultInfo(0).ErrorCount == 0)
            {
                throw new ParsingFailureException("Multiple parsing results are found. There's ambiguity in your grammar");
            }

            object result = engine.GetResult(0, m_errorManager);

            return((T)result);
        }
Example #4
0
        public T Parse(SourceReader source, CompilationErrorList errorList, CancellationToken ctoken)
        {
            CodeContract.RequiresArgumentNotNull(source, "source");

            if (!m_isInitialized)
            {
                OnInitialize();
            }

            Scanner scanner;

            scanner = new Scanner(m_scannerInfo);
            scanner.SetTriviaTokens(m_triviaTokens.Select(t => t.Index).ToArray());
            scanner.ErrorList      = errorList;
            scanner.RecoverErrors  = true;
            scanner.LexicalErrorId = m_errorDefinition.LexicalErrorId;

            scanner.SetSource(source);

            ParserEngine engine = new ParserEngine(m_transitionTable, m_errorDefinition);

            engine.EnableDeletionRecovery    = EnableDeletionRecovery;
            engine.EnableInsertionRecovery   = EnableInsertionRecovery;
            engine.EnableReplacementRecovery = EnableReplacementRecovery;

            Lexeme r = scanner.Read();

            while (true)
            {
                try
                {
                    engine.Input(r, ctoken);
                }
                catch (PanicRecoverException prex)
                {
                    var follow = prex.PossibleFollow;

                    HashSet <int> validTokens = new HashSet <int>(follow.Select(p =>
                    {
                        Terminal t = p as Terminal;
                        if (t != null)
                        {
                            return(t.Token.Index);
                        }
                        else
                        {
                            return(m_scannerInfo.EndOfStreamTokenIndex);
                        }
                    }));

                    while (!validTokens.Contains(r.TokenIndex) && !r.IsEndOfStream)
                    {
                        r = scanner.Read();
                    }

                    continue;
                }

                if (r.IsEndOfStream)
                {
                    break;
                }

                r = scanner.Read();
            }

            if (engine.AcceptedCount == 0)
            {
                throw new ParsingFailureException("There's no parsing result");
            }

            if (engine.AcceptedCount > 1 && engine.GetResultInfo(0).ErrorCount == 0)
            {
                throw new ParsingFailureException("Multiple parsing results are found. There's ambiguity in your grammar");
            }

            object result = engine.GetResult(0, errorList);

            return((T)result);
        }