public ParserRunner(Parser <T> parser, ParserContext context) { CodeContract.RequiresArgumentNotNull(parser, "parser"); CodeContract.RequiresArgumentNotNull(context, "context"); m_runner = parser.BuildParser(FinalFuture); Debug.Assert(m_runner != null); Context = context; }
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); }
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); }
private ParserFunc(ParserFunc that) : base(that) { _startIdx = that._startIdx; // 浅拷贝,因为必须在全局保持状态一致 _backtracings = that._backtracings; // 浅拷贝,因为是不变量 _tokens = that._tokens; _bnfDefination = that._bnfDefination; _currentNonTerminal = that._currentNonTerminal; _currentTokenIdx = that._currentTokenIdx; _ruleItemIdx = that._ruleItemIdx; // 浅拷贝,因为回溯会覆盖之前的结果,不会产生影响 _childs = that._childs; // 浅拷贝,因为是不变量 _ruleList = that._ruleList; _ruleIdx = that._ruleIdx; }
public void Assign(Parser <T> assignedParser) { CodeContract.RequiresArgumentNotNull(assignedParser, "assignedParser"); Rule = assignedParser.Rule; }
public void Assign <U>(Parser <U> assignedParser) where U : class, T { CodeContract.RequiresArgumentNotNull(assignedParser, "assignedParser"); Rule = assignedParser.Rule; }
public Parser(ParserFunc <T> rule) { Rule = rule; }
public static Parser <T> Wrap <T>(ParserFunc <T> rule) { CodeContract.RequiresArgumentNotNull(rule, "rule"); return(new Parser <T>(rule)); }