internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { var at = reader.Consumed; var tempReader = reader; data.ClearData(); var inc = Include.TryMatch(data.GetIndexed(0), ref reader, out var match, out status); if (inc) { if (Exclude.TryMatch(data.GetIndexed(1, true, true), ref tempReader, out _, out var tempStatus)) { status = tempStatus; reader = tempReader; return(false); } } else { return(false); } data.AddMatch(0, match); return(true); }
internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { status = OperationStatus.Done; data.MayContinue = false; foreach (var token in Tokens.Skip(data.Index)) { data.ClearData(data.Index); var d = data.GetIndexed(data.Index); if (!token.TryMatch(d, ref reader, out var match, out status)) { data.MayContinue = false; if (data.Index > 0) { data.Index--; } return(false); } data.AddMatch(data.Index, match); // if (status == OperationStatus.NeedMoreData) // return true; data.Index++; } data.MayContinue = false; return(true); }
internal PartialExecutionData GetIndexed(int i, bool silent = false, bool revokeAuthority = false) { Trace.WriteLine($"Retrieving data for {i}"); if (subData.TryGetValue(i, out var val)) { return(val); } var data = subData[i] = new PartialExecutionData(); if (silent || MatchMap == null) { data.MatchMap = null; } if (revokeAuthority) { data.Authoritative = false; } else { data.Authoritative = Authoritative; data.AuthoritativeSource = AuthoritativeSource; } return(data); }
internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { status = OperationStatus.Done; if (data.Index > Max) { data.MayContinue = false; return(false); } if (data.Consumed != 0) { reader.Advance(data.Consumed - reader.Consumed); } for (; data.Index <= Max; data.Index++) { data.MayContinue = false; var at = reader.Consumed; if (!BaseToken.TryMatch(data.GetIndexed(data.Index, revokeAuthority: data.Index >= Min), ref reader, out var match, out status) || reader.Consumed == at) { goto end; } data.Consumed = reader.Consumed; data.ClearData(data.Index); data.AddMatch(data.Index, match); if (data.Index >= Min) { data.MayContinue = true; goto end; } } end: if (data.Index < Min) { status = OperationStatus.NeedMoreData; return(false); } else { if (status != OperationStatus.NeedMoreData) { status = OperationStatus.Done; } else { data.MayContinue = true; } return(true); } }
internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { if (reader.Remaining < Match.Length) { return(DoneWith(status = OperationStatus.NeedMoreData)); } if (!reader.IsNext(Match.Span)) { return(DoneWith(status = OperationStatus.InvalidData)); } data.AddData(this, ref reader, Match.Length); return(DoneWith(status = OperationStatus.Done)); }
internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { status = OperationStatus.InvalidData; var at = reader.Consumed; data.ClearData(); data.MayContinue = true; foreach (var token in Tokens.Skip(data.Index)) { try { var d = data.GetIndexed(data.Index, revokeAuthority: true); // var tempReader = reader; if (token.TryMatch(d, ref reader, out var match, out var tempStatus)) { if (reader.Consumed == at) { continue; } data.AddMatch(data.Index, match); status = tempStatus; if (d.MayContinue) { data.Index--; } return(true); } if (tempStatus == OperationStatus.NeedMoreData) { status = tempStatus; } } finally { data.Index++; } } data.MayContinue = false; return(false); }
internal bool ExecutePartial(ref SequenceReader <byte> reader, [MaybeNullWhen(false)] out TokenMatch match, out OperationStatus status, ref object?partialData) { if (!(partialData is PartialExecutionData ped)) { partialData = ped = new PartialExecutionData(); } var start = reader.Position; var tempReader = reader; if (!TryMatch(ped, ref reader, out match, out status) && status != OperationStatus.NeedMoreData) { var errReader = reader; reader = tempReader; ThrowParsingException(errReader); } return(status == OperationStatus.NeedMoreData); }
internal override bool TryMatchInternal(PartialExecutionData data, ref SequenceReader <byte> reader, out OperationStatus status) { if (reader.Remaining < Low.Length) { return(DoneWith(status = OperationStatus.NeedMoreData)); } var value = data.ReadData(ref reader, Low.Length); var lo = Low.Span.SequenceCompareTo(value); var hi = High.Span.SequenceCompareTo(value); if (lo > 0 || hi < 0) { return(DoneWith(status = OperationStatus.InvalidData)); } data.AddData(this, value); return(DoneWith(status = OperationStatus.Done)); }
internal bool TryMatch(PartialExecutionData data, ref SequenceReader <byte> reader, [MaybeNullWhen(false)] out TokenMatch match, out OperationStatus status) { #if TRACE var seq = reader.Sequence.Slice(reader.Consumed); #endif Trace.WriteLineIf(Debug && data.Index != 0, $"{Trace.IndentLevel} Continuing with {this}, index {data.Index}"); Trace.Indent(); Trace.WriteLineIf(Debug, $"{Trace.IndentLevel} Running {this} on {new TokenMatch(this, seq.ToArray())}"); data.Authoritative |= IsAuthoritative; if (data.AuthoritativeSource == null || IsAuthoritative) { data.AuthoritativeSource = this; } var tempReader = reader; var ok = TryMatchInternal(data, ref tempReader, out status); while (!ok && data.HasContinuations) { Trace.WriteLineIf(Debug, $"{Trace.IndentLevel} Continuing on {this}, index {data.Index}"); tempReader = reader; ok = TryMatchInternal(data, ref tempReader, out status); } if (ok) { reader = tempReader; } else if (data.Authoritative) { ThrowParsingException(tempReader, data.AuthoritativeSource); } if (ok && data.Matches is IEnumerable <TokenMatch> list) { match = new TokenMatch(this, list); if (IsLiteral) { match = new TokenMatch(this, match.Combine()); } else if (Combine) { match = match.Collapse(); } Trace.WriteLineIf(Debug, $"{Trace.IndentLevel} Matched {this}: {match}"); } else { match = null; } Trace.WriteLineIf(Debug, $"{Trace.IndentLevel} Running {this}: {status} (ok: {ok})"); Trace.Unindent(); var hash = data.GetDynamicHashCode(); if (ok && data.LastHash != -1 && data.LastHash == hash) { throw new Exception("Infinite loop detected!"); } else { data.LastHash = hash; } return(ok); }
internal abstract bool TryMatchInternal(PartialExecutionData ped, ref SequenceReader <byte> reader, out OperationStatus status);