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.List;
17 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
18 import net.sourceforge.phpdt.ui.text.IColorManager;
19 import net.sourceforge.phpeclipse.IPreferenceConstants;
20 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
21 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
22 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.rules.EndOfLineRule;
27 import org.eclipse.jface.text.rules.ICharacterScanner;
28 import org.eclipse.jface.text.rules.IRule;
29 import org.eclipse.jface.text.rules.IToken;
30 import org.eclipse.jface.text.rules.IWordDetector;
31 import org.eclipse.jface.text.rules.MultiLineRule;
32 import org.eclipse.jface.text.rules.Token;
33 import org.eclipse.jface.text.rules.WhitespaceRule;
34 import org.eclipse.jface.text.rules.WordRule;
39 public class PHPCodeScanner extends AbstractJavaScanner {
42 * Rule to detect java operators.
46 protected class OperatorRule implements IRule {
49 private final char[] PHP_OPERATORS = {
75 /** Token to return for this rule */
76 private final IToken fToken;
78 /** Token to return for braces */
79 private final IToken fTokenBraces;
82 * Creates a new operator rule.
85 * Token to use for this rule
87 public OperatorRule(IToken token, IToken tokenBraces) {
89 fTokenBraces = tokenBraces;
94 * Is this character an operator character?
97 * Character to determine whether it is an operator character
98 * @return <code>true</code> iff the character is an operator, <code>false</code> otherwise.
100 public boolean isOperator(char character) {
101 for (int index = 0; index < PHP_OPERATORS.length; index++) {
102 if (PHP_OPERATORS[index] == character)
109 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
111 public IToken evaluate(ICharacterScanner scanner) {
113 int character = scanner.read();
114 if (character == '{' || character == '}') {
117 if (isOperator((char) character)) {
118 int lastCharacter = character;
119 character = scanner.read();
120 if (!isOperator((char) character)) {
124 if (checkPHPTag(scanner, lastCharacter, character)) {
125 return Token.UNDEFINED;
128 lastCharacter = character;
129 character = scanner.read();
130 if (checkPHPTag(scanner, lastCharacter, character)) {
133 } while (isOperator((char) character));
138 return Token.UNDEFINED;
143 * Check if lastCharacter/character are a PHP start or end token ( <? ... ?> )
146 * @param lastCharacter
150 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
151 if (lastCharacter == '<' && character == '?') {
155 } else if (lastCharacter == '?' && character == '>') {
164 protected class SingleQuoteStringRule implements IRule {
166 /** Token to return for this rule */
167 private final IToken fToken;
169 public SingleQuoteStringRule(IToken token) {
175 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
177 public IToken evaluate(ICharacterScanner scanner) {
179 int character = scanner.read();
181 if (character=='\'') {
184 character = scanner.read();
185 if (character == '\\') {
186 character = scanner.read();
187 } else if (character == '\'') {
194 return Token.UNDEFINED;
200 protected class AccentStringRule implements IRule {
202 /** Token to return for this rule */
203 private final IToken fToken;
205 public AccentStringRule(IToken token) {
211 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
213 public IToken evaluate(ICharacterScanner scanner) {
215 int character = scanner.read();
217 if (character=='`') {
220 character = scanner.read();
221 if (character == '\\') {
222 character = scanner.read();
223 } else if (character == '`') {
230 return Token.UNDEFINED;
236 protected class DoubleQuoteStringRule implements IRule {
238 /** Token to return for this rule */
239 private final IToken fToken;
241 public DoubleQuoteStringRule(IToken token) {
247 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
249 public IToken evaluate(ICharacterScanner scanner) {
251 int character = scanner.read();
253 if (character=='"') {
256 character = scanner.read();
257 if (character == '\\') {
258 character = scanner.read();
259 } else if (character == '"') {
266 return Token.UNDEFINED;
272 private class PHPWordRule extends WordRule {
273 private StringBuffer fBuffer = new StringBuffer();
275 public PHPWordRule(IWordDetector detector) {
276 super(detector, Token.UNDEFINED);
279 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
280 super(detector, defaultToken);
283 public IToken evaluate(ICharacterScanner scanner) {
284 int c = scanner.read();
285 boolean isVariable = false;
291 return Token.UNDEFINED;
294 if (c == '=') { // <?=
295 return getToken(IPreferenceConstants.PHP_TAG);
297 if (c != 'p' && c != 'P') {
299 return getToken(IPreferenceConstants.PHP_TAG);
302 if (c != 'h' && c != 'H') {
305 return getToken(IPreferenceConstants.PHP_TAG);
308 if (c != 'p' && c != 'P') {
312 return getToken(IPreferenceConstants.PHP_TAG);
314 return getToken(IPreferenceConstants.PHP_TAG);
323 return getToken(IPreferenceConstants.PHP_TAG);
327 return Token.UNDEFINED;
329 if (fDetector.isWordStart((char) c)) {
333 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
335 fBuffer.setLength(0);
337 fBuffer.append((char) c);
339 } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
343 return getToken(IPreferenceConstants.PHP_VARIABLE);
345 IToken token = (IToken) fWords.get(fBuffer.toString());
349 if (fDefaultToken.isUndefined())
350 unreadBuffer(scanner);
352 return fDefaultToken;
357 return Token.UNDEFINED;
361 //private PHPColorProvider fColorProvider;
363 private static String[] fgTokenProperties = {
364 IPreferenceConstants.PHP_MULTILINE_COMMENT,
365 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
366 IPreferenceConstants.PHP_TAG,
367 IPreferenceConstants.PHP_KEYWORD,
368 IPreferenceConstants.PHP_FUNCTIONNAME,
369 IPreferenceConstants.PHP_VARIABLE,
370 IPreferenceConstants.PHP_STRING_DQ,
371 IPreferenceConstants.PHP_STRING_SQ,
372 IPreferenceConstants.PHP_TYPE,
373 IPreferenceConstants.PHP_CONSTANT,
374 IPreferenceConstants.PHP_DEFAULT,
375 IPreferenceConstants.PHP_OPERATOR,
376 IPreferenceConstants.PHP_BRACE_OPERATOR,
377 IPreferenceConstants.PHP_KEYWORD_RETURN };
380 * Creates a PHP code scanner
382 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
383 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
384 super(manager, store);
389 * @see AbstractJavaScanner#getTokenProperties()
391 protected String[] getTokenProperties() {
392 return fgTokenProperties;
396 * @see AbstractJavaScanner#createRules()
398 protected List createRules() {
399 List rules = new ArrayList();
400 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
401 // Add rule for single line comments.
402 rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
403 rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
404 // Add rule for strings and character constants.
405 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
406 // rules.add(new SingleQuoteStringRule(token));
407 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
408 // rules.add(new DoubleQuoteStringRule(token));
409 rules.add(new AccentStringRule(token));
411 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
412 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
413 // Add generic whitespace rule.
414 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
415 // Add word rule for keywords, types, and constants.
416 token = getToken(IPreferenceConstants.PHP_DEFAULT);
417 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
419 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
420 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
421 Token type = getToken(IPreferenceConstants.PHP_TYPE);
422 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
424 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
425 // String strbuffer = null; unused
426 PHPElement elbuffer = null;
428 for (int i = 0; i < buffer.size(); i++) {
429 // while ((buffer != null)
430 // && (!buffer.isEmpty()
431 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
432 elbuffer = (PHPElement) buffer.get(i);
433 if (elbuffer instanceof PHPKeyword) {
434 name = ((PHPKeyword) elbuffer).getName();
435 if (!name.equals("return")) {
436 wordRule.addWord(name, keyword);
438 } else if (elbuffer instanceof PHPFunction) {
439 wordRule.addWord(((PHPFunction) elbuffer).getName(), functionName);
440 } else if (elbuffer instanceof PHPType) {
441 wordRule.addWord(elbuffer.getName(), type);
442 } else if (elbuffer instanceof PHPConstant) {
443 wordRule.addWord(elbuffer.getName(), constant);
447 // Add word rule for keyword 'return'.
448 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
449 wordRule.addWord("return", token);
451 // Add rule for operators and brackets (at the end !)
452 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR)));
456 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));