Example #1
0
        public static void Pack(BinaryReader input, BinaryWriter output)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            CgtReader reader      = new CgtReader(input);
            CgtWriter writer      = new CgtWriter(output);
            string    header      = reader.ReadHeaderString();
            Match     headerMatch = FileHeader.Match(header);

            if (headerMatch.Groups["version"].Value != "1.0")
            {
                throw new FileLoadException("The File Header is invalid or of an unsupported version for packing, only uncompressed V1.0 Compiled Grammar Files are supported");
            }
            writer.WriteHeaderString(header);
            while (reader.HasMoreData())
            {
                CgtRecordType recordType = reader.ReadNextRecord();
                switch (recordType)
                {
                case CgtRecordType.Charsets:
                    int        index   = reader.ReadIntegerEntry();
                    string     chars   = reader.ReadStringEntry();
                    DfaCharset charset = new DfaCharset(null, index, chars);
                    if (charset.SequenceLength >= 4)
                    {
                        writer.WriteNextRecord(CgtRecordType.PackedCharsets, 4);
                        writer.WriteIntegerEntry(index);
                        writer.WriteIntegerEntry(charset.SequenceStart);
                        writer.WriteIntegerEntry(charset.SequenceEnd);
                        writer.WriteStringEntry(new string(charset.CharactersExcludingSequence));
                    }
                    else
                    {
                        writer.WriteNextRecord(CgtRecordType.Charsets, 2);
                        writer.WriteIntegerEntry(index);
                        writer.WriteStringEntry(chars);
                    }
                    break;

                default:
                    writer.WriteNextRecord(recordType, reader.EntryCount);
                    while (reader.EntryCount > 0)
                    {
                        reader.CopyEntry(writer);
                    }
                    break;
                }
            }
            output.Flush();
        }
Example #2
0
 internal void Initialize(Symbol acceptSymbol, ICollection <CompiledGrammar.DfaEdge> edges)
 {
     if (edges == null)
     {
         throw new ArgumentNullException("edges");
     }
     if ((this.acceptSymbol != null) || (sequenceState != null) || (transitionVector.Count > 0))
     {
         throw new InvalidOperationException("The DfaState has already been initialized!");
     }
     this.acceptSymbol = acceptSymbol;
     if (edges.Count == 0)
     {
         Debug.Assert(acceptSymbol != null);
         sequenceStart = char.MinValue;
         sequenceEnd   = char.MaxValue;
         sequenceState = null;
     }
     else
     {
         DfaCharset sequence       = null;
         int        sequenceLength = 0;
         foreach (CompiledGrammar.DfaEdge edge in edges)
         {
             DfaCharset charset = Owner.GetDfaCharset(edge.CharSetIndex);
             if (charset.SequenceLength > sequenceLength)
             {
                 sequence       = charset;
                 sequenceLength = charset.SequenceLength;
             }
         }
         Debug.Assert(sequence != null);
         foreach (CompiledGrammar.DfaEdge edge in edges)
         {
             DfaState           targetDfaState = Owner.GetDfaState(edge.TargetIndex);
             DfaCharset         charset        = Owner.GetDfaCharset(edge.CharSetIndex);
             ICollection <char> characters;
             if (ReferenceEquals(charset, sequence))
             {
                 Debug.Assert(sequenceState == null);
                 characters    = charset.CharactersExcludingSequence;
                 sequenceStart = charset.SequenceStart;
                 sequenceEnd   = charset.SequenceEnd;
                 sequenceState = targetDfaState;
             }
             else
             {
                 characters = charset.CharactersIncludingSequence;
             }
             foreach (char ch in characters)
             {
                 transitionVector.Add(ch, targetDfaState);
             }
         }
     }
 }
Example #3
0
        /// <summary>
        /// Read char set information.
        /// </summary>
        /// <param name="context"></param>
        private void ReadPackedCharset(LoadContext context)
        {
            int           index         = context.ReadIntegerEntry();
            char          sequenceStart = (char)context.ReadIntegerEntry();
            char          sequenceEnd   = (char)context.ReadIntegerEntry();
            StringBuilder result        = new StringBuilder(context.ReadStringEntry());

            result.Capacity = result.Length + (sequenceEnd - sequenceStart) + 1;
            for (char c = sequenceStart; c <= sequenceEnd; c++)
            {
                result.Append(c);
            }
            charSetTable[index] = new DfaCharset(this, index, result.ToString());
        }
Example #4
0
        /// <summary>
        /// Read char set information.
        /// </summary>
        /// <param name="context"></param>
        private void ReadRangeCharset(LoadContext context)
        {
            int index = context.ReadIntegerEntry();

            context.ReadIntegerEntry();             // unicode plane
            int rangeCount = context.ReadIntegerEntry();

            context.ReadEmptyEntry();             // reserved
            Debug.Assert(rangeCount == context.EntryCount / 2);
            StringBuilder result = new StringBuilder();

            while (context.EntryCount > 0)
            {
                char ch  = (char)context.ReadIntegerEntry();
                char end = (char)context.ReadIntegerEntry();
                while (ch <= end)
                {
                    result.Append(ch++);
                }
            }
            charSetTable[index] = new DfaCharset(this, index, result.ToString());
        }
Example #5
0
        /// <summary>
        /// Read char set information.
        /// </summary>
        /// <param name="context"></param>
        private void ReadCharset(LoadContext context)
        {
            int index = context.ReadIntegerEntry();

            charSetTable[index] = new DfaCharset(this, index, context.ReadStringEntry());
        }