private void AddIncrement(StringMatchData matchData) { _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.PHR, sourcePosition: matchData.StartIndex)); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.VR, new object[] { 1 }, sourcePosition: matchData.StartIndex)); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.RPlus, sourcePosition: matchData.StartIndex)); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.RR, sourcePosition: matchData.StartIndex)); }
private StringMatchData PreMatchPattern(ref State state, PatternMatcher pattern) { int startOffset = state.CurrentIndex; StringMatchData stringMatchData = null; (bool success, int length) = pattern.IsMatch(state.Code, state.CurrentIndex); if (success) { stringMatchData = new StringMatchData { Name = pattern.Name, Text = state.Code.Substring(startOffset, length), StartIndex = startOffset, Length = length, Id = pattern.Id, IsNoise = pattern.IsNoise, Mergable = pattern.Mergable }; if (!stringMatchData.IsNoise) { state.DistinctStringMatches.Add(stringMatchData); state.DistinctIndex++; state.MaxDistinctIndex++; } state.CurrentIndex += stringMatchData.Length; } return(stringMatchData); }
protected override (bool success, StringMatchData matchData) MatchPattern(ref State state, PatternMatcher pattern, bool required, bool readOnly = false) { if (pattern != null) { bool success = false; int distinctIndex = state.DistinctIndex; if (distinctIndex < state.MaxDistinctIndex) { StringMatchData stringMatchData = state.DistinctStringMatches[distinctIndex]; if (stringMatchData != null) { success = stringMatchData.Id == pattern.Id; } if (success && !readOnly) { state.DistinctIndex++; state.CurrentIndex += stringMatchData.Length; } else if (!required) { success = true; } return(success, stringMatchData); } else { return(false, default);
private bool MatchFragmentPartsOrderedModeCharacterRange(ref State state, FragmentMatchData matchData) { bool success = true; bool partSuccess; int matchCount = 0; StringMatchData stringMatchData = null; int distinctIndex = state.DistinctIndex; ; success = MatchPartByTextMatcherAnyChar(ref state, matchData); if (!success) { if (stringMatchData != null) { state.CurrentIndex = stringMatchData.StartIndex; state.DistinctIndex = distinctIndex; } goto Break; } else { matchCount++; } distinctIndex = state.DistinctIndex; (partSuccess, stringMatchData) = MatchPatternDash(ref state, true, false); success = partSuccess; if (!success) { goto Break; } success = MatchPartByTextMatcherAnyChar(ref state, matchData); if (!success) { if (stringMatchData != null) { state.CurrentIndex = stringMatchData.StartIndex; state.DistinctIndex = distinctIndex; } goto Break; } else { matchCount++; } Break: success = success || 2 <= matchCount; if (!success) { state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex); } return(success); }
private bool PreMatchPatterns(ref State state) { int codeLength = state.Code.Length; List <PatternMatcher> patterns = LanguageMatcher.Patterns; int patternsCount = LanguageMatcher.Patterns.Count; bool success = true; bool previousNoise = false; int currentIndex; while ((currentIndex = state.CurrentIndex) < codeLength) { success = false; for (int patternIndex = 0; patternIndex < patternsCount; patternIndex++) { PatternMatcher pattern = patterns[patternIndex]; StringMatchData matchData = PreMatchPattern(ref state, pattern); success = matchData != null; if (success) { if (_logMatches) { state.MatchLogBuilder.AppendLine($"{currentIndex}. Prematched {matchData.Name}: {matchData.Text}"); } if (matchData.IsNoise) { previousNoise = true; } else if (previousNoise) { if (state.DistinctIndex > 1) { StringMatchData previousMatchData = state.DistinctStringMatches[state.DistinctIndex - 2]; if (previousMatchData.Name == matchData.Name && previousMatchData.Mergable) { previousMatchData.Text += matchData.Text; previousMatchData.Length = state.CurrentIndex - previousMatchData.StartIndex; state.DistinctIndex--; state.MaxDistinctIndex--; state.DistinctStringMatches.RemoveAt(state.DistinctIndex); } } previousNoise = false; } break; } } if (!success) { break; } } state.CurrentIndex = 0; state.DistinctIndex = 0; return(success); }
private bool MatchFragmentBoundsCaret(ref State state, bool readOnly, out StringMatchData matchData) { bool success; (success, matchData) = MatchPatternCaret(ref state, true, readOnly); if (!success) { state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex); } return(success); }
private void AddAssignment(FragmentMatchData matchData) { _isAssignmentTarget = true; FragmentMatchData target = (FragmentMatchData)matchData.Parts[0]; StringMatchData assignmentEqual = (StringMatchData)matchData.Parts[1]; FragmentMatchData evaluable = (FragmentMatchData)matchData.Parts[2]; AddItem(target, true); _isAssignmentTarget = false; if (_instructions.Last().Code == InstructionCode.LRR) { throw new LanguageConstraintException($"Cannot assign to the return of a function.", target.StartIndex + target.Length); } if (assignmentEqual.Name != "Equal") { _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.CPHR, sourcePosition: target.StartIndex)); } _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.PHR, sourcePosition: target.StartIndex)); AddItem(evaluable); switch (assignmentEqual.Name) { case "MinusEqual": _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.RSubtract, sourcePosition: target.StartIndex)); break; case "PlusEqual": _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.RPlus, sourcePosition: target.StartIndex)); break; } _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.RR, sourcePosition: target.StartIndex)); }
private (bool success, StringMatchData matchData) MatchPatternCaret(ref State state, bool required, bool readOnly = false) { Rerun: bool success = false; int distinctIndex = state.DistinctIndex; if (distinctIndex >= state.MaxDistinctIndex) { int length; int startOffset = state.CurrentIndex; (success, length) = MatchLiteral0(state.Code, state.CurrentIndex); StringMatchData stringMatchData = default; if (success) { stringMatchData = new StringMatchData { Name = "Caret", Text = state.Code.Substring(startOffset, length), StartIndex = startOffset, Length = length, IsNoise = false, Id = 3 }; state.DistinctStringMatches.Add(stringMatchData); success = stringMatchData != null; state.CheckFlags[3] = distinctIndex + 1; state.MaxDistinctIndex++; if (!readOnly) { state.DistinctIndex++; state.CurrentIndex += stringMatchData.Length; } } else if (!required) { success = true; } return(success, stringMatchData); } else { StringMatchData stringMatchData = state.DistinctStringMatches[distinctIndex]; if (stringMatchData != null) { success = stringMatchData.Id != 3; if (stringMatchData.IsNoise) { state.DistinctIndex++; state.CurrentIndex += stringMatchData.Length; goto Rerun; } success = stringMatchData.Id == 3; } else { int length; int startOffset = state.CurrentIndex; (success, length) = MatchLiteral0(state.Code, state.CurrentIndex); if (success) { stringMatchData = new StringMatchData { Name = "Caret", Text = state.Code.Substring(startOffset, length), StartIndex = startOffset, Length = length, IsNoise = false, Id = 3 }; state.DistinctStringMatches[distinctIndex] = stringMatchData; } } if (success && !readOnly) { state.DistinctIndex++; state.CurrentIndex += stringMatchData.Length; } if (!required) { success = true; } return(success, stringMatchData); } }
private bool MatchFragmentPartsMultipleModeCharacterClassBody(ref State state, FragmentMatchData matchData) { bool overallSuccess = false; bool subSuccess = false; bool delimiterSuccess = false; StringMatchData range = default; int matchCount = 0; int distinctIndex = state.DistinctIndex; ; do { subSuccess = false; bool individualSuccess; individualSuccess = MatchFragmentHexRange(ref state, matchData); subSuccess |= individualSuccess; if (individualSuccess) { matchCount++; distinctIndex = state.DistinctIndex; (delimiterSuccess, range) = (true, null); goto Break; } individualSuccess = MatchPartByTextMatcherHexChar(ref state, matchData); subSuccess |= individualSuccess; if (individualSuccess) { matchCount++; distinctIndex = state.DistinctIndex; (delimiterSuccess, range) = (true, null); goto Break; } individualSuccess = MatchFragmentCharacterRange(ref state, matchData); subSuccess |= individualSuccess; if (individualSuccess) { matchCount++; distinctIndex = state.DistinctIndex; (delimiterSuccess, range) = (true, null); goto Break; } individualSuccess = MatchPartByTextMatcherAnyChar(ref state, matchData); subSuccess |= individualSuccess; if (individualSuccess) { matchCount++; distinctIndex = state.DistinctIndex; (delimiterSuccess, range) = (true, null); goto Break; } Break: overallSuccess |= subSuccess; }while (subSuccess && delimiterSuccess); if (delimiterSuccess && range != null) { state.CurrentIndex = range.StartIndex; state.DistinctIndex = distinctIndex; } bool thresholdSuccess = 1 <= matchCount; bool success = overallSuccess || thresholdSuccess; if (!success) { state.FailureIndex = Math.Max(state.FailureIndex ?? 0, state.CurrentIndex); } return(success); }
private static string GetStringMatchDataText(StringMatchData matchData) { return(matchData.ToString()); }
public JsonValue(StringMatchData matchData, JsonDataType dataType) : base(dataType) { _matchData = matchData; }
private void AddBoolean(StringMatchData matchData) { bool boolValue = bool.Parse(matchData.ToString()); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.VR, new object[] { boolValue }, sourcePosition: matchData.StartIndex)); }
private void AddBreak(StringMatchData matchData) { _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.US, new object[] { "break" }, sourcePosition: matchData.StartIndex, interruptable: true)); AddReprocessInstruction(InstructionCode.J, _instructions.Count); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.J, new object[] { "break" }, sourcePosition: matchData.StartIndex)); }
private void AddStringLiteral(StringMatchData matchData) { string literal = matchData.ToString(); _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.VR, new object[] { Regex.Unescape(literal.Substring(1, literal.Length - 2)) }, sourcePosition: matchData.StartIndex)); }
private void AddNull(StringMatchData matchData) { _instructions.Add(InstructionProvider <GroupState> .GetInstruction(InstructionCode.VR, new object[] { null }, sourcePosition: matchData.StartIndex)); }