private void CollectError(RecoveryParser rp, FlattenSequences results) { //var text = rp.ParseResult.Text; var expected = new Dictionary <NSpan, HashSet <ParsedSequenceAndSubrule> >(); var failSeq = default(ParsedSequence); var failSubrule = default(ParsedSubrule); var skipRecovery = false; foreach (var result in results) { var reverse = result.ToArray().Reverse(); foreach (var x in reverse) { var ins = x.TokenChanges; var seq = x.Seq; var subrule = x.Subrule; if (skipRecovery) { if (!ins.HasChanges && seq.ParsingSequence.RuleName != "s") { skipRecovery = false; //Debug.WriteLine(x); HashSet <ParsedSequenceAndSubrule> parsedNodes; var span = new NSpan(failSubrule.Begin, subrule.Begin); if (!expected.TryGetValue(span, out parsedNodes)) { parsedNodes = new HashSet <ParsedSequenceAndSubrule>(); expected[span] = parsedNodes; } if (failSubrule.IsEmpty) { parsedNodes.Add(new ParsedSequenceAndSubrule(failSeq, failSubrule)); } else { parsedNodes.Add(new ParsedSequenceAndSubrule(seq, subrule)); } } } else { if (ins.HasChanges) { failSeq = seq; failSubrule = subrule; skipRecovery = true; } } } } var parseResult = rp.ParseResult; foreach (var e in expected) { parseResult.ReportError(new ExpectedSubrulesError(new Location(parseResult.OriginalSource, e.Key.StartPos, e.Key.EndPos), e.Value)); } }
private FlattenSequences FlattenSequence( FlattenSequences prevs, ParseResult parseResult, ParsedSequence seq, int end, TokenChanges sequenceInsertedTokens, Dictionary <ParsedSequenceKey, SequenceTokenChanges> memiozation) { //var seqTxt = parseResult.Text.Substring(seq.StartPos, end - seq.StartPos); if (seq.StartPos == 8 && end == 15) { Debug.Assert(true); } SequenceTokenChanges first; var key = new ParsedSequenceKey(seq, end); if (!memiozation.TryGetValue(key, out first)) { Debug.Assert(false); } var parses = first.SubrulesTokenChanges; if (first.TotalTokenChanges.IsFail) { return(new FlattenSequences()); } if (sequenceInsertedTokens != first.TotalTokenChanges) { //Debug.Assert(false); return(new FlattenSequences()); } var firstSubrules = seq.GetFirstSubrules(parses.Keys).ToArray(); var total = new FlattenSequences(); foreach (var firstSubrule in firstSubrules) { //var txt = parseResult.Text.Substring(firstSubrule.Begin, firstSubrule.End - firstSubrule.Begin); //var stateIndex = firstSubrule.State; //var state = stateIndex < 0 ? null : seq.ParsingSequence.States[stateIndex]; var insertedTokens = parses[firstSubrule]; if (insertedTokens.IsFail) { continue; } var result = FlattenSubrule(prevs, parseResult, seq, parses, firstSubrule, insertedTokens, memiozation); total.AddRange(result); } return(total); }
public static void PrintPaths(ParseResult parseResult, Dictionary<ParsedSequenceAndSubrule, bool> deletedToken, FlattenSequences paths) { var results = new List<XNode> { new XText(RecoveryDebug.CurrentTestName + "\r\n" + parseResult.DebugText + "\r\n\r\n") }; foreach (var path in paths) PrintPath(results, parseResult.Text, deletedToken, path); var template = XElement.Parse(HtmlTemplate); var content = template.Descendants("content").First(); Debug.Assert(content.Parent != null); content.Parent.ReplaceAll(results); var filePath = Path.ChangeExtension(Path.GetTempFileName(), ".html"); template.Save(filePath); Process.Start(filePath); }
private FlattenSequences FlattenSequence( FlattenSequences prevs, ParseResult parseResult, ParsedSequence seq, int end, TokenChanges sequenceInsertedTokens, Dictionary<ParsedSequenceKey, SequenceTokenChanges> memiozation) { //var seqTxt = parseResult.Text.Substring(seq.StartPos, end - seq.StartPos); if (seq.StartPos == 8 && end == 15) Debug.Assert(true); SequenceTokenChanges first; var key = new ParsedSequenceKey(seq, end); if (!memiozation.TryGetValue(key, out first)) Debug.Assert(false); var parses = first.SubrulesTokenChanges; if (first.TotalTokenChanges.IsFail) return new FlattenSequences(); if (sequenceInsertedTokens != first.TotalTokenChanges) { //Debug.Assert(false); return new FlattenSequences(); } var firstSubrules = seq.GetFirstSubrules(parses.Keys).ToArray(); var total = new FlattenSequences(); foreach (var firstSubrule in firstSubrules) { //var txt = parseResult.Text.Substring(firstSubrule.Begin, firstSubrule.End - firstSubrule.Begin); //var stateIndex = firstSubrule.State; //var state = stateIndex < 0 ? null : seq.ParsingSequence.States[stateIndex]; var insertedTokens = parses[firstSubrule]; if (insertedTokens.IsFail) continue; var result = FlattenSubrule(prevs, parseResult, seq, parses, firstSubrule, insertedTokens, memiozation); total.AddRange(result); } return total; }
private FlattenSequences FlattenSubrule(FlattenSequences prevs, ParseResult parseResult, ParsedSequence seq, SubrulesTokenChanges parses, ParsedSubrule subrule, TokenChanges tokenChanges, Dictionary<ParsedSequenceKey, SequenceTokenChanges> memiozation) { Begin: //var txt = parseResult.Text.Substring(subrule.Begin, subrule.End - subrule.Begin); //var stateIndex = subrule.State; //var state = stateIndex < 0 ? null : seq.ParsingSequence.States[stateIndex]; if (subrule.End == 11) { } var currentNodes = new FlattenSequences(); //var subruledDesc = seq.GetSubruleDescription(subrule.State); if (subrule.IsEmpty) { //if (subruleInsertedTokens > 0) // Debug.WriteLine("Inserted = " + subruleInsertedTokens + " - " + subruledDesc + " Seq: " + seq); } else { var sequences = seq.GetSequencesForSubrule(subrule).ToArray(); if (sequences.Length > 1) { } foreach (var subSequences in sequences) { //Debug.WriteLine(subruledDesc); var result = FlattenSequence(prevs, parseResult, subSequences, subrule.End, tokenChanges, memiozation); currentNodes.AddRange(result); } } if (currentNodes.Count == 0) // если не было сабсиквенсов, надо создать продолжения из текущего сабруля { foreach (var prev in prevs) currentNodes.Add(new ParsedSequenceAndSubrules.Cons(new SubruleTokenChanges(seq, subrule, tokenChanges), prev)); } var nextSubrules = seq.GetNextSubrules(subrule, parses.Keys).ToArray(); switch (nextSubrules.Length) { case 0: return currentNodes; case 1: { var nextSubrule = nextSubrules[0]; if (nextSubrule.State == 9 && nextSubrule.Begin == 8 && nextSubrule.End == 15) { } tokenChanges = parses[nextSubrule]; if (tokenChanges.IsFail) return currentNodes; // recursive self call... prevs = currentNodes; subrule = nextSubrule; goto Begin; return null; } default: { var resultNodes = new FlattenSequences(); foreach (var nextSubrule in nextSubrules) { var newSubruleInsertedTokens = parses[nextSubrule]; if (newSubruleInsertedTokens.IsFail) continue; var result = FlattenSubrule(currentNodes, parseResult, seq, parses, nextSubrule, newSubruleInsertedTokens, memiozation); resultNodes.AddRange(result); } return resultNodes; } } }
private void CollectError(RecoveryParser rp, FlattenSequences results) { //var text = rp.ParseResult.Text; var expected = new Dictionary<NSpan, HashSet<ParsedSequenceAndSubrule>>(); var failSeq = default(ParsedSequence); var failSubrule = default(ParsedSubrule); var skipRecovery = false; foreach (var result in results) { var reverse = result.ToArray().Reverse(); foreach (var x in reverse) { var ins = x.TokenChanges; var seq = x.Seq; var subrule = x.Subrule; if (skipRecovery) { if (!ins.HasChanges && seq.ParsingSequence.RuleName != "s") { skipRecovery = false; //Debug.WriteLine(x); HashSet<ParsedSequenceAndSubrule> parsedNodes; var span = new NSpan(failSubrule.Begin, subrule.Begin); if (!expected.TryGetValue(span, out parsedNodes)) { parsedNodes = new HashSet<ParsedSequenceAndSubrule>(); expected[span] = parsedNodes; } if (failSubrule.IsEmpty) parsedNodes.Add(new ParsedSequenceAndSubrule(failSeq, failSubrule)); else parsedNodes.Add(new ParsedSequenceAndSubrule(seq, subrule)); } } else { if (ins.HasChanges) { failSeq = seq; failSubrule = subrule; skipRecovery = true; } } } } var parseResult = rp.ParseResult; foreach (var e in expected) parseResult.ReportError(new ExpectedSubrulesError(new Location(parseResult.OriginalSource, e.Key.StartPos, e.Key.EndPos), e.Value)); }
public static void PrintPaths(ParseResult parseResult, Dictionary <ParsedSequenceAndSubrule, bool> deletedToken, FlattenSequences paths) { var results = new List <XNode> { new XText(RecoveryDebug.CurrentTestName + "\r\n" + parseResult.DebugText + "\r\n\r\n") }; foreach (var path in paths) { PrintPath(results, parseResult.Text, deletedToken, path); } var template = XElement.Parse(HtmlTemplate); var content = template.Descendants("content").First(); Debug.Assert(content.Parent != null); content.Parent.ReplaceAll(results); var filePath = Path.ChangeExtension(Path.GetTempFileName(), ".html"); template.Save(filePath); Process.Start(filePath); }
private FlattenSequences FlattenSubrule(FlattenSequences prevs, ParseResult parseResult, ParsedSequence seq, SubrulesTokenChanges parses, ParsedSubrule subrule, TokenChanges tokenChanges, Dictionary <ParsedSequenceKey, SequenceTokenChanges> memiozation) { Begin: //var txt = parseResult.Text.Substring(subrule.Begin, subrule.End - subrule.Begin); //var stateIndex = subrule.State; //var state = stateIndex < 0 ? null : seq.ParsingSequence.States[stateIndex]; if (subrule.End == 11) { } var currentNodes = new FlattenSequences(); //var subruledDesc = seq.GetSubruleDescription(subrule.State); if (subrule.IsEmpty) { //if (subruleInsertedTokens > 0) // Debug.WriteLine("Inserted = " + subruleInsertedTokens + " - " + subruledDesc + " Seq: " + seq); } else { var sequences = seq.GetSequencesForSubrule(subrule).ToArray(); if (sequences.Length > 1) { } foreach (var subSequences in sequences) { //Debug.WriteLine(subruledDesc); var result = FlattenSequence(prevs, parseResult, subSequences, subrule.End, tokenChanges, memiozation); currentNodes.AddRange(result); } } if (currentNodes.Count == 0) // если не было сабсиквенсов, надо создать продолжения из текущего сабруля { foreach (var prev in prevs) { currentNodes.Add(new ParsedSequenceAndSubrules.Cons(new SubruleTokenChanges(seq, subrule, tokenChanges), prev)); } } var nextSubrules = seq.GetNextSubrules(subrule, parses.Keys).ToArray(); switch (nextSubrules.Length) { case 0: return(currentNodes); case 1: { var nextSubrule = nextSubrules[0]; if (nextSubrule.State == 9 && nextSubrule.Begin == 8 && nextSubrule.End == 15) { } tokenChanges = parses[nextSubrule]; if (tokenChanges.IsFail) { return(currentNodes); } // recursive self call... prevs = currentNodes; subrule = nextSubrule; goto Begin; return(null); } default: { var resultNodes = new FlattenSequences(); foreach (var nextSubrule in nextSubrules) { var newSubruleInsertedTokens = parses[nextSubrule]; if (newSubruleInsertedTokens.IsFail) { continue; } var result = FlattenSubrule(currentNodes, parseResult, seq, parses, nextSubrule, newSubruleInsertedTokens, memiozation); resultNodes.AddRange(result); } return(resultNodes); } } }