-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da7db49
commit ccda146
Showing
18 changed files
with
556 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
#if !NET8_0_OR_GREATER | ||
using System.Runtime.CompilerServices; | ||
#endif | ||
|
||
namespace Parlot; | ||
|
||
[Flags] | ||
internal enum CharacterMask : byte | ||
{ | ||
None = 0, | ||
IdentifierStart = 1, | ||
IdentifierPart = 2, | ||
WhiteSpace = 4, | ||
WhiteSpaceOrNewLine = 8 | ||
} | ||
|
||
#if !NET8_0_OR_GREATER | ||
public static partial class Character | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsDecimalDigit(char ch) => IsInRange(ch, '0', '9'); | ||
|
||
public static bool IsIdentifierStart(char ch) | ||
{ | ||
return (_characterData[ch] & (byte)CharacterMask.IdentifierStart) != 0; | ||
} | ||
|
||
public static bool IsIdentifierPart(char ch) | ||
{ | ||
return (_characterData[ch] & (byte)CharacterMask.IdentifierPart) != 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsHexDigit(char ch) => HexConverter.IsHexChar(ch); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#if NET8_0_OR_GREATER | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Parlot; | ||
|
||
public static partial class Character | ||
{ | ||
internal const string DecimalDigits = "0123456789"; | ||
internal const string Alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
internal const string AlphaNumeric = Alpha + DecimalDigits; | ||
internal const string DefaultIdentifierStart = "$_" + Alpha; | ||
internal const string DefaultIdentifierPart = "$_" + AlphaNumeric; | ||
internal const string HexDigits = "0123456789abcdefABCDEF"; | ||
internal const string NewLines = "\n\r\v"; | ||
|
||
internal static readonly SearchValues<char> _decimalDigits = SearchValues.Create(DecimalDigits); | ||
internal static readonly SearchValues<char> _hexDigits = SearchValues.Create(HexDigits); | ||
internal static readonly SearchValues<char> _identifierStart = SearchValues.Create(DefaultIdentifierStart); | ||
internal static readonly SearchValues<char> _identifierPart = SearchValues.Create(DefaultIdentifierPart); | ||
internal static readonly SearchValues<char> _newLines = SearchValues.Create(NewLines); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsDecimalDigit(char ch) => _decimalDigits.Contains(ch); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsIdentifierStart(char ch) => _identifierStart.Contains(ch); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsIdentifierPart(char ch) => _identifierPart.Contains(ch); | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool IsHexDigit(char ch) => _hexDigits.Contains(ch); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#if NET8_0_OR_GREATER | ||
using System; | ||
using System.Buffers; | ||
|
||
namespace Parlot.Fluent; | ||
|
||
internal sealed class IdentifierLiteral : Parser<TextSpan> | ||
{ | ||
private readonly SearchValues<char> _startSearchValues; | ||
private readonly SearchValues<char> _partSearchValues; | ||
|
||
public IdentifierLiteral(SearchValues<char> startSearchValues, SearchValues<char> partSearchValues) | ||
{ | ||
_startSearchValues = startSearchValues; | ||
_partSearchValues = partSearchValues; | ||
|
||
// Since we assume these can't container new lines, we can check this here. | ||
if (partSearchValues.Contains('\n') || startSearchValues.Contains('\r')) | ||
{ | ||
throw new InvalidOperationException("Identifiers cannot contain new lines."); | ||
} | ||
} | ||
|
||
public override bool Parse(ParseContext context, ref ParseResult<TextSpan> result) | ||
{ | ||
context.EnterParser(this); | ||
|
||
var span = context.Scanner.Cursor.Span; | ||
|
||
if (span.Length == 0 || !_startSearchValues.Contains(span[0])) | ||
{ | ||
return false; | ||
} | ||
|
||
var index = span.Slice(1).IndexOfAnyExcept(_partSearchValues); | ||
|
||
// If index == -1 the whole input is a match | ||
var size = index == -1 ? span.Length : index + 1; | ||
|
||
var start = context.Scanner.Cursor.Position.Offset; | ||
context.Scanner.Cursor.AdvanceNoNewLines(size); | ||
result.Set(start, start + size, new TextSpan(context.Scanner.Buffer, start, size)); | ||
return true; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.