Skip to content

Commit

Permalink
warning comment
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwanjacobi committed Feb 21, 2024
1 parent 4eb447d commit 5d34ff0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src2/Maja/Maja.Compiler/Diagnostics/DiagnosticList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Maja.Compiler.Syntax;

namespace Maja.Compiler.Diagnostics;
Expand Down Expand Up @@ -27,6 +28,9 @@ internal void AddRange(IEnumerable<DiagnosticMessage> messages)

public bool HasDiagnostics => _messages.Count > 0;

public bool Has(DiagnosticMessageKind kind)
=> _messages.Any(m => m.MessageKind == kind);

public IEnumerator<DiagnosticMessage> GetEnumerator()
=> _messages.GetEnumerator();

Expand Down
6 changes: 6 additions & 0 deletions src2/Maja/Maja.Compiler/Syntax/SyntaxTreeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public override SyntaxNodeOrToken[] VisitTerminal(ITerminalNode node)
SyntaxToken? token = SyntaxToken.TryNew(node.Symbol.Type, node.GetText(), location);
if (token is SyntaxToken knownToken)
{
if (token is CommentToken commentToken &&
commentToken.Text.StartsWith("##"))
{
Diagnostics.Add(DiagnosticMessageKind.Warning, token.Location, $"Warning Comment: {token.Text}");
}

return new[] { new SyntaxNodeOrToken(knownToken) };
}
return Empty;
Expand Down
2 changes: 1 addition & 1 deletion src2/Maja/Maja.Compiler/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Started. Have ErrorToken with description.
- [ ] Resolve operators to functions (later)
- [ ] local (nested) functions and types
- [ ] Emit: output `System.CodeDom.Compiler.GeneratedCode` and `Maja.AliasAttribute`
- [ ] Diagnostics: Generate compiler warning for `##` comments
- [ ] Allow unnamed parameter(s) together with named parameters (`IrArgumentMatcher`)
- [ ] Allow named type-parameters (`IrArgumentMatcher`)
- [ ] Compile-time code: emit compile-time assembly. Call compile-time custom code.
Expand All @@ -24,6 +23,7 @@ Started. Have ErrorToken with description.

### Done

- [x] Diagnostics: Generate compiler warning for `##` comments
- [x] UnitTests for Eval
- [x] Emit: Transform loop expression to C# compatible for-expression.
- [x] IR: Function Type (IrTypeFunction/FunctionTypeSymbol)
Expand Down
13 changes: 10 additions & 3 deletions src2/Maja/Maja.UnitTests/Compiler/Syntax/BasicSyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Linq;
using Maja.Compiler.Diagnostics;

namespace Maja.UnitTests.Compiler.Syntax;

public class BasicSyntaxTests
Expand Down Expand Up @@ -32,9 +35,13 @@ public void FunctionDeclaration_Comments()
Tokens.Indent1 + "ret #_ nothing" + Tokens.Eol
;

var result = Syntax.Parse(code);
result.Should().NotBeNull();

var result = Syntax.ParseCore(code);
result.Root.Should().NotBeNull();
result.Diagnostics.Should().HaveCount(1);
var msg = result.Diagnostics.First();
msg.MessageKind.Should().Be(DiagnosticMessageKind.Warning);
msg.Text.Should().Contain("Warning");

Syntax.RoundTrip(code, _output);
}

Expand Down
15 changes: 12 additions & 3 deletions src2/Maja/Maja.UnitTests/Compiler/Syntax/Syntax.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using Maja.Compiler.Diagnostics;
using Maja.Compiler.Syntax;

namespace Maja.UnitTests.Compiler.Syntax;

internal static class Syntax
{
public static CompilationUnitSyntax Parse(string code, bool throwOnError = true)
public static CompilationUnitSyntax Parse(string code, bool throwOnError = true, [CallerMemberName] string sourceName = "")
{
var tree = SyntaxTree.Parse(code, "SyntaxTests");
var tree = SyntaxTree.Parse(code, sourceName);

if (throwOnError &&
(tree.Root.HasError || tree.Diagnostics.Any()))
(tree.Root.HasError ||
tree.Diagnostics.Has(DiagnosticMessageKind.Error) || tree.Diagnostics.Has(DiagnosticMessageKind.Critical)))
{
var errTxt = String.Join(Environment.NewLine,
tree.Root.GetErrors().Select(err => err.Text)
Expand All @@ -23,6 +26,12 @@ public static CompilationUnitSyntax Parse(string code, bool throwOnError = true)
return tree.Root;
}

public static SyntaxTree ParseCore(string code, [CallerMemberName] string sourceName = "")
{
var tree = SyntaxTree.Parse(code, sourceName);
return tree;
}

public static string Write(CompilationUnitSyntax root)
{
var writer = new SyntaxWriter();
Expand Down

0 comments on commit 5d34ff0

Please sign in to comment.