Skip to content

Commit

Permalink
Merge pull request #4 from practicefusion/fixing-parser-bug
Browse files Browse the repository at this point in the history
fix: throw parse exception when parsing improper fraction
  • Loading branch information
JonathanMalek authored May 11, 2022
2 parents 847a7c3 + b1287d0 commit 36f85bc
Show file tree
Hide file tree
Showing 11 changed files with 2,377 additions and 2,354 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,5 @@ MigrationBackup/
# Test data
console/mme.csv
dataconsole/mme.csv

.DS_Store
4,694 changes: 2,351 additions & 2,343 deletions src/Core/PracticeFusion.MmeCalculator.Core/Parsers/Generated/DefaultLexer.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from c:\src\sigparser\src\Grammar\DefaultParser.g4 by ANTLR 4.9.2
// Generated from /Users/jonathanmalek/src/mmecalculator/src/Grammar/DefaultParser.g4 by ANTLR 4.9.2

// Unreachable code detected
#pragma warning disable 0162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from c:\src\sigparser\src\Grammar\DefaultParser.g4 by ANTLR 4.9.2
// Generated from /Users/jonathanmalek/src/mmecalculator/src/Grammar/DefaultParser.g4 by ANTLR 4.9.2

// Unreachable code detected
#pragma warning disable 0162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from c:\src\sigparser\src\Grammar\DefaultParser.g4 by ANTLR 4.9.2
// Generated from /Users/jonathanmalek/src/mmecalculator/src/Grammar/DefaultParser.g4 by ANTLR 4.9.2

// Unreachable code detected
#pragma warning disable 0162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from c:\src\sigparser\src\Grammar\DefaultParser.g4 by ANTLR 4.9.2
// Generated from /Users/jonathanmalek/src/mmecalculator/src/Grammar/DefaultParser.g4 by ANTLR 4.9.2

// Unreachable code detected
#pragma warning disable 0162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// </auto-generated>
//------------------------------------------------------------------------------

// Generated from c:\src\sigparser\src\Grammar\DefaultParser.g4 by ANTLR 4.9.2
// Generated from /Users/jonathanmalek/src/mmecalculator/src/Grammar/DefaultParser.g4 by ANTLR 4.9.2

// Unreachable code detected
#pragma warning disable 0162
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public string Normalize(string inbound)

// replace fractions
normalized = Regex.Replace(normalized, @"(\d) 1(\\|\/)2", "$1.5", RegexOptions.Compiled);
normalized = Regex.Replace(normalized, @"1(\\|\/)2", "0.5", RegexOptions.Compiled);
// not preceded by another digit
normalized = Regex.Replace(normalized, @"(?<!\d)1(\\|\/)2", "0.5", RegexOptions.Compiled);

// replace pairs of numbers "take 1 (1) tablets" -> "take 1 tablets"
normalized = Regex.Replace(normalized, @"(\d+)\s\(\1\)", "$1", RegexOptions.Compiled);
Expand Down
2 changes: 1 addition & 1 deletion src/Grammar/DefaultLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fragment NUMERICCOMMA: ',';
fragment NUMERIC:
DIGIT+ (NUMERICCOMMA DIGIT+)* (DOT DIGIT+)?
| DOT DIGIT+
| DIGIT FORWARDSLASH DIGIT;
| DIGIT+ FORWARDSLASH DIGIT+;

// numbers
HALF: H A L F;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ public void NullContextShouldThrowParseException()
_helper.NullContextShouldThrowParseException();
}

[TestMethod]
public void DoesNotYetParseOneQuarterAsAFraction()
[DataTestMethod]
[DataRow("1/3")] // not 0.5
[DataRow("1/4")]
[DataRow("1/10")]
[DataRow("20/10")] // not improper fractions
[DataRow("11/2")]
public void DoesNotParseFractionsOtherThanOneHalf(string statement)
{
var tree = _helper.DefaultParser("1/4").numericValue();
var tree = _helper.DefaultParser(statement).numericValue();
_helper.Visitor.Invoking(x => x.VisitRoot(tree)).Should().
Throw<ParsingException>().WithMessage("Expected a numeric value").
WithInnerException<ParsingException>().WithMessage("Failed to parse 1/4 as a valid number.");
WithInnerException<ParsingException>().WithMessage("Failed to parse " + statement + " as a valid number.");
}

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ public void NormalizeFractionsGTOne()
{
var result = _stringPreprocessor.Normalize(@"take 3 1/2 tablets");
result.Should().BeEquivalentTo("take 3.5 tablets");
}

[TestMethod]
public void DoNotNormalizeFractionsGTOneWithNoLeadingIntegerSpace()
{
var result = _stringPreprocessor.Normalize(@"take 31/2 tablets");
result.Should().BeEquivalentTo("take 31/2 tablets");
}

[TestMethod]
Expand Down

0 comments on commit 36f85bc

Please sign in to comment.