b96202a46f367309e7e6be03773ce9ece61ba273
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / PHPPartitionScanner.java
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
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
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;
28
29 /**
30  * This scanner recognizes the JavaDoc comments and Java multi line comments.
31  */
32 public class PHPPartitionScanner extends RuleBasedPartitionScanner {
33
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";
38         /**
39          * Detector for empty comments.
40          */
41         static class EmptyCommentDetector implements IWordDetector {
42
43                 /* (non-Javadoc)
44                 * Method declared on IWordDetector
45                 */
46                 public boolean isWordStart(char c) {
47                         return (c == '/');
48                 }
49
50                 /* (non-Javadoc)
51                 * Method declared on IWordDetector
52                 */
53                 public boolean isWordPart(char c) {
54                         return (c == '*' || c == '/');
55                 }
56         };
57         
58         /**
59          * 
60          */
61         static class WordPredicateRule extends WordRule implements IPredicateRule {
62                 
63                 private IToken fSuccessToken;
64                 
65                 public WordPredicateRule(IToken successToken) {
66                         super(new EmptyCommentDetector());
67                         fSuccessToken= successToken;
68                         addWord("/**/", fSuccessToken);
69                 }
70                 
71                 /*
72                  * @see org.eclipse.jface.text.rules.IPredicateRule#evaluate(ICharacterScanner, boolean)
73                  */
74                 public IToken evaluate(ICharacterScanner scanner, boolean resume) {
75                         return super.evaluate(scanner);
76                 }
77
78                 /*
79                  * @see org.eclipse.jface.text.rules.IPredicateRule#getSuccessToken()
80                  */
81                 public IToken getSuccessToken() {
82                         return fSuccessToken;
83                 }
84         };
85
86         /**
87          * Creates the partitioner and sets up the appropriate rules.
88          */
89         public PHPPartitionScanner() {
90                 super();
91
92 //              IToken javaDoc= new Token(JAVA_DOC);
93                 IToken comment= new Token(JAVA_MULTILINE_COMMENT);
94     IToken php = new Token(PHP);
95  
96                 List rules= new ArrayList();
97
98                 // Add rule for single line comments.
99         //      rules.add(new EndOfLineRule("//", Token.UNDEFINED));
100
101                 // Add rule for strings and character constants.
102   //            rules.add(new SingleLineRule("\"", "\"", Token.UNDEFINED, '\\'));
103         //      rules.add(new SingleLineRule("'", "'", Token.UNDEFINED, '\\')); 
104
105                 // Add special case word rule.
106                 rules.add(new WordPredicateRule(comment));
107
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
115
116
117                 IPredicateRule[] result= new IPredicateRule[rules.size()];
118                 rules.toArray(result);
119                 setPredicateRules(result);
120         }
121 }