diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java index 25dfea1a86b566b95fd0b422ad6a40b659dcf700..1ab72f9d3cc73670730e705a7efbbba3247f27e3 100644 --- a/src/main/java/org/sablecc/sablecc/parser/Parser.java +++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java @@ -37,45 +37,26 @@ public class Parser implements IParser this.lexer = lexer; } - private void computePositions(Object resultNodeOrList, List<?> beginNodeList, List<?> endNodeList) { - if (resultNodeOrList instanceof List<?>) { + private void computePositions(PositionedNode node, List<?> beginNodeList, List<?> endNodeList) { + // This method should only ever be called for newly created AST nodes (that are not tokens). + assert !(node instanceof Token); + assert node.getStartPos() == null; + assert node.getEndPos() == null; + + PositionedNode beginNode = findBeginNode(beginNodeList); + if (beginNode == null) { /* - * special case: this is a list of nodes, for example an identifier - * list, so we don't want to assign positions to the entire list, - * but the last element added to it + * Sometimes (haven't found out why) we get empty list here. In + * the only observed cases we were looking for the source range + * of the whole parse unit. Then the start pos is the first char. */ - final List<?> nodeList = (List<?>) resultNodeOrList; - if (nodeList.isEmpty()) { - // no positions for empty lists... - return; - } - resultNodeOrList = nodeList.get(nodeList.size() - 1); - } - - final PositionedNode node = (PositionedNode) resultNodeOrList; - - // Add node positions if they haven't been calculated yet. - // Tokens are skipped because they always have positions - - // this avoids creating unnecessary SourcePosition objects. - if (!(node instanceof Token) && node.getStartPos() == null) { - assert node.getEndPos() == null; - PositionedNode beginNode = findBeginNode(beginNodeList); - if (beginNode == null) { - /* - * Sometimes (haven't found out why) we get empty list here. In - * the only observed cases we were looking for the source range - * of the whole parse unit. Then the start pos is the first char. - */ - node.setStartPos(new SourcePosition(1, 1)); - } else { - node.setStartPos(beginNode.getStartPos()); - } - - PositionedNode endNode = findEndNode(endNodeList); - node.setEndPos(endNode.getEndPos()); + node.setStartPos(new SourcePosition(1, 1)); } else { - assert node.getEndPos() != null; + node.setStartPos(beginNode.getStartPos()); } + + PositionedNode endNode = findEndNode(endNodeList); + node.setEndPos(endNode.getEndPos()); } private PositionedNode findBeginNode(final List<?> list) { diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index 65591e4fdc6d3a12c6363512d56eba2b144b90ef..7a52153a435b52057221297ce5fe3ec0728ca3d4 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -45,45 +45,26 @@ public class Parser implements IParser this.lexer = lexer; } - private void computePositions(Object resultNodeOrList, List<?> beginNodeList, List<?> endNodeList) { - if (resultNodeOrList instanceof List<?>) { + private void computePositions(PositionedNode node, List<?> beginNodeList, List<?> endNodeList) { + // This method should only ever be called for newly created AST nodes (that are not tokens). + assert !(node instanceof Token); + assert node.getStartPos() == null; + assert node.getEndPos() == null; + + PositionedNode beginNode = findBeginNode(beginNodeList); + if (beginNode == null) { /* - * special case: this is a list of nodes, for example an identifier - * list, so we don't want to assign positions to the entire list, - * but the last element added to it + * Sometimes (haven't found out why) we get empty list here. In + * the only observed cases we were looking for the source range + * of the whole parse unit. Then the start pos is the first char. */ - final List<?> nodeList = (List<?>) resultNodeOrList; - if (nodeList.isEmpty()) { - // no positions for empty lists... - return; - } - resultNodeOrList = nodeList.get(nodeList.size() - 1); - } - - final PositionedNode node = (PositionedNode) resultNodeOrList; - - // Add node positions if they haven't been calculated yet. - // Tokens are skipped because they always have positions - - // this avoids creating unnecessary SourcePosition objects. - if (!(node instanceof Token) && node.getStartPos() == null) { - assert node.getEndPos() == null; - PositionedNode beginNode = findBeginNode(beginNodeList); - if (beginNode == null) { - /* - * Sometimes (haven't found out why) we get empty list here. In - * the only observed cases we were looking for the source range - * of the whole parse unit. Then the start pos is the first char. - */ - node.setStartPos(new SourcePosition(1, 1)); - } else { - node.setStartPos(beginNode.getStartPos()); - } - - PositionedNode endNode = findEndNode(endNodeList); - node.setEndPos(endNode.getEndPos()); + node.setStartPos(new SourcePosition(1, 1)); } else { - assert node.getEndPos() != null; + node.setStartPos(beginNode.getStartPos()); } + + PositionedNode endNode = findEndNode(endNodeList); + node.setEndPos(endNode.getEndPos()); } private PositionedNode findBeginNode(final List<?> list) {