Skip to content

Commit 0bb025f

Browse files
committed
progress
1 parent 6b1dd48 commit 0bb025f

18 files changed

+465
-2734
lines changed

agentic/pretty_printer.md

Lines changed: 145 additions & 2442 deletions
Large diffs are not rendered by default.

crates/pgt_pretty_print/src/nodes/alter_enum_stmt.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use crate::emitter::{EventEmitter, GroupKind};
33
use pgt_query::protobuf::AlterEnumStmt;
44

55
use super::node_list::emit_dot_separated_list;
6+
use super::string::{emit_keyword, emit_single_quoted_str};
67

78
pub(super) fn emit_alter_enum_stmt(e: &mut EventEmitter, n: &AlterEnumStmt) {
89
e.group_start(GroupKind::AlterEnumStmt);
910

1011
e.token(TokenKind::ALTER_KW);
1112
e.space();
12-
e.token(TokenKind::IDENT("TYPE".to_string()));
13+
emit_keyword(e, "TYPE");
1314
e.space();
1415

1516
// Enum type name (qualified)
@@ -22,20 +23,20 @@ pub(super) fn emit_alter_enum_stmt(e: &mut EventEmitter, n: &AlterEnumStmt) {
2223
// Check if this is ADD VALUE or RENAME VALUE
2324
if !n.old_val.is_empty() {
2425
// RENAME VALUE old TO new
25-
e.token(TokenKind::IDENT("RENAME".to_string()));
26+
emit_keyword(e, "RENAME");
2627
e.space();
27-
e.token(TokenKind::IDENT("VALUE".to_string()));
28+
emit_keyword(e, "VALUE");
2829
e.space();
29-
e.token(TokenKind::IDENT(format!("'{}'", n.old_val)));
30+
emit_single_quoted_str(e, &n.old_val);
3031
e.space();
3132
e.token(TokenKind::TO_KW);
3233
e.space();
33-
e.token(TokenKind::IDENT(format!("'{}'", n.new_val)));
34+
emit_single_quoted_str(e, &n.new_val);
3435
} else {
3536
// ADD VALUE [ IF NOT EXISTS ] new_value [ BEFORE old_value | AFTER old_value ]
3637
e.token(TokenKind::ADD_KW);
3738
e.space();
38-
e.token(TokenKind::IDENT("VALUE".to_string()));
39+
emit_keyword(e, "VALUE");
3940

4041
if n.skip_if_new_val_exists {
4142
e.space();
@@ -48,19 +49,19 @@ pub(super) fn emit_alter_enum_stmt(e: &mut EventEmitter, n: &AlterEnumStmt) {
4849

4950
if !n.new_val.is_empty() {
5051
e.space();
51-
e.token(TokenKind::IDENT(format!("'{}'", n.new_val)));
52+
emit_single_quoted_str(e, &n.new_val);
5253
}
5354

5455
// Optional BEFORE/AFTER clause
5556
if !n.new_val_neighbor.is_empty() {
5657
e.space();
5758
if n.new_val_is_after {
58-
e.token(TokenKind::IDENT("AFTER".to_string()));
59+
emit_keyword(e, "AFTER");
5960
} else {
60-
e.token(TokenKind::IDENT("BEFORE".to_string()));
61+
emit_keyword(e, "BEFORE");
6162
}
6263
e.space();
63-
e.token(TokenKind::IDENT(format!("'{}'", n.new_val_neighbor)));
64+
emit_single_quoted_str(e, &n.new_val_neighbor);
6465
}
6566
}
6667

crates/pgt_pretty_print/src/nodes/alter_foreign_server_stmt.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,36 @@ use crate::TokenKind;
22
use crate::emitter::{EventEmitter, GroupKind, LineType};
33
use pgt_query::protobuf::AlterForeignServerStmt;
44

5-
use super::node_list::emit_comma_separated_list;
5+
use super::{
6+
node_list::emit_comma_separated_list,
7+
string::{emit_identifier_maybe_quoted, emit_keyword, emit_single_quoted_str},
8+
};
69

710
pub(super) fn emit_alter_foreign_server_stmt(e: &mut EventEmitter, n: &AlterForeignServerStmt) {
811
e.group_start(GroupKind::AlterForeignServerStmt);
912

1013
e.token(TokenKind::ALTER_KW);
1114
e.space();
12-
e.token(TokenKind::IDENT("SERVER".to_string()));
15+
emit_keyword(e, "SERVER");
1316
e.space();
1417

1518
if !n.servername.is_empty() {
16-
e.token(TokenKind::IDENT(n.servername.clone()));
19+
emit_identifier_maybe_quoted(e, &n.servername);
1720
}
1821

1922
if n.has_version && !n.version.is_empty() {
2023
e.line(LineType::SoftOrSpace);
2124
e.indent_start();
22-
e.token(TokenKind::IDENT("VERSION".to_string()));
25+
emit_keyword(e, "VERSION");
2326
e.space();
24-
e.token(TokenKind::IDENT(format!("'{}'", n.version)));
27+
emit_single_quoted_str(e, &n.version);
2528
e.indent_end();
2629
}
2730

2831
if !n.options.is_empty() {
2932
e.line(LineType::SoftOrSpace);
3033
e.indent_start();
31-
e.token(TokenKind::IDENT("OPTIONS".to_string()));
34+
emit_keyword(e, "OPTIONS");
3235
e.space();
3336
e.token(TokenKind::L_PAREN);
3437
emit_comma_separated_list(e, &n.options, |n, e| {

crates/pgt_pretty_print/src/nodes/alter_subscription_stmt.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::node_list::emit_comma_separated_list;
1+
use super::{
2+
node_list::emit_comma_separated_list,
3+
string::{emit_identifier_maybe_quoted, emit_keyword, emit_single_quoted_str},
4+
};
25
use crate::{
36
TokenKind,
47
emitter::{EventEmitter, GroupKind},
@@ -10,9 +13,9 @@ pub(super) fn emit_alter_subscription_stmt(e: &mut EventEmitter, n: &AlterSubscr
1013

1114
e.token(TokenKind::ALTER_KW);
1215
e.space();
13-
e.token(TokenKind::IDENT("SUBSCRIPTION".to_string()));
16+
emit_keyword(e, "SUBSCRIPTION");
1417
e.space();
15-
e.token(TokenKind::IDENT(n.subname.clone()));
18+
emit_identifier_maybe_quoted(e, &n.subname);
1619

1720
e.space();
1821

@@ -22,41 +25,41 @@ pub(super) fn emit_alter_subscription_stmt(e: &mut EventEmitter, n: &AlterSubscr
2225
// OPTIONS - handled via options field below
2326
}
2427
2 => {
25-
e.token(TokenKind::IDENT("CONNECTION".to_string()));
28+
emit_keyword(e, "CONNECTION");
2629
e.space();
27-
e.token(TokenKind::IDENT(format!("'{}'", n.conninfo)));
30+
emit_single_quoted_str(e, &n.conninfo);
2831
}
2932
3 => {
3033
e.token(TokenKind::SET_KW);
3134
e.space();
32-
e.token(TokenKind::IDENT("PUBLICATION".to_string()));
35+
emit_keyword(e, "PUBLICATION");
3336
e.space();
3437
emit_comma_separated_list(e, &n.publication, super::emit_node);
3538
}
3639
4 => {
37-
e.token(TokenKind::IDENT("ADD".to_string()));
40+
emit_keyword(e, "ADD");
3841
e.space();
39-
e.token(TokenKind::IDENT("PUBLICATION".to_string()));
42+
emit_keyword(e, "PUBLICATION");
4043
e.space();
4144
emit_comma_separated_list(e, &n.publication, super::emit_node);
4245
}
4346
5 => {
4447
e.token(TokenKind::DROP_KW);
4548
e.space();
46-
e.token(TokenKind::IDENT("PUBLICATION".to_string()));
49+
emit_keyword(e, "PUBLICATION");
4750
e.space();
4851
emit_comma_separated_list(e, &n.publication, super::emit_node);
4952
}
5053
6 => {
51-
e.token(TokenKind::IDENT("REFRESH".to_string()));
54+
emit_keyword(e, "REFRESH");
5255
e.space();
53-
e.token(TokenKind::IDENT("PUBLICATION".to_string()));
56+
emit_keyword(e, "PUBLICATION");
5457
}
5558
7 => {
56-
e.token(TokenKind::IDENT("ENABLE".to_string()));
59+
emit_keyword(e, "ENABLE");
5760
}
5861
8 => {
59-
e.token(TokenKind::IDENT("SKIP".to_string()));
62+
emit_keyword(e, "SKIP");
6063
}
6164
_ => {}
6265
}

crates/pgt_pretty_print/src/nodes/comment_stmt.rs

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,70 @@ use crate::{
44
};
55
use pgt_query::protobuf::CommentStmt;
66

7+
use super::string::{emit_keyword, emit_single_quoted_str};
8+
79
pub(super) fn emit_comment_stmt(e: &mut EventEmitter, n: &CommentStmt) {
810
e.group_start(GroupKind::CommentStmt);
911

10-
e.token(TokenKind::IDENT("COMMENT".to_string()));
12+
emit_keyword(e, "COMMENT");
1113
e.space();
1214
e.token(TokenKind::ON_KW);
1315
e.space();
1416

1517
// Object type - map ObjectType enum to keyword
16-
let object_type_str = match n.objtype {
17-
1 => "ACCESS METHOD", // ObjectAccessMethod
18-
2 => "AGGREGATE", // ObjectAggregate
19-
6 => "CAST", // ObjectCast
20-
7 => "COLUMN", // ObjectColumn
21-
8 => "COLLATION", // ObjectCollation
22-
9 => "CONVERSION", // ObjectConversion
23-
10 => "DATABASE", // ObjectDatabase
24-
13 => "DOMAIN", // ObjectDomain
25-
14 => "CONSTRAINT", // ObjectDomconstraint
26-
15 => "EVENT TRIGGER", // ObjectEventTrigger
27-
16 => "EXTENSION", // ObjectExtension
28-
17 => "FOREIGN DATA WRAPPER", // ObjectFdw
29-
18 => "SERVER", // ObjectForeignServer
30-
19 => "FOREIGN TABLE", // ObjectForeignTable
31-
20 => "FUNCTION", // ObjectFunction
32-
21 => "INDEX", // ObjectIndex
33-
22 => "LANGUAGE", // ObjectLanguage
34-
23 => "LARGE OBJECT", // ObjectLargeobject
35-
24 => "MATERIALIZED VIEW", // ObjectMatview
36-
25 => "OPERATOR CLASS", // ObjectOpclass
37-
26 => "OPERATOR", // ObjectOperator
38-
27 => "OPERATOR FAMILY", // ObjectOpfamily
39-
29 => "POLICY", // ObjectPolicy
40-
30 => "PROCEDURE", // ObjectProcedure
41-
31 => "PUBLICATION", // ObjectPublication
42-
34 => "ROLE", // ObjectRole
43-
35 => "ROUTINE", // ObjectRoutine
44-
36 => "RULE", // ObjectRule
45-
37 => "SCHEMA", // ObjectSchema
46-
38 => "SEQUENCE", // ObjectSequence
47-
39 => "SUBSCRIPTION", // ObjectSubscription
48-
40 => "STATISTICS", // ObjectStatisticExt
49-
41 => "CONSTRAINT", // ObjectTabconstraint
50-
42 => "TABLE", // ObjectTable
51-
43 => "TABLESPACE", // ObjectTablespace
52-
44 => "TRANSFORM", // ObjectTransform
53-
45 => "TRIGGER", // ObjectTrigger
54-
46 => "TEXT SEARCH CONFIGURATION", // ObjectTsconfiguration
55-
47 => "TEXT SEARCH DICTIONARY", // ObjectTsdictionary
56-
48 => "TEXT SEARCH PARSER", // ObjectTsparser
57-
49 => "TEXT SEARCH TEMPLATE", // ObjectTstemplate
58-
51 => "TYPE", // ObjectType
59-
52 => "USER MAPPING", // ObjectUsermapping
60-
53 => "VIEW", // ObjectView
61-
_ => "OBJECT",
18+
let object_type_tokens: &[&str] = match n.objtype {
19+
1 => &["ACCESS", "METHOD"], // ObjectAccessMethod
20+
2 => &["AGGREGATE"], // ObjectAggregate
21+
6 => &["CAST"], // ObjectCast
22+
7 => &["COLUMN"], // ObjectColumn
23+
8 => &["COLLATION"], // ObjectCollation
24+
9 => &["CONVERSION"], // ObjectConversion
25+
10 => &["DATABASE"], // ObjectDatabase
26+
13 => &["DOMAIN"], // ObjectDomain
27+
14 => &["CONSTRAINT"], // ObjectDomconstraint
28+
15 => &["EVENT", "TRIGGER"], // ObjectEventTrigger
29+
16 => &["EXTENSION"], // ObjectExtension
30+
17 => &["FOREIGN", "DATA", "WRAPPER"], // ObjectFdw
31+
18 => &["FOREIGN", "SERVER"], // ObjectForeignServer
32+
19 => &["FOREIGN", "TABLE"], // ObjectForeignTable
33+
20 => &["FUNCTION"], // ObjectFunction
34+
21 => &["INDEX"], // ObjectIndex
35+
22 => &["LANGUAGE"], // ObjectLanguage
36+
23 => &["LARGE", "OBJECT"], // ObjectLargeobject
37+
24 => &["MATERIALIZED", "VIEW"], // ObjectMatview
38+
25 => &["OPERATOR", "CLASS"], // ObjectOpclass
39+
26 => &["OPERATOR"], // ObjectOperator
40+
27 => &["OPERATOR", "FAMILY"], // ObjectOpfamily
41+
29 => &["POLICY"], // ObjectPolicy
42+
30 => &["PROCEDURE"], // ObjectProcedure
43+
31 => &["PUBLICATION"], // ObjectPublication
44+
34 => &["ROLE"], // ObjectRole
45+
35 => &["ROUTINE"], // ObjectRoutine
46+
36 => &["RULE"], // ObjectRule
47+
37 => &["SCHEMA"], // ObjectSchema
48+
38 => &["SEQUENCE"], // ObjectSequence
49+
39 => &["SUBSCRIPTION"], // ObjectSubscription
50+
40 => &["STATISTICS"], // ObjectStatisticExt
51+
41 => &["CONSTRAINT"], // ObjectTabconstraint
52+
42 => &["TABLE"], // ObjectTable
53+
43 => &["TABLESPACE"], // ObjectTablespace
54+
44 => &["TRANSFORM"], // ObjectTransform
55+
45 => &["TRIGGER"], // ObjectTrigger
56+
46 => &["TEXT", "SEARCH", "CONFIGURATION"], // ObjectTsconfiguration
57+
47 => &["TEXT", "SEARCH", "DICTIONARY"], // ObjectTsdictionary
58+
48 => &["TEXT", "SEARCH", "PARSER"], // ObjectTsparser
59+
49 => &["TEXT", "SEARCH", "TEMPLATE"], // ObjectTstemplate
60+
51 => &["TYPE"], // ObjectType
61+
52 => &["USER", "MAPPING"], // ObjectUsermapping
62+
53 => &["VIEW"], // ObjectView
63+
_ => &["OBJECT"],
6264
};
63-
e.token(TokenKind::IDENT(object_type_str.to_string()));
65+
for (idx, token) in object_type_tokens.iter().enumerate() {
66+
if idx > 0 {
67+
e.space();
68+
}
69+
emit_keyword(e, token);
70+
}
6471
e.space();
6572

6673
// Object name
@@ -76,7 +83,7 @@ pub(super) fn emit_comment_stmt(e: &mut EventEmitter, n: &CommentStmt) {
7683
if n.comment.is_empty() {
7784
e.token(TokenKind::NULL_KW);
7885
} else {
79-
e.token(TokenKind::IDENT(format!("'{}'", n.comment)));
86+
emit_single_quoted_str(e, &n.comment);
8087
}
8188

8289
e.token(TokenKind::SEMICOLON);

crates/pgt_pretty_print/src/nodes/copy_stmt.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::node_list::emit_comma_separated_list;
1+
use super::{
2+
node_list::emit_comma_separated_list,
3+
string::{emit_keyword, emit_single_quoted_str},
4+
};
25
use crate::{
36
TokenKind,
47
emitter::{EventEmitter, GroupKind},
@@ -56,14 +59,14 @@ pub(super) fn emit_copy_stmt(e: &mut EventEmitter, n: &CopyStmt) {
5659

5760
// PROGRAM or filename
5861
if n.is_program {
59-
e.token(TokenKind::IDENT("PROGRAM".to_string()));
62+
emit_keyword(e, "PROGRAM");
6063
e.space();
6164
}
6265

6366
if !n.filename.is_empty() {
64-
e.token(TokenKind::IDENT(format!("'{}'", n.filename)));
67+
emit_single_quoted_str(e, &n.filename);
6568
} else {
66-
e.token(TokenKind::IDENT("STDOUT".to_string()));
69+
emit_keyword(e, "STDOUT");
6770
}
6871

6972
// Options

crates/pgt_pretty_print/src/nodes/create_conversion_stmt.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use super::node_list::emit_dot_separated_list;
1+
use super::{
2+
node_list::emit_dot_separated_list,
3+
string::{emit_keyword, emit_single_quoted_str},
4+
};
25
use crate::{
36
TokenKind,
47
emitter::{EventEmitter, GroupKind},
@@ -16,7 +19,7 @@ pub(super) fn emit_create_conversion_stmt(e: &mut EventEmitter, n: &CreateConver
1619
e.space();
1720
}
1821

19-
e.token(TokenKind::IDENT("CONVERSION".to_string()));
22+
emit_keyword(e, "CONVERSION");
2023
e.space();
2124

2225
// Conversion name
@@ -25,11 +28,11 @@ pub(super) fn emit_create_conversion_stmt(e: &mut EventEmitter, n: &CreateConver
2528
e.space();
2629
e.token(TokenKind::FOR_KW);
2730
e.space();
28-
e.token(TokenKind::IDENT(format!("'{}'", n.for_encoding_name)));
31+
emit_single_quoted_str(e, &n.for_encoding_name);
2932
e.space();
3033
e.token(TokenKind::TO_KW);
3134
e.space();
32-
e.token(TokenKind::IDENT(format!("'{}'", n.to_encoding_name)));
35+
emit_single_quoted_str(e, &n.to_encoding_name);
3336
e.space();
3437
e.token(TokenKind::FROM_KW);
3538
e.space();

0 commit comments

Comments
 (0)