Esempio n. 1
0
 // Splits a string into lines based on
 static private IEnumerable <StringSlice> SplitIntoLines(IBlockParsingRules inRules, StringSlice inString)
 {
     if (inRules.CustomLineSplitter != null)
     {
         return(inString.EnumeratedSplit(inRules.CustomLineSplitter, StringSplitOptions.None));
     }
     return(inString.EnumeratedSplit(inRules.LineDelimiters, StringSplitOptions.None));
 }
        public IEnumerator ParseAsync <TPackage>(IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TPackage : ScriptableDataBlockPackage <TBlock>
        {
            if (m_Parsed)
            {
                return(null);
            }

            TPackage self = (TPackage)this;

            return(BlockParser.ParseAsync(ref self, name, Source(), inRules, inGenerator, inCache));
        }
Esempio n. 3
0
        // Generates a prioritized set of prefixes to analyze, from most specific to least specific
        static private PrefixType[] GeneratePrefixPriority(IBlockParsingRules inRules)
        {
            List <PriorityValue <PrefixType> > buffer = new List <PriorityValue <PrefixType> >(6);

            AddPrefixPriority(buffer, inRules.BlockIdPrefix, PrefixType.BlockId);
            AddPrefixPriority(buffer, inRules.BlockMetaPrefix, PrefixType.BlockMeta);
            AddPrefixPriority(buffer, inRules.BlockHeaderEndPrefix, PrefixType.BlockHeaderEnd);
            AddPrefixPriority(buffer, inRules.BlockContentPrefix, PrefixType.BlockContent);
            AddPrefixPriority(buffer, inRules.BlockEndPrefix, PrefixType.BlockEnd);
            AddPrefixPriority(buffer, inRules.PackageMetaPrefix, PrefixType.PackageMeta);
            buffer.Sort();

            PrefixType[] priority = new PrefixType[buffer.Count];
            for (int i = 0; i < buffer.Count; ++i)
            {
                priority[i] = buffer[i].Value;
            }
            return(priority);
        }
Esempio n. 4
0
            public void Dispose()
            {
                if (Builder != null)
                {
                    BlockParser.ReturnStringBuilder(Builder);
                    Builder = null;
                }

                if (ContentBuilder != null)
                {
                    BlockParser.ReturnStringBuilder(ContentBuilder);
                    ContentBuilder = null;
                }

                PrefixPriorities = null;
                Rules            = null;
                Generator        = null;
                TagDelimiters    = null;
                Cache            = null;

                Package      = null;
                CurrentBlock = null;
            }
Esempio n. 5
0
        static internal IEnumerator ParseFile(string inFileName, StringSlice inFile, TPackage ioPackage, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache)
        {
            var state = new ParseState();

            state.PrefixPriorities = GeneratePrefixPriority(inRules);
            state.Rules            = inRules;
            state.Generator        = inGenerator;
            state.TagDelimiters    = new BlockParser.BlockTagDelimiters(inRules);
            state.Package          = ioPackage;
            state.Builder          = BlockParser.RentStringBuilder();
            state.ContentBuilder   = BlockParser.RentStringBuilder();
            state.Cache            = inCache ?? BlockMetaCache.Default;

            using (var disposeRef = state)
            {
                uint lineNumber = 0;
                state.Position = new BlockFilePosition(inFileName, lineNumber);
                inGenerator.OnStart(state, state.Package);

                foreach (var rawLine in SplitIntoLines(inRules, inFile))
                {
                    lineNumber++;
                    state.Position = new BlockFilePosition(inFileName, lineNumber);

                    LineResult result = ParseLine(ref state, rawLine);
                    if (result == LineResult.Exception)
                    {
                        inGenerator.OnEnd(state, state.Package, true);
                        yield break;
                    }

                    yield return(null);
                }

                if (state.CurrentBlock != null)
                {
                    state.Error |= !TryEndBlock(ref state, StringSlice.Empty);
                }

                inGenerator.OnEnd(state, state.Package, state.Error);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Parses the given file contents into blocks asynchronously.
        /// Each MoveNext() call on the returned IEnumerator will parse one line.
        /// </summary>
        static public TPackage ParseAsync <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache, out IEnumerator outLoader)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            if (inFile.IsEmpty)
            {
                outLoader = null;
                return(null);
            }

            if (inRules == null)
            {
                throw new ArgumentNullException("inRules");
            }
            if (inGenerator == null)
            {
                throw new ArgumentNullException("inGenerator");
            }

            string   fileName = string.IsNullOrEmpty(inFileName) ? NullFilename : inFileName;
            TPackage package  = inGenerator.CreatePackage(fileName);

            outLoader = InternalBlockParser <TBlock, TPackage> .ParseFile(fileName, inFile, package, inRules, inGenerator, inCache);

            return(package);
        }
Esempio n. 7
0
 /// <summary>
 /// Parses the given file contents into blocks asynchronously.
 /// Each MoveNext() call on the returned IEnumerator will parse one line.
 /// </summary>
 static public TPackage ParseAsync <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, out IEnumerator outLoader)
     where TBlock : class, IDataBlock
     where TPackage : class, IDataBlockPackage <TBlock>
 {
     return(ParseAsync <TBlock, TPackage>(inFileName, inFile, inRules, inGenerator, null, out outLoader));
 }
Esempio n. 8
0
        /// <summary>
        /// Parses the given file contents into blocks
        /// and merges into the given package.
        /// </summary>
        static public void Parse <TBlock, TPackage>(ref TPackage ioPackage, string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            IEnumerator parser = ParseAsync(ref ioPackage, inFileName, inFile, inRules, inGenerator, inCache);

            if (parser != null)
            {
                while (parser.MoveNext())
                {
                    ;
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Parses the given file contents into blocks.
        /// </summary>
        static public TPackage Parse <TBlock, TPackage>(string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            IEnumerator parser;
            TPackage    package = ParseAsync(inFileName, inFile, inRules, inGenerator, inCache, out parser);

            if (parser != null)
            {
                while (parser.MoveNext())
                {
                    ;
                }
            }

            return(package);
        }
Esempio n. 10
0
 public BlockTagDelimiters(IBlockParsingRules inBlockParser)
 {
     m_BlockParser = inBlockParser;
 }
Esempio n. 11
0
        /// <summary>
        /// Parses the given file contents into blocks asynchronously
        /// and merges into the given package.
        /// Each MoveNext() call on the returned IEnumerator will parse one line.
        /// </summary>
        static public IEnumerator ParseAsync <TBlock, TPackage>(ref TPackage ioPackage, string inFileName, StringSlice inFile, IBlockParsingRules inRules, IBlockGenerator <TBlock, TPackage> inGenerator, BlockMetaCache inCache = null)
            where TBlock : class, IDataBlock
            where TPackage : class, IDataBlockPackage <TBlock>
        {
            if (inFile.IsEmpty)
            {
                return(null);
            }

            if (inRules == null)
            {
                throw new ArgumentNullException("inRules");
            }
            if (inGenerator == null)
            {
                throw new ArgumentNullException("inGenerator");
            }

            string fileName = string.IsNullOrEmpty(inFileName) ? NullFilename : inFileName;

            if (ioPackage == null)
            {
                ioPackage = inGenerator.CreatePackage(fileName);
            }
            return(InternalBlockParser <TBlock, TPackage> .ParseFile(fileName, inFile, ioPackage, inRules, inGenerator, inCache));
        }