Exemplo n.º 1
0
        private void TryParseContentItems(YNode node, ref Dictionary <string, string> dict, string prefix = null)
        {
            switch (node)
            {
            case YKeyValuePair keyValuePair:
                string key = null;
                try
                {
                    key = (string)keyValuePair.Key;
                }
                catch (System.Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                key = string.IsNullOrEmpty(prefix) ? key : string.Concat(prefix, this.Separator, key);
                if (keyValuePair.Value is YScalar valueScalar)
                {
                    dict[key] = valueScalar.Value;
                }
                else
                {
                    this.TryParseContentItems(keyValuePair.Value, ref dict, key);
                }

                break;

            case YSequence sequence:
                // TODO: need suppoer Enum fields
                foreach (var sequenceChild in sequence.Children)
                {
                    if (sequenceChild is YScalar yScalar)
                    {
                        dict[string.Concat(prefix, this.Separator, yScalar.Value)] = yScalar.Value;
                    }
                }

                break;

            case YMapping mapping:
                foreach (var mapItem in mapping)
                {
                    this.TryParseContentItems(mapItem, ref dict, prefix);
                }

                break;

            case IEnumerable <YNode> collection:
                foreach (var colItem in collection)
                {
                    this.TryParseContentItems(colItem, ref dict, prefix);
                }

                break;
            }
        }
Exemplo n.º 2
0
        private static void TryGetContentItemsAsDynamic(YNode node, ref IDictionary <string,  object> expandoObject)
        {
            switch (node)
            {
            case YKeyValuePair keyValuePair:
                string key = null;
                try
                {
                    key = (string)keyValuePair.Key;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                if (keyValuePair.Value is YScalar valueScalar)
                {
                    expandoObject[key] = valueScalar.Value;
                }
                else
                {
                    var mapExpandoObject = new ExpandoObject() as IDictionary <string, object>;
                    expandoObject[key] = mapExpandoObject;
                    TryGetContentItemsAsDynamic(keyValuePair.Value, ref mapExpandoObject);
                }

                break;

            case YSequence sequence:
                // TODO: need support Enum fields
                throw new NotSupportedException();

            case YMapping mapping:
                foreach (var mapItem in mapping)
                {
                    TryGetContentItemsAsDynamic(mapItem, ref expandoObject);
                }

                break;

            case IEnumerable <YNode> collection:
                foreach (var colItem in collection)
                {
                    TryGetContentItemsAsDynamic(colItem, ref expandoObject);
                }

                break;
            }
        }
Exemplo n.º 3
0
 private static void FindDuplicateNodes(YNode node, HashSet<YNode> visited, HashSet<YNode> duplicates)
 {
     if (visited.Contains(node))
     {
         duplicates.Add(node);
     }
     else
     {
         visited.Add(node);
         foreach (var subNode in node.SubNodes)
         {
             FindDuplicateNodes(subNode, visited, duplicates);
         }
     }
 }
Exemplo n.º 4
0
 private static void FindDuplicateNodes(YNode node, HashSet <YNode> visited, HashSet <YNode> duplicates)
 {
     if (visited.Contains(node))
     {
         duplicates.Add(node);
     }
     else
     {
         visited.Add(node);
         foreach (var subNode in node.SubNodes)
         {
             FindDuplicateNodes(subNode, visited, duplicates);
         }
     }
 }
Exemplo n.º 5
0
        internal new static YKeyValuePair Parse(Tokenizer tokenizer)
        {
            switch (tokenizer.Current.Kind)
            {
            case TokenKind.MappingKey:
            {
                tokenizer.MoveNext();

                var key = YNode.Parse(tokenizer);

                if (tokenizer.Current.Kind != TokenKind.MappingValue)
                {
                    return(new YKeyValuePair(key, new YScalar(null)));
                }

                tokenizer.MoveNext();

                var value = YNode.Parse(tokenizer);

                return(new YKeyValuePair(key, value));
            }

            case TokenKind.Alias:
                var keyAlias = YNode.Parse(tokenizer);
                tokenizer.MoveNext();
                var valueAlias = YNode.Parse(tokenizer);
                return(new YKeyValuePair(keyAlias, valueAlias));

            default:
            {
                var key = YNode.Parse(tokenizer);
                if (tokenizer.Current.Kind != TokenKind.MappingValue)
                {
                    throw ParseException.UnexpectedToken(tokenizer, TokenKind.MappingValue);
                }

                tokenizer.MoveNext();
                if (tokenizer.Current.Kind == TokenKind.Anchor)
                {
                    return(null);
                }

                var value = YNode.Parse(tokenizer);

                return(new YKeyValuePair(key, value));
            }
            }
        }
Exemplo n.º 6
0
            private static void GenerateNodeEvents(IntPtr pNativeEmitter, YNode node, IDictionary <YNode, string> aliases, HashSet <YNode> visited)
            {
                string alias;
                bool   hasAlias = aliases.TryGetValue(node, out alias);
                bool   isAnchor = hasAlias && !visited.Contains(node);
                bool   isAlias  = hasAlias && !isAnchor;

                if (isAnchor)
                {
                    visited.Add(node);
                }

                if (isAlias)
                {
                    GenerateEvent(pNativeEmitter, x => CreateEventAlias((YamlEvent *)x, alias));
                }
                else
                {
                    string anchor       = isAnchor ? alias : null;
                    var    scalarNode   = node as YScalar;
                    var    sequenceNode = node as YSequence;
                    var    mappingNode  = node as YMapping;
                    if (scalarNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, scalarNode, aliases, anchor);
                    }
                    else if (sequenceNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, sequenceNode, aliases, anchor, visited);
                    }
                    else if (mappingNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, mappingNode, aliases, anchor, visited);
                    }
                }
            }
Exemplo n.º 7
0
 /// <summary>
 ///  型'<see cref="CAP.Yencon.YBoolean"/>'の新しいインスタンスを生成します。
 /// </summary>
 /// <param name="parent">
 ///  新しい論理値の親セクションまたは親配列です。
 /// </param>
 /// <param name="name">新しい論理値の名前です。</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="CAP.Yencon.Exceptions.InvalidNodeNameException"/>
 protected YBoolean(YNode parent, string name) : base(parent, name)
 {
 }
Exemplo n.º 8
0
 public void MergeY(YNode source, int offset, int length)
 {
     Array.Copy(source._keys, 0, _keys, offset, length);
     Array.Copy(source._values, 0, _values, offset, length);
 }
Exemplo n.º 9
0
 public void Split(YNode target, int offset, int length)
 {
     Array.Copy(_keys, offset, target._keys, 0, length);
     Array.Copy(_values, offset, target._values, 0, length);
 }
Exemplo n.º 10
0
 /// <summary>
 ///  上書きされた場合、ノードを書き込みます。
 /// </summary>
 /// <param name="node">書き込むノードを表すオブジェクトです。</param>
 public abstract void Write(YNode node);
Exemplo n.º 11
0
 private void SetAnchor(string anchor, YNode node)
 {
     if (!string.IsNullOrEmpty(anchor))
         anchors[anchor] = node;
 }
Exemplo n.º 12
0
        // .ToString()

        /// <summary>
        ///  型'<see cref="CAP.Yencon.YNumber"/>'の新しいインスタンスを生成します。
        /// </summary>
        /// <param name="parent">
        ///  新しい数値の親セクションまたは親配列です。
        /// </param>
        /// <param name="name">新しい数値の名前です。</param>
        /// <exception cref="System.ArgumentNullException"/>
        /// <exception cref="System.ArgumentException"/>
        /// <exception cref="CAP.Yencon.Exceptions.InvalidNodeNameException"/>
        protected YNumber(YNode parent, string name) : base(parent, name)
        {
        }
Exemplo n.º 13
0
 string Annotator(int i, YNode n)
 {
     return(string.Format("node {0} depth {1}", i, n.Depth));
 }
Exemplo n.º 14
0
 /// <summary>
 ///  型'<see cref="CAP.Yencon.YString"/>'の新しいインスタンスを生成します。
 /// </summary>
 /// <param name="parent">
 ///  新しいリンク文字列の親セクションまたは親配列です。
 /// </param>
 /// <param name="name">新しいリンク文字列の名前です。</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="CAP.Yencon.Exceptions.InvalidNodeNameException"/>
 protected YLink(YNode parent, string name) : base(parent, name)
 {
 }
Exemplo n.º 15
0
 protected override bool RemoveNodeCore(YNode node)
 {
     return(_section?.RemoveNode(node) ?? false);
 }
Exemplo n.º 16
0
 public YKeyValuePair(YNode key, YNode value = null)
     : base(YNodeStyle.Block)
 {
     this.Key   = key;
     this.Value = value;
 }
Exemplo n.º 17
0
 public YStringInternal(YNode parent, string name)
     : base(parent, name)
 {
 }
Exemplo n.º 18
0
 public YCommentInternal(YNode parent)
     : base(parent)
 {
 }
Exemplo n.º 19
0
 public YEmptyInternal(YNode parent, string name)
     : base(parent, name)
 {
 }
Exemplo n.º 20
0
 /// <summary>
 ///  型'<see cref="CAP.Yencon.YString"/>'の新しいインスタンスを生成します。
 /// </summary>
 /// <param name="parent">
 ///  新しい空値の親セクションまたは親配列です。
 /// </param>
 /// <param name="name">新しい空値の名前です。</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="CAP.Yencon.Exceptions.InvalidNodeNameException"/>
 protected YEmpty(YNode parent, string name) : base(parent, name)
 {
 }
Exemplo n.º 21
0
            private static void GenerateNodeEvents(IntPtr pNativeEmitter, YNode node, IDictionary<YNode, string> aliases, HashSet<YNode> visited)
            {
                string alias;
                bool hasAlias = aliases.TryGetValue(node, out alias);
                bool isAnchor = hasAlias && !visited.Contains(node);
                bool isAlias = hasAlias && !isAnchor;
                if (isAnchor)
                {
                    visited.Add(node);
                }

                if (isAlias)
                {
                    GenerateEvent(pNativeEmitter, x => CreateEventAlias((YamlEvent*)x, alias));
                }
                else
                {
                    string anchor = isAnchor ? alias : null;
                    var scalarNode = node as YScalar;
                    var sequenceNode = node as YSequence;
                    var mappingNode = node as YMapping;
                    if (scalarNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, scalarNode, aliases, anchor);
                    }
                    else if (sequenceNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, sequenceNode, aliases, anchor, visited);
                    }
                    else if (mappingNode != null)
                    {
                        GenerateSubTypeEvents(pNativeEmitter, mappingNode, aliases, anchor, visited);
                    }
                }
            }
Exemplo n.º 22
0
 string Annotator(int i,YNode n)
 {
     return string.Format("node {0} depth {1}",i,n.Depth);
 }
Exemplo n.º 23
0
 public YArrayInternal(YNode parent, string name) : base(parent, name)
 {
     _children     = new List <YNode>();
     this.Children = _children.AsReadOnly();
 }
Exemplo n.º 24
0
 /// <summary>
 ///  型'<see cref="CAP.Yencon.YString"/>'の新しいインスタンスを生成します。
 /// </summary>
 /// <param name="parent">
 ///  新しいコメントの親セクションまたは親配列です。
 /// </param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentException"/>
 protected YComment(YNode parent) : base(parent, string.Empty)
 {
 }
Exemplo n.º 25
0
 protected override bool RemoveNodeCore(YNode node)
 {
     return(_children.Remove(node));
 }
Exemplo n.º 26
0
 /// <summary>
 ///  型'<see cref="CAP.Yencon.YString"/>'の新しいインスタンスを生成します。
 /// </summary>
 /// <param name="parent">
 ///  新しい文字列値の親セクションまたは親配列です。
 /// </param>
 /// <param name="name">新しい文字列値の名前です。</param>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="CAP.Yencon.Exceptions.InvalidNodeNameException"/>
 protected YString(YNode parent, string name) : base(parent, name)
 {
 }
Exemplo n.º 27
0
        static DocumentGrammar()
        {
            Boolean.Rule = Choice(
                YamlLexer.True.Constant(YBoolean.True),
                YamlLexer.False.Constant(YBoolean.False));

            Null.Rule   = YamlLexer.Null.Constant(YNull.Null);
            Number.Rule = YamlLexer.Number.BindLexeme(YNumber.FromString);

            QuotedString.Rule = Choice(
                YamlLexer.SingleQuotedString.BindLexeme(YString.FromQuotedString),
                YamlLexer.DoubleQuotedString.BindLexeme(YString.FromQuotedString));

            UnquotedString.Rule = YamlLexer.UnquotedString.BindLexeme(YString.FromString);

            AnyString.Rule = Choice(QuotedString, UnquotedString);

            Alias.Rule = YamlLexer.Alias.BindLexeme(YAlias.FromString);

            BlockScalarText.Rule =
                from header in YamlLexer.BlockScalarHeader.Lexeme()
                from lines in Between(
                    ScopeTokenKind.ScopeBegin.Kind(),
                    OneOrMore(Choice(YamlLexer.BlockScalarLine.Lexeme(), YamlLexer.NewLine.Lexeme())),
                    ScopeTokenKind.ScopeEnd.Kind())
                select YString.FromBlockScalar(header, lines);

            BlockSequence.Rule =
                OneOrMore(YamlLexer.BlockSequenceItemMarker.Kind().Take(Node))
                .Bind(YSequence.FromList);

            FlowSequence.Rule =
                Between(YamlLexer.FlowSequenceBegin.Kind(), ZeroOrMore(FlowNode, YamlLexer.FlowItemSeparator.Kind()), YamlLexer.FlowSequenceEnd.Kind())
                .Bind(YSequence.FromList);

            BlockMapping.Rule =
                OneOrMore(NameValuePair(AnyString, YamlLexer.MappingSeparator.Kind(), Choice(Node, Alias)))
                .Bind(YMapping.FromDictionary);

            FlowMapping.Rule =
                Between(
                    YamlLexer.FlowMappingBegin.Kind(),
                    ZeroOrMore(
                        NameValuePair(
                            AnyString,
                            YamlLexer.MappingSeparator.Kind(),
                            Choice(FlowNode, Alias)),
                        YamlLexer.FlowItemSeparator.Kind()),
                    YamlLexer.FlowMappingEnd.Kind())
                .Bind(YMapping.FromDictionary);

            FlowNode.Rule =
                from anchor in Optional(YamlLexer.Anchor.Lexeme())
                from node in Choice <YNode>(
                    FlowSequence,
                    Attempt(FlowMapping), // enclosed into attempt lest it consumes QuotedString, UnquotedString
                    QuotedString,
                    Number,
                    Boolean,
                    Null,
                    UnquotedString)
                select YNode.AttachAnchor(node, anchor);

            BlockNode.Rule = Between(
                ScopeTokenKind.ScopeBegin.Kind(),
                from anchor in Optional(YamlLexer.Anchor.Lexeme())
                from node in Choice <YNode>(
                    BlockSequence,
                    Attempt(BlockMapping), // enclosed into attempt lest it consumes QuotedString, UnquotedString
                    QuotedString,
                    Number,
                    Boolean,
                    Null,
                    BlockScalarText,
                    UnquotedString)
                select YNode.AttachAnchor(node, anchor),
                ScopeTokenKind.ScopeEnd.Kind());

            Node.Rule = Choice(FlowNode, BlockNode);

            TagDirective.Rule = YamlLexer.TagDirective.BindLexeme(YTagDirective.FromLexeme);

            YamlVersionDirective.Rule = YamlLexer.VersionDirective.BindLexeme(YVersionDirective.FromLexeme);

            Directive.Rule = Choice <YDirective>(TagDirective, YamlVersionDirective);

            BareDocument.Rule = Node.Bind(YDocument.FromBareNode);

            ExplicitDocument.Rule = YamlLexer.DirectivesEndMarker.Kind().Take(Optional(Node)).Bind(YDocument.FromBareNode);

            DirectivesDocument.Rule =
                from directives in OneOrMore(Directive)
                from body in YamlLexer.DirectivesEndMarker.Kind().Take(Node)
                select YDocument.Create(directives, body);

            Document.Rule =
                OccupiesEntireInput(Choice(DirectivesDocument, ExplicitDocument, BareDocument).ThenSkip(Optional(YamlLexer.DocumentEndMarker.Lexeme())));
        }