Skip to content

Commit

Permalink
(#61) Update the preprocessor to handle trailing comments on #include
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Jan 17, 2024
1 parent 8840f9e commit 21b5c2d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
13 changes: 11 additions & 2 deletions Cesium.Parser.Tests/PreprocessorTests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Cesium.Core;
using Cesium.Preprocessor;
using Cesium.TestFramework;
Expand All @@ -7,7 +8,10 @@ namespace Cesium.Parser.Tests.PreprocessorTests;

public class PreprocessorTests : VerifyTestBase
{
private static async Task DoTest(string source, Dictionary<string, string>? standardHeaders = null, Dictionary<string, IList<IToken<CPreprocessorTokenType>>>? defines = null)
private static async Task DoTest(
[StringSyntax("cpp")] string source,
Dictionary<string, string>? standardHeaders = null,
Dictionary<string, IList<IToken<CPreprocessorTokenType>>>? defines = null)
{
string result = await DoPreprocess(source, standardHeaders, defines);
await Verify(result, GetSettings());
Expand Down Expand Up @@ -45,12 +49,17 @@ int test()
}", new() { ["foo.h"] = "void foo() {}", ["bar.h"] = "int bar = 0;" });

[Fact]
public Task IncludeTrailingWhiltespacesIgnored() => DoTest($@"#include <foo.h>{"\t\t\t" /*Make whitespaces visible here */}
public Task IncludeTrailingWhitespacesIgnored() => DoTest($@"#include <foo.h>{"\t\t\t" /*Make whitespaces visible here */}
#include <bar.h>
int test()
{{
}}", new() { ["foo.h"] = "void foo() {}", ["bar.h"] = "int bar = 0;" });

[Fact]
public Task IncludeTrailingCommentsAreAllowed() => DoTest(
"#include <foo.h> // comment",
new() { ["foo.h"] = "int x = 0;" });

[Fact]
public Task IncludeNoWhitespaces() => DoTest(@"#include<foo.h>
int test()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int x = 0;

This file was deleted.

5 changes: 2 additions & 3 deletions Cesium.Preprocessor/CPreprocessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using Cesium.Core;
Expand Down Expand Up @@ -486,9 +485,9 @@ IEnumerable<IToken<CPreprocessorTokenType>> ConsumeLineAll()

bool hasRemaining;
while ((hasRemaining = enumerator.MoveNext())
&& enumerator.Current is { Kind: WhiteSpace })
&& enumerator.Current is { Kind: WhiteSpace or Comment })
{
// eat remaining whitespace
// eat remaining whitespace and comments
}

if (hasRemaining && enumerator.Current is var t and not { Kind: WhiteSpace })
Expand Down

0 comments on commit 21b5c2d

Please sign in to comment.