Example #1
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();
            }
        }
Example #2
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 #3
0
        private void ParseTokens()
        {
            if (_tokens.Current.IsNonSignificant())
            {
                ConsumeWhitelines();
            }

            while (_tokens.Current.Type != TokenType.EndOfFile)
            {
                switch (_tokens.Current.Type)
                {
                case TokenType.Include:
                    var parsedInclude = ParseInclude(null);
                    if (_root.Type != HoconType.Object)
                    {
                        _root.Clear();
                        _root.Add(parsedInclude.GetObject());
                    }
                    else
                    {
                        _root.Add(parsedInclude.GetObject());
                    }
                    break;

                // Hocon config file may contain one array and one array only
                case TokenType.StartOfArray:
                    _root.Clear();
                    _root.Add(ParseArray(null));
                    ConsumeWhitelines();
                    if (_tokens.Current.Type != TokenType.EndOfFile)
                    {
                        throw HoconParserException.Create(_tokens.Current, Path, "Hocon config can only contain one array or one object.");
                    }
                    return;

                case TokenType.StartOfObject:
                {
                    var parsedObject = ParseObject(null);
                    if (_root.Type != HoconType.Object)
                    {
                        _root.Clear();
                        _root.Add(parsedObject);
                    }
                    else
                    {
                        _root.Add(parsedObject.GetObject());
                    }
                    break;
                }

                case TokenType.LiteralValue:
                {
                    if (_tokens.Current.IsNonSignificant())
                    {
                        ConsumeWhitelines();
                    }
                    if (_tokens.Current.Type != TokenType.LiteralValue)
                    {
                        break;
                    }

                    var parsedObject = ParseObject(null);
                    if (_root.Type != HoconType.Object)
                    {
                        _root.Clear();
                        _root.Add(parsedObject);
                    }
                    else
                    {
                        _root.Add(parsedObject.GetObject());
                    }
                    break;
                }

                case TokenType.Comment:
                case TokenType.EndOfLine:
                case TokenType.EndOfFile:
                case TokenType.EndOfObject:
                case TokenType.EndOfArray:
                    _tokens.Next();
                    break;

                default:
                    throw HoconParserException.Create(_tokens.Current, null, $"Illegal token type: {_tokens.Current.Type}", null);
                }
            }
        }