How to annotate PostgreSQL ASTs with location information
This text introduces libpg_query, a C library that parses SQL queries into abstract syntax trees (ASTs) and is used by PostgreSQL. The AST includes start locations for every expression but lacks end locations, making it difficult to write syntax or expression highlighters. However, using the PostgreSQL lexer, one can recover expressions' end locations. The text then demonstrates how to use libpg_query in TypeScript with Node.js and provides a solution for annotating ASTs with start and stop locations of expressions. It also discusses potential improvements to the approach, such as handling aliases, parentheses, and special tokens like SELECT.
Company
Propel Data
Date published
Oct. 14, 2024
Author(s)
Mark Roberts
Word count
2583
Hacker News points
4
Language
English