示例#1
0
        /// <summary>
        /// reads lines and passes discovered syntax to the elementparser
        /// </summary>
        /// <typeparam name="O">Type of the object</typeparam>
        /// <param name="obj">Instantiated object</param>
        /// <param name="parser">Parser to fill object with data</param>
        /// <returns>Filled object</returns>

        private O parseObject <O>(O obj, IElementParser <O> parser)
        {
            for (lineIndex += 0; lineIndex < lines.Length; lineIndex++)
            {
                //empty line or comment
                string line   = lines[lineIndex];
                Regex  ignore = new Regex(@"^\s*(#|\s*$)");
                if (ignore.Match(line).Success)
                {
                    continue;
                }

                //jaml statement
                Regex regex = new Regex("^(?<layer>\\s*)(?<key>\\S*)\\s*[:-]\\s*(?<value>\".*\"|.*?)\\s*(?:#|$)");
                Match match = regex.Match(line);
                if (!match.Success)
                {
                    throw new JamlException("Could not parse \"" + line + "\"" + getLocationInfo() + ". Unexpected format");
                }
                int layer = match.Groups["layer"].Value.Length;

                //finding out if last defined element has fields to be filled (LAYER_UP) or if this object is done (LAYER_DOWN)
                int layerDiff = checkLayer(layer);
                if (layerDiff == LAYER_DOWN)
                {
                    layers.Pop();
                    return(obj);
                }
                if (layerDiff == LAYER_UP)
                {
                    dynamic nextObject = layers.Peek().LastElement;
                    layers.Push(new Layer(layer, null));
                    parseElement(nextObject);
                    // it might be that with the end of the parsed child object, the parent ends as well, decrementing index = checking same line against parent
                    lineIndex--;
                    continue;
                }

                // if jaml statement found in same layer, it's a field for the current object (layerObject of most recent layer), so pass it to the parser
                layers.Peek().LastElement = parser.acceptMatch(this, obj, match);
            }

            //eof -> return object
            return(obj);
        }