Skip to content

Commit

Permalink
Continued work on expression parsing, added operator expression parsi…
Browse files Browse the repository at this point in the history
…ng and argument list parsing. #6
  • Loading branch information
bruce-dunwiddie committed Nov 1, 2020
1 parent ef0be6c commit 9886117
Show file tree
Hide file tree
Showing 27 changed files with 1,201 additions and 372 deletions.
49 changes: 43 additions & 6 deletions TSQL_Parser/TSQL_Parser/Clauses/Parsers/TSQLSelectClauseParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Text;

using TSQL.Elements;
using TSQL.Elements.Parsers;
using TSQL.Statements;
using TSQL.Tokens;

Expand All @@ -21,16 +23,12 @@ public TSQLSelectClause Parse(ITSQLTokenizer tokenizer)

select.Tokens.Add(tokenizer.Current);

// can contain ALL, DISTINCT, TOP, PERCENT, WITH TIES, AS

// ends with FROM, semicolon, or keyword other than those listed above, when used outside of parens

// recursively walk down and back up parens
// need to part TOP, DISTINCT, etc here

TSQLSubqueryHelper.ReadUntilStop(
tokenizer,
select,
new List<TSQLFutureKeywords>() {},
new List<TSQLFutureKeywords>() { },
new List<TSQLKeywords>() {
TSQLKeywords.INTO,
TSQLKeywords.FROM,
Expand All @@ -46,6 +44,45 @@ public TSQLSelectClause Parse(ITSQLTokenizer tokenizer)
},
lookForStatementStarts: true);

// TODO: switch logic to use below once expression parsers are fully functional

//while (tokenizer.MoveNext() &&
// !tokenizer.Current.IsCharacter(TSQLCharacters.Semicolon) &&
// !(
// tokenizer.Current.Type == TSQLTokenType.Keyword &&
// (
// tokenizer.Current.AsKeyword.Keyword.In(
// TSQLKeywords.INTO,
// TSQLKeywords.FROM,
// TSQLKeywords.WHERE,
// TSQLKeywords.GROUP,
// TSQLKeywords.HAVING,
// TSQLKeywords.ORDER,
// TSQLKeywords.UNION,
// TSQLKeywords.EXCEPT,
// TSQLKeywords.INTERSECT,
// TSQLKeywords.FOR,
// TSQLKeywords.OPTION) ||
// tokenizer.Current.AsKeyword.Keyword.IsStatementStart()
// )
// ))
//{
// if (tokenizer.Current.IsWhitespace() ||
// tokenizer.Current.IsComment() ||
// tokenizer.Current.IsCharacter(TSQLCharacters.Comma))
// {
// select.Tokens.Add(tokenizer.Current);
// }
// else
// {
// TSQLSelectColumn column = new TSQLSelectColumnParser().Parse(tokenizer);

// select.Tokens.AddRange(column.Tokens);

// select.Columns.Add(column);
// }
//};

return select;
}
}
Expand Down
8 changes: 1 addition & 7 deletions TSQL_Parser/TSQL_Parser/Clauses/TSQLSelectClause.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ internal TSQLSelectClause()

}

public List<TSQLSelectColumn> Columns
{
get
{
return null;
}
}
public List<TSQLSelectColumn> Columns { get; } = new List<TSQLSelectColumn>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,53 @@
using System.Text;
using System.Threading.Tasks;

using TSQL.Expressions;
using TSQL.Expressions.Parsers;
using TSQL.Tokens;

namespace TSQL.Elements.Parsers
{
internal class TSQLSelectColumnParser
{
public TSQLSelectColumn Parse(ITSQLTokenizer tokenizer)
{
return null;
TSQLSelectColumn column = new TSQLSelectColumn();

TSQLExpression columnExpression = new TSQLExpressionFactory().Parse(tokenizer);

column.Expression = columnExpression;

column.Tokens.AddRange(columnExpression.Tokens);

if (tokenizer.Current.IsKeyword(TSQLKeywords.AS))
{
column.Tokens.Add(tokenizer.Current);

tokenizer.MoveNext();
}

while (
tokenizer.Current.IsWhitespace() ||
tokenizer.Current.IsComment())
{
column.Tokens.Add(tokenizer.Current);

tokenizer.MoveNext();
}

if (tokenizer.Current != null &&
tokenizer.Current.Type.In(
TSQLTokenType.Identifier,
TSQLTokenType.IncompleteIdentifier))
{
column.Tokens.Add(tokenizer.Current);

column.Alias = tokenizer.Current;
}

tokenizer.MoveNext();

return column;
}
}
}
17 changes: 3 additions & 14 deletions TSQL_Parser/TSQL_Parser/Elements/TSQLSelectColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,14 @@
using System.Threading.Tasks;

using TSQL.Expressions;
using TSQL.Tokens;

namespace TSQL.Elements
{
public class TSQLSelectColumn : TSQLElement
{
public string Alias
{
get
{
return null;
}
}
public TSQLToken Alias { get; internal set; }

public TSQLExpression Expression
{
get
{
return null;
}
}
public TSQLExpression Expression { get; internal set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using TSQL.Tokens;

namespace TSQL.Expressions.Parsers
{
internal class TSQLArgumentListParser
{
public TSQLArgumentList Parse(ITSQLTokenizer tokenizer)
{
List<TSQLExpression> arguments = new List<TSQLExpression>();

TSQLExpressionFactory factory = new TSQLExpressionFactory();

List<TSQLToken> tokens = new List<TSQLToken>();

// need to do this before starting the argument loop
// so we can handle an empty argument list of just whitespace
// and comments
while (
tokenizer.Current != null &&
(
tokenizer.Current.IsComment() ||
tokenizer.Current.IsWhitespace()
))
{
tokens.Add(tokenizer.Current);

tokenizer.MoveNext();
}

while (
tokenizer.Current != null &&
!tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses))
{
while (
tokenizer.Current != null &&
(
tokenizer.Current.IsComment() ||
tokenizer.Current.IsWhitespace()
))
{
tokens.Add(tokenizer.Current);

tokenizer.MoveNext();
}

TSQLExpression argument = factory.Parse(tokenizer);

tokens.AddRange(argument.Tokens);

arguments.Add(argument);

if (tokenizer.Current.IsCharacter(TSQLCharacters.Comma))
{
tokens.Add(tokenizer.Current);
}

if (
tokenizer.Current != null &&
!tokenizer.Current.IsCharacter(TSQLCharacters.CloseParentheses))
{
tokenizer.MoveNext();
}
}

TSQLArgumentList argList = new TSQLArgumentList(
arguments);

argList.Tokens.AddRange(tokens);

return argList;
}
}
}
Loading

0 comments on commit 9886117

Please sign in to comment.