Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/Props/Shared.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<!-- Language-level settings -->
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);NU5128</NoWarn>

<!-- Analysis -->
<AnalysisLevel>latest</AnalysisLevel>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Props/SourceGenerator.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- Source Generator project settings -->
<Project>
<Import Project="Shared.Build.props" />
<!--<Import Project="Shared.Build.props" />-->

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down
2 changes: 1 addition & 1 deletion Sources/SynKit/Libraries/C.Syntax/CToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public CToken(Text.Location range, string text, Text.Range logicalRange, string
public bool Equals(CToken? other) =>
other is not null
&& this.Range == other.Range
&& this.Location.File.Path == other.Location.File.Path
&& this.Location.File?.Path == other.Location.File?.Path
&& this.Text == other.Text
&& this.Kind == other.Kind
&& this.LogicalRange == other.LogicalRange
Expand Down
3 changes: 3 additions & 0 deletions Sources/SynKit/Libraries/Lexer/ICharStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ namespace Yoakke.SynKit.Lexer;
/// </summary>
public interface ICharStream : IPeekableStream<char>
{
/// <summary>
/// Gets the source file which provide content of this char stream
/// </summary>
public ISourceFile SourceFile { get; }

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions Sources/SynKit/Libraries/Lexer/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public sealed record Token<TKind>(Range Range, Location Location, string Text, T

/// <inheritdoc/>
public bool Equals(IToken<TKind>? other) => this.Equals(other as Token<TKind>);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
public bool Equals(Token<TKind>? other) =>
other is not null
&& this.Range == other.Range
Expand Down
5 changes: 3 additions & 2 deletions Sources/SynKit/Libraries/Parser/ParseError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Yoakke.SynKit.Parser;

Expand All @@ -12,7 +13,7 @@ namespace Yoakke.SynKit.Parser;
/// </summary>
public class ParseError
{
private ParseErrorElementDictionary elements;
private readonly ParseErrorElementDictionary elements;

/// <summary>
/// The error cases in different parse contexts.
Expand Down Expand Up @@ -69,7 +70,7 @@ private ParseError(ParseErrorElementDictionary elements, object? got, IComparabl
// TODO: Think this through
// NOTE: Could it ever happen that first.Got and second.Got are different but neither are null?
// Would we want to unify these and move them to ParseErrorElement or something?
// Since position is here now, Got is kind of a utility we have here, we could just aswell have a
// Since position is here now, Got is kind of a utility we have here, we could just as well have a
// 'reason' for each element
return new(
elements,
Expand Down
40 changes: 20 additions & 20 deletions Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ namespace Yoakke.SynKit.Parser;

internal class ParseErrorElementDictionary : IReadOnlyDictionary<string, ParseErrorElement>
{
private string? firstKey;
private ParseErrorElement? firstItem;
private string? secondKey;
private ParseErrorElement? secondItem;
private Dictionary<string, ParseErrorElement>? elements;
private readonly string? firstKey;
private readonly ParseErrorElement? firstItem;
private readonly string? secondKey;
private readonly ParseErrorElement? secondItem;
private readonly Dictionary<string, ParseErrorElement>? elements;

private ParseErrorElementDictionary()
{
Expand Down Expand Up @@ -77,32 +77,32 @@ public bool ContainsKey(string key)
}
else
{
return this.firstKey == key || (this.secondKey is null ? false : this.firstKey == key);
return this.firstKey == key || (this.secondKey is null ? false : this.secondKey == key);
}
}

public IEnumerator<KeyValuePair<string, ParseErrorElement>> GetEnumerator() =>
this.elements is null
? (secondKey is null)
? new Enumerator(this.firstKey!, this.firstItem!) : new TwoElementEnumerator(this.firstKey!, this.firstItem!, this.secondKey, this.secondItem)
? (this.secondKey is null)
? new Enumerator(this.firstKey!, this.firstItem!) : new TwoElementEnumerator(this.firstKey!, this.firstItem!, this.secondKey, this.secondItem!)
: this.elements.GetEnumerator();
public bool TryGetValue(string key, out ParseErrorElement value)
{
if (this.elements is null)
{
if (this.firstKey == key)
{
value = firstItem;
value = this.firstItem!;
return true;
}

if (this.secondKey == key)
{
value = secondItem;
value = this.secondItem!;
return true;
}

value = default;
value = default!;
return false;
}

Expand Down Expand Up @@ -165,22 +165,22 @@ public Enumerator(string key, ParseErrorElement value)
this._current = new KeyValuePair<string, ParseErrorElement>(key, value);
}

public KeyValuePair<string, ParseErrorElement> Current
public readonly KeyValuePair<string, ParseErrorElement> Current
{
get
{
if (!valid)
if (!this.valid)
{
throw new InvalidOperationException("The enumerator is not valid.");
}

return _current;
return this._current;
}
}

object IEnumerator.Current => this.Current;
readonly object IEnumerator.Current => this.Current;

public void Dispose() {}
public readonly void Dispose() {}
public bool MoveNext()
{
if (!this.valid)
Expand Down Expand Up @@ -211,22 +211,22 @@ public TwoElementEnumerator(string firstKey, ParseErrorElement firstValue, strin
this._secondPair = new KeyValuePair<string, ParseErrorElement>(secondKey, secondValue);
}

public KeyValuePair<string, ParseErrorElement> Current
public readonly KeyValuePair<string, ParseErrorElement> Current
{
get
{
if (consumed > 2)
if (this.consumed > 2)
{
throw new InvalidOperationException("The enumerator is not valid.");
}

return consumed == 1 ? _firstPair : _secondPair;
return this.consumed == 1 ? this._firstPair : this._secondPair;
}
}

object IEnumerator.Current => this.Current;

public void Dispose() { }
public readonly void Dispose() { }
public bool MoveNext()
{
if (this.consumed >= 2)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SynKit/Libraries/Parser/ParseErrorExpectationSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public object Current
{
get
{
if (!valid)
if (!this.valid)
{
throw new InvalidOperationException("The enumerator is not valid.");
}

return _current ?? throw new InvalidOperationException("The enumerator is not valid.");
return this._current ?? throw new InvalidOperationException("The enumerator is not valid.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SynKit/Libraries/Parser/ParseResult.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class ParseResult
/// </summary>
/// <param name="expected">The expected element.</param>
/// <param name="got">The token encountered instead.</param>
/// <param name="position">The position where the error occured.</param>
/// <param name="position">The position where the error occurred.</param>
/// <param name="context">The rule context the error occurred in.</param>
/// <returns>The created <see cref="ParseError"/>.</returns>
public static ParseError Error(object expected, object? got, IComparable position, string context) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void WriteSourceGroup(IEnumerable<SourceDiagnosticInfo> infos)

// Print the ┌─ <file name>
this.buffer.ForegroundColor = this.Style.DefaultColor;
this.buffer.Write($"{lineNumberPadding} ┌─ {sourceFile.Path}");
this.buffer.Write($"{lineNumberPadding} ┌─ {sourceFile?.Path ?? "(unknown)"}");
// If there is a primary info, write the line and column
if (primaryInfo != null) this.buffer.Write($":{primaryInfo.Location.Range.Start.Line + 1}:{primaryInfo.Location.Range.Start.Column + 1}");
this.buffer.WriteLine();
Expand Down Expand Up @@ -225,7 +225,7 @@ private void WriteSourceLine(SourceLine line)
private void WriteAnnotationLine(AnnotationLine line, string prefix)
{
var sourceFile = line.Annotations!.First().Location.File;
var lineText = sourceFile.GetLine(line.AnnotatedLine).TrimEnd();
var lineText = sourceFile?.GetLine(line.AnnotatedLine).TrimEnd() ?? "(unknown)";

// Order annotations by starting position
var annotationsOrdered = line.Annotations!.OrderBy(si => si.Location.Range.Start).ToList();
Expand Down Expand Up @@ -316,7 +316,7 @@ private IEnumerable<LinePrimitive> CollectLinePrimitives(IEnumerable<SourceDiagn
{
// We need to group the spanned informations per line
var groupedInfos = infos.GroupBy(si => si.Location.Range.Start.Line).ToList();
var sourceFile = infos.First().Location.File;
var sourceFile = infos.First().Location.File!;

// Now we collect each line primitive
int? lastLineIndex = null;
Expand Down
4 changes: 2 additions & 2 deletions Sources/SynKit/Tests/Parser.Tests/ParseErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void EnumerateTwoElements()
var secondError = new ParseError("^", null, 12, "other_expression");
var mergedError = firstError | secondError;

var result = mergedError.Elements.ToList();
var result = mergedError!.Elements.ToList();

Assert.Equal("expression", result[0].Key);
Assert.Equal("expression", result[0].Value.Context);
Expand All @@ -121,7 +121,7 @@ public void EnumerateThreeElements()
var thirdError = new ParseError("^", null, 12, "third_expression");
var mergedError = firstError | secondError | thirdError;

var result = mergedError.Elements.ToList();
var result = mergedError!.Elements.ToList();

Assert.Equal("expression", result[0].Key);
Assert.Equal("expression", result[0].Value.Context);
Expand Down
Loading