Пример #1
0
 private string StringifyConstant(SchemaConstant constant)
 {
     return($"{constant.Value}");
 }
Пример #2
0
        /// <summary>
        /// Parse constant from <paramref name="startElementPosition"/> position in <paramref name="text"/>
        /// string and return element's constant.
        /// </summary>
        /// <param name="text">String that will be parsed</param>
        /// <param name="startElementPosition">Start position of element that has constant value</param>
        /// <param name="elementName">Constant parent's name</param>
        /// <param name="constant">Builded constant or null if constant doesn't exist</param>
        /// <returns>Position after element close tag</returns>
        /// <exception cref="ParseException">Throw if document has invalid structure</exception>
        private int ParseConstant(string text, int startElementPosition, string elementName, out SchemaConstant constant)
        {
            constant = null;

            var constantMatch = _constantRx.Match(text, startElementPosition);

            if (!constantMatch.Success) // constant doesn't exist
            {
                return(startElementPosition);
            }

            var constantTextGroup = constantMatch.Groups[2];

            // check open tag
            var beforeOpenTagGroup    = constantMatch.Groups[1];
            var beforeOpenTagPosition = PositionAfterElement(beforeOpenTagGroup);

            if (beforeOpenTagPosition != startElementPosition)
            {
                return(startElementPosition); // means that found another element's comment
            }
            var openTagNameMatch = _elementNameRx.Match(text, beforeOpenTagPosition);

            if (!openTagNameMatch.Success)
            {
                throw new ParseException("Invalid or missing open tag", beforeOpenTagPosition);
            }

            var openTagName = openTagNameMatch.Groups[1].Value;

            if (openTagName != elementName)
            {
                throw new ParseException("Name of open tag doesn't match with name of parent element", beforeOpenTagPosition);
            }

            // check close tag
            var beforeCloseTagGroup    = constantMatch.Groups[3];
            var beforeCloseTagPosition = PositionAfterElement(beforeCloseTagGroup);

            var closeTagMatch = _elementCloseTagRx.Match(text, beforeCloseTagPosition);

            if (!closeTagMatch.Success)
            {
                throw new ParseException("Opened element should be closed", beforeCloseTagPosition);
            }

            var closeTagNameMatch = _elementNameRx.Match(text, beforeCloseTagPosition);

            if (!closeTagNameMatch.Success)
            {
                throw new ParseException("Invalid or missing close tag", beforeCloseTagPosition);
            }

            var closeTagName = closeTagNameMatch.Groups[1].Value;

            if (closeTagName != elementName)
            {
                throw new ParseException("Name of close tag doesn't match with name of parent element", beforeCloseTagPosition);
            }

            if (closeTagMatch.Index != beforeCloseTagPosition)
            {
                throw new ParseException("Element should be closed after constant", beforeCloseTagPosition);
            }

            var constantBuilder = BuilderFactory.Create();

            constantBuilder.SetValue(constantTextGroup.Value);
            constant = constantBuilder.BuildConstant();

            return(PositionAfterElement(closeTagMatch));
        }