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;
28 //import org.eclipse.jface.text.Assert;
29 import org.eclipse.core.runtime.Assert;
30 import org.eclipse.jface.text.rules.ICharacterScanner;
31 import org.eclipse.jface.text.rules.IRule;
32 import org.eclipse.jface.text.rules.IToken;
33 import org.eclipse.jface.text.rules.IWordDetector;
34 import org.eclipse.jface.text.rules.MultiLineRule;
35 import org.eclipse.jface.text.rules.Token;
36 import org.eclipse.jface.text.rules.WhitespaceRule;
37 import org.eclipse.jface.text.rules.WordRule;
42 public class PHPCodeScanner extends AbstractJavaScanner {
45 * Rule to detect java operators.
49 protected class OperatorRule implements IRule {
52 private final char[] PHP_OPERATORS = { ';', '(', ')', '.', '=', '/',
53 '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',',
54 '|', '&', '^', '%', '~', '@' };
56 /** Token to return for this rule */
57 private final IToken fToken;
59 /** Token to return for braces */
60 private final IToken fTokenBraces;
62 /** Token to return for heredocs */
63 private final IToken fTokenHeredoc;
66 * Creates a new operator rule.
69 * Token to use for this rule
73 public OperatorRule(IToken token, IToken tokenBraces,
74 IToken tokenHeredoc) {
76 fTokenBraces = tokenBraces;
77 fTokenHeredoc = tokenHeredoc;
81 * Is this character an operator character?
84 * Character to determine whether it is an operator character
85 * @return <code>true</code> iff the character is an operator,
86 * <code>false</code> otherwise.
88 public boolean isOperator(char character) {
89 for (int index = 0; index < PHP_OPERATORS.length; index++) {
90 if (PHP_OPERATORS[index] == character)
97 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
99 public IToken evaluate(ICharacterScanner scanner) {
101 int character = scanner.read();
102 if (character == '{' || character == '}') {
105 if (isOperator((char) character)) {
106 int lastCharacter = character;
107 character = scanner.read();
108 // the readHEREDOC(scanner) call doesn't work, if we have our
109 // own partitions for single quoted
110 // or double quoted strings:
112 // if (lastCharacter == '<' && character == '<') {
113 // int heredocCharacter = scanner.read();
114 // if (heredocCharacter == '<') {
115 // // start of heredoc comment;
116 // if (readHEREDOC(scanner)) {
117 // return fTokenHeredoc;
123 if (!isOperator((char) character)) {
127 if (checkPHPTag(scanner, lastCharacter, character)) {
128 return Token.UNDEFINED;
131 lastCharacter = character;
132 character = scanner.read();
133 if (checkPHPTag(scanner, lastCharacter, character)) {
136 if (character == ICharacterScanner.EOF) {
139 } while (isOperator((char) character));
144 return Token.UNDEFINED;
148 // private boolean readHEREDOC(ICharacterScanner scanner) {
149 // // search until heredoc ends
151 // StringBuffer buf = new StringBuffer();
152 // char[] heredocIdent;
154 // ch = scanner.read();
155 // if (!Scanner.isPHPIdentifierStart((char)ch)) {
160 // while (Scanner.isPHPIdentifierPart((char)ch)) {
161 // buf.append((char)ch);
162 // ch = scanner.read();
164 // if (ch==ICharacterScanner.EOF) {
167 // heredocIdent = buf.toString().toCharArray();
169 // ch = scanner.read();
170 // if (ch==ICharacterScanner.EOF) {
173 // if (ch == '\n') { // heredoc could end after a newline
176 // if (pos == heredocIdent.length) {
179 // ch = scanner.read(); // ignore escaped character
180 // if (ch != heredocIdent[pos]) {
183 // if (ch==ICharacterScanner.EOF) {
193 * Check if lastCharacter/character are a PHP start or end token ( <?
197 * @param lastCharacter
201 private boolean checkPHPTag(ICharacterScanner scanner,
202 int lastCharacter, int character) {
203 if (lastCharacter == '<' && character == '?') {
207 } else if (lastCharacter == '?' && character == '>') {
216 protected class AccentStringRule implements IRule {
218 /** Token to return for this rule */
219 private final IToken fToken;
221 public AccentStringRule(IToken token) {
227 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
229 public IToken evaluate(ICharacterScanner scanner) {
231 int character = scanner.read();
233 if (character == '`') {
235 while (character != ICharacterScanner.EOF) {
236 character = scanner.read();
237 if (character == '\\') {
238 character = scanner.read();
239 } else if (character == '`') {
244 return Token.UNDEFINED;
247 return Token.UNDEFINED;
253 private class PHPWordRule extends WordRule {
254 private StringBuffer fBuffer = new StringBuffer();
256 protected Map fWordsIgnoreCase = new HashMap();
258 public PHPWordRule(IWordDetector detector) {
259 super(detector, Token.UNDEFINED);
262 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
263 super(detector, defaultToken);
267 * Adds a word and the token to be returned if it is detected.
270 * the word this rule will search for, may not be
273 * the token to be returned if the word has been found, may
274 * not be <code>null</code>
276 public void addWordIgnoreCase(String word, IToken token) {
277 Assert.isNotNull(word);
278 Assert.isNotNull(token);
280 fWordsIgnoreCase.put(word, token);
283 public IToken evaluate(ICharacterScanner scanner) {
284 int c = scanner.read();
285 boolean isVariable = false;
286 boolean isUnderscore = false;
293 return Token.UNDEFINED;
296 if (c == '=') { // <?=
297 return getToken(IPreferenceConstants.PHP_TAG);
299 if (c != 'p' && c != 'P') {
301 return getToken(IPreferenceConstants.PHP_TAG);
304 if (c != 'h' && c != 'H') {
307 return getToken(IPreferenceConstants.PHP_TAG);
310 if (c != 'p' && c != 'P') {
314 return getToken(IPreferenceConstants.PHP_TAG);
316 return getToken(IPreferenceConstants.PHP_TAG);
325 return getToken(IPreferenceConstants.PHP_TAG);
329 return Token.UNDEFINED;
331 if (fDetector.isWordStart((char) c)) {
335 if (fColumn == UNDEFINED
336 || (fColumn == scanner.getColumn() - 1)) {
338 fBuffer.setLength(0);
339 fBuffer.append((char) c);
344 while (c != ICharacterScanner.EOF
345 && fDetector.isWordPart((char) c)) {
346 fBuffer.append((char) c);
353 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
355 return getToken(IPreferenceConstants.PHP_VARIABLE);
357 word = fBuffer.toString();
358 IToken token = (IToken) fWords.get(word);
362 token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
366 if (fDefaultToken.isUndefined())
367 unreadBuffer(scanner);
369 return fDefaultToken;
374 return Token.UNDEFINED;
378 // private PHPColorProvider fColorProvider;
380 private static String[] fgTokenProperties = {
381 IPreferenceConstants.PHP_MULTILINE_COMMENT,
382 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
383 IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
384 IPreferenceConstants.PHP_FUNCTIONNAME,
385 IPreferenceConstants.PHP_VARIABLE,
386 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
387 IPreferenceConstants.PHP_STRING_DQ,
388 IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
389 IPreferenceConstants.PHP_CONSTANT,
390 IPreferenceConstants.PHP_DEFAULT,
391 IPreferenceConstants.PHP_OPERATOR,
392 IPreferenceConstants.PHP_BRACE_OPERATOR,
393 IPreferenceConstants.PHP_KEYWORD_RETURN };
396 * Creates a PHP code scanner
398 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
400 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
401 super(manager, store);
406 * @see AbstractJavaScanner#getTokenProperties()
408 protected String[] getTokenProperties() {
409 return fgTokenProperties;
413 * @see AbstractJavaScanner#createRules()
415 protected List createRules() {
416 List rules = new ArrayList();
417 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
418 // Add rule for single line comments.
419 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
420 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
421 // Add rule for strings and character constants.
422 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
423 // rules.add(new SingleQuoteStringRule(token));
424 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
425 // rules.add(new DoubleQuoteStringRule(token));
426 rules.add(new AccentStringRule(token));
428 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
429 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
430 // Add generic whitespace rule.
431 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
432 // Add word rule for keywords, types, and constants.
433 token = getToken(IPreferenceConstants.PHP_DEFAULT);
434 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
436 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
437 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
438 Token type = getToken(IPreferenceConstants.PHP_TYPE);
439 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
441 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
442 // String strbuffer = null; unused
443 PHPElement elbuffer = null;
445 for (int i = 0; i < buffer.size(); i++) {
446 // while ((buffer != null)
447 // && (!buffer.isEmpty()
448 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
449 elbuffer = (PHPElement) buffer.get(i);
450 if (elbuffer instanceof PHPKeyword) {
451 name = ((PHPKeyword) elbuffer).getName();
452 if (!name.equals("return")) {
453 wordRule.addWord(name, keyword);
455 } else if (elbuffer instanceof PHPFunction) {
456 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(),
458 } else if (elbuffer instanceof PHPType) {
459 wordRule.addWord(elbuffer.getName(), type);
460 } else if (elbuffer instanceof PHPConstant) {
461 wordRule.addWord(elbuffer.getName(), constant);
465 // Add word rule for keyword 'return'.
466 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
467 wordRule.addWord("return", token);
469 // Add rule for operators and brackets (at the end !)
470 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR),
471 getToken(IPreferenceConstants.PHP_BRACE_OPERATOR),
472 getToken(IPreferenceConstants.PHP_STRING_DQ)));
476 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));