Skip to content

Commit

Permalink
Merge pull request #42 from calogero81/antlr-line-column-error
Browse files Browse the repository at this point in the history
Added line and charPositionInLine to ParseTreeNode
  • Loading branch information
julianthome authored May 12, 2021
2 parents 9b52d7d + aa52ef9 commit 5018606
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ examples please have a look at [grammars-v4](#grammars-v4)).
- [Alan Stewart](https://github.com/alankstewart) (code style improvements, performance improvements, Tool customization)
- Radoslaw Cymer (bugfixes, code style improvements, typos in Javadoc)
- [Nikolas Havrikov](https://github.com/havrikov) (code style, bugfixes, performance improvements)
- [Calogero G. Zarba](https://github.com/calogero81) (feature improvements)

# TOC

Expand Down
2 changes: 1 addition & 1 deletion inmemantlr-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.julianthome</groupId>
<artifactId>inmemantlr</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.8.0</version>
<version>1.9.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
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
2 changes: 1 addition & 1 deletion inmemantlr-tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>com.github.julianthome</groupId>
<artifactId>inmemantlr</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.8.0</version>
<version>1.9.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.julianthome</groupId>
<artifactId>inmemantlr</artifactId>
<version>1.8.0</version>
<version>1.9.0</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit 5018606

Please sign in to comment.