public override void Chain(Trampoline t, Input input, OnChainResult result) { Left.Chain(t, input, resLeft => { if (resLeft is ISuccess) { Right.Chain(t, resLeft.Remainder, resRight => { if (resRight is ISuccess) { var sucLeft = resLeft as ISuccess; var sucRight = resRight as ISuccess; result(Result.Success(resRight.Remainder, Combinator(sucLeft.Value, sucRight.Value))); } else { result(resRight); } }); } else { result(resLeft); } }); }
public void Add(Parser parser, Input input, OnChainResult continuation) { var tuple = new QueueTuple { Parser = parser, Input = input }; BackLinks.GetOrAdd(input, parsers => { if (!parsers.ContainsKey(parser)) { parsers.Add(parser, new HashSet<OnChainResult>()); } }, () => { var parsers = new Dictionary<Parser, HashSet<OnChainResult>>(); parsers.Add(parser, new HashSet<OnChainResult>()); return parsers; } ); BackLinks[input][parser].Add(continuation); Popped.Get(input, parsers => { if (parsers.ContainsKey(parser)) { foreach (var result in parsers[parser]) { Misc.Trace("Revisited: {0} *=> {1}", tuple, result); continuation(result); } return true; } return false; }, () => { Done.GetOrAdd(input, parsers => { if (!parsers.Contains(parser)) { AddTuple(parsers, tuple); } }, () => { var parsers = new HashSet<Parser>(); AddTuple(parsers, tuple); return parsers; } ); } ); }
public override void Chain(Trampoline t, Input input, OnChainResult result) { result(Parse(input)); }
public override void Chain(Trampoline t, Input input, OnChainResult result) { if (IsLL1()) { if (input.EOF) { result(Result.Failure(input, FailureType.UnexpectedEndOfStream)); } else { var predict = Predict(); Parser parser; if (predict.TryGetValue(input.Current, out parser)) { parser.Chain(t, input, result); } else { result(Result.Failure(input, FailureType.UnexpectedChars)); } } } else { var thunk = new ThunkParser(this, (tr, i, onResult) => { var results = new HashSet<IResult>(); var predicted = false; var gather = Gather(); foreach (var p in gather) { if ((!i.EOF || p.First() == Misc.UniversalCharSet) || (i.EOF || p.First().Contains(i.Current))) { predicted = true; tr.Add(p, i, r => { if (!results.Contains(r)) { Misc.Trace("Reduced: {0} *=> {1}", this, r); onResult(r); results.Add(r); } }); } } if (!predicted) { if (i.EOF) { onResult(Result.Failure(i, FailureType.UnexpectedEndOfStream)); } else { onResult(Result.Failure(i, FailureType.UnexpectedChars)); } } }); t.Add(thunk, input, result); } }
public override void Chain(Trampoline t, Input input, OnChainResult result) { ChainDelegate(t, input, result); }