Skip to content

Commit

Permalink
Refactor: Reviewed parsing process for SpacialValueWord (e.g., true, …
Browse files Browse the repository at this point in the history
…false, null)

The parsing process for special values like true, false, and null has been reviewed. This improvement ensures that these special values are interpreted correctly, distinguishing them clearly from other values and enabling more accurate parsing.
  • Loading branch information
mk3008 committed Nov 16, 2024
1 parent 6c5cb80 commit 7a4465e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 138 deletions.
1 change: 0 additions & 1 deletion src/Carbunql.LexicalAnalyzer/Lexer.Expression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public static IEnumerable<Lex> ReadExpressionLexes(ReadOnlyMemory<char> memory,
continue;
}


// alias, expression separator, or 'from' keyword
break;
}
Expand Down
123 changes: 24 additions & 99 deletions src/Carbunql.LexicalAnalyzer/Lexer.Value.CharactorValue.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;

namespace Carbunql.LexicalAnalyzer;

public static partial class Lexer
{
/// <summary>
/// Defines a set of characters considered as symbols that terminate an identifier.
/// </summary>
//internal static readonly HashSet<char> Symbols = new HashSet<char>
//{
// '+', '-', '*', '/', '%', // Arithmetic operators
// '(', ')', '[', ']', '{', '}', // Brackets and braces
// '~', '@', '#', '$', '^', '&', // Special symbols
// '!', '?', ':', ';', ',', '.', '<', '>', '=', '|', '\\', // Other symbols
// '`', '"', '\'' // Quotation marks
//};

private static HashSet<string> SpecialValueWords = new HashSet<string>
{
"true",
"false",
"null",

"current_timestamp",
"current_date",
"current_time",
"timestamp",
"now",

"yesterday",
"today",
"tomorrow",

"-infinity",
"infinity",
"allballs",
"epoch",
};

[MemberNotNullWhen(true)]
public static bool TryParseCharactorValue(ReadOnlyMemory<char> memory, int start, out Lex lex, out int endPosition)
Expand All @@ -41,9 +49,9 @@ public static bool TryParseCharactorValue(ReadOnlyMemory<char> memory, int start
// Special word (e.g. true, false, null, timestamp)
foreach (var keyword in SpecialValueWords.Where(x => x.Length == length))
{
if (memory.EqualsWordIgnoreCase(position, keyword, out position))
if (memory.EqualsWordIgnoreCase(start, keyword, out _))
{
lex = new Lex(memory, LexType.Type, start, position - start);
lex = new Lex(memory, LexType.Value, start, position - start);
endPosition = position;
return true;
}
Expand Down Expand Up @@ -83,89 +91,6 @@ public static bool TryParseCharactorValue(ReadOnlyMemory<char> memory, int start
return true;
}


[MemberNotNullWhen(true)]
private static bool TryParseCharactorValues(ReadOnlyMemory<char> memory, int position, out IEnumerable<Lex> lexes)
{
lexes = Enumerable.Empty<Lex>();
var start = position;

if (TryGetCharacterEndPosition(memory, position, out _))
{
lexes = ParseCharactorValues(memory, position);
return true;
}
return false;
}

[MemberNotNullWhen(true)]
private static IEnumerable<Lex> ParseCharactorValues(ReadOnlyMemory<char> memory, int position)
{
var start = position;

if (!TryGetCharacterEndPosition(memory, position, out position))
{
throw new FormatException();
}

// check next charactor
if (memory.IsAtEndOrWhiteSpace(position))
{
if (IsSpacialValueWord(memory, start, position - start))
{
yield return new Lex(memory, LexType.Value, start, position - start);
}

yield return new Lex(memory, LexType.Column, start, position - start);
yield break;
}

// Identifier separator
if (memory.EqualsChar(position, '.', out _))
{
yield return new Lex(memory, LexType.Namespace, start, position - start);

position++;//skip comma
start = position;
while (TryGetCharacterEndPosition(memory, position, out position))
{
if (memory.EqualsChar(position, '.', out _))
{
yield return new Lex(memory, LexType.Namespace, start, position - start);
position++;//skip comma
start = position;
continue;
}
else
{
yield return new Lex(memory, LexType.Column, start, position - start);
yield break;
}
}
throw new FormatException();
}

// left paren
if (memory.EqualsChar(position, '(', out _))
{
yield return new Lex(memory, LexType.Function, start, position - start);
yield break;
}

// mulitiword


// other
if (IsSpacialValueWord(memory, start, position - start))
{
yield return new Lex(memory, LexType.Value, start, position - start);
yield break;
}

yield return new Lex(memory, LexType.Column, start, position - start);
yield break;
}

/// <summary>
/// Attempts to find the end position of a valid identifier starting at a given position in the specified memory segment.
/// </summary>
Expand Down
38 changes: 0 additions & 38 deletions src/Carbunql.LexicalAnalyzer/Lexer.Value.SpecialValue.cs

This file was deleted.

0 comments on commit 7a4465e

Please sign in to comment.