Beispiel #1
0
    private static bool IsInsideToken(SCG.Dictionary<ParseRecord, bool> memoization, CompositeGrammar compositeGrammar, ParseRecord record)
    {
      bool res;
      if (memoization.TryGetValue(record, out res))
        return res;

      if (record.Sequence.ParsingSequence.SequenceInfo is SequenceInfo.Ast)
      {
        var parser = record.Sequence.ParsingSequence.SequenceInfo.Parser;
        res = compositeGrammar.Tokens.ContainsKey(parser) || compositeGrammar.VoidTokens.ContainsKey(parser);
        memoization[record] = res;
        if(res)
          return res;
      }

      foreach (var caller in record.Sequence.Callers)
      {
        res = IsInsideToken(memoization, compositeGrammar, caller);
        if (res)
        {
          memoization[record] = true;
          return true;
        }
      }

      memoization[record] = false;
      return false;
    }
Beispiel #2
0
        private static bool IsInsideToken(SCG.Dictionary <ParseRecord, bool> memoization, CompositeGrammar compositeGrammar, ParseRecord record)
        {
            bool res;

            if (memoization.TryGetValue(record, out res))
            {
                return(res);
            }

            if (record.Sequence.ParsingSequence.SequenceInfo is SequenceInfo.Ast)
            {
                var parser = record.Sequence.ParsingSequence.SequenceInfo.Parser;
                res = compositeGrammar.Tokens.ContainsKey(parser) || compositeGrammar.VoidTokens.ContainsKey(parser);
                memoization[record] = res;
                if (res)
                {
                    return(res);
                }
            }

            foreach (var caller in record.Sequence.Callers)
            {
                res = IsInsideToken(memoization, compositeGrammar, caller);
                if (res)
                {
                    memoization[record] = true;
                    return(true);
                }
            }

            memoization[record] = false;
            return(false);
        }
Beispiel #3
0
    private void RecoverAllWays(RecoveryParser rp)
    {
      // ReSharper disable once RedundantAssignment
      int maxPos = rp.MaxPos;
      var failPositions = new HashSet<int>();
      var deleted = new List<Tuple<int, ParsedSequence>>();


      do
      {

        var tmpDeleted = FindMaxFailPos(rp);
        if (rp.MaxPos != rp.ParseResult.Text.Length)
          UpdateParseErrorCount();

        if (!CheckUnclosedToken(rp))
          deleted.AddRange(tmpDeleted);
        else
        { }


        maxPos = rp.MaxPos;
        failPositions.Add(maxPos);

        var records = new SCG.Queue<ParseRecord>(rp.Records[maxPos]);
        var prevRecords = new SCG.HashSet<ParseRecord>(rp.Records[maxPos]);

        do
        {
          if (_parseResult.TerminateParsing)
            throw new OperationCanceledException();

          while (records.Count > 0)
          {
            var record = records.Dequeue();

            if (record.IsComplete)
            {
              rp.StartParseSubrule(maxPos, record);
              continue;
            }
            if (record.Sequence.IsToken)
              continue;

            foreach (var state in record.ParsingState.Next)
            {
              var newRecord = new ParseRecord(record.Sequence, state, maxPos);
              if (!rp.Records[maxPos].Contains(newRecord))
              {
                records.Enqueue(newRecord);
                prevRecords.Add(newRecord);
              }
            }

            rp.SubruleParsed(maxPos, maxPos, record);
            rp.PredictionOrScanning(maxPos, record, false);
          }

          rp.Parse();

          foreach (var record in rp.Records[maxPos])
            if (!prevRecords.Contains(record))
              records.Enqueue(record);
          prevRecords.UnionWith(rp.Records[maxPos]);
        }
        while (records.Count > 0);
      }
      while (rp.MaxPos > maxPos);

      foreach (var del in deleted)
        DeleteTokens(rp, del.Item1, del.Item2, NumberOfTokensForSpeculativeDeleting);
      rp.Parse();
    }
Beispiel #4
0
        private void RecoverAllWays(RecoveryParser rp)
        {
            // ReSharper disable once RedundantAssignment
            int maxPos        = rp.MaxPos;
            var failPositions = new HashSet <int>();
            var deleted       = new List <Tuple <int, ParsedSequence> >();


            do
            {
                var tmpDeleted = FindMaxFailPos(rp);
                if (rp.MaxPos != rp.ParseResult.Text.Length)
                {
                    UpdateParseErrorCount();
                }

                if (!CheckUnclosedToken(rp))
                {
                    deleted.AddRange(tmpDeleted);
                }
                else
                {
                }


                maxPos = rp.MaxPos;
                failPositions.Add(maxPos);

                var records     = new SCG.Queue <ParseRecord>(rp.Records[maxPos]);
                var prevRecords = new SCG.HashSet <ParseRecord>(rp.Records[maxPos]);

                do
                {
                    if (_parseResult.TerminateParsing)
                    {
                        throw new OperationCanceledException();
                    }

                    while (records.Count > 0)
                    {
                        var record = records.Dequeue();

                        if (record.IsComplete)
                        {
                            rp.StartParseSubrule(maxPos, record);
                            continue;
                        }
                        if (record.Sequence.IsToken)
                        {
                            continue;
                        }

                        foreach (var state in record.ParsingState.Next)
                        {
                            var newRecord = new ParseRecord(record.Sequence, state, maxPos);
                            if (!rp.Records[maxPos].Contains(newRecord))
                            {
                                records.Enqueue(newRecord);
                                prevRecords.Add(newRecord);
                            }
                        }

                        rp.SubruleParsed(maxPos, maxPos, record);
                        rp.PredictionOrScanning(maxPos, record, false);
                    }

                    rp.Parse();

                    foreach (var record in rp.Records[maxPos])
                    {
                        if (!prevRecords.Contains(record))
                        {
                            records.Enqueue(record);
                        }
                    }
                    prevRecords.UnionWith(rp.Records[maxPos]);
                }while (records.Count > 0);
            }while (rp.MaxPos > maxPos);

            foreach (var del in deleted)
            {
                DeleteTokens(rp, del.Item1, del.Item2, NumberOfTokensForSpeculativeDeleting);
            }
            rp.Parse();
        }