partiql / partiql-lang-kotlin

PartiQL libraries and tools in Kotlin.
https://partiql.org/
Apache License 2.0
539 stars 60 forks source link

[V1] Add source location and spans to V1 AstNodes #1608

Open RCHowell opened 1 week ago

RCHowell commented 1 week ago
public abstract class AstNode {

    @NotNull
    public final Span span;

    // .........
}

public class Location {
    public final int line;
    public final int column;
}

public class Span {
    public final Location start;
    public final Location end;
}

Something like https://github.com/apache/calcite/blob/main/core/src/main/java/org/apache/calcite/sql/parser/SqlParserPos.java

But it would make sense to have these be lazy so that we don't create many needless objects until the locations are actually used .. just to cut down on object creation for ASTs. Really, a node should only need its line/column pair and can compute a span from the children.

alancai98 commented 1 day ago

Alternative modeling could follow what we do in partiql-lang-rust. That is, create a parser result which stores the AST along with a source location map https://github.com/partiql/partiql-lang-rust/blob/3f9d17fb38edafb23c1908af27b06c78d66680cd/partiql-parser/src/lib.rs#L81-L86.

Modeling with a map should offer the same functionality as spans on every node and not require additional fields.