private HoconRoot ParseText(string text, bool resolveSubstitutions, HoconIncludeCallbackAsync includeCallback) { if (string.IsNullOrWhiteSpace(text)) { throw new HoconParserException( $"Parameter {nameof(text)} is null or empty.\n" + "If you want to create an empty Hocon HoconRoot, use \"{}\" instead."); } if (includeCallback != null) { _includeCallback = includeCallback; } try { _tokens = new HoconTokenizer(text).Tokenize(); _root = new HoconValue(null); ParseTokens(); if (resolveSubstitutions) { ResolveSubstitutions(); } } catch (HoconTokenizerException e) { throw HoconParserException.Create(e, null, $"Error while tokenizing Hocon: {e.Message}", e); } catch (HoconException e) { throw HoconParserException.Create(_tokens.Current, Path, e.Message, e); } return(new HoconRoot(_root, _substitutions)); }
// parse path value private HoconPath ParseKey() { // sanity check if (_tokens.Current.IsNonSignificant() || _tokens.Current.Type != TokenType.LiteralValue) { throw HoconParserException.Create(_tokens.Current, Path, $"Internal parser error, ParseKey() is called on an invalid token. Should be `{TokenType.LiteralValue}`, found `{_tokens.Current.Type}` instead."); } var keyTokens = new HoconTokenizerResult(); while (_tokens.Current.Type == TokenType.LiteralValue) { keyTokens.Add(_tokens.Current); _tokens.Next(); } // TODO: this is janky, fix this keyTokens.Reverse(); while (keyTokens.Count > 0 && keyTokens[0].LiteralType == TokenLiteralType.Whitespace) { keyTokens.RemoveAt(0); } keyTokens.Reverse(); keyTokens.Add(new Token("", TokenType.EndOfFile, null)); return(HoconPath.FromTokens(keyTokens)); }
internal static HoconPath FromTokens(HoconTokenizerResult tokens) { if (tokens == null) { throw new ArgumentNullException(nameof(tokens)); } var result = new List <string>(); var sb = new StringBuilder(); while (tokens.Current.Type == TokenType.LiteralValue) { switch (tokens.Current.LiteralType) { case TokenLiteralType.TripleQuotedLiteralValue: throw HoconParserException.Create(tokens.Current, null, "Triple quoted string could not be used in path expression."); case TokenLiteralType.QuotedLiteralValue: // Normalize quoted keys, remove the quotes if the key doesn't need them. //sb.Append(tokens.Current.Value.NeedQuotes() ? $"\"{tokens.Current.Value}\"" : tokens.Current.Value); sb.Append(tokens.Current.Value); break; default: var split = tokens.Current.Value.Split('.'); for (var i = 0; i < split.Length - 1; ++i) { sb.Append(split[i]); result.Add(sb.ToString()); sb.Clear(); } sb.Append(split[split.Length - 1]); break; } tokens.Next(); } result.Add(sb.ToString()); return(new HoconPath(result)); }