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.compiler.parser.Scanner;
20 import net.sourceforge.phpdt.internal.ui.text.AbstractJavaScanner;
21 import net.sourceforge.phpdt.ui.text.IColorManager;
22 import net.sourceforge.phpeclipse.IPreferenceConstants;
23 import net.sourceforge.phpeclipse.phpeditor.PHPSyntaxRdr;
24 import net.sourceforge.phpeclipse.phpeditor.util.PHPWhitespaceDetector;
25 import net.sourceforge.phpeclipse.phpeditor.util.PHPWordDetector;
27 import org.eclipse.jface.preference.IPreferenceStore;
28 import org.eclipse.jface.text.Assert;
29 import org.eclipse.jface.text.BadLocationException;
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 ',', '|', '&', '^', '%', '~', '@' };
55 /** Token to return for this rule */
56 private final IToken fToken;
58 /** Token to return for braces */
59 private final IToken fTokenBraces;
61 /** Token to return for heredocs */
62 private final IToken fTokenHeredoc;
65 * Creates a new operator rule.
68 * Token to use for this rule
72 public OperatorRule(IToken token, IToken tokenBraces, 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 own partitions for single quoted
107 // or double quoted strings:
109 // if (lastCharacter == '<' && character == '<') {
110 // int heredocCharacter = scanner.read();
111 // if (heredocCharacter == '<') {
112 // // start of heredoc comment;
113 // if (readHEREDOC(scanner)) {
114 // return fTokenHeredoc;
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 if (character == ICharacterScanner.EOF) {
136 } while (isOperator((char) character));
141 return Token.UNDEFINED;
145 // private boolean readHEREDOC(ICharacterScanner scanner) {
146 // // search until heredoc ends
148 // StringBuffer buf = new StringBuffer();
149 // char[] heredocIdent;
151 // ch = scanner.read();
152 // if (!Scanner.isPHPIdentifierStart((char)ch)) {
157 // while (Scanner.isPHPIdentifierPart((char)ch)) {
158 // buf.append((char)ch);
159 // ch = scanner.read();
161 // if (ch==ICharacterScanner.EOF) {
164 // heredocIdent = buf.toString().toCharArray();
166 // ch = scanner.read();
167 // if (ch==ICharacterScanner.EOF) {
170 // if (ch == '\n') { // heredoc could end after a newline
173 // if (pos == heredocIdent.length) {
176 // ch = scanner.read(); // ignore escaped character
177 // if (ch != heredocIdent[pos]) {
180 // if (ch==ICharacterScanner.EOF) {
190 * Check if lastCharacter/character are a PHP start or end token ( <? ...
194 * @param lastCharacter
198 private boolean checkPHPTag(ICharacterScanner scanner, int lastCharacter, int character) {
199 if (lastCharacter == '<' && character == '?') {
203 } else if (lastCharacter == '?' && character == '>') {
212 protected class AccentStringRule implements IRule {
214 /** Token to return for this rule */
215 private final IToken fToken;
217 public AccentStringRule(IToken token) {
223 * @see org.eclipse.jface.text.rules.IRule#evaluate(org.eclipse.jface.text.rules.ICharacterScanner)
225 public IToken evaluate(ICharacterScanner scanner) {
227 int character = scanner.read();
229 if (character == '`') {
231 while (character != ICharacterScanner.EOF) {
232 character = scanner.read();
233 if (character == '\\') {
234 character = scanner.read();
235 } else if (character == '`') {
240 return Token.UNDEFINED;
243 return Token.UNDEFINED;
249 private class PHPWordRule extends WordRule {
250 private StringBuffer fBuffer = new StringBuffer();
252 protected Map fWordsIgnoreCase = new HashMap();
254 public PHPWordRule(IWordDetector detector) {
255 super(detector, Token.UNDEFINED);
258 public PHPWordRule(IWordDetector detector, IToken defaultToken) {
259 super(detector, defaultToken);
263 * Adds a word and the token to be returned if it is detected.
266 * the word this rule will search for, may not be <code>null</code>
268 * the token to be returned if the word has been found, may not be
271 public void addWordIgnoreCase(String word, IToken token) {
272 Assert.isNotNull(word);
273 Assert.isNotNull(token);
275 fWordsIgnoreCase.put(word, token);
278 public IToken evaluate(ICharacterScanner scanner) {
279 int c = scanner.read();
280 boolean isVariable = false;
281 boolean isUnderscore = false;
288 return Token.UNDEFINED;
291 if (c == '=') { // <?=
292 return getToken(IPreferenceConstants.PHP_TAG);
294 if (c != 'p' && c != 'P') {
296 return getToken(IPreferenceConstants.PHP_TAG);
299 if (c != 'h' && c != 'H') {
302 return getToken(IPreferenceConstants.PHP_TAG);
305 if (c != 'p' && c != 'P') {
309 return getToken(IPreferenceConstants.PHP_TAG);
311 return getToken(IPreferenceConstants.PHP_TAG);
320 return getToken(IPreferenceConstants.PHP_TAG);
324 return Token.UNDEFINED;
326 if (fDetector.isWordStart((char) c)) {
330 if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
332 fBuffer.setLength(0);
333 fBuffer.append((char) c);
338 while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c)) {
339 fBuffer.append((char) c);
346 return getToken(IPreferenceConstants.PHP_VARIABLE_DOLLAR);
348 return getToken(IPreferenceConstants.PHP_VARIABLE);
350 word = fBuffer.toString();
351 IToken token = (IToken) fWords.get(word);
355 token = (IToken) fWordsIgnoreCase.get(word.toLowerCase());
359 if (fDefaultToken.isUndefined())
360 unreadBuffer(scanner);
362 return fDefaultToken;
367 return Token.UNDEFINED;
371 // private PHPColorProvider fColorProvider;
373 private static String[] fgTokenProperties = { IPreferenceConstants.PHP_MULTILINE_COMMENT,
374 IPreferenceConstants.PHP_SINGLELINE_COMMENT, IPreferenceConstants.PHP_TAG, IPreferenceConstants.PHP_KEYWORD,
375 IPreferenceConstants.PHP_FUNCTIONNAME, IPreferenceConstants.PHP_VARIABLE, IPreferenceConstants.PHP_VARIABLE_DOLLAR,
376 IPreferenceConstants.PHP_STRING_DQ, IPreferenceConstants.PHP_STRING_SQ, IPreferenceConstants.PHP_TYPE,
377 IPreferenceConstants.PHP_CONSTANT, IPreferenceConstants.PHP_DEFAULT, IPreferenceConstants.PHP_OPERATOR,
378 IPreferenceConstants.PHP_BRACE_OPERATOR, IPreferenceConstants.PHP_KEYWORD_RETURN };
381 * Creates a PHP code scanner
383 // public PHPCodeScanner(JavaColorManager provider, IPreferenceStore store) {
384 public PHPCodeScanner(IColorManager manager, IPreferenceStore store) {
385 super(manager, store);
390 * @see AbstractJavaScanner#getTokenProperties()
392 protected String[] getTokenProperties() {
393 return fgTokenProperties;
397 * @see AbstractJavaScanner#createRules()
399 protected List createRules() {
400 List rules = new ArrayList();
401 Token token = getToken(IPreferenceConstants.PHP_SINGLELINE_COMMENT);
402 // Add rule for single line comments.
403 // rules.add(new EndOfLineRule("//", token)); //$NON-NLS-1$
404 // rules.add(new EndOfLineRule("#", token)); //$NON-NLS-1$
405 // Add rule for strings and character constants.
406 // token = getToken(IPreferenceConstants.PHP_STRING_SQ);
407 // rules.add(new SingleQuoteStringRule(token));
408 // token = getToken(IPreferenceConstants.PHP_STRING_DQ);
409 // rules.add(new DoubleQuoteStringRule(token));
410 rules.add(new AccentStringRule(token));
412 token = getToken(IPreferenceConstants.PHP_MULTILINE_COMMENT);
413 rules.add(new MultiLineRule("/*", "*/", token)); //$NON-NLS-2$ //$NON-NLS-1$
414 // Add generic whitespace rule.
415 rules.add(new WhitespaceRule(new PHPWhitespaceDetector()));
416 // Add word rule for keywords, types, and constants.
417 token = getToken(IPreferenceConstants.PHP_DEFAULT);
418 PHPWordRule wordRule = new PHPWordRule(new PHPWordDetector(), token);
420 Token keyword = getToken(IPreferenceConstants.PHP_KEYWORD);
421 Token functionName = getToken(IPreferenceConstants.PHP_FUNCTIONNAME);
422 Token type = getToken(IPreferenceConstants.PHP_TYPE);
423 Token constant = getToken(IPreferenceConstants.PHP_CONSTANT);
425 ArrayList buffer = PHPSyntaxRdr.getSyntaxData();
426 // String strbuffer = null; unused
427 PHPElement elbuffer = null;
429 for (int i = 0; i < buffer.size(); i++) {
430 // while ((buffer != null)
431 // && (!buffer.isEmpty()
432 // && ((elbuffer = (PHPElement) buffer.remove(0)) != null))) {
433 elbuffer = (PHPElement) buffer.get(i);
434 if (elbuffer instanceof PHPKeyword) {
435 name = ((PHPKeyword) elbuffer).getName();
436 if (!name.equals("return")) {
437 wordRule.addWord(name, keyword);
439 } else if (elbuffer instanceof PHPFunction) {
440 wordRule.addWordIgnoreCase(((PHPFunction) elbuffer).getName(), functionName);
441 } else if (elbuffer instanceof PHPType) {
442 wordRule.addWord(elbuffer.getName(), type);
443 } else if (elbuffer instanceof PHPConstant) {
444 wordRule.addWord(elbuffer.getName(), constant);
448 // Add word rule for keyword 'return'.
449 token = getToken(IPreferenceConstants.PHP_KEYWORD_RETURN);
450 wordRule.addWord("return", token);
452 // Add rule for operators and brackets (at the end !)
453 rules.add(new OperatorRule(getToken(IPreferenceConstants.PHP_OPERATOR), getToken(IPreferenceConstants.PHP_BRACE_OPERATOR),
454 getToken(IPreferenceConstants.PHP_STRING_DQ)));
458 setDefaultReturnToken(getToken(IPreferenceConstants.PHP_DEFAULT));