Skip to content

Commit cb59c2a

Browse files
committed
Lexer improvements for highlighter
1 parent a8fca66 commit cb59c2a

File tree

8 files changed

+397
-103
lines changed

8 files changed

+397
-103
lines changed

sonar-objective-c-plugin/src/main/java/org/sonar/objectivec/api/ObjectiveCPunctuator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public enum ObjectiveCPunctuator implements TokenType {
8989

9090
MINUSLT("->"),
9191
MINUSLTSTAR("->*"),
92+
DOT("."),
9293
DOTSTAR(".*");
9394

9495
private final String value;

sonar-objective-c-plugin/src/main/java/org/sonar/objectivec/api/ObjectiveCTokenType.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@
1919
*/
2020
package org.sonar.objectivec.api;
2121

22+
import com.google.common.collect.ImmutableList;
2223
import com.sonar.sslr.api.AstNode;
2324
import com.sonar.sslr.api.TokenType;
2425

25-
public enum ObjectiveCTokenType implements TokenType {
26+
import java.util.List;
2627

27-
NUMERIC_LITERAL;
28+
public enum ObjectiveCTokenType implements TokenType {
29+
CHARACTER_LITERAL,
30+
DOUBLE_LITERAL,
31+
FLOAT_LITERAL,
32+
INTEGER_LITERAL,
33+
LONG_LITERAL,
34+
STRING_LITERAL;
2835

2936
@Override
3037
public String getName() {
@@ -41,4 +48,7 @@ public boolean hasToBeSkippedFromAst(AstNode node) {
4148
return false;
4249
}
4350

51+
public static List numberLiterals() {
52+
return ImmutableList.of(DOUBLE_LITERAL, FLOAT_LITERAL, INTEGER_LITERAL, LONG_LITERAL);
53+
}
4454
}

sonar-objective-c-plugin/src/main/java/org/sonar/objectivec/highlighter/SourceFileOffsets.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

sonar-objective-c-plugin/src/main/java/org/sonar/objectivec/highlighter/SyntaxHighlighterVisitor.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.sonar.api.batch.fs.InputFile;
3434
import org.sonar.api.source.Highlightable;
3535
import org.sonar.objectivec.api.ObjectiveCKeyword;
36+
import org.sonar.objectivec.api.ObjectiveCTokenType;
3637
import org.sonar.squidbridge.SquidAstVisitor;
3738

3839
import java.io.IOException;
@@ -100,11 +101,13 @@ public void visitNode(AstNode astNode) {
100101

101102
@Override
102103
public void visitToken(Token token) {
104+
// Use org.sonar.api.batch.sensor.highlighting.TypeOfText here?
105+
103106
for (Trivia trivia : token.getTrivia()) {
104107
if (trivia.isComment()) {
105108
Token triviaToken = trivia.getToken();
106109
if (triviaToken.getValue().startsWith("/**")) {
107-
highlightToken(triviaToken, "jd");
110+
highlightToken(triviaToken, "j");
108111
} else if (triviaToken.getValue().startsWith("/*")) {
109112
highlightToken(triviaToken, "cppd");
110113
} else {
@@ -116,6 +119,15 @@ public void visitToken(Token token) {
116119
if (token.getType() instanceof ObjectiveCKeyword) {
117120
highlightToken(token, "k");
118121
}
122+
123+
if (ObjectiveCTokenType.numberLiterals().contains(token.getType())) {
124+
highlightToken(token, "c");
125+
}
126+
127+
if (ObjectiveCTokenType.STRING_LITERAL.equals(token.getType())
128+
|| ObjectiveCTokenType.CHARACTER_LITERAL.equals(token.getType())) {
129+
highlightToken(token, "s");
130+
}
119131
}
120132

121133
private void highlightToken(Token token, String typeOfText) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* SonarQube Objective-C (Community) Plugin
3+
* Copyright (C) 2012-2016 OCTO Technology, Backelite, and contributors
4+
* mailto:sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.objectivec.lexer;
21+
22+
import com.sonar.sslr.impl.Lexer;
23+
import org.sonar.sslr.channel.Channel;
24+
import org.sonar.sslr.channel.CodeReader;
25+
26+
/**
27+
* @author Sonar C++ Plugin (Community) authors
28+
*/
29+
public class BackslashChannel extends Channel<Lexer> {
30+
@Override
31+
public boolean consume(CodeReader code, Lexer output) {
32+
char ch = (char) code.peek();
33+
34+
if ((ch == '\\') && isNewLine(code.charAt(1))) {
35+
// just throw away the backslash
36+
code.pop();
37+
return true;
38+
}
39+
40+
return false;
41+
}
42+
43+
private static boolean isNewLine(char ch) {
44+
return (ch == '\n') || (ch == '\r');
45+
}
46+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* SonarQube Objective-C (Community) Plugin
3+
* Copyright (C) 2012-2016 OCTO Technology, Backelite, and contributors
4+
* mailto:sonarqube@googlegroups.com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.objectivec.lexer;
21+
22+
import com.sonar.sslr.api.Token;
23+
import com.sonar.sslr.impl.Lexer;
24+
import org.sonar.objectivec.api.ObjectiveCTokenType;
25+
import org.sonar.sslr.channel.Channel;
26+
import org.sonar.sslr.channel.CodeReader;
27+
28+
/**
29+
* @author Sonar C++ Plugin (Community) authors
30+
*/
31+
public class CharacterLiteralsChannel extends Channel<Lexer> {
32+
private static final char EOF = (char) -1;
33+
34+
private final StringBuilder sb = new StringBuilder();
35+
36+
private int index;
37+
private char ch;
38+
39+
@Override
40+
public boolean consume(CodeReader code, Lexer output) {
41+
int line = code.getLinePosition();
42+
int column = code.getColumnPosition();
43+
index = 0;
44+
readPrefix(code);
45+
if ((ch != '\'')) {
46+
return false;
47+
}
48+
if (!read(code)) {
49+
return false;
50+
}
51+
readUdSuffix(code);
52+
for (int i = 0; i < index; i++) {
53+
sb.append((char) code.pop());
54+
}
55+
output.addToken(Token.builder()
56+
.setLine(line)
57+
.setColumn(column)
58+
.setURI(output.getURI())
59+
.setValueAndOriginalValue(sb.toString())
60+
.setType(ObjectiveCTokenType.CHARACTER_LITERAL)
61+
.build());
62+
sb.setLength(0);
63+
return true;
64+
}
65+
66+
private boolean read(CodeReader code) {
67+
index++;
68+
while (code.charAt(index) != ch) {
69+
if (code.charAt(index) == EOF) {
70+
return false;
71+
}
72+
if (code.charAt(index) == '\\') {
73+
// escape
74+
index++;
75+
}
76+
index++;
77+
}
78+
index++;
79+
return true;
80+
}
81+
82+
private void readPrefix(CodeReader code) {
83+
ch = code.charAt(index);
84+
if ((ch == 'u') || (ch == 'U') || ch == 'L') {
85+
index++;
86+
ch = code.charAt(index);
87+
}
88+
}
89+
90+
private void readUdSuffix(CodeReader code) {
91+
for (int start_index = index, len = 0; ; index++) {
92+
char c = code.charAt(index);
93+
if (c == EOF) {
94+
return;
95+
}
96+
if ((c >= 'a' && c <= 'z')
97+
|| (c >= 'A' && c <= 'Z')
98+
|| (c == '_')) {
99+
len++;
100+
} else {
101+
if (c >= '0' && c <= '9') {
102+
if (len > 0) {
103+
len++;
104+
} else {
105+
index = start_index;
106+
return;
107+
}
108+
} else {
109+
return;
110+
}
111+
}
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)