Skip to content

Commit

Permalink
Added line and charPositionInLine to ParseTreeNode
Browse files Browse the repository at this point in the history
  • Loading branch information
calogero75 committed Apr 26, 2021
1 parent 9b52d7d commit 32c11df
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ public DefaultTreeListener(Predicate<String> filter) {
@Override
public void visitTerminal(TerminalNode terminalNode) {
if(includeTerminals) {
Token token = terminalNode.getSymbol();
ParseTreeNode n = parseTree.newNode(nodeptr,"",
terminalNode.toString(),
terminalNode.getSymbol().getStartIndex(),
terminalNode.getSymbol().getStopIndex());
token.getStartIndex(),
token.getStopIndex(),
token.getLine(),
token.getCharPositionInLine());
nodeptr.addChild(n);
}
}
Expand All @@ -108,7 +111,9 @@ public void enterEveryRule(ParserRuleContext ctx) {
Token e = ctx.getStop();
ParseTreeNode n = parseTree.newNode(nodeptr, rule, ctx.getText(),
s != null ? s.getStartIndex() : 0,
e != null ? e.getStopIndex() : 0);
e != null ? e.getStopIndex() : 0,
s != null ? s.getLine() : 0,
s != null ? s.getCharPositionInLine() : 0);
nodeptr.addChild(n);
nodeptr = n;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class ParseTree {
* @param label value of root non-terminal node
*/
public ParseTree(String nt, String label) {
root = newNode(null, nt, label,0,0);
root = newNode(null, nt, label,0,0,0,0);
}

/**
Expand Down Expand Up @@ -101,16 +101,19 @@ private ParseTreeNode newNode(ParseTreeNode parent) {
/**
* create new ast node
*
* @param parent parent node
* @param nt name of node to be created
* @param label value of node to be created
* @param sidx start index
* @param eidx end index
* @param parent parent node
* @param nt name of node to be created
* @param label value of node to be created
* @param sidx start index
* @param eidx end index
* @param line line
* @param charPositionInLine character position in line
* @return newly created node
*/
public ParseTreeNode newNode(ParseTreeNode parent, String nt, String label, int sidx,
int eidx) {
ParseTreeNode rn = new ParseTreeNode(this, parent, nt, label, sidx, eidx);
int eidx, int line, int charPositionInLine) {
ParseTreeNode rn = new ParseTreeNode(this, parent, nt, label, sidx, eidx,
line, charPositionInLine);
nodes.add(rn);
return rn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class ParseTreeNode {
private int sidx = 0;
private int eidx = 0;

private int line = 0;
private int charPositionInLine = 0;

private List<ParseTreeNode> children;
private static int cnt = 0;

Expand All @@ -59,21 +62,25 @@ private ParseTreeNode(ParseTree tree) {
/**
* constructor
*
* @param tree tree to whom the node belongs to
* @param parent parent node
* @param nt non terminal id
* @param sidx start index
* @param eidx end index
* @param label label
* @param tree tree to whom the node belongs to
* @param parent parent node
* @param nt non terminal id
* @param sidx start index
* @param eidx end index
* @param label label
* @param line line
* @param charPositionInLine character position in line
*/
protected ParseTreeNode(ParseTree tree, ParseTreeNode parent, String nt, String label, int
sidx, int eidx) {
sidx, int eidx, int line, int charPositionInLine) {
this(tree);
ntype = nt;
this.label = label;
this.parent = parent;
this.sidx = sidx;
this.eidx = eidx;
this.line = line;
this.charPositionInLine = charPositionInLine;
}

/**
Expand All @@ -89,6 +96,8 @@ protected ParseTreeNode(ParseTree tree, ParseTreeNode nod) {
label = nod.label;
this.eidx = nod.eidx;
this.sidx = nod.sidx;
this.line = nod.line;
this.charPositionInLine = nod.charPositionInLine;
for (ParseTreeNode c : nod.children) {
ParseTreeNode cnod = new ParseTreeNode(tree, c);
cnod.parent = this;
Expand Down Expand Up @@ -246,13 +255,28 @@ public int getSidx() {
}

/**
* get ent index
* get end index
* @return end index
*/
public int getEidx() {
return eidx;
}

/**
* get line
* @return line
*/
public int getLine() {
return line;
}

/**
* get character position in line
* @return character position in line
*/
public int getCharPositionInLine() {
return charPositionInLine;
}

/**
* check whether node is terminal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ private void expand(ParseTree orig, ParseTreeNode par,
toinj.getRule(),
toinj.getLabel(),
toinj.getSidx(),
toinj.getEidx());
toinj.getEidx(),
toinj.getLine(),
toinj.getCharPositionInLine());


for (ParseTreeNode c : toinj.getChildren()) {
Expand Down Expand Up @@ -76,7 +78,9 @@ public void inject(ParseTree rcv,
toinject.getRoot().getFirstChild().getRule(),
toinject.getRoot().getFirstChild().getLabel(),
toinject.getRoot().getFirstChild().getSidx(),
toinject.getRoot().getFirstChild().getEidx());
toinject.getRoot().getFirstChild().getEidx(),
toinject.getRoot().getFirstChild().getLine(),
toinject.getRoot().getFirstChild().getCharPositionInLine());

//par.addChild(nn);

Expand Down

0 comments on commit 32c11df

Please sign in to comment.