Skip to content

Commit

Permalink
Fix #420 - Expression with trailing space (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Apr 29, 2021
1 parent 3e23165 commit f85d478
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build",
"program": "${workspaceFolder}/xFunc.Benchmark/bin/Debug/netcoreapp3.1/xFunc.Benchmark.dll",
"program": "${workspaceFolder}/xFunc.Benchmark/bin/Debug/net5.0/xFunc.Benchmark.dll",
"args": [],
"cwd": "${workspaceFolder}/xFunc.Benchmark",
"console": "internalConsole",
Expand All @@ -17,7 +17,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build",
"program": "${workspaceFolder}/xFunc.DotnetTool/bin/Debug/netcoreapp3.0/xFunc.DotnetTool.dll",
"program": "${workspaceFolder}/xFunc.DotnetTool/bin/Debug/net5.0/xFunc.DotnetTool.dll",
"args": [
"interactive"
],
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Parser.TokenReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ private void EnsureEnoughSpace()
// read from enumerator and write to buffer
if (readIndex == writeIndex)
{
var result = lexer.MoveNext();
if (!result)
var hasNextItem = lexer.MoveNext();
if (!hasNextItem)
IsEnd = true;

if (scopeCount == 0)
Expand Down
4 changes: 2 additions & 2 deletions xFunc.Maths/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public IExpression Parse(string expression)
});

private IExpression? ParseOperand(ref TokenReader tokenReader)
=> ParseComplexNumber(ref tokenReader) ??
=> ParsePolarComplexNumber(ref tokenReader) ??
ParseNumber(ref tokenReader) ??
ParseIf(ref tokenReader) ??
ParseFunctionOrVariable(ref tokenReader) ??
Expand Down Expand Up @@ -755,7 +755,7 @@ private ImmutableArray<IExpression> ParseParameterList(ref TokenReader tokenRead
return new Number(number.NumberValue);
}

private IExpression? ParseComplexNumber(ref TokenReader tokenReader)
private IExpression? ParsePolarComplexNumber(ref TokenReader tokenReader)
=> tokenReader.Scoped(this, static (Parser parser, ref TokenReader reader) =>
{
// plus symbol can be ignored
Expand Down
33 changes: 0 additions & 33 deletions xFunc.Maths/Tokenization/Lexer.WhiteSpace.cs

This file was deleted.

16 changes: 13 additions & 3 deletions xFunc.Maths/Tokenization/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public Lexer(string function)
/// </returns>
public bool MoveNext()
{
while (function.Length > 0)
while (HasSymbols())
{
SkipWhiteSpaces();

var result = CreateNumberToken() ||
CreateIdToken() ||
CreateOperatorToken() ||
Expand All @@ -68,6 +66,18 @@ public bool MoveNext()
return false;
}

private bool HasSymbols()
{
var index = 0;
while (index < function.Length && char.IsWhiteSpace(function[index]))
index++;

if (index > 0)
function = function[index..];

return function.Length > 0;
}

/// <summary>
/// Gets the element in the collection at the current position of the enumerator.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions xFunc.Tests/ParserTests/ParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,5 +705,14 @@ public void ToOctTest()
[Fact]
public void ToHexTest()
=> ParseTest("tohex(10)", new ToHex(new Number(10)));

[Fact]
public void LeadingSpaces()
=> ParseTest(" 1", Number.One);

[Fact]
public void TrailingSpaces()
=> ParseTest("1 ", Number.One);

}
}

0 comments on commit f85d478

Please sign in to comment.