001package gudusoft.gsqlparser.resolver2.model;
002
003import gudusoft.gsqlparser.nodes.TParseTreeNode;
004
005/**
006 * Represents the location where a column or expression is defined.
007 * Used for tracking data lineage and error reporting.
008 */
009public class SourceLocation {
010    private final TParseTreeNode definitionNode;
011    private final int line;
012    private final int column;
013    private final String fileName;
014
015    public SourceLocation(TParseTreeNode definitionNode) {
016        this.definitionNode = definitionNode;
017        if (definitionNode != null && definitionNode.getStartToken() != null) {
018            this.line = (int) definitionNode.getStartToken().lineNo;
019            this.column = (int) definitionNode.getStartToken().columnNo;
020            // TSourceToken doesn't have file name info in current version
021            this.fileName = null;
022        } else {
023            this.line = -1;
024            this.column = -1;
025            this.fileName = null;
026        }
027    }
028
029    public SourceLocation(int line, int column, String fileName) {
030        this.definitionNode = null;
031        this.line = line;
032        this.column = column;
033        this.fileName = fileName;
034    }
035
036    public TParseTreeNode getDefinitionNode() {
037        return definitionNode;
038    }
039
040    public int getLine() {
041        return line;
042    }
043
044    public int getColumn() {
045        return column;
046    }
047
048    public String getFileName() {
049        return fileName;
050    }
051
052    @Override
053    public String toString() {
054        if (fileName != null) {
055            return String.format("%s:%d:%d", fileName, line, column);
056        }
057        return String.format("line %d, column %d", line, column);
058    }
059}