Skip to content

Commit

Permalink
Add: Testing operations using parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
mk3008 committed Nov 4, 2024
1 parent f88122b commit 7635fa5
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 140 deletions.
51 changes: 25 additions & 26 deletions src/Carbunql.LexicalAnalyzer/Lexer.Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@ private static bool TryParseCommentStartLex(ReadOnlyMemory<char> memory, ref int
return false;
}

/// <summary>
/// Parses and removes comments until a non-comment Lex is reached, starting from the specified non-comment state.
/// </summary>
/// <param name="memory">The string to be parsed.</param>
/// <param name="previous">The previous Lex indicating a non-comment state, or null if no previous state exists.</param>
/// <returns>An enumeration of Lexes after comments have been removed.</returns>
public static IEnumerable<Lex> ParseUntilNonComment(ReadOnlyMemory<char> memory, Lex? previous = null)
{
// Invalid if the previous Lex is in a comment state
if (previous?.Type == LexType.LineCommentStart
|| previous?.Type == LexType.BlockCommentStart
|| previous?.Type == LexType.HitCommentStart
|| previous?.Type == LexType.Comment)
{
throw new InvalidOperationException("Previous Lex must be in a non-comment state.");
}

// Start position is 0 if previous is null
int position = previous?.EndPosition ?? 0;

return ParseUntilNonComment(memory, position);
}
///// <summary>
///// Parses and removes comments until a non-comment Lex is reached, starting from the specified non-comment state.
///// </summary>
///// <param name="memory">The string to be parsed.</param>
///// <param name="previous">The previous Lex indicating a non-comment state, or null if no previous state exists.</param>
///// <returns>An enumeration of Lexes after comments have been removed.</returns>
//public static IEnumerable<Lex> ParseUntilNonComment(ReadOnlyMemory<char> memory, Lex? previous = null)
//{
// // Invalid if the previous Lex is in a comment state
// if (previous?.Type == LexType.LineCommentStart
// || previous?.Type == LexType.BlockCommentStart
// || previous?.Type == LexType.HitCommentStart
// || previous?.Type == LexType.Comment)
// {
// throw new InvalidOperationException("Previous Lex must be in a non-comment state.");
// }

// // Start position is 0 if previous is null
// int position = previous?.EndPosition ?? 0;

// return ParseUntilNonComment(memory, position);
//}

public static IEnumerable<Lex> ParseUntilNonComment(ReadOnlyMemory<char> memory, int position)
{
Expand Down Expand Up @@ -105,8 +105,7 @@ private static bool TryParseBlockCommentStart(ReadOnlyMemory<char> memory, ref i
{
lex = default;

// Must be at least 4 characters (minimum comment /**/)
if (memory.HasFewerThanChars(position, 4))
if (memory.HasFewerThanChars(position, 2))
{
return false;
}
Expand All @@ -116,7 +115,7 @@ private static bool TryParseBlockCommentStart(ReadOnlyMemory<char> memory, ref i
if (memory.Span[position] == '/' && memory.Span[position + 1] == '*')
{
// Check for /*+
if (memory.Span[position + 2] == '+')
if (memory.HasChar(position + 2) && memory.Span[position + 2] == '+')
{
position += 3;
lex = new Lex(memory, LexType.HitCommentStart, start, position - start);
Expand Down Expand Up @@ -152,7 +151,7 @@ private static Lex ParseLineComment(ReadOnlyMemory<char> memory, ref int positio
var start = position;

// exclude line comment end symbol
while (position < memory.Length)
while (!memory.IsAtEnd(position))
{
char current = memory.Span[position];

Expand Down
18 changes: 18 additions & 0 deletions src/Carbunql.LexicalAnalyzer/Lexer.Paren.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Diagnostics.CodeAnalysis;

namespace Carbunql.LexicalAnalyzer;

public static partial class Lexer
{
[MemberNotNullWhen(true)]
private static bool TryParseLeftParen(ReadOnlyMemory<char> memory, ref int position, out Lex lex)
{
return TryParseSingleCharLex(memory, ref position, '(', LexType.LeftParen, out lex);
}

private static Lex ParseRightParen(ReadOnlyMemory<char> memory, ref int position)
{
if (TryParseSingleCharLex(memory, ref position, ')', LexType.RightParen, out var lex)) return lex;
throw new FormatException($"Expected a closing parenthesis ')' at position {position} in the input string.");
}
}
110 changes: 55 additions & 55 deletions src/Carbunql.LexicalAnalyzer/Lexer.Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,70 +21,70 @@ private static bool TryParseWildCard(ReadOnlyMemory<char> memory, ref int positi
return TryParseSingleCharLex(memory, ref position, '*', LexType.WildCard, out lex);
}

[MemberNotNullWhen(true)]
public static bool TryParseLetter(ReadOnlyMemory<char> memory, ref int position, out Lex lex)
{
lex = default;
//[MemberNotNullWhen(true)]
//public static bool TryParseLetter(ReadOnlyMemory<char> memory, ref int position, out Lex lex)
//{
// lex = default;

if (memory.Length < position + 1)
{
return false;
}
// if (memory.Length < position + 1)
// {
// return false;
// }

int start = position;
// int start = position;

while (position < memory.Length)
{
char current = memory.Span[position];
// while (position < memory.Length)
// {
// char current = memory.Span[position];

if (char.IsLetter(current) || current == '_')
{
position++;
continue;
}
// if (char.IsLetter(current) || current == '_')
// {
// position++;
// continue;
// }

break;
}
// break;
// }

// If no letter were found
if (start == position)
{
return false;
}
// // If no letter were found
// if (start == position)
// {
// return false;
// }

//check next lex
int tempPos = position;
SkipWhiteSpaces(memory, ref tempPos);
// //check next lex
// int tempPos = position;
// SkipWhiteSpaces(memory, ref tempPos);

LexType lexType;
if (tempPos < memory.Length)
{
char nextChar = memory.Span[tempPos];
if (nextChar == '.')
{
lexType = LexType.SchemaOrTableOrColumn;
}
else if (nextChar == ',' || char.IsWhiteSpace(nextChar))
{
lexType = LexType.Column;
}
else if (nextChar == '(')
{
lexType = LexType.Function;
}
else
{
lexType = LexType.Value;
}
}
else
{
lexType = LexType.Value;
}
// LexType lexType;
// if (tempPos < memory.Length)
// {
// char nextChar = memory.Span[tempPos];
// if (nextChar == '.')
// {
// lexType = LexType.SchemaOrTableOrColumn;
// }
// else if (nextChar == ',' || char.IsWhiteSpace(nextChar))
// {
// lexType = LexType.Column;
// }
// else if (nextChar == '(')
// {
// lexType = LexType.Function;
// }
// else
// {
// lexType = LexType.Value;
// }
// }
// else
// {
// lexType = LexType.Value;
// }

lex = new Lex(memory, lexType, start, position - start);
return true;
}
// lex = new Lex(memory, lexType, start, position - start);
// return true;
//}

//[MemberNotNullWhen(true)]
//public static bool TryParseLetterValueLex(ReadOnlyMemory<char> memory, ref int position, out Lex lex)
Expand Down
Loading

0 comments on commit 7635fa5

Please sign in to comment.