Skip to content

Commit

Permalink
Introduce column number in AST nodes (#1724)
Browse files Browse the repository at this point in the history
Integrate ServiceNow changes to support column numbers in the parser, IR, and AST.
  • Loading branch information
andreabergia authored Nov 26, 2024
1 parent 3e899f6 commit f6c86fc
Show file tree
Hide file tree
Showing 14 changed files with 1,567 additions and 373 deletions.
152 changes: 95 additions & 57 deletions rhino/src/main/java/org/mozilla/javascript/IRFactory.java

Large diffs are not rendered by default.

29 changes: 20 additions & 9 deletions rhino/src/main/java/org/mozilla/javascript/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,24 @@ public Node(int nodeType, Node left, Node mid, Node right) {
right.next = null;
}

public Node(int nodeType, int line) {
public Node(int nodeType, int line, int column) {
type = nodeType;
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node child, int line) {
public Node(int nodeType, Node child, int line, int column) {
this(nodeType, child);
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node left, Node right, int line) {
public Node(int nodeType, Node left, Node right, int line, int column) {
this(nodeType, left, right);
lineno = line;
setLineColumnNumber(line, column);
}

public Node(int nodeType, Node left, Node mid, Node right, int line) {
public Node(int nodeType, Node left, Node mid, Node right, int line, int column) {
this(nodeType, left, mid, right);
lineno = line;
setLineColumnNumber(line, column);
}

public static Node newNumber(double number) {
Expand Down Expand Up @@ -531,8 +531,9 @@ public int getLineno() {
return lineno;
}

public void setLineno(int lineno) {
public void setLineColumnNumber(int lineno, int column) {
this.lineno = lineno;
this.column = column;
}

/** Can only be called when <code>getType() == Token.NUMBER</code> */
Expand Down Expand Up @@ -1253,11 +1254,21 @@ private static void appendPrintId(Node n, Map<Node, Integer> printIds, StringBui
}
}

/**
* @return the column of where a Node is defined in source. If the column is -1, it was never
* initialized. One-based.
* <p>May be overridden by sub classes
*/
public int getColumn() {
return column;
}

protected int type = Token.ERROR; // type of the node, e.g. Token.NAME
protected Node next; // next sibling
protected Node first; // first element of a linked list of children
protected Node last; // last element of a linked list of children
protected int lineno = -1;
private int column = -1;

/**
* Linked list of properties. Since vast majority of nodes would have no more then 2 properties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ private void transformCompilationUnit_r(
unwind = new Node(Token.LEAVEWITH);
}
if (unwindBlock == null) {
unwindBlock = new Node(Token.BLOCK, node.getLineno());
unwindBlock = new Node(Token.BLOCK);
unwind.setLineColumnNumber(node.getLineno(), node.getColumn());
}
unwindBlock.addChildToBack(unwind);
}
Expand Down Expand Up @@ -297,7 +298,8 @@ private void transformCompilationUnit_r(
// to a LETEXPR
if (n.getType() != Token.LETEXPR) throw Kit.codeBug();
}
Node pop = new Node(Token.EXPR_VOID, n, node.getLineno());
Node pop = new Node(Token.EXPR_VOID, n);
pop.setLineColumnNumber(node.getLineno(), node.getColumn());
result.addChildToBack(pop);
}
node = replaceCurrent(parent, previous, node, result);
Expand Down
Loading

0 comments on commit f6c86fc

Please sign in to comment.