From 32c11dffefdfd4ed3281b2d87bcbd2a46d098bec Mon Sep 17 00:00:00 2001 From: calogero_zarba Date: Mon, 26 Apr 2021 17:24:16 +0200 Subject: [PATCH 1/2] Added line and charPositionInLine to ParseTreeNode --- .../listener/DefaultTreeListener.java | 11 +++-- .../org/snt/inmemantlr/tree/ParseTree.java | 19 +++++---- .../snt/inmemantlr/tree/ParseTreeNode.java | 40 +++++++++++++++---- .../utils/ParseTreeManipulator.java | 8 +++- 4 files changed, 57 insertions(+), 21 deletions(-) diff --git a/inmemantlr-api/src/main/java/org/snt/inmemantlr/listener/DefaultTreeListener.java b/inmemantlr-api/src/main/java/org/snt/inmemantlr/listener/DefaultTreeListener.java index 8dfc533..d34cd8b 100644 --- a/inmemantlr-api/src/main/java/org/snt/inmemantlr/listener/DefaultTreeListener.java +++ b/inmemantlr-api/src/main/java/org/snt/inmemantlr/listener/DefaultTreeListener.java @@ -88,10 +88,13 @@ public DefaultTreeListener(Predicate 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); } } @@ -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; } diff --git a/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTree.java b/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTree.java index 1d3036f..d1a8609 100644 --- a/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTree.java +++ b/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTree.java @@ -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); } /** @@ -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; } diff --git a/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTreeNode.java b/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTreeNode.java index d2b812a..45c9d2a 100644 --- a/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTreeNode.java +++ b/inmemantlr-api/src/main/java/org/snt/inmemantlr/tree/ParseTreeNode.java @@ -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 children; private static int cnt = 0; @@ -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; } /** @@ -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; @@ -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 diff --git a/inmemantlr-api/src/main/java/org/snt/inmemantlr/utils/ParseTreeManipulator.java b/inmemantlr-api/src/main/java/org/snt/inmemantlr/utils/ParseTreeManipulator.java index 10c1be7..3150603 100644 --- a/inmemantlr-api/src/main/java/org/snt/inmemantlr/utils/ParseTreeManipulator.java +++ b/inmemantlr-api/src/main/java/org/snt/inmemantlr/utils/ParseTreeManipulator.java @@ -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()) { @@ -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); From aa52ef98ff3eae87e5384053c757e974b343e862 Mon Sep 17 00:00:00 2001 From: calogero_zarba Date: Tue, 11 May 2021 12:45:33 +0200 Subject: [PATCH 2/2] Bumped version to 1.9.0, and updated README.md --- README.md | 1 + inmemantlr-api/pom.xml | 2 +- inmemantlr-tool/pom.xml | 2 +- pom.xml | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f9c389a..0915d50 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/inmemantlr-api/pom.xml b/inmemantlr-api/pom.xml index a5f3678..3d45739 100644 --- a/inmemantlr-api/pom.xml +++ b/inmemantlr-api/pom.xml @@ -7,7 +7,7 @@ com.github.julianthome inmemantlr ../pom.xml - 1.8.0 + 1.9.0 4.0.0 diff --git a/inmemantlr-tool/pom.xml b/inmemantlr-tool/pom.xml index 948b848..0029096 100644 --- a/inmemantlr-tool/pom.xml +++ b/inmemantlr-tool/pom.xml @@ -7,7 +7,7 @@ com.github.julianthome inmemantlr ../pom.xml - 1.8.0 + 1.9.0 4.0.0 diff --git a/pom.xml b/pom.xml index af4c5b4..f1ac203 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.github.julianthome inmemantlr - 1.8.0 + 1.9.0 pom