Skip to content
Snippets Groups Projects
Commit 16ce887f authored by dgelessus's avatar dgelessus
Browse files

Move IToken special case from PositionedNode into Token

parent b324cf25
Branches
Tags
No related merge requests found
Pipeline #86308 passed
......@@ -24,29 +24,10 @@ public class PositionedNode {
}
public SourcePosition getStartPos() {
/*
* Special treatment for tokens because they don't get a position in the
* PositionAspect
*/
if (this instanceof IToken) {
final IToken token = (IToken) this;
return new SourcePosition(token.getLine(), token.getPos());
}
return startPos;
}
public SourcePosition getEndPos() {
/*
* Special treatment for tokens because they don't get a position in the
* PositionAspect
*/
if (this instanceof IToken) {
final IToken token = (IToken) this;
return new SourcePosition(token.getLine(), token.getPos()
+ token.getText().length());
}
return endPos;
}
......
......@@ -3,7 +3,7 @@
package org.sablecc.sablecc.node;
import de.hhu.stups.sablecc.patch.IToken;
import de.hhu.stups.sablecc.patch.SourcePosition;
public abstract class Token extends Node implements IToken
......@@ -47,6 +47,9 @@ public abstract class Token extends Node implements IToken
public void setText(String text)
{
this.text = text;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
@Override
......@@ -59,6 +62,9 @@ public abstract class Token extends Node implements IToken
public void setLine(int line)
{
this.line = line;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
@Override
......@@ -71,6 +77,35 @@ public abstract class Token extends Node implements IToken
public void setPos(int pos)
{
this.pos = pos;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
// startPos and endPos are calculated lazily to avoid creating two SourcePosition objects for every token.
@Override
public SourcePosition getStartPos()
{
SourcePosition startPos = super.getStartPos();
if(startPos == null)
{
startPos = new SourcePosition(this.line, this.pos);
setStartPos(startPos);
}
return startPos;
}
@Override
public SourcePosition getEndPos()
{
SourcePosition endPos = super.getEndPos();
if(endPos == null)
{
endPos = new SourcePosition(this.line, this.pos + (this.text == null ? 0 : this.text.length()));
setEndPos(endPos);
}
return endPos;
}
@Override
......
......@@ -192,7 +192,7 @@ Macro:Token
package $0$;
import de.hhu.stups.sablecc.patch.IToken;
import de.hhu.stups.sablecc.patch.SourcePosition;
public abstract class Token extends Node implements IToken
......@@ -236,6 +236,9 @@ public abstract class Token extends Node implements IToken
public void setText(String text)
{
this.text = text;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
@Override
......@@ -248,6 +251,9 @@ public abstract class Token extends Node implements IToken
public void setLine(int line)
{
this.line = line;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
@Override
......@@ -260,6 +266,35 @@ public abstract class Token extends Node implements IToken
public void setPos(int pos)
{
this.pos = pos;
// Invalidate any already calculated SourcePositions
setStartPos(null);
setEndPos(null);
}
// startPos and endPos are calculated lazily to avoid creating two SourcePosition objects for every token.
@Override
public SourcePosition getStartPos()
{
SourcePosition startPos = super.getStartPos();
if(startPos == null)
{
startPos = new SourcePosition(this.line, this.pos);
setStartPos(startPos);
}
return startPos;
}
@Override
public SourcePosition getEndPos()
{
SourcePosition endPos = super.getEndPos();
if(endPos == null)
{
endPos = new SourcePosition(this.line, this.pos + (this.text == null ? 0 : this.text.length()));
setEndPos(endPos);
}
return endPos;
}
@Override
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment