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
10 Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.eclipse.jface.text.rules.EndOfLineRule;
17 import org.eclipse.jface.text.rules.ICharacterScanner;
18 import org.eclipse.jface.text.rules.IPredicateRule;
19 import org.eclipse.jface.text.rules.IRule;
20 import org.eclipse.jface.text.rules.IToken;
21 import org.eclipse.jface.text.rules.IWordDetector;
22 import org.eclipse.jface.text.rules.MultiLineRule;
23 import org.eclipse.jface.text.rules.RuleBasedPartitionScanner;
24 import org.eclipse.jface.text.rules.RuleBasedScanner;
25 import org.eclipse.jface.text.rules.SingleLineRule;
26 import org.eclipse.jface.text.rules.Token;
27 import org.eclipse.jface.text.rules.WordRule;
30 * This scanner recognizes the JavaDoc comments and Java multi line comments.
32 public class PHPPartitionScanner extends RuleBasedPartitionScanner {
34 private final static String SKIP= "__skip"; //$NON-NLS-1$
35 public final static String JAVA_MULTILINE_COMMENT= "__html_multiline_comment"; //$NON-NLS-1$
36 // public final static String JAVA_DOC= "__java_javadoc"; //$NON-NLS-1$
37 public final static String PHP= "__php";
39 * Detector for empty comments.
41 static class EmptyCommentDetector implements IWordDetector {
44 * Method declared on IWordDetector
46 public boolean isWordStart(char c) {
51 * Method declared on IWordDetector
53 public boolean isWordPart(char c) {
54 return (c == '*' || c == '/');
61 static class WordPredicateRule extends WordRule implements IPredicateRule {
63 private IToken fSuccessToken;
65 public WordPredicateRule(IToken successToken) {
66 super(new EmptyCommentDetector());
67 fSuccessToken= successToken;
68 addWord("/**/", fSuccessToken);
72 * @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(ICharacterScanner, boolean)
74 public IToken evaluate(ICharacterScanner scanner, boolean resume) {
75 return super.evaluate(scanner);
79 * @see org.eclipse.jface.text.rules.IPredicateRule#getSuccessToken()
81 public IToken getSuccessToken() {
87 * Creates the partitioner and sets up the appropriate rules.
89 public PHPPartitionScanner() {
92 // IToken javaDoc= new Token(JAVA_DOC);
93 IToken comment= new Token(JAVA_MULTILINE_COMMENT);
94 IToken php = new Token(PHP);
96 List rules= new ArrayList();
98 // Add rule for single line comments.
99 // rules.add(new EndOfLineRule("//", Token.UNDEFINED));
101 // Add rule for strings and character constants.
102 // rules.add(new SingleLineRule("\"", "\"", Token.UNDEFINED, '\\'));
103 // rules.add(new SingleLineRule("'", "'", Token.UNDEFINED, '\\'));
105 // Add special case word rule.
106 rules.add(new WordPredicateRule(comment));
108 // Add rules for multi-line comments and javadoc.
109 //rules.add(new MultiLineRule("/**", "*/", javaDoc));
110 rules.add(new MultiLineRule("<!--", "-->", comment));
111 rules.add(new MultiLineRule("<? ", "?>", php));
112 rules.add(new MultiLineRule("<?php", "?>", php));
113 rules.add(new MultiLineRule("<?PHP", "?>", php));
114 //Add rule for processing instructions
117 IPredicateRule[] result= new IPredicateRule[rules.size()];
118 rules.toArray(result);
119 setPredicateRules(result);