1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
9 IBM Corporation - Initial implementation
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
19 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
20 import net.sourceforge.phpdt.ui.text.IColorManager;
21 import net.sourceforge.phpeclipse.IPreferenceConstants;
22 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
23 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
24 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.text.Assert;
28 import org.eclipse.jface.text.rules.ICharacterScanner;
29 import org.eclipse.jface.text.rules.IRule;
30 import org.eclipse.jface.text.rules.IToken;
31 import org.eclipse.jface.text.rules.IWordDetector;
32 import org.eclipse.jface.text.rules.MultiLineRule;
33 import org.eclipse.jface.text.rules.Token;
34 import org.eclipse.jface.text.rules.WhitespaceRule;
35 import org.eclipse.jface.text.rules.WordRule;
40 public class PHPCodeScanner extends AbstractJavaScanner {
43 * Rule to detect java operators.
47 protected class OperatorRule implements IRule {
50 private final char[] PHP_OPERATORS = {
76 /** Token to return for this rule */
77 private final IToken fToken;
79 /** Token to return for braces */
80 private final IToken fTokenBraces;
83 * Creates a new operator rule.
86 * Token to use for this rule
88 public OperatorRule(IToken token, IToken tokenBraces) {
90 fTokenBraces = tokenBraces;
95 * Is this character an operator character?
98 * Character to determine whether it is an operator character
99 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
101 public boolean isOperator(char character) {
102 for (int index = 0; index < PHP_OPERATORS.length; index++) {
103 if (PHP_OPERATORS[index] == character)
110 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
112 public IToken evaluate(ICharacterScanner scanner) {
114 int character = scanner.read();
115 if (character == '{' || character == '}') {
118 if (isOperator((char) character)) {
119 int lastCharacter = character;
120 character = scanner.read();
121 if (!isOperator((char) character)) {
125 if (checkPHPTag(scanner, lastCharacter, character)) {
126 return Token.UNDEFINED;
129 lastCharacter = character;
130 character = scanner.read();
131 if (checkPHPTag(scanner, lastCharacter, character)) {
134 } while (isOperator((char) character));
139 return Token.UNDEFINED;
144 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
147 * @param lastCharacter
151 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
152 if (lastCharacter == '<' && character == '?') {
156 } else if (lastCharacter == '?' && character == '>') {
166 protected class AccentStringRule implements IRule {
168 /** Token to return for this rule */
169 private final IToken fToken;
171 public AccentStringRule(IToken token) {
177 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
179 public IToken evaluate(ICharacterScanner scanner) {
181 int character = scanner.read();
183 if (character == '`') {
185 while (character != ICharacterScanner.EOF) {
186 character = scanner.read();
187 if (character == '\\') {
188 character = scanner.read();
189 } else if (character == '`') {
194 return Token.UNDEFINED;
197 return Token.UNDEFINED;
203 private class PHPWordRule extends WordRule {
204 private StringBuffer fBuffer = new StringBuffer();
205 protected Map fWordsIgnoreCase = new HashMap();
207 public PHPWordRule(IWordDetector detector) {
208 super(detector, Token.UNDEFINED);
211 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
212 super(detector, defaultToken);
216 * Adds a word and the token to be returned if it is detected.
218 * @param word the word this rule will search for, may not be <code>null</code>
219 * @param token the token to be returned if the word has been found, may not be <code>null</code>
221 public void addWordIgnoreCase(String word, IToken token) {
222 Assert.isNotNull(word);
223 Assert.isNotNull(token);
225 fWordsIgnoreCase.put(word, token);
228 public IToken evaluate(ICharacterScanner scanner) {
229 int c = scanner.read();
230 boolean isVariable = false;
231 boolean isUnderscore = false;
238 return Token.UNDEFINED;
241 if (c == '=') { // <?=
242 return getToken(IPreferenceConstants.PHP_TAG);
244 if (c != 'p' && c != 'P') {
246 return getToken(IPreferenceConstants.PHP_TAG);
249 if (c != 'h' && c != 'H') {
252 return getToken(IPreferenceConstants.PHP_TAG);
255 if (c != 'p' && c != 'P') {
259 return getToken(IPreferenceConstants.PHP_TAG);
261 return getToken(IPreferenceConstants.PHP_TAG);
270 return getToken(IPreferenceConstants.PHP_TAG);
274 return Token.UNDEFINED;
276 if (fDetector.isWordStart((char) c)) {
280 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
282 fBuffer.setLength(0);
283 fBuffer.append((char) c);
288 while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
289 fBuffer.append((char) c);
296 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
298 return getToken(IPreferenceConstants.PHP_VARIABLE);
300 word = fBuffer.toString();
301 IToken token = (IToken) fWords.get(word);
305 token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
309 if (fDefaultToken.isUndefined())
310 unreadBuffer(scanner);
312 return fDefaultToken;
317 return Token.UNDEFINED;
321 //private PHPColorProvider fColorProvider;
323 private static String[] fgTokenProperties = {
324 IPreferenceConstants.PHP_MULTILINE_COMMENT,
325 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
326 IPreferenceConstants.PHP_TAG,
327 IPreferenceConstants.PHP_KEYWORD,
328 IPreferenceConstants.PHP_FUNCTIONNAME,
329 IPreferenceConstants.PHP_VARIABLE,
330 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
331 IPreferenceConstants.PHP_STRING_DQ,
332 IPreferenceConstants.PHP_STRING_SQ,
333 IPreferenceConstants.PHP_TYPE,
334 IPreferenceConstants.PHP_CONSTANT,
335 IPreferenceConstants.PHP_DEFAULT,
336 IPreferenceConstants.PHP_OPERATOR,
337 IPreferenceConstants.PHP_BRACE_OPERATOR,
338 IPreferenceConstants.PHP_KEYWORD_RETURN };
341 * Creates a PHP code scanner
343 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
344 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
345 super(manager, store);
350 * @see AbstractJavaScanner#getTokenProperties()
352 protected String[] getTokenProperties() {
353 return fgTokenProperties;
357 * @see AbstractJavaScanner#createRules()
359 protected List createRules() {
360 List rules = new ArrayList();
361 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
362 // Add rule for single line comments.
363 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
364 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
365 // Add rule for strings and character constants.
366 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
367 // rules.add(new SingleQuoteStringRule(token));
368 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
369 // rules.add(new DoubleQuoteStringRule(token));
370 rules.add(new AccentStringRule(token));
372 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
373 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
374 // Add generic whitespace rule.
375 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
376 // Add word rule for keywords, types, and constants.
377 token = getToken(IPreferenceConstants.PHP_DEFAULT);
378 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
380 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
381 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
382 Token type = getToken(IPreferenceConstants.PHP_TYPE);
383 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
385 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
386 // String strbuffer = null; unused
387 PHPElement elbuffer = null;
389 for (int i = 0; i < buffer.size(); i++) {
390 // while ((buffer != null)
391 // && (!buffer.isEmpty()
392 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
393 elbuffer = (PHPElement) buffer.get(i);
394 if (elbuffer instanceof PHPKeyword) {
395 name = ((PHPKeyword) elbuffer).getName();
396 if (!name.equals("return")) {
397 wordRule.addWord(name, keyword);
399 } else if (elbuffer instanceof PHPFunction) {
400 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(), functionName);
401 } else if (elbuffer instanceof PHPType) {
402 wordRule.addWord(elbuffer.getName(), type);
403 } else if (elbuffer instanceof PHPConstant) {
404 wordRule.addWord(elbuffer.getName(), constant);
408 // Add word rule for keyword 'return'.
409 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
410 wordRule.addWord("return", token);
412 // Add rule for operators and brackets (at the end !)
413 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
417 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));