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
71 changes: 68 additions & 3 deletions Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
? this.elements is null
? throw new KeyNotFoundException($"The key {key} was not found in the dictionary")
: this.elements[key]
: this.firstItem!;
: this.firstKey == key
? this.firstItem!
: throw new KeyNotFoundException($"The key {key} was not found in the dictionary");

public IEnumerable<string> Keys => this.firstKey is null ? this.elements!.Keys : new[] { this.firstKey };

Expand All @@ -44,8 +46,27 @@
public int Count => this.firstKey is null ? this.elements is null ? 0 : this.elements.Count : 1;

public bool ContainsKey(string key) => this.firstKey is null ? this.elements is null ? false : this.elements.ContainsKey(key) : this.firstKey == key;
public IEnumerator<KeyValuePair<string, ParseErrorElement>> GetEnumerator() => throw new NotImplementedException();
public bool TryGetValue(string key, out ParseErrorElement value) => throw new NotImplementedException();
public IEnumerator<KeyValuePair<string, ParseErrorElement>> GetEnumerator() =>
this.elements is null
? new Enumerator(this.firstKey!, this.firstItem!)
: this.elements.GetEnumerator();
public bool TryGetValue(string key, out ParseErrorElement value)
{
if (this.elements is null)
{
if (this.firstKey == key)
{
value = firstItem;

Check warning on line 59 in Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs

View workflow job for this annotation

GitHub Actions / Solution (windows-latest)

Possible null reference assignment.

Check warning on line 59 in Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs

View workflow job for this annotation

GitHub Actions / Solution (ubuntu-latest)

Possible null reference assignment.

Check warning on line 59 in Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs

View workflow job for this annotation

GitHub Actions / Solution (macos-latest)

Possible null reference assignment.
return true;
}

value = default;

Check warning on line 63 in Sources/SynKit/Libraries/Parser/ParseErrorElementDictionary.cs

View workflow job for this annotation

GitHub Actions / Solution (ubuntu-latest)

Cannot convert null literal to non-nullable reference type.
return false;
}

return this.elements.TryGetValue(key, out value);
}

IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator();

public ParseErrorElementDictionary Merge(ParseErrorElementDictionary other)
Expand Down Expand Up @@ -91,4 +112,48 @@

return new (elements);
}

private struct Enumerator : IEnumerator<KeyValuePair<string, ParseErrorElement>>
{
private bool valid;
private KeyValuePair<string, ParseErrorElement> _current;
public Enumerator(string key, ParseErrorElement value)
{
this._current = new KeyValuePair<string, ParseErrorElement>(key, value);
}

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

return _current;
}
}

object IEnumerator.Current => this.Current;

public void Dispose() {}
public bool MoveNext()
{
if (!this.valid)
{
this.valid = true;
return true;
}
else
{
this.valid = false;
return false;
}
}
public void Reset()
{
this.valid = false;
}
}
}
5 changes: 3 additions & 2 deletions Sources/SynKit/Tests/Parser.Tests/ExpressionParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ internal partial class Parser
[InlineData(1, "(2 * 3 + 1 << 1 > 1 == 1 & 123 | 22 && 1 || 0) || (2 * 3 + 1 << 1 > 1 == 1 & 123 | 22 && 1 || 0)")]
public void Tests(int value, string text) => Assert.Equal(value, Eval(text));

[Fact(Timeout = 1000, Skip = "In order for this test to pass, BNF desuar should be refactored")]
[Fact]
public async System.Threading.Tasks.Task LongExpressionParsedWithinExpectedTime()
{
await Task.Yield();
var text = "(((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))+((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))) + (((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))))))))+((((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))+(((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))) + ((((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))+(((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))) + ((((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1))) + (((2 + 2) + (1 + 1)) + ((2 + 2) + (1 + 1)))))))))";
Assert.Equal(1536, Eval(text));
var result = Eval(text);
Assert.Equal(1536, result);
}
}
30 changes: 30 additions & 0 deletions Sources/SynKit/Tests/Parser.Tests/ParseErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
// Source repository: https://github.com/LanguageDev/Yoakke

using System.Linq;
using Xunit;

namespace Yoakke.SynKit.Parser.Tests;
Expand Down Expand Up @@ -82,4 +83,33 @@ public void MergeTwoErrorsFromDifferentExpressions()
Assert.Equal(1, result.Elements["other_expression"].Expected.Count);
Assert.True(result.Elements["other_expression"].Expected.Contains("^"));
}

[Fact]
public void EnumerateWhenSingleElement()
{
var firstError = new ParseError("^", null, 12, "expression");

var result = firstError.Elements.Single();

Assert.Equal("expression", result.Key);
Assert.Equal("expression", result.Value.Context);
Assert.True(result.Value.Expected.Contains("^"));
}

[Fact]
public void EnumerateTwoElements()
{
var firstError = new ParseError("^", null, 12, "expression");
var secondError = new ParseError("^", null, 12, "other_expression");
var mergedError = firstError | secondError;

var result = mergedError.Elements.ToList();

Assert.Equal("expression", result[0].Key);
Assert.Equal("expression", result[0].Value.Context);
Assert.True(result[0].Value.Expected.Contains("^"));
Assert.Equal("other_expression", result[1].Key);
Assert.Equal("other_expression", result[1].Value.Context);
Assert.True(result[1].Value.Expected.Contains("^"));
}
}
Loading