-
Notifications
You must be signed in to change notification settings - Fork 3
/
AQLScanner.java
218 lines (188 loc) · 5.51 KB
/
AQLScanner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*******************************************************************************
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.ide.ui.viewer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
/**
* The AQL {@link ITokenScanner}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class AQLScanner implements ITokenScanner {
/**
* Keyword regular expression.
*/
private static final String KEYWORD_REGEX = "if|then|else|endif|let|in|true|false|null|not|and|or";
/**
* String literal regular expression.
*/
private static final String STRING_LITERAL_REGEX = "'([^'\\\\]|\\\\.)*'";
/**
* Keyword regular expression.
*/
private static final String ECLASSIFIER_TYPE_REGEX = "[a-zA-Z_][a-zA-Z_0-9]*::[a-zA-Z_][a-zA-Z_0-9]*";
/**
* Keyword regular expression.
*/
private static final String ECLASSIFIER_SET_TYPE_REGEX = "\\{\\s*" + ECLASSIFIER_TYPE_REGEX
+ "(\\s*\\|\\s*" + ECLASSIFIER_TYPE_REGEX + ")*\\s*\\}";
/**
* Scanner {@link Pattern}.
*/
private static final Pattern SCANNER = Pattern.compile("(\\s+)|((?<![a-zA-Z])(" + KEYWORD_REGEX
+ ")(?![a-zA-Z]))|(" + STRING_LITERAL_REGEX
+ ")|((?<![a-zA-Z])((String|Integer|Real|Boolean)(?![a-zA-Z]))|Sequence(?=\\()|OrderedSet(?=\\()|"
+ ECLASSIFIER_SET_TYPE_REGEX + "|" + ECLASSIFIER_TYPE_REGEX + ")");
/**
* White spaces group index.
*/
private static final int WS_GROUP = 1;
/**
* Keyword group index.
*/
private static final int KEYWORD_GROUP = 2;
/**
* Keyword group index.
*/
private static final int STRING_GROUP = 4;
/**
* Keyword group index.
*/
private static final int TYPE_LITERAL_GROUP = 6;
/**
* Keyword.
*/
private final IToken keywordToken;
/**
* {@link org.eclipse.acceleo.query.ast.StringLiteral StringLiteral}.
*/
private final IToken stringToken;
/**
* {@link org.eclipse.acceleo.query.ast.TypeLiteral TypeLiteral}.
*/
private final IToken typeLiteralToken;
/**
* The current {@link Matcher}.
*/
private Matcher matcher;
/**
* The offset in the document.
*/
private int documentOffset;
/**
* The expression length.
*/
private int expressionLength;
/**
* The current token start position.
*/
private int currentOffset;
/**
* The current token length.
*/
private int currentLength;
/**
* Tells if the matcher is ready or if we should call find().
*/
private boolean matcherReady;
/**
* Constructor.
*
* @param colorManager
* the {@link ColorManager}
*/
public AQLScanner(ColorManager colorManager) {
keywordToken = new Token(new TextAttribute(colorManager.getColor(IAQLColorConstants.KEYWORD)));
stringToken = new Token(new TextAttribute(colorManager.getColor(IAQLColorConstants.STRING)));
typeLiteralToken = new Token(new TextAttribute(colorManager.getColor(
IAQLColorConstants.TYPE_LITERAL)));
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.text.rules.ITokenScanner#setRange(org.eclipse.jface.text.IDocument, int, int)
*/
public void setRange(IDocument document, int offset, int length) {
try {
matcher = SCANNER.matcher(document.get(offset, length));
expressionLength = length;
documentOffset = offset;
currentOffset = 0;
currentLength = 0;
} catch (BadLocationException e) {
// nothing to do here
}
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.text.rules.ITokenScanner#nextToken()
*/
public IToken nextToken() {
final IToken res;
if (matcherReady || matcher.find()) {
if (matcher.start() == currentOffset + currentLength) {
matcherReady = false;
currentOffset = matcher.start();
currentLength = matcher.end() - matcher.start();
if (matcher.group(WS_GROUP) != null) {
res = Token.WHITESPACE;
} else if (matcher.group(KEYWORD_GROUP) != null) {
res = keywordToken;
} else if (matcher.group(STRING_GROUP) != null) {
res = stringToken;
} else if (matcher.group(TYPE_LITERAL_GROUP) != null) {
res = typeLiteralToken;
} else {
throw new IllegalStateException("should not happen.");
}
} else {
matcherReady = true;
res = Token.UNDEFINED;
currentOffset = currentOffset + currentLength;
currentLength = matcher.start() - currentOffset;
}
} else if (currentOffset + currentLength < expressionLength) {
matcherReady = false;
res = Token.UNDEFINED;
currentOffset = currentOffset + currentLength;
currentLength = expressionLength - currentOffset;
} else {
matcherReady = false;
res = Token.EOF;
currentOffset = expressionLength;
currentLength = 0;
}
return res;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.text.rules.ITokenScanner#getTokenOffset()
*/
public int getTokenOffset() {
return documentOffset + currentOffset;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.jface.text.rules.ITokenScanner#getTokenLength()
*/
public int getTokenLength() {
return currentLength;
}
}