Exemple #1
0
        public override ParserFunc <TFuture> BuildParser <TFuture>(Future <Lexeme, TFuture> future)
        {
            ParserFunc <TFuture> scan = null;

            scan = (scanner, context) =>
            {
                var s1 = scanner.Fork();

                var l = scanner.Read();

                int tokenIndex;
                if (LexerStateIndex.HasValue)
                {
                    tokenIndex = l.GetTokenIndex(LexerStateIndex.Value);
                }
                else
                {
                    tokenIndex = l.TokenIndex;
                }

                if (tokenIndex == ExpectedToken.Index && m_qualificationPredicate(l))
                {
                    var r = context.StepResult(0, () => future(l)(scanner, context));
                    return(r);
                }
                else
                {
                    Lexeme          correctionLexeme = l.GetErrorCorrectionLexeme(ExpectedToken.Index, MissingCorrection);
                    ErrorCorrection insertCorrection = new InsertedErrorCorrection(ExpectedToken.ToString(), correctionLexeme.Value.Span);

                    if (l.IsEndOfStream)
                    {
                        scanner.Join(s1);
                        return(context.StepResult(1, () => future(correctionLexeme)(scanner, context), insertCorrection)); //insert
                    }
                    else
                    {
                        ErrorCorrection deleteCorrection = new DeletedErrorCorrection(l);
                        return(context.ChooseBest(context.StepResult(1, () => future(correctionLexeme)(s1, context), insertCorrection), //insert
                                                  context.StepResult(1, () => scan(scanner, context), deleteCorrection)));              //delete
                    }
                }
            };

            return(scan);
        }
Exemple #2
0
        public override ParserFunc <TFuture> BuildParser <TFuture>(Future <Lexeme, TFuture> future)
        {
            ParserFunc <TFuture> scan = null;

            scan = (scanner, context) =>
            {
                var l = scanner.Read();

                if (l.IsEndOfStream)
                {
                    return(context.StepResult(0, () => future(l)(scanner, context)));
                }
                ErrorCorrection deleteCorrection = new DeletedErrorCorrection(l);
                return(context.StepResult(1, () => scan(scanner, context), deleteCorrection)); //delete to recover
            };

            return(scan);
        }