This class represents the root type for a HOCON (Human-Optimized Config Object Notation) configuration object.
Inheritance: IMightBeAHoconObject
Example #1
0
        private void ParseObject(HoconValue owner, bool root,string currentPath)
        {
            try
            {
                PushDiagnostics("{");

                if (owner.IsObject())
                {
                    //the value of this KVP is already an object
                }
                else
                {
                    //the value of this KVP is not an object, thus, we should add a new
                    owner.NewValue(new HoconObject());
                }

                HoconObject currentObject = owner.GetObject();

                while (!_reader.EoF)
                {
                    Token t = _reader.PullNext();
                    switch (t.Type)
                    {
                        case TokenType.Include:
                            var included = _includeCallback(t.Value);
                            var substitutions = included.Substitutions;
                            foreach (var substitution in substitutions)
                            {
                                //fixup the substitution, add the current path as a prefix to the substitution path
                                substitution.Path = currentPath + "." + substitution.Path;
                            }
                            _substitutions.AddRange(substitutions);
                            var otherObj = included.Value.GetObject();
                            owner.GetObject().Merge(otherObj);

                            break;
                        case TokenType.EoF:
                            if (!string.IsNullOrEmpty(currentPath))
                            {
                                throw new HoconParserException(string.Format("Expected end of object but found EoF {0}",GetDiagnosticsStackTrace()));
                            }
                            break;
                        case TokenType.Key:
                            HoconValue value = currentObject.GetOrCreateKey(t.Value);
                            var nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value;
                            ParseKeyContent(value, nextPath);
                            if (!root)
                                return;
                            break;

                        case TokenType.ObjectEnd:
                            return;
                    }
                }
            }
            finally
            {
                PopDiagnostics();
            }
        }
Example #2
0
        /// <summary>
        /// Parses the object.
        /// </summary>
        /// <param name="owner">The owner.</param>
        /// <param name="root">if set to <c>true</c> [root].</param>
        private void ParseObject(HoconValue owner, bool root)
        {
            if (owner.IsObject())
            {
                //the value of this KVP is already an object
            }
            else
            {
                //the value of this KVP is not an object, thus, we should add a new
                owner.NewValue(new HoconObject());
            }

            HoconObject currentObject = owner.GetObject();

            while (!reader.EoF)
            {
                Token t = reader.PullNext();
                switch (t.Type)
                {
                    case TokenType.EoF:
                        break;
                    case TokenType.Key:
                        HoconValue value = currentObject.GetOrCreateKey(t.Value);
                        ParseKeyContent(value);
                        if (!root)
                            return;
                        break;

                    case TokenType.ObjectEnd:
                        return;
                }
            }
        }
Example #3
0
        public Config(Config source, Config fallback)
        {
            if (source == null)
                throw new ArgumentNullException("source");

            _node = source._node;
            this._fallback = fallback;
        }
Example #4
0
        public Config(HoconRoot root)
        {
            if (root.Value == null)
                throw new ArgumentNullException("root.Value");

            this._node = root.Value;
            this._substitutions = root.Substitutions;
        }
Example #5
0
 /// <summary>
 /// Wraps this <see cref="HoconValue"/> into a new <see cref="Config"/> object at the specified key.
 /// </summary>
 /// <param name="key">The key designated to be the new root element.</param>
 /// <returns>A <see cref="Config"/> with the given key as the root element.</returns>
 public Config AtKey(string key)
 {
     var o = new HoconObject();
     o.GetOrCreateKey(key);
     o.Items[key] = this;
     var r = new HoconValue();
     r.Values.Add(o);
     return new Config(new HoconRoot(r));
 }
Example #6
0
 public HoconValue GetOrCreateKey(string key)
 {
     if (_children.ContainsKey(key))
     {
         return _children[key];
     }
     var child = new HoconValue();
     _children.Add(key, child);
     return child;
 }
Example #7
0
 public HoconValue GetOrCreateKey(string key)
 {
     if (Items.ContainsKey(key))
     {
         return Items[key];
     }
     var child = new HoconValue();
     Items.Add(key, child);
     return child;
 }
Example #8
0
        private void ParseObject(HoconValue owner, bool root,string currentPath)
        {
            if (owner.IsObject())
            {
                //the value of this KVP is already an object
            }
            else
            {
                //the value of this KVP is not an object, thus, we should add a new
                owner.NewValue(new HoconObject());
            }

            HoconObject currentObject = owner.GetObject();

            while (!_reader.EoF)
            {
                Token t = _reader.PullNext();
                switch (t.Type)
                {
                    case TokenType.Include:
                        var included = _includeCallback(t.Value);
                        var substitutions = included.Substitutions;
                        foreach (var substitution in substitutions)
                        {
                            //fixup the substitution, add the current path as a prefix to the substitution path
                            substitution.Path = currentPath + "." + substitution.Path;
                        }
                        _substitutions.AddRange(substitutions);
                        var otherObj = included.Value.GetObject();
                        owner.GetObject().Merge(otherObj);

                        break;
                    case TokenType.EoF:
                        break;
                    case TokenType.Key:
                        HoconValue value = currentObject.GetOrCreateKey(t.Value);
                        var nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value;
                        ParseKeyContent(value, nextPath);
                        if (!root)
                            return;
                        break;

                    case TokenType.ObjectEnd:
                        return;
                }
            }
        }
Example #9
0
        /// <summary>
        /// Parses the text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns>HoconValue.</returns>
        /// <exception cref="System.Exception">Unresolved substitution: + sub.Path</exception>
        private HoconRoot ParseText(string text)
        {
            root = new HoconValue();
            reader = new HoconTokenizer(text);
            reader.PullWhitespaceAndComments();
            ParseObject(root, true);

            var c = new Config(new HoconRoot(root,Enumerable.Empty<HoconSubstitution>()));
            foreach (HoconSubstitution sub in substitutions)
            {
                HoconValue res = c.GetValue(sub.Path);
                if (res == null)
                    throw new Exception("Unresolved substitution:" + sub.Path);
                sub.ResolvedValue = res;
            }
            return new HoconRoot(root, substitutions);
        }
Example #10
0
        private HoconRoot ParseText(string text,Func<string,HoconRoot> includeCallback)
        {
            _includeCallback = includeCallback;
            _root = new HoconValue();
            _reader = new HoconTokenizer(text);
            _reader.PullWhitespaceAndComments();
            ParseObject(_root, true,"");

            var c = new Config(new HoconRoot(_root, Enumerable.Empty<HoconSubstitution>()));
            foreach (HoconSubstitution sub in _substitutions)
            {
                HoconValue res = c.GetValue(sub.Path);
                if (res == null)
                    throw new FormatException("Unresolved substitution:" + sub.Path);
                sub.ResolvedValue = res;
            }
            return new HoconRoot(_root, _substitutions);
        }
Example #11
0
 private void ParseKeyContent(HoconValue value)
 {
     while (!reader.EoF)
     {
         Token t = reader.PullNext();
         switch (t.Type)
         {
             case TokenType.Dot:
                 ParseObject(value, false);
                 return;
             case TokenType.Assign:
                 ParseValue(value);
                 return;
             case TokenType.ObjectStart:
                 ParseObject(value, true);
                 return;
         }
     }
 }
Example #12
0
        /// <summary>
        /// Retrieves the next value token from the tokenizer and appends it
        /// to the supplied element <paramref name="owner"/>.
        /// </summary>
        /// <param name="owner">The element to append the next token.</param>
        /// <exception cref="System.Exception">End of file reached while trying to read a value</exception>
        public void ParseValue(HoconValue owner,string currentPath)
        {
            if (_reader.EoF)
                throw new Exception("End of file reached while trying to read a value");

            _reader.PullWhitespaceAndComments();
            while (_reader.IsValue())
            {
                Token t = _reader.PullValue();

                switch (t.Type)
                {
                    case TokenType.EoF:
                        break;
                    case TokenType.LiteralValue:
                        if (owner.IsObject())
                        {
                            //needed to allow for override objects
                            owner.Clear();
                        }
                        var lit = new HoconLiteral
                        {
                            Value = t.Value
                        };
                        owner.AppendValue(lit);

                        break;
                    case TokenType.ObjectStart:
                        ParseObject(owner, true,currentPath);
                        break;
                    case TokenType.ArrayStart:
                        HoconArray arr = ParseArray(currentPath);
                        owner.AppendValue(arr);
                        break;
                    case TokenType.Substitute:
                        HoconSubstitution sub = ParseSubstitution(t.Value);
                        _substitutions.Add(sub);
                        owner.AppendValue(sub);
                        break;
                }
                if (_reader.IsSpaceOrTab())
                {
                    ParseTrailingWhitespace(owner);
                }
            }

            IgnoreComma();
        }
Example #13
0
 private void ParseKeyContent(HoconValue value,string currentPath)
 {
     while (!_reader.EoF)
     {
         Token t = _reader.PullNext();
         switch (t.Type)
         {
             case TokenType.Dot:
                 ParseObject(value, false,currentPath);
                 return;
             case TokenType.Assign:
                 
                 if (!value.IsObject())
                 {
                     //if not an object, then replace the value.
                     //if object. value should be merged
                     value.Clear();
                 }
                 ParseValue(value,currentPath);
                 return;
             case TokenType.ObjectStart:
                 ParseObject(value, true,currentPath);
                 return;
         }
     }
 }
Example #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HoconRoot"/> class.
 /// </summary>
 /// <param name="value">The value to associate with this element.</param>
 public HoconRoot(HoconValue value)
 {
     Value = value;
     Substitutions = Enumerable.Empty<HoconSubstitution>();
 }
Example #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HoconRoot"/> class.
 /// </summary>
 /// <param name="value">The value to associate with this element.</param>
 /// <param name="substitutions">An enumeration of substitutions to associate with this element.</param>
 public HoconRoot(HoconValue value, IEnumerable<HoconSubstitution> substitutions)
 {
     Value = value;
     Substitutions = substitutions;
 }
Example #16
0
 public HoconRoot(HoconValue value)
 {
     Value         = value;
     Substitutions = Enumerable.Empty <HoconSubstitution>();
 }
Example #17
0
 public Config(Config source, Config fallback)
 {
     node = source.node;
     this.fallback = fallback;
 }
Example #18
0
 public Config(HoconValue node)
 {
     this._node = node;
 }
Example #19
0
 /// <summary>
 /// Parses the array.
 /// </summary>
 /// <returns>HoconArray.</returns>
 public HoconArray ParseArray()
 {
     var arr = new HoconArray();
     while (!reader.EoF && !reader.IsArrayEnd())
     {
         var v = new HoconValue();
         ParseValue(v);
         arr.Add(v);
         reader.PullWhitespaceAndComments();
     }
     reader.PullArrayEnd();
     return arr;
 }
Example #20
0
        /// <summary>
        /// Retrieves the next value token from the tokenizer and appends it
        /// to the supplied element <paramref name="owner"/>.
        /// </summary>
        /// <param name="owner">The element to append the next token.</param>
        /// <exception cref="System.Exception">End of file reached while trying to read a value</exception>
        public void ParseValue(HoconValue owner, string currentPath)
        {
            if (_reader.EoF)
            {
                throw new HoconParserException("End of file reached while trying to read a value");
            }

            _reader.PullWhitespaceAndComments();
            var start = _reader.Index;

            try
            {
                while (_reader.IsValue())
                {
                    Token t = _reader.PullValue();

                    switch (t.Type)
                    {
                    case TokenType.EoF:
                        break;

                    case TokenType.LiteralValue:
                        if (owner.IsObject())
                        {
                            //needed to allow for override objects
                            owner.Clear();
                        }
                        var lit = new HoconLiteral
                        {
                            Value = t.Value
                        };
                        owner.AppendValue(lit);

                        break;

                    case TokenType.ObjectStart:
                        ParseObject(owner, true, currentPath);
                        break;

                    case TokenType.ArrayStart:
                        HoconArray arr = ParseArray(currentPath);
                        owner.AppendValue(arr);
                        break;

                    case TokenType.Substitute:
                        HoconSubstitution sub = ParseSubstitution(t.Value);
                        _substitutions.Add(sub);
                        owner.AppendValue(sub);
                        break;
                    }
                    if (_reader.IsSpaceOrTab())
                    {
                        ParseTrailingWhitespace(owner);
                    }
                }

                IgnoreComma();
            }
            catch (HoconTokenizerException tokenizerException)
            {
                throw new HoconParserException(string.Format("{0}\r{1}", tokenizerException.Message, GetDiagnosticsStackTrace()), tokenizerException);
            }
            finally
            {
                //no value was found, tokenizer is still at the same position
                if (_reader.Index == start)
                {
                    throw new HoconParserException(string.Format("Hocon syntax error {0}\r{1}", _reader.GetHelpTextAtIndex(start), GetDiagnosticsStackTrace()));
                }
            }
        }
Example #21
0
 private void ParseTrailingWhitespace(HoconValue owner)
 {
     Token ws = _reader.PullSpaceOrTab();
     //single line ws should be included if string concat
     if (ws.Value.Length > 0)
     {
         var wsLit = new HoconLiteral
         {
             Value = ws.Value,
         };
         owner.AppendValue(wsLit);
     }
 }
Example #22
0
 /// <summary>
 /// Retrieves the next array token from the tokenizer.
 /// </summary>
 /// <returns>An array of elements retrieved from the token.</returns>
 public HoconArray ParseArray(string currentPath)
 {
     var arr = new HoconArray();
     while (!_reader.EoF && !_reader.IsArrayEnd())
     {
         var v = new HoconValue();
         ParseValue(v,currentPath);
         arr.Add(v);
         _reader.PullWhitespaceAndComments();
     }
     _reader.PullArrayEnd();
     return arr;
 }
Example #23
0
        /// <summary>
        /// Retrieves the next value token from the tokenizer and appends it
        /// to the supplied element <paramref name="owner"/>.
        /// </summary>
        /// <param name="owner">The element to append the next token.</param>
        /// <exception cref="System.Exception">End of file reached while trying to read a value</exception>
        public void ParseValue(HoconValue owner,string currentPath)
        {
            if (_reader.EoF)
                throw new HoconParserException("End of file reached while trying to read a value");

            _reader.PullWhitespaceAndComments();
            var start = _reader.Index;
            try
            {
                while (_reader.IsValue())
                {
                    Token t = _reader.PullValue();

                    switch (t.Type)
                    {
                        case TokenType.EoF:
                            break;
                        case TokenType.LiteralValue:
                            if (owner.IsObject())
                            {
                                //needed to allow for override objects
                                owner.Clear();
                            }
                            var lit = new HoconLiteral
                            {
                                Value = t.Value
                            };
                            owner.AppendValue(lit);

                            break;
                        case TokenType.ObjectStart:
                            ParseObject(owner, true, currentPath);
                            break;
                        case TokenType.ArrayStart:
                            HoconArray arr = ParseArray(currentPath);
                            owner.AppendValue(arr);
                            break;
                        case TokenType.Substitute:
                            HoconSubstitution sub = ParseSubstitution(t.Value);
                            _substitutions.Add(sub);
                            owner.AppendValue(sub);
                            break;
                    }
                    if (_reader.IsSpaceOrTab())
                    {
                        ParseTrailingWhitespace(owner);
                    }
                }

                IgnoreComma();
            }
            catch(HoconTokenizerException tokenizerException)
            {
                throw new HoconParserException(string.Format("{0}\r{1}", tokenizerException.Message, GetDiagnosticsStackTrace()),tokenizerException);
            }
            finally
            {
                //no value was found, tokenizer is still at the same position
                if (_reader.Index == start)
                {
                    throw new HoconParserException(string.Format("Hocon syntax error {0}\r{1}",_reader.GetHelpTextAtIndex(start),GetDiagnosticsStackTrace()));
                }
            }
        }
Example #24
0
 public HoconRoot(HoconValue value, IEnumerable <HoconSubstitution> substitutions)
 {
     Value         = value;
     Substitutions = substitutions;
 }
Example #25
0
        private void ParseObject(HoconValue owner, bool root, string currentPath)
        {
            try
            {
                PushDiagnostics("{");

                if (owner.IsObject())
                {
                    //the value of this KVP is already an object
                }
                else
                {
                    //the value of this KVP is not an object, thus, we should add a new
                    owner.NewValue(new HoconObject());
                }

                HoconObject currentObject = owner.GetObject();

                while (!_reader.EoF)
                {
                    Token t = _reader.PullNext();
                    switch (t.Type)
                    {
                    case TokenType.Include:
                        var included      = _includeCallback(t.Value);
                        var substitutions = included.Substitutions;
                        foreach (var substitution in substitutions)
                        {
                            //fixup the substitution, add the current path as a prefix to the substitution path
                            substitution.Path = currentPath + "." + substitution.Path;
                        }
                        _substitutions.AddRange(substitutions);
                        var otherObj = included.Value.GetObject();
                        owner.GetObject().Merge(otherObj);

                        break;

                    case TokenType.EoF:
                        if (!string.IsNullOrEmpty(currentPath))
                        {
                            throw new HoconParserException(string.Format("Expected end of object but found EoF {0}", GetDiagnosticsStackTrace()));
                        }
                        break;

                    case TokenType.Key:
                        HoconValue value    = currentObject.GetOrCreateKey(t.Value);
                        var        nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value;
                        ParseKeyContent(value, nextPath);
                        if (!root)
                        {
                            return;
                        }
                        break;

                    case TokenType.ObjectEnd:
                        return;
                    }
                }
            }
            finally
            {
                PopDiagnostics();
            }
        }
Example #26
0
 public Config(Config source, Config fallback)
 {
     _node = source._node;
     this._fallback = fallback;
 }
Example #27
0
        private void ParseKeyContent(HoconValue value,string currentPath)
        {
            try
            {
                var last = currentPath.Split('.').Last();
                PushDiagnostics(string.Format("{0} = ", last));
                while (!_reader.EoF)
                {
                    Token t = _reader.PullNext();
                    switch (t.Type)
                    {
                        case TokenType.Dot:
                            ParseObject(value, false, currentPath);
                            return;
                        case TokenType.Assign:

                            if (!value.IsObject())
                            {
                                //if not an object, then replace the value.
                                //if object. value should be merged
                                value.Clear();
                            }
                            ParseValue(value, currentPath);
                            return;
                        case TokenType.ObjectStart:
                            ParseObject(value, true, currentPath);
                            return;
                    }
                }
            }
            finally
            {
                PopDiagnostics();
            }
        }