Skip to content

Commit

Permalink
fix: statement start token maps
Browse files Browse the repository at this point in the history
  • Loading branch information
cvng committed Jan 30, 2024
1 parent 6f8418b commit b1ab67e
Show file tree
Hide file tree
Showing 9 changed files with 568 additions and 130 deletions.
1 change: 1 addition & 0 deletions crates/codegen/src/get_node_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ fn custom_handlers(node: &Node) -> TokenStream {
"CompositeTypeStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
tokens.push(TokenProperty::from(Token::TypeP));
tokens.push(TokenProperty::from(Token::As));
},
"CreatedbStmt" => quote! {
tokens.push(TokenProperty::from(Token::Create));
Expand Down
1 change: 1 addition & 0 deletions crates/parser/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ mod tests {
vec![
TokenProperty::from(SyntaxKind::Create),
TokenProperty::from(SyntaxKind::TypeP),
TokenProperty::from(SyntaxKind::As),
],
)
}
Expand Down
67 changes: 38 additions & 29 deletions crates/parser/src/parse/statement_start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,25 +191,57 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
],
));

// CREATE [ OR REPLACE ] OPERATOR
// CREATE OPERATOR
m.push((
SyntaxKind::DefineStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Optional(SyntaxKind::Or),
SyntaxToken::Optional(SyntaxKind::Replace),
SyntaxToken::Required(SyntaxKind::Operator),
],
));

// CREATE [ OR REPLACE ] TYPE
// CREATE TYPE name
m.push((
SyntaxKind::DefineStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Optional(SyntaxKind::Or),
SyntaxToken::Optional(SyntaxKind::Replace),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
],
));

// CREATE TYPE name AS
m.push((
SyntaxKind::CompositeTypeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
],
));

// CREATE TYPE name AS ENUM
m.push((
SyntaxKind::CreateEnumStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::EnumP),
],
));

// CREATE TYPE name AS RANGE
m.push((
SyntaxKind::CreateRangeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::Range),
],
));

Expand Down Expand Up @@ -622,28 +654,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
],
));

m.push((
SyntaxKind::CreateEnumStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::EnumP),
],
));

m.push((
SyntaxKind::CreateRangeStmt,
&[
SyntaxToken::Required(SyntaxKind::Create),
SyntaxToken::Required(SyntaxKind::TypeP),
SyntaxToken::Required(SyntaxKind::Ident),
SyntaxToken::Required(SyntaxKind::As),
SyntaxToken::Required(SyntaxKind::Range),
],
));

m.push((
SyntaxKind::CreateFdwStmt,
&[
Expand Down Expand Up @@ -956,7 +966,6 @@ pub static STATEMENT_START_TOKEN_MAPS: LazyLock<Vec<HashMap<SyntaxKind, Vec<Toke
// AlterObjectDependsStmt,
// AlterObjectSchemaStmt,
// AlterOwnerStmt,
// CompositeTypeStmt,
// AlterEnumStmt,
// AlterTsdictionaryStmt,
// AlterTsconfigurationStmt,
Expand Down
2 changes: 1 addition & 1 deletion crates/parser/tests/data/statements/valid/0046.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TYPE type1;
CREATE TYPE type1 AS (attr1 int4, attr2 bool);
CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool);
/* TODO: CREATE TYPE type1 AS (attr1 int4 COLLATE collation1, attr2 bool); */ SELECT 1;
CREATE TYPE type1 AS ENUM ('value1', 'value2', 'value3');
CREATE TYPE type1 AS RANGE (subtype = int4);
CREATE TYPE type1 AS RANGE (subtype = int4, receive = receive_func, passedbyvalue);
Expand Down
40 changes: 33 additions & 7 deletions crates/parser/tests/snapshots/statements/valid/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,39 @@ description: CREATE TYPE type1;
---
Parse {
cst: SourceFile@0..18
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Ascii59@17..18 ";"
DefineStmt@0..18
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Ascii59@17..18 ";"
,
errors: [],
stmts: [],
stmts: [
RawStmt {
stmt: DefineStmt(
DefineStmt {
kind: ObjectType,
oldstyle: false,
defnames: [
Node {
node: Some(
String(
String {
sval: "type1",
},
),
),
},
],
args: [],
definition: [],
if_not_exists: false,
replace: false,
},
),
range: 0..17,
},
],
}
161 changes: 141 additions & 20 deletions crates/parser/tests/snapshots/statements/valid/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,147 @@ description: "CREATE TYPE type1 AS (attr1 int4, attr2 bool);"
---
Parse {
cst: SourceFile@0..46
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
Ident@12..17 "type1"
Whitespace@17..18 " "
As@18..20 "AS"
Whitespace@20..21 " "
Ascii40@21..22 "("
Ident@22..27 "attr1"
Whitespace@27..28 " "
Ident@28..32 "int4"
Ascii44@32..33 ","
Whitespace@33..34 " "
Ident@34..39 "attr2"
Whitespace@39..40 " "
Ident@40..44 "bool"
Ascii41@44..45 ")"
Ascii59@45..46 ";"
CompositeTypeStmt@0..46
Create@0..6 "CREATE"
Whitespace@6..7 " "
TypeP@7..11 "TYPE"
Whitespace@11..12 " "
RangeVar@12..17
Ident@12..17 "type1"
Whitespace@17..18 " "
As@18..20 "AS"
Whitespace@20..21 " "
Ascii40@21..22 "("
ColumnDef@22..32
Ident@22..27 "attr1"
Whitespace@27..28 " "
TypeName@28..32
Ident@28..32 "int4"
Ascii44@32..33 ","
Whitespace@33..34 " "
ColumnDef@34..44
Ident@34..39 "attr2"
Whitespace@39..40 " "
TypeName@40..44
Ident@40..44 "bool"
Ascii41@44..45 ")"
Ascii59@45..46 ";"
,
errors: [],
stmts: [],
stmts: [
RawStmt {
stmt: CompositeTypeStmt(
CompositeTypeStmt {
typevar: Some(
RangeVar {
catalogname: "",
schemaname: "",
relname: "type1",
inh: false,
relpersistence: "p",
alias: None,
location: 12,
},
),
coldeflist: [
Node {
node: Some(
ColumnDef(
ColumnDef {
colname: "attr1",
type_name: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "int4",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 28,
},
),
compression: "",
inhcount: 0,
is_local: true,
is_not_null: false,
is_from_type: false,
storage: "",
raw_default: None,
cooked_default: None,
identity: "",
identity_sequence: None,
generated: "",
coll_clause: None,
coll_oid: 0,
constraints: [],
fdwoptions: [],
location: 22,
},
),
),
},
Node {
node: Some(
ColumnDef(
ColumnDef {
colname: "attr2",
type_name: Some(
TypeName {
names: [
Node {
node: Some(
String(
String {
sval: "bool",
},
),
),
},
],
type_oid: 0,
setof: false,
pct_type: false,
typmods: [],
typemod: -1,
array_bounds: [],
location: 40,
},
),
compression: "",
inhcount: 0,
is_local: true,
is_not_null: false,
is_from_type: false,
storage: "",
raw_default: None,
cooked_default: None,
identity: "",
identity_sequence: None,
generated: "",
coll_clause: None,
coll_oid: 0,
constraints: [],
fdwoptions: [],
location: 34,
},
),
),
},
],
},
),
range: 0..45,
},
],
}
Loading

0 comments on commit b1ab67e

Please sign in to comment.