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 = { ';', '(', ')', '.', '=', '/', '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!',
51 ',', '|', '&', '^', '%', '~', '@' };
53 /** Token to return for this rule */
54 private final IToken fToken;
56 /** Token to return for braces */
57 private final IToken fTokenBraces;
59 /** Token to return for heredocs */
60 private final IToken fTokenHeredoc;
63 * Creates a new operator rule.
66 * Token to use for this rule
70 public OperatorRule(IToken token, IToken tokenBraces, IToken tokenHeredoc) {
72 fTokenBraces = tokenBraces;
73 fTokenHeredoc = tokenHeredoc;
77 * Is this character an operator character?
80 * Character to determine whether it is an operator character
81 * @return <code>true</code> iff the character is an operator,
82 * <code>false</code> otherwise.
84 public boolean isOperator(char character) {
85 for (int index = 0; index < PHP_OPERATORS.length; index++) {
86 if (PHP_OPERATORS[index] == character)
93 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
95 public IToken evaluate(ICharacterScanner scanner) {
97 int character = scanner.read();
98 if (character == '{' || character == '}') {
101 if (isOperator((char) character)) {
102 int lastCharacter = character;
103 character = scanner.read();
104 // the readHEREDOC(scanner) call doesn't work, if we have our own partitions for single quoted
105 // or double quoted strings:
107 // if (lastCharacter == '<' && character == '<') {
108 // int heredocCharacter = scanner.read();
109 // if (heredocCharacter == '<') {
110 // // start of heredoc comment;
111 // if (readHEREDOC(scanner)) {
112 // return fTokenHeredoc;
118 if (!isOperator((char) character)) {
122 if (checkPHPTag(scanner, lastCharacter, character)) {
123 return Token.UNDEFINED;
126 lastCharacter = character;
127 character = scanner.read();
128 if (checkPHPTag(scanner, lastCharacter, character)) {
131 if (character == ICharacterScanner.EOF) {
134 } while (isOperator((char) character));
139 return Token.UNDEFINED;
143 // private boolean readHEREDOC(ICharacterScanner scanner) {
144 // // search until heredoc ends
146 // StringBuffer buf = new StringBuffer();
147 // char[] heredocIdent;
149 // ch = scanner.read();
150 // if (!Scanner.isPHPIdentifierStart((char)ch)) {
155 // while (Scanner.isPHPIdentifierPart((char)ch)) {
156 // buf.append((char)ch);
157 // ch = scanner.read();
159 // if (ch==ICharacterScanner.EOF) {
162 // heredocIdent = buf.toString().toCharArray();
164 // ch = scanner.read();
165 // if (ch==ICharacterScanner.EOF) {
168 // if (ch == '\n') { // heredoc could end after a newline
171 // if (pos == heredocIdent.length) {
174 // ch = scanner.read(); // ignore escaped character
175 // if (ch != heredocIdent[pos]) {
178 // if (ch==ICharacterScanner.EOF) {
188 * Check if lastCharacter/character are a PHP start or end token ( <? ...
192 * @param lastCharacter
196 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
197 if (lastCharacter == '<' && character == '?') {
201 } else if (lastCharacter == '?' && character == '>') {
210 protected class AccentStringRule implements IRule {
212 /** Token to return for this rule */
213 private final IToken fToken;
215 public AccentStringRule(IToken token) {
221 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
223 public IToken evaluate(ICharacterScanner scanner) {
225 int character = scanner.read();
227 if (character == '`') {
229 while (character != ICharacterScanner.EOF) {
230 character = scanner.read();
231 if (character == '\\') {
232 character = scanner.read();
233 } else if (character == '`') {
238 return Token.UNDEFINED;
241 return Token.UNDEFINED;
247 private class PHPWordRule extends WordRule {
248 private StringBuffer fBuffer = new StringBuffer();
250 protected Map fWordsIgnoreCase = new HashMap();
252 public PHPWordRule(IWordDetector detector) {
253 super(detector, Token.UNDEFINED);
256 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
257 super(detector, defaultToken);
261 * Adds a word and the token to be returned if it is detected.
264 * the word this rule will search for, may not be <code>null</code>
266 * the token to be returned if the word has been found, may not be
269 public void addWordIgnoreCase(String word, IToken token) {
270 Assert.isNotNull(word);
271 Assert.isNotNull(token);
273 fWordsIgnoreCase.put(word, token);
276 public IToken evaluate(ICharacterScanner scanner) {
277 int c = scanner.read();
278 boolean isVariable = false;
279 boolean isUnderscore = false;
286 return Token.UNDEFINED;
289 if (c == '=') { // <?=
290 return getToken(IPreferenceConstants.PHP_TAG);
292 if (c != 'p' && c != 'P') {
294 return getToken(IPreferenceConstants.PHP_TAG);
297 if (c != 'h' && c != 'H') {
300 return getToken(IPreferenceConstants.PHP_TAG);
303 if (c != 'p' && c != 'P') {
307 return getToken(IPreferenceConstants.PHP_TAG);
309 return getToken(IPreferenceConstants.PHP_TAG);
318 return getToken(IPreferenceConstants.PHP_TAG);
322 return Token.UNDEFINED;
324 if (fDetector.isWordStart((char) c)) {
328 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
330 fBuffer.setLength(0);
331 fBuffer.append((char) c);
336 while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
337 fBuffer.append((char) c);
344 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
346 return getToken(IPreferenceConstants.PHP_VARIABLE);
348 word = fBuffer.toString();
349 IToken token = (IToken) fWords.get(word);
353 token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
357 if (fDefaultToken.isUndefined())
358 unreadBuffer(scanner);
360 return fDefaultToken;
365 return Token.UNDEFINED;
369 // private PHPColorProvider fColorProvider;
371 private static String[] fgTokenProperties = { IPreferenceConstants.PHP_MULTILINE_COMMENT,
372 IPreferenceConstants.PHP_SINGLELINE_COMMENT, IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
373 IPreferenceConstants.PHP_FUNCTIONNAME, IPreferenceConstants.PHP_VARIABLE, IPreferenceConstants.PHP_VARIABLE_DOLLAR,
374 IPreferenceConstants.PHP_STRING_DQ, IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
375 IPreferenceConstants.PHP_CONSTANT, IPreferenceConstants.PHP_DEFAULT, IPreferenceConstants.PHP_OPERATOR,
376 IPreferenceConstants.PHP_BRACE_OPERATOR, IPreferenceConstants.PHP_KEYWORD_RETURN };
379 * Creates a PHP code scanner
381 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
382 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
383 super(manager, store);
388 * @see AbstractJavaScanner#getTokenProperties()
390 protected String[] getTokenProperties() {
391 return fgTokenProperties;
395 * @see AbstractJavaScanner#createRules()
397 protected List createRules() {
398 List rules = new ArrayList();
399 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
400 // Add rule for single line comments.
401 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
402 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
403 // Add rule for strings and character constants.
404 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
405 // rules.add(new SingleQuoteStringRule(token));
406 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
407 // rules.add(new DoubleQuoteStringRule(token));
408 rules.add(new AccentStringRule(token));
410 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
411 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
412 // Add generic whitespace rule.
413 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
414 // Add word rule for keywords, types, and constants.
415 token = getToken(IPreferenceConstants.PHP_DEFAULT);
416 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
418 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
419 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
420 Token type = getToken(IPreferenceConstants.PHP_TYPE);
421 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
423 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
424 // String strbuffer = null; unused
425 PHPElement elbuffer = null;
427 for (int i = 0; i < buffer.size(); i++) {
428 // while ((buffer != null)
429 // && (!buffer.isEmpty()
430 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
431 elbuffer = (PHPElement) buffer.get(i);
432 if (elbuffer instanceof PHPKeyword) {
433 name = ((PHPKeyword) elbuffer).getName();
434 if (!name.equals("return")) {
435 wordRule.addWord(name, keyword);
437 } else if (elbuffer instanceof PHPFunction) {
438 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(), functionName);
439 } else if (elbuffer instanceof PHPType) {
440 wordRule.addWord(elbuffer.getName(), type);
441 } else if (elbuffer instanceof PHPConstant) {
442 wordRule.addWord(elbuffer.getName(), constant);
446 // Add word rule for keyword 'return'.
447 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
448 wordRule.addWord("return", token);
450 // Add rule for operators and brackets (at the end !)
451 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR),
452 getToken(IPreferenceConstants.PHP_STRING_DQ)));
456 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));