Exemplo n.º 1
0
        private static IEnumerable <JSegment> ProcessRawIndexer(string segment)
        {
            //assume starts with [ and ends with ] but may include extra segmentation ']['
            //multiple indexers may exist in a row
            var buffer = new StringBuffer(segment);
            var next   = buffer.MoveNextNonEmptyChar();

            JUtils.ValidateChar(next, JConstants.SegmentIndexerStart);

            List <JSegment> indexers        = new List <JSegment>();
            bool            hasMoreSegments = true;

            while (hasMoreSegments)
            {
                var segmentValue = buffer.MoveNext((s, i, c) => s[i + 1] == JConstants.SegmentIndexerEnd);
                next = buffer.MoveNext();
                indexers.Add(new JSegment(JSegment.SegmentType.Indexer, n => ExpandIndexer(n, segmentValue), segmentValue));
                next = buffer.MoveNextNonEmptyChar();
                if (!next.HasValue)
                {
                    hasMoreSegments = false;
                }
                else
                {
                    JUtils.ValidateChar(next, JConstants.SegmentIndexerStart, "Expected an indexer but got an incomplete token.");
                }
            }

            return(indexers);
        }
Exemplo n.º 2
0
        public static JContentNode ParseNode(StringBuffer buffer)
        {
            var next = buffer.MoveNextNonEmptyChar();

            JUtils.ValidateChar(next, JConstants.NodeOpeningToken);

            Dictionary <string, JNode> node = new Dictionary <string, JNode>();
            bool hasMoreNodes = true;

            while (hasMoreNodes)
            {
                var key = ParseNodeKey(buffer);
                JUtils.ValidateChar(buffer.MoveNextNonEmptyChar(), JConstants.KeyValueSeparationToken);
                var value = ParseNodeValue(buffer);
                //value.Key = key;
                node.Add(key, value);
                next = buffer.MoveNextNonEmptyChar();
                if (next == JConstants.NodeClosingToken)
                {
                    hasMoreNodes = false;
                }
                else
                {
                    JUtils.ValidateChar(next, JConstants.SeparatorToken);
                }
            }

            return(new JContentNode(node));
        }
Exemplo n.º 3
0
        private static JNode ParseEnumerationValue(StringBuffer buffer)
        {
            char?next;

            //array entry point
            buffer.MoveNext();
            List <JNode> entries      = new List <JNode>();
            bool         hasNextValue = true;

            while (hasNextValue)
            {
                var entry = ParseNodeValue(buffer);
                entries.Add(entry);
                next = buffer.MoveNextNonEmptyChar();
                if (next == JConstants.EnumerationClosingToken)
                {
                    hasNextValue = false;
                }
                else
                {
                    JUtils.ValidateChar(next, JConstants.SeparatorToken);
                }
            }

            return(new JEnumerationNode(entries));
        }
Exemplo n.º 4
0
        public static JNode ParseNodeValue(StringBuffer buffer)
        {
            var next = buffer.MoveNextNonEmptyChar();

            JUtils.ValidateChar(next);
            buffer.MovePrev();
            // ReSharper disable once PossibleInvalidOperationException - see ValidateChar
            switch (next.Value)
            {
            case JConstants.NodeOpeningToken:    //parsing object
                return(ParseNode(buffer));

            case JConstants.NodeClosingToken:    //parsing an empty node
                //i came here because the node that triggered just ended without info
                return(new JContentNode(null));

            case JConstants.EnumerationOpeningToken:    //parsing array
                return(ParseEnumerationValue(buffer));

            case JConstants.EnumerationClosingToken:    //todo - i need it here
                return(new JEnumerationNode(null));

            case JConstants.KeyValueOpeningClosingToken:    //parsing string end value
                buffer.MoveNext();
                var val = buffer.MoveNext((s, i, c) => s[i + 1] == JConstants.KeyValueOpeningClosingToken);
                JUtils.ValidateChar(buffer.MoveNext(), JConstants.KeyValueOpeningClosingToken);
                return(new JValueNode(val));

            default:    //parsing boolean,null, number end value
                return(ParseLowLevelNodeValue(buffer, next));
            }
        }
Exemplo n.º 5
0
        public static string ParseNodeKey(StringBuffer buffer)
        {
            var next = buffer.MoveNextNonEmptyChar();

            JUtils.ValidateChar(next, JConstants.KeyValueOpeningClosingToken);
            var value = buffer.MoveNext(JConstants.KeyValueOpeningClosingToken);

            buffer.MovePrev();
            value = value.Substring(0, value.Length - 1);
            JUtils.ValidateNonEmpty(value);
            next = buffer.MoveNext();
            JUtils.ValidateChar(next, JConstants.KeyValueOpeningClosingToken);
            return(value);
        }