1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
use lang.bs;
use core.lang;
optional delimiter = lang.bs.SDelimiter;
required delimiter = lang.bs.SRequiredDelimiter;
// This file contains the table declarations of the SQL plugin.
// TODO: We should maybe provide "sql name" token to allow for names with spaces in them.
// The top-level table declaration.
SPlainFileItem => DatabaseDecl(name, env, contents) : "DATABASE" #keyword ~ lang.bs.SName name #typeName, "{" [, SDatabase contents, ]+ "}";
// Contents of a database.
Database SDatabase();
SDatabase => Database() : (SDBItem(me),)*;
// An item in a DB declaration.
void SDBItem(Database to);
SDBItem => to : STable -> add;
SDBItem => to : SIndex -> add;
// More here!
// Declaration of an index.
IndexDecl SIndex();
SIndex => IndexDecl(pos, name, table, cols) : "INDEX" #keyword ~ SName name ~ "ON" #keyword ~ SName table, "(", SNameList cols, ")", ";";
SIndex => IndexDecl(pos, table, cols) : "INDEX" #keyword ~ "ON" #keyword ~ SName table, "(", SNameList cols, ")", ";";
// Declaration of a single table.
Table STable();
STable => Table(name) : "TABLE" #keyword ~ SName name, "(" [, STableContent(me), ]+ ")", ";";
// Table content.
void STableContent(Table to);
STableContent => to : STableItem(to) - (, ",", STableItem(to))*;
// Something we can declare inside a table.
void STableItem(Table to);
STableItem => to : SColumn -> add;
STableItem => to : SPKDecl -> add;
// Declaration of a primary key.
Array<SStr> SPKDecl();
SPKDecl => cols : "PRIMARY" #keyword ~ "KEY" #keyword, "(", SNameList cols, ")";
Column SColumn();
// Note: Using SModifiers here is wrong due to whitespace.
SColumn => Column(name, type) : SName name ~ SDatatype type - (~ SModifier(me))*;
// Data types in table declarations.
SQLType SDatatype();
SDatatype => x : SBaseType x;
SDatatype => sized(x, size) : SBaseType x, "(", "[0-9]+" size, ")";
SQLType SBaseType() #typeName;
SBaseType => sqlInteger() : "INTEGER";
SBaseType => sqlInteger() : "INT";
SBaseType => sqlReal() : "REAL";
SBaseType => sqlText() : "TEXT";
// Parse zero or more modifiers.
void SModifiers(Column c);
SModifiers : ;
SModifiers : SModifier(c) - (~ SModifier(c))*;
// Modifiers for columns.
void SModifier(Column to) #keyword;
SModifier => to : "PRIMARY" ~ "KEY" -> setPrimary;
SModifier => to : "ALLOW" ~ "NULL" -> setAllowNull;
SModifier => to : "UNIQUE" -> setUnique;
SModifier => to : "AUTOINCREMENT" -> setAutoIncrement;
SModifier => to : "AUTO_INCREMENT" -> setAutoIncrement;
SModifier => to : "DEFAULT" ~ SLiteral -> setDefault;
SModifier => to : ("NOT" ~ "NULL")@ -> setNotNull;
|