Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Sep 9, 2024
1 parent 58d1c11 commit 937b958
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
2 changes: 0 additions & 2 deletions ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7299,7 +7299,6 @@ export type Expr =

export type Node =
| { A_ArrayExpr: A_ArrayExpr }
| { A_Const: A_Const }
| { A_Expr: A_Expr }
| { A_Indices: A_Indices }
| { A_Indirection: A_Indirection }
Expand Down Expand Up @@ -7420,7 +7419,6 @@ export type Node =
| { JsonObjectAgg: JsonObjectAgg }
| { JsonObjectConstructor: JsonObjectConstructor }
| { JsonValueExpr: JsonValueExpr }
| { List: List }
| { LoadStmt: LoadStmt }
| { LockingClause: LockingClause }
| { MergeStmt: MergeStmt }
Expand Down
12 changes: 0 additions & 12 deletions node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,12 +547,6 @@ export class NodePath<TNodeTag extends NodeTag = NodeTag> {
isString(): this is NodePath<"String"> {
return this.tag === "String"
}
isList(): this is NodePath<"List"> {
return this.tag === "List"
}
isA_Const(): this is NodePath<"A_Const"> {
return this.tag === "A_Const"
}
}

function isTaggedNode(node: object, tag: string) {
Expand Down Expand Up @@ -1091,11 +1085,5 @@ export const NodeTag = {
},
isString(node: object | undefined): node is { String: import("./ast").String } {
return node != null && isTaggedNode(node, "String")
},
isList(node: object | undefined): node is { List: import("./ast").List } {
return node != null && isTaggedNode(node, "List")
},
isA_Const(node: object | undefined): node is { A_Const: import("./ast").A_Const } {
return node != null && isTaggedNode(node, "A_Const")
}
}
33 changes: 21 additions & 12 deletions scripts/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,24 @@ async function main() {
...Object.values(structsByModule).flatMap((s) => Object.keys(s)),
]

// These are not used anywhere. They're for libpg_query's internal use.
const skippedEntities = new Set([
"BlockNumber",
"BlockId",
"BlockIdData",
"NodeTag",
"ParallelVacuumState",
"QuerySource",
"VacAttrStatsP",
"pg_wchar",
])

const shouldSkipEntity = (name: string) => {
if (skippedEntities.has(name)) {
return true
}
if (name.startsWith("Block")) {
return true
}
return false
}
const shouldSkipEntity = (name: string) => skippedEntities.has(name)

// When true, warn about unknown types.
const debugUnknownTypes = false

// A type is unknown if it's not found in typeMappings or entityNames.
const unknownTypes = new Set<string>()
const warnUnknownType = (name: string, label = "type") => {
if (debugUnknownTypes && !unknownTypes.has(name)) {
Expand All @@ -130,6 +128,7 @@ async function main() {
}
}

// Map a C type to a TypeScript type.
const applyTypeMapping = (name: string, c_type: string): string => {
const rawType = c_type.replace(/\*$/, "")
const mapping = (name && typeMappings[name]) || typeMappings[rawType]
Expand All @@ -146,8 +145,11 @@ async function main() {
return "any"
}

// This is the output TypeScript code.
let code = ""

// Typedefs are mostly mapped to type aliases, but at least one is a bitmask,
// which gets mapped to an enum.
for (const typeDef of typeDefs) {
if (shouldSkipEntity(typeDef.new_type_name)) {
continue
Expand Down Expand Up @@ -360,6 +362,7 @@ async function main() {
fieldName,
inferredTags,
)

fieldType =
"(" +
inferredTags
Expand Down Expand Up @@ -394,26 +397,32 @@ async function main() {
}
}

// These are untested by libpg_query's test suite, so they've been incorrectly
// marked as type aliases.
typeAliases.delete("CreateExtensionStmt")
typeAliases.delete("AlterExtensionStmt")
typeAliases.delete("AlterExtensionContentsStmt")

for (const name of typeAliases) {
nodeTypes.delete(name)
expressionTypes.delete(name)
nodeTypes.delete(name)
}

nodeTypes.forEach((name) => {
if (!entityNames.includes(name)) {
// console.warn("Unknown node type: %O", name)
nodeTypes.delete(name)
}
})

nodeTypes.add("List")
nodeTypes.add("A_Const")
// These expression types neither end in "Expr" nor have a field named "xpr".
// Nonetheless, they are still valid expressions.
expressionTypes.add("A_Const")
expressionTypes.add("ParamRef")

// nodeTypes.add("List")
// nodeTypes.add("A_Const")

code += "\n"
code += "export type QualifiedName = { String: String }[]\n"

Expand Down

0 comments on commit 937b958

Please sign in to comment.