Skip to content

Commit

Permalink
Add checks for additional numeric literal syntax error edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed Nov 6, 2023
1 parent 9a9f606 commit 87d5048
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 40 deletions.
17 changes: 13 additions & 4 deletions Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,15 +786,24 @@ public static OperandType DetermineOperandType(string operand)
: Regex.Match(operand, @"[^0-9_\.](?<!^-)");
if (invalidMatch.Success)
{
throw new SyntaxError(string.Format(Strings.Assembler_Error_Numeric_Invalid_Character, operand, new string(' ', invalidMatch.Index)));
throw new SyntaxError(string.Format(Strings.Assembler_Error_Literal_Invalid_Character, operand, new string(' ', invalidMatch.Index)));
}
if (operand[0] == '.' && operand.Length == 1)
// Edge-case syntax errors not detected by invalid character regular expressions
if ((operand[0] == '.' && operand.Length == 1) || operand == "-.")
{
throw new SyntaxError(Strings.Assembler_Error_Floating_Point_Decimal_Only);
throw new SyntaxError(Strings.Assembler_Error_Literal_Floating_Point_Decimal_Only);
}
if (operand[0] == '-' && operand.Length == 1)
{
throw new SyntaxError(Strings.Assembler_Error_Literal_Negative_Dash_Only);
}
if (operand.IndexOf('.') != operand.LastIndexOf('.'))
{
throw new SyntaxError(string.Format(Strings.Assembler_Error_Numeric_Too_Many_Points, operand, new string(' ', operand.LastIndexOf('.'))));
throw new SyntaxError(string.Format(Strings.Assembler_Error_Literal_Too_Many_Points, operand, new string(' ', operand.LastIndexOf('.'))));
}
if (operand is "0x" or "0b")
{
throw new SyntaxError(Strings.Assembler_Error_Literal_Base_Prefix_Only);
}
return OperandType.Literal;
}
Expand Down
84 changes: 51 additions & 33 deletions Resources/Localization/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions Resources/Localization/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,16 @@ Consult the language reference for a list of all valid mnemonic/operand combinat
{1}^
Label names may not contain symbols other than underscores, and cannot start with a numeral.</value>
</data>
<data name="Assembler_Error_Numeric_Invalid_Character" xml:space="preserve">
<data name="Assembler_Error_Literal_Invalid_Character" xml:space="preserve">
<value>Invalid character in numeric literal:
{0}
{1}^
Did you forget a '0x' prefix before a hexadecimal number or put a digit other than 1 or 0 in a binary number?</value>
</data>
<data name="Assembler_Error_Floating_Point_Decimal_Only" xml:space="preserve">
<data name="Assembler_Error_Literal_Floating_Point_Decimal_Only" xml:space="preserve">
<value>Floating point numeric literals must contain a digit on at least one side of the decimal point.</value>
</data>
<data name="Assembler_Error_Numeric_Too_Many_Points" xml:space="preserve">
<data name="Assembler_Error_Literal_Too_Many_Points" xml:space="preserve">
<value>Numeric literal contains more than one decimal point:
{0}
{1}^</value>
Expand Down Expand Up @@ -1018,4 +1018,10 @@ Press ENTER to continue, or type a command ('help' for command list): </value>
<data name="Assembler_Error_Label_Empty_Name" xml:space="preserve">
<value>Label names cannot be empty. Did you mean to include a colon here?</value>
</data>
<data name="Assembler_Error_Literal_Negative_Dash_Only" xml:space="preserve">
<value>Negative numeric literals must contain at least one digit.</value>
</data>
<data name="Assembler_Error_Literal_Base_Prefix_Only" xml:space="preserve">
<value>Numeric literals with a base prefix (0x or 0b) must contain at least one digit after the prefix.</value>
</data>
</root>

0 comments on commit 87d5048

Please sign in to comment.