internal static bool TryParseNode(Genero4glParser parser, out CreateTableStatement node, IModuleResult containingModule) { node = null; bool result = false; bool temp = false; int tempIndex = -1; if (parser.PeekToken(TokenKind.TempKeyword)) { tempIndex = parser.Token.Span.Start; temp = true; parser.NextToken(); } if (parser.PeekToken(TokenKind.TableKeyword)) { result = true; node = new CreateTableStatement(); node.TempTable = temp; if (tempIndex >= 0) { node.StartIndex = tempIndex; } else { node.StartIndex = parser.Token.Span.Start; } parser.NextToken(); if (parser.PeekToken(TokenKind.IfKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.NotKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.ExistsKeyword)) { parser.NextToken(); } else { parser.ReportSyntaxError("Expecting \"exists\" keyword in create table statement."); } } else { parser.ReportSyntaxError("Expecting \"not\" keyword in create table statement."); } } FglNameExpression nameExpr; if (FglNameExpression.TryParseNode(parser, out nameExpr)) { node.TableName = nameExpr; } else { parser.ReportSyntaxError("Invalid name found for create table statement."); } if (parser.PeekToken(TokenKind.LeftParenthesis)) { parser.NextToken(); CreatedTableColumn tableCol; while (CreatedTableColumn.TryParseNode(parser, out tableCol) && tableCol != null) { node.Children.Add(tableCol.StartIndex, tableCol); if (parser.PeekToken(TokenKind.Comma)) { parser.NextToken(); } else { break; } } if (parser.PeekToken(TokenKind.RightParenthesis)) { parser.NextToken(); } else { parser.ReportSyntaxError("Expected right-paren in create table statement."); } if (parser.PeekToken(TokenKind.WithKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.NoKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.LogKeyword)) { parser.NextToken(); } else { parser.ReportSyntaxError("Expecting \"log\" keyword in create table statement."); } } else { parser.ReportSyntaxError("Expecting \"no\" keyword in create table statement."); } } if (parser.PeekToken(TokenKind.InKeyword)) { parser.NextToken(); if (FglNameExpression.TryParseNode(parser, out nameExpr)) { node.TablespaceName = nameExpr; } else { parser.ReportSyntaxError("Invalid name found for create table statement."); } } if (parser.PeekToken(TokenKind.ExtentKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.SizeKeyword)) { parser.NextToken(); ExpressionNode extSize; if (FglExpressionNode.TryGetExpressionNode(parser, out extSize)) { node.ExtentSize = extSize; } else { parser.ReportSyntaxError("Invalid expression found for extent size in create table statement."); } } else { parser.ReportSyntaxError("Expecting \"size\" keyword in create table statement."); } } if (parser.PeekToken(TokenKind.NextKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.SizeKeyword)) { parser.NextToken(); ExpressionNode extSize; if (FglExpressionNode.TryGetExpressionNode(parser, out extSize)) { node.NextSize = extSize; } else { parser.ReportSyntaxError("Invalid expression found for next size in create table statement."); } } else { parser.ReportSyntaxError("Expecting \"size\" keyword in create table statement."); } } if (parser.PeekToken(TokenKind.LockKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.ModeKeyword)) { parser.NextToken(); switch (parser.PeekToken().Kind) { case TokenKind.PageKeyword: parser.NextToken(); break; case TokenKind.RowKeyword: parser.NextToken(); break; default: parser.ReportSyntaxError("Expecting \"page\" or \"row\" keyword in create table statement."); break; } } else { parser.ReportSyntaxError("Expecting \"mode\" keyword in create table statement."); } } } else { parser.ReportSyntaxError("Expected left-paren in create table statement."); } containingModule.BindTableResult(node, parser); } return(result); }
public static bool TryParseNode(Genero4glParser parser, out CreatedTableColumn node) { node = new CreatedTableColumn(); node.StartIndex = parser.Token.Span.Start; node.UniqueColumns = new List <FglNameExpression>(); node.ReferencingTableColumnNames = new List <FglNameExpression>(); bool result = true; switch (parser.PeekToken().Kind) { case TokenKind.UniqueKeyword: { parser.NextToken(); if (parser.PeekToken(TokenKind.LeftParenthesis)) { parser.NextToken(); FglNameExpression nameExpr; while (FglNameExpression.TryParseNode(parser, out nameExpr)) { node.UniqueColumns.Add(nameExpr); if (parser.PeekToken(TokenKind.Comma)) { parser.NextToken(); } else { break; } } if (parser.PeekToken(TokenKind.RightParenthesis)) { parser.NextToken(); } else { parser.ReportSyntaxError("Expected right-paren in create table statement."); } } else { parser.ReportSyntaxError("Expected left-paren in create table statement."); } break; } case TokenKind.PrimaryKeyword: case TokenKind.CheckKeyword: case TokenKind.ForeignKeyword: result = false; parser.ReportSyntaxError("Token not supported at this time for create table statements."); break; default: { FglNameExpression colName; if (FglNameExpression.TryParseNode(parser, out colName)) { node.ColumnName = colName; } else { parser.ReportSyntaxError("Invalid name found for table column in create table statement."); } TypeReference dataType; if (TypeReference.TryParseNode(parser, out dataType)) { // TODO: should probably ensure that only SQL compatible types are used here... node.DataType = dataType; } else { parser.ReportSyntaxError("Invalid data-type found for table column in create table statement."); } if (parser.PeekToken(TokenKind.DefaultKeyword)) { parser.NextToken(); ExpressionNode defVal; if (FglExpressionNode.TryGetExpressionNode(parser, out defVal)) { node.DefaultValue = defVal; } else { parser.ReportSyntaxError("Invalid expression found for default value in create table statement."); } } if (parser.PeekToken(TokenKind.NotKeyword)) { parser.NextToken(); if (parser.PeekToken(TokenKind.NullKeyword)) { parser.NextToken(); node.NotNull = true; } else { parser.ReportSyntaxError("Expecting \"null\" keyword in create table statement."); } } // TODO: do the other modifiers (primary key, unique, check, references) } break; } return(result); }