Skip to content

Commit

Permalink
Merge pull request #114 from SeedV/bugfix_112_113
Browse files Browse the repository at this point in the history
Fix issue #112, #113
  • Loading branch information
wixette authored Dec 29, 2021
2 parents 70c0236 + a4ce0eb commit 2945585
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
9 changes: 0 additions & 9 deletions csharp/src/SeedLang/Ast/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ internal static NumberConstantExpression NumberConstant(double value, Range rang
return new NumberConstantExpression(value, range);
}

// The factory method to create a number constant expression from a string.
internal static NumberConstantExpression NumberConstant(string value, Range range) {
try {
return NumberConstant(double.Parse(value), range);
} catch (System.Exception) {
return NumberConstant(0, range);
}
}

// The factory method to create a string constant expression.
internal static StringConstantExpression StringConstant(string value, Range range) {
return new StringConstantExpression(value, range);
Expand Down
2 changes: 1 addition & 1 deletion csharp/src/SeedLang/Runtime/ValueHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ internal static double StringToNumber(string value) {
internal static void CheckOverflow(double value, Range range = null) {
// TODO: do we need separate NaN as another runtime error?
if (double.IsInfinity(value) || double.IsNaN(value)) {
throw new DiagnosticException(SystemReporters.SeedAst, Severity.Error, "", range,
throw new DiagnosticException(SystemReporters.SeedRuntime, Severity.Error, "", range,
Message.RuntimeErrorOverflow);
}
}
Expand Down
10 changes: 8 additions & 2 deletions csharp/src/SeedLang/X/BaseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ internal bool Parse(string source, string module, DiagnosticCollection collectio
node = null;
return false;
}
node = visitor.Visit(program);
return true;
try {
node = visitor.Visit(program);
return true;
} catch (DiagnosticException e) {
collection.Report(e.Diagnostic);
node = null;
return false;
}
}

protected abstract Lexer MakeLexer(ICharStream stream);
Expand Down
3 changes: 2 additions & 1 deletion csharp/src/SeedLang/X/VisitorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ internal NoneConstantExpression BuildNoneConstant(IToken token) {
// Builds a number constant expresssion.
internal NumberConstantExpression BuildNumberConstant(IToken token) {
TextRange range = HandleConstantOrVariableExpression(token, SyntaxType.Number);
return Expression.NumberConstant(token.Text, range);
double value = double.Parse(token.Text);
return Expression.NumberConstant(value, range);
}

// Builds a string constant expresssion.
Expand Down
20 changes: 18 additions & 2 deletions csharp/tests/SeedLang.Tests/X/SeedCalcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,28 @@ public void TestValidateNumber(string input, bool result) {
" [Ln 1, Col 0 - Ln 1, Col 0] NumberConstantExpression (0)",

"Number [Ln 1, Col 0 - Ln 1, Col 0]")]

[InlineData("0\n",

"[Ln 1, Col 0 - Ln 1, Col 0] ExpressionStatement\n" +
" [Ln 1, Col 0 - Ln 1, Col 0] NumberConstantExpression (0)",

"Number [Ln 1, Col 0 - Ln 1, Col 0]")]

[InlineData("0\n\n",

"[Ln 1, Col 0 - Ln 1, Col 0] ExpressionStatement\n" +
" [Ln 1, Col 0 - Ln 1, Col 0] NumberConstantExpression (0)",

"Number [Ln 1, Col 0 - Ln 1, Col 0]")]

[InlineData("0.",

"[Ln 1, Col 0 - Ln 1, Col 1] ExpressionStatement\n" +
" [Ln 1, Col 0 - Ln 1, Col 1] NumberConstantExpression (0)",

"Number [Ln 1, Col 0 - Ln 1, Col 1]")]

[InlineData(".0",

"[Ln 1, Col 0 - Ln 1, Col 1] ExpressionStatement\n" +
Expand Down Expand Up @@ -294,15 +310,15 @@ public void TestSeedCalcParser(string input, string expected, string expectedTok

[InlineData(".3.",
new string[] {
"SyntaxErrorUnwantedToken '.' <EOF>",
"SyntaxErrorUnwantedToken '.' {<EOF>, NEWLINE}",
},

"Number [Ln 1, Col 0 - Ln 1, Col 1]," +
"Unknown [Ln 1, Col 2 - Ln 1, Col 2]")]

[InlineData(".3@",
new string[] {
"SyntaxErrorUnwantedToken '@' <EOF>",
"SyntaxErrorUnwantedToken '@' {<EOF>, NEWLINE}",
},

"Number [Ln 1, Col 0 - Ln 1, Col 1]," +
Expand Down
4 changes: 3 additions & 1 deletion grammars/SeedCalc.g4
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ grammar SeedCalc;
* Parser rules
*/

expressionStatement: expression EOF;
expressionStatement: expression NEWLINE* EOF;

expression: sum;

Expand Down Expand Up @@ -67,6 +67,8 @@ DECIMAL_INTEGER: NON_ZERO_DIGIT DIGIT* | '0'+;

FLOAT_NUMBER: POINT_FLOAT | EXPONENT_FLOAT;

NEWLINE: ('\r'? '\n' | '\r' | '\f') SPACES?;

SKIP_: SPACES -> skip;

UNKNOWN_CHAR: .;
Expand Down

0 comments on commit 2945585

Please sign in to comment.