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,
384 IPreferenceConstants.PHP_KEYWORD,
385 IPreferenceConstants.PHP_FUNCTIONNAME,
386 IPreferenceConstants.PHP_VARIABLE,
387 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
388 IPreferenceConstants.PHP_STRING_DQ,
389 IPreferenceConstants.PHP_STRING_SQ,
390 IPreferenceConstants.PHP_TYPE,
391 IPreferenceConstants.PHP_CONSTANT,
392 IPreferenceConstants.PHP_DEFAULT,
393 IPreferenceConstants.PHP_OPERATOR,
394 IPreferenceConstants.PHP_BRACE_OPERATOR,
395 IPreferenceConstants.PHP_KEYWORD_RETURN };
398 * Creates a PHP code scanner
400 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
402 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
403 super(manager, store);
408 * @see AbstractJavaScanner#getTokenProperties()
410 protected String[] getTokenProperties() {
411 return fgTokenProperties;
415 * @see AbstractJavaScanner#createRules()
417 protected List createRules() {
418 List rules = new ArrayList();
419 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
420 // Add rule for single line comments.
421 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
422 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
423 // Add rule for strings and character constants.
424 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
425 // rules.add(new SingleQuoteStringRule(token));
426 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
427 // rules.add(new DoubleQuoteStringRule(token));
428 rules.add(new AccentStringRule(token));
430 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
431 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
432 // Add generic whitespace rule.
433 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
434 // Add word rule for keywords, types, and constants.
435 token = getToken(IPreferenceConstants.PHP_DEFAULT);
436 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
438 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
439 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
440 Token type = getToken(IPreferenceConstants.PHP_TYPE);
441 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
443 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
444 // String strbuffer = null; unused
445 PHPElement elbuffer = null;
447 for (int i = 0; i < buffer.size(); i++) {
448 // while ((buffer != null)
449 // && (!buffer.isEmpty()
450 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
451 elbuffer = (PHPElement) buffer.get(i);
452 if (elbuffer instanceof PHPKeyword) {
453 name = ((PHPKeyword) elbuffer).getName();
454 if (!name.equals("return")) {
455 wordRule.addWord(name, keyword);
457 } else if (elbuffer instanceof PHPFunction) {
458 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(),
460 } else if (elbuffer instanceof PHPType) {
461 wordRule.addWord(elbuffer.getName(), type);
462 } else if (elbuffer instanceof PHPConstant) {
463 wordRule.addWord(elbuffer.getName(), constant);
467 // Add word rule for keyword 'return'.
468 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
469 wordRule.addWord("return", token);
471 // Add rule for operators and brackets (at the end !)
472 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR),
473 getToken(IPreferenceConstants.PHP_BRACE_OPERATOR),
474 getToken(IPreferenceConstants.PHP_STRING_DQ)));
478 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));