Skip to content

Commit 9d9f7f1

Browse files
ArkatufusAaronontheweb
authored andcommitted
Fix unable to use include keyword at root level (#69)
1 parent 59fbc47 commit 9d9f7f1

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/HOCON.Tests/HoconTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,32 @@ Task<string> IncludeCallback(HoconCallbackType t, string s)
478478
Assert.Equal("hello", config.GetString("a.b.y"));
479479
}
480480

481+
[Theory]
482+
[InlineData(@"{
483+
include ""foo""
484+
a : include ""foo""
485+
}")]
486+
[InlineData(@"
487+
include ""foo""
488+
a : include ""foo""
489+
")]
490+
public void CanParseIncludeInRoot(string hocon)
491+
{
492+
var includeHocon = @"
493+
x = 123
494+
y = hello
495+
";
496+
Task<string> IncludeCallback(HoconCallbackType t, string s)
497+
=> Task.FromResult(includeHocon);
498+
499+
var config = Parser.Parse(hocon, IncludeCallback);
500+
501+
Assert.Equal(123, config.GetInt("x"));
502+
Assert.Equal("hello", config.GetString("y"));
503+
Assert.Equal(123, config.GetInt("a.x"));
504+
Assert.Equal("hello", config.GetString("a.y"));
505+
}
506+
481507
[Fact]
482508
public void CanParseArrayInclude()
483509
{

src/HOCON/Parser.cs

+5-31
Original file line numberDiff line numberDiff line change
@@ -242,35 +242,17 @@ private void ParseTokens()
242242
switch (_tokens.Current.Type)
243243
{
244244
case TokenType.Include:
245-
var parsedInclude = ParseInclude(null);
246-
if (_root.Type != HoconType.Object)
247-
{
248-
_root.Clear();
249-
_root.Add(parsedInclude.GetObject());
250-
}
251-
else
252-
_root.Add(parsedInclude.GetObject());
245+
_root.Add(ParseInclude(_root));
253246
break;
254247

255248
// Hocon config file may contain one array and one array only
256249
case TokenType.StartOfArray:
257-
_root.Clear();
258-
_root.Add(ParseArray(null));
259-
ConsumeWhitelines();
260-
if (_tokens.Current.Type != TokenType.EndOfFile)
261-
throw HoconParserException.Create(_tokens.Current, Path, "Hocon config can only contain one array or one object.");
262-
return;
250+
_root.Add(ParseArray(_root));
251+
break;
263252

264253
case TokenType.StartOfObject:
265254
{
266-
var parsedObject = ParseObject(null);
267-
if (_root.Type != HoconType.Object)
268-
{
269-
_root.Clear();
270-
_root.Add(parsedObject);
271-
}
272-
else
273-
_root.Add(parsedObject.GetObject());
255+
_root.Add(ParseObject(_root).GetObject());
274256
break;
275257
}
276258

@@ -281,20 +263,12 @@ private void ParseTokens()
281263
if (_tokens.Current.Type != TokenType.LiteralValue)
282264
break;
283265

284-
var parsedObject = ParseObject(null);
285-
if (_root.Type != HoconType.Object)
286-
{
287-
_root.Clear();
288-
_root.Add(parsedObject);
289-
}
290-
else
291-
_root.Add(parsedObject.GetObject());
266+
_root.Add(ParseObject(_root).GetObject());
292267
break;
293268
}
294269

295270
case TokenType.Comment:
296271
case TokenType.EndOfLine:
297-
case TokenType.EndOfFile:
298272
case TokenType.EndOfObject:
299273
case TokenType.EndOfArray:
300274
_tokens.Next();

0 commit comments

Comments
 (0)