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 '\\', '+', '-', '*', '[', ']', '<', '>', ':', '?', '!', ',',
52 '|', '&', '^', '%', '~', '@' };
54 /** Token to return for this rule */
55 private final IToken fToken;
57 /** Token to return for braces */
58 private final IToken fTokenBraces;
60 /** Token to return for heredocs */
61 private final IToken fTokenHeredoc;
64 * Creates a new operator rule.
67 * Token to use for this rule
71 public OperatorRule(IToken token, IToken tokenBraces,
72 IToken tokenHeredoc) {
74 fTokenBraces = tokenBraces;
75 fTokenHeredoc = tokenHeredoc;
79 * Is this character an operator character?
82 * Character to determine whether it is an operator character
83 * @return <code>true</code> iff the character is an operator,
84 * <code>false</code> otherwise.
86 public boolean isOperator(char character) {
87 for (int index = 0; index < PHP_OPERATORS.length; index++) {
88 if (PHP_OPERATORS[index] == character)
95 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
97 public IToken evaluate(ICharacterScanner scanner) {
99 int character = scanner.read();
100 if (character == '{' || character == '}') {
103 if (isOperator((char) character)) {
104 int lastCharacter = character;
105 character = scanner.read();
106 // the readHEREDOC(scanner) call doesn't work, if we have our
107 // own partitions for single quoted
108 // or double quoted strings:
110 // if (lastCharacter == '<' && character == '<') {
111 // int heredocCharacter = scanner.read();
112 // if (heredocCharacter == '<') {
113 // // start of heredoc comment;
114 // if (readHEREDOC(scanner)) {
115 // return fTokenHeredoc;
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 if (character == ICharacterScanner.EOF) {
137 } while (isOperator((char) character));
142 return Token.UNDEFINED;
146 // private boolean readHEREDOC(ICharacterScanner scanner) {
147 // // search until heredoc ends
149 // StringBuffer buf = new StringBuffer();
150 // char[] heredocIdent;
152 // ch = scanner.read();
153 // if (!Scanner.isPHPIdentifierStart((char)ch)) {
158 // while (Scanner.isPHPIdentifierPart((char)ch)) {
159 // buf.append((char)ch);
160 // ch = scanner.read();
162 // if (ch==ICharacterScanner.EOF) {
165 // heredocIdent = buf.toString().toCharArray();
167 // ch = scanner.read();
168 // if (ch==ICharacterScanner.EOF) {
171 // if (ch == '\n') { // heredoc could end after a newline
174 // if (pos == heredocIdent.length) {
177 // ch = scanner.read(); // ignore escaped character
178 // if (ch != heredocIdent[pos]) {
181 // if (ch==ICharacterScanner.EOF) {
191 * Check if lastCharacter/character are a PHP start or end token ( <?
195 * @param lastCharacter
199 private boolean checkPHPTag(ICharacterScanner scanner,
200 int lastCharacter, int character) {
201 if (lastCharacter == '<' && character == '?') {
205 } else if (lastCharacter == '?' && character == '>') {
214 protected class AccentStringRule implements IRule {
216 /** Token to return for this rule */
217 private final IToken fToken;
219 public AccentStringRule(IToken token) {
225 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
227 public IToken evaluate(ICharacterScanner scanner) {
229 int character = scanner.read();
231 if (character == '`') {
233 while (character != ICharacterScanner.EOF) {
234 character = scanner.read();
235 if (character == '\\') {
236 character = scanner.read();
237 } else if (character == '`') {
242 return Token.UNDEFINED;
245 return Token.UNDEFINED;
251 private class PHPWordRule extends WordRule {
252 private StringBuffer fBuffer = new StringBuffer();
254 protected Map fWordsIgnoreCase = new HashMap();
256 public PHPWordRule(IWordDetector detector) {
257 super(detector, Token.UNDEFINED);
260 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
261 super(detector, defaultToken);
265 * Adds a word and the token to be returned if it is detected.
268 * the word this rule will search for, may not be
271 * the token to be returned if the word has been found, may
272 * not be <code>null</code>
274 public void addWordIgnoreCase(String word, IToken token) {
275 Assert.isNotNull(word);
276 Assert.isNotNull(token);
278 fWordsIgnoreCase.put(word, token);
281 public IToken evaluate(ICharacterScanner scanner) {
282 int c = scanner.read();
283 boolean isVariable = false;
284 boolean isUnderscore = 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
334 || (fColumn == scanner.getColumn() - 1)) {
336 fBuffer.setLength(0);
337 fBuffer.append((char) c);
342 while (c != ICharacterScanner.EOF
343 && fDetector.isWordPart((char) c)) {
344 fBuffer.append((char) c);
351 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
353 return getToken(IPreferenceConstants.PHP_VARIABLE);
355 word = fBuffer.toString();
356 IToken token = (IToken) fWords.get(word);
360 token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
364 if (fDefaultToken.isUndefined())
365 unreadBuffer(scanner);
367 return fDefaultToken;
372 return Token.UNDEFINED;
376 // private PHPColorProvider fColorProvider;
378 private static String[] fgTokenProperties = {
379 IPreferenceConstants.PHP_MULTILINE_COMMENT,
380 IPreferenceConstants.PHP_SINGLELINE_COMMENT,
381 IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
382 IPreferenceConstants.PHP_FUNCTIONNAME,
383 IPreferenceConstants.PHP_VARIABLE,
384 IPreferenceConstants.PHP_VARIABLE_DOLLAR,
385 IPreferenceConstants.PHP_STRING_DQ,
386 IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
387 IPreferenceConstants.PHP_CONSTANT,
388 IPreferenceConstants.PHP_DEFAULT,
389 IPreferenceConstants.PHP_OPERATOR,
390 IPreferenceConstants.PHP_BRACE_OPERATOR,
391 IPreferenceConstants.PHP_KEYWORD_RETURN };
394 * Creates a PHP code scanner
396 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store)
398 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
399 super(manager, store);
404 * @see AbstractJavaScanner#getTokenProperties()
406 protected String[] getTokenProperties() {
407 return fgTokenProperties;
411 * @see AbstractJavaScanner#createRules()
413 protected List createRules() {
414 List rules = new ArrayList();
415 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
416 // Add rule for single line comments.
417 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
418 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
419 // Add rule for strings and character constants.
420 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
421 // rules.add(new SingleQuoteStringRule(token));
422 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
423 // rules.add(new DoubleQuoteStringRule(token));
424 rules.add(new AccentStringRule(token));
426 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
427 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
428 // Add generic whitespace rule.
429 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
430 // Add word rule for keywords, types, and constants.
431 token = getToken(IPreferenceConstants.PHP_DEFAULT);
432 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
434 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
435 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
436 Token type = getToken(IPreferenceConstants.PHP_TYPE);
437 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
439 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
440 // String strbuffer = null; unused
441 PHPElement elbuffer = null;
443 for (int i = 0; i < buffer.size(); i++) {
444 // while ((buffer != null)
445 // && (!buffer.isEmpty()
446 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
447 elbuffer = (PHPElement) buffer.get(i);
448 if (elbuffer instanceof PHPKeyword) {
449 name = ((PHPKeyword) elbuffer).getName();
450 if (!name.equals("return")) {
451 wordRule.addWord(name, keyword);
453 } else if (elbuffer instanceof PHPFunction) {
454 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(),
456 } else if (elbuffer instanceof PHPType) {
457 wordRule.addWord(elbuffer.getName(), type);
458 } else if (elbuffer instanceof PHPConstant) {
459 wordRule.addWord(elbuffer.getName(), constant);
463 // Add word rule for keyword 'return'.
464 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
465 wordRule.addWord("return", token);
467 // Add rule for operators and brackets (at the end !)
468 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR),
469 getToken(IPreferenceConstants.PHP_BRACE_OPERATOR),
470 getToken(IPreferenceConstants.PHP_STRING_DQ)));
474 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));