Skip to content

Commit 33baa34

Browse files
committed
SONARPY-1989 Extend Token to hold original and virtual locations (#1872)
1 parent 94e5a5e commit 33baa34

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

python-frontend/src/main/java/org/sonar/plugins/python/api/tree/Token.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public interface Token extends Tree {
3434

3535
TokenType type();
3636

37+
int physicalLine();
38+
39+
int physicalColumn();
40+
41+
int includedEscapeChars();
42+
3743
}

python-frontend/src/main/java/org/sonar/python/tree/TokenImpl.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,49 @@ public class TokenImpl extends PyTree implements Token {
3333
private com.sonar.sslr.api.Token token;
3434
private List<Trivia> trivia;
3535

36+
private Integer line;
37+
private Integer column;
38+
private int includedEscapeChars;
39+
3640
public TokenImpl(com.sonar.sslr.api.Token token) {
3741
this.token = token;
3842
this.trivia = token.getTrivia().stream().map(tr -> new TriviaImpl(new TokenImpl(tr.getToken()))).collect(Collectors.toList());
3943
}
4044

45+
public TokenImpl(com.sonar.sslr.api.Token token, int line, int column, int includedEscapeChars) {
46+
this(token);
47+
this.line = line;
48+
this.column = column;
49+
this.includedEscapeChars = includedEscapeChars;
50+
}
51+
4152
@Override
4253
public String value() {
4354
return token.getValue();
4455
}
4556

4657
@Override
4758
public int line() {
48-
return token.getLine();
59+
return line != null ? line : physicalLine();
4960
}
5061

5162
@Override
5263
public int column() {
64+
return column != null ? column : physicalColumn();
65+
}
66+
67+
@Override
68+
public int physicalLine() {
69+
return token.getLine();
70+
}
71+
72+
@Override
73+
public int includedEscapeChars() {
74+
return includedEscapeChars;
75+
}
76+
77+
@Override
78+
public int physicalColumn() {
5379
return token.getColumn();
5480
}
5581

0 commit comments

Comments
 (0)