Skip to content

Commit

Permalink
* adding create table stmt test
Browse files Browse the repository at this point in the history
  • Loading branch information
itod committed Mar 27, 2014
1 parent 2cbb7d3 commit 73996dd
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 23 deletions.
31 changes: 27 additions & 4 deletions res/create_table_stmt.grammar
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
createTableStmt = 'CREATE'! tempOpt 'TABLE'! existsOpt databaseName ';';
tempOpt = ('TEMP' | 'TEMPORARY')?;
existsOpt = ('IF' 'NOT' 'EXISTS')?;
databaseName = QuotedString;
createTableStmt = 'CREATE'! tempOpt 'TABLE'! existsOpt databaseName ';'
{
NSString *dbName = POP();
BOOL ifExists = POP_BOOL();
BOOL isTemp = POP_BOOL();
NSLog(@"%@, %d, %d", dbName, ifExists, isTemp);
// go to town
// myCreateTable(dbName, ifExists, isTemp);
};
databaseName = QuotedString
{
NSString *dbName = POP_STR();
// trim quotes
dbName = [dbName substringWithRange:NSMakeRange(1, [dbName length]-2)];
PUSH(dbName);
};

tempOpt
= ('TEMP'! | 'TEMPORARY'!) { PUSH(@YES); }
| Empty { PUSH(@NO); }
;

existsOpt
= ('IF'! 'NOT'! 'EXISTS'!) { PUSH(@YES); }
| Empty { PUSH(@NO); }
;

55 changes: 36 additions & 19 deletions test/CreateTableStmtParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,58 @@ - (void)createTableStmt_ {
[self existsOpt_];
[self databaseName_];
[self match:CREATETABLESTMT_TOKEN_KIND_SEMI_COLON discard:NO];
[self execute:(id)^{

NSString *dbName = POP();
BOOL ifExists = POP_BOOL();
BOOL isTemp = POP_BOOL();
NSLog(@"%@, %d, %d", dbName, ifExists, isTemp);
// go to town
// myCreateTable(dbName, ifExists, isTemp);

}];

[self fireDelegateSelector:@selector(parser:didMatchCreateTableStmt:)];
}

- (void)databaseName_ {

[self matchQuotedString:NO];
[self execute:(id)^{

NSString *dbName = POP_STR();
// trim quotes
dbName = [dbName substringWithRange:NSMakeRange(1, [dbName length]-2)];
PUSH(dbName);

}];

[self fireDelegateSelector:@selector(parser:didMatchDatabaseName:)];
}

- (void)tempOpt_ {

if ([self predicts:CREATETABLESTMT_TOKEN_KIND_TEMP, CREATETABLESTMT_TOKEN_KIND_TEMPORARY, 0]) {
if ([self predicts:CREATETABLESTMT_TOKEN_KIND_TEMP, 0]) {
[self match:CREATETABLESTMT_TOKEN_KIND_TEMP discard:NO];
} else if ([self predicts:CREATETABLESTMT_TOKEN_KIND_TEMPORARY, 0]) {
[self match:CREATETABLESTMT_TOKEN_KIND_TEMPORARY discard:NO];
} else {
[self raise:@"No viable alternative found in rule 'tempOpt'."];
}
if ([self predicts:CREATETABLESTMT_TOKEN_KIND_TEMP, 0]) {
[self match:CREATETABLESTMT_TOKEN_KIND_TEMP discard:YES];
} else if ([self predicts:CREATETABLESTMT_TOKEN_KIND_TEMPORARY, 0]) {
[self match:CREATETABLESTMT_TOKEN_KIND_TEMPORARY discard:YES];
}

[self fireDelegateSelector:@selector(parser:didMatchTempOpt:)];
}

- (void)existsOpt_ {

if ([self speculate:^{ [self match:CREATETABLESTMT_TOKEN_KIND_IF discard:NO]; [self match:CREATETABLESTMT_TOKEN_KIND_NOT_UPPER discard:NO]; [self match:CREATETABLESTMT_TOKEN_KIND_EXISTS discard:NO]; }]) {
[self match:CREATETABLESTMT_TOKEN_KIND_IF discard:NO];
[self match:CREATETABLESTMT_TOKEN_KIND_NOT_UPPER discard:NO];
[self match:CREATETABLESTMT_TOKEN_KIND_EXISTS discard:NO];
if ([self predicts:CREATETABLESTMT_TOKEN_KIND_IF, 0]) {
[self match:CREATETABLESTMT_TOKEN_KIND_IF discard:YES];
[self match:CREATETABLESTMT_TOKEN_KIND_NOT_UPPER discard:YES];
[self match:CREATETABLESTMT_TOKEN_KIND_EXISTS discard:YES];
[self execute:(id)^{
PUSH(@YES);
}];
}

[self fireDelegateSelector:@selector(parser:didMatchExistsOpt:)];
}

- (void)databaseName_ {

[self matchQuotedString:NO];

[self fireDelegateSelector:@selector(parser:didMatchDatabaseName:)];
}

@end

0 comments on commit 73996dd

Please sign in to comment.