Allow underscores, dashes, and digits in identifiers

This commit is contained in:
Yoshi Askharoun
2024-02-10 21:37:12 -06:00
parent 3329591b5a
commit 93fa998caa
5 changed files with 28 additions and 12 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ partial class Lexer
return Result.Success(directiveBodyItem, input);
}
var identifierResult = Parse.Letter.AtLeastOnce().Text().Invoke(input);
var identifierResult = Identifier.Invoke(input);
if (!identifierResult.WasSuccessful)
return Result.Failure<IBodyItem>(input, "Invalid code", []);
+3 -3
View File
@@ -12,7 +12,7 @@ partial class Lexer
var line = input.Line;
var column = input.Column;
var directiveBeginResult = Parse.String(".")(input);
var directiveBeginResult = Parse.Char('.')(input);
input = directiveBeginResult.Remainder;
if (!directiveBeginResult.WasSuccessful)
return Result.Failure<IDirective>(input, "Invalid directive", ["Expected '.', followed by an identifier"]);
@@ -20,7 +20,7 @@ partial class Lexer
var directiveIdResult = WordText(input);
input = directiveIdResult.Remainder;
if (!directiveIdResult.WasSuccessful)
return Result.Failure<IDirective>(input, "Invalid directive", ["Expected an identifier"]);
return Result.Failure<IDirective>(input, "Invalid directive", ["Expected a valid directive name"]);
IDirective directive;
switch (directiveIdResult.Value.ToUpperInvariant())
@@ -48,7 +48,7 @@ partial class Lexer
if (StatementEnd(input).WasSuccessful)
return Result.Failure<IImport>(input, "Invalid export directive", ["Expected export information"]);
var labelPrefixResult = AlphanumericText.Token()(input);
var labelPrefixResult = Identifier.Token()(input);
input = labelPrefixResult.Remainder;
if (!labelPrefixResult.WasSuccessful)
return Result.Failure<IImport>(input, "Invalid export directive", ["Expected prefix of labels to export"]);
+20 -7
View File
@@ -48,19 +48,32 @@ partial class Lexer
break;
case "TYPE":
var typePrefixResult = WordText.Token()(input);
var typePrefixResult = Identifier.Token()(input);
input = typePrefixResult.Remainder;
if (!typePrefixResult.WasSuccessful)
return Result.Failure<IImport>(input, "Invalid type import", ["Expected a valid namespace prefix"]);
input = Parse.Char(':')(input).Remainder;
var typeNamespaceDelimitterResult = Parse.Char(':')(input);
input = typeNamespaceDelimitterResult.Remainder;
var typeNameResult = WordText(input);
input = typeNameResult.Remainder;
if (!typeNameResult.WasSuccessful)
return Result.Failure<IImport>(input, "Invalid type import", ["Expected a valid type name"]);
string typeName, typePrefix;
if (typeNamespaceDelimitterResult.WasSuccessful)
{
var typeNameResult = Identifier(input);
input = typeNameResult.Remainder;
if (!typeNameResult.WasSuccessful)
return Result.Failure<IImport>(input, "Invalid type import", ["Expected a valid type name"]);
import = new TypeImport(typePrefixResult.Value, typeNameResult.Value);
typePrefix = typePrefixResult.Value;
typeName = typeNameResult.Value;
}
else
{
typePrefix = null;
typeName = typePrefixResult.Value;
}
import = new TypeImport(typePrefix, typeName);
break;
default:
+2
View File
@@ -12,6 +12,8 @@ public static partial class Lexer
public static readonly Parser<string> WholeNumber = Parse.Digit.AtLeastOnce().Text();
public static readonly Parser<string> Identifier = Parse.LetterOrDigit.Or(Parse.Chars('_', '-')).AtLeastOnce().Text();
public static readonly Parser<string> Uri = Parse.LetterOrDigit.Or(Parse.Chars(":/!._-")).AtLeastOnce().Text();
public static readonly Parser<string> StatementEnd = Parse.Char(';').Return(";").Or(Parse.LineTerminator);