001: /*
002: * PHPTokenMarker.java - Token marker for PHP
003: * Copyright (C) 1999 Clancy Malcolm
004: * Based on HTMLTokenMarker.java Copyright (C) 1998, 1999 Slava Pestov
005: *
006: * You may use and modify this package for any purpose. Redistribution is
007: * permitted, in both source and binary form, provided that this notice
008: * remains intact in all source distributions of this package.
009: */
010:
011: package org.syntax.jedit.tokenmarker;
012:
013: import javax.swing.text.Segment;
014:
015: import org.syntax.jedit.KeywordMap;
016: import org.syntax.jedit.SyntaxUtilities;
017:
018: /**
019: * PHP token marker.
020: *
021: * @author Clancy Malcolm
022: * @version $Id: PHPTokenMarker.java,v 1.1 1999/12/14 04:20:35 sp Exp $
023: */
024: public class PHPTokenMarker extends TokenMarker {
025: public static final byte SCRIPT = Token.INTERNAL_FIRST;
026:
027: public byte markTokensImpl(byte token, Segment line, int lineIndex) {
028: char[] array = line.array;
029: int offset = line.offset;
030: lastOffset = offset;
031: lastKeyword = offset;
032: int length = line.count + offset;
033: boolean backslash = false;
034:
035: loop: for (int i = offset; i < length; i++) {
036: int i1 = (i + 1);
037:
038: char c = array[i];
039: if (c == '\\') {
040: backslash = !backslash;
041: continue;
042: }
043:
044: switch (token) {
045: case Token.NULL: // HTML text
046: backslash = false;
047: switch (c) {
048: case '<':
049: addToken(i - lastOffset, token);
050: lastOffset = lastKeyword = i;
051: if (SyntaxUtilities.regionMatches(false, line, i1,
052: "!--")) {
053: i += 3;
054: token = Token.COMMENT1;
055: } else if (SyntaxUtilities.regionMatches(true,
056: line, i1, "?php")) {
057: addToken(5, Token.LABEL);
058: lastOffset = lastKeyword = (i += 4) + 1;
059: token = SCRIPT;
060: } else if (SyntaxUtilities.regionMatches(true,
061: line, i1, "?")) {
062: addToken(2, Token.LABEL);
063: lastOffset = lastKeyword = (i += 1) + 1;
064: token = SCRIPT;
065: } else if (SyntaxUtilities.regionMatches(true,
066: line, i1, "script>")) {
067: addToken(8, Token.LABEL);
068: lastOffset = lastKeyword = (i += 7) + 1;
069: token = SCRIPT;
070: } else {
071: token = Token.KEYWORD1;
072: }
073: break;
074: case '&':
075: addToken(i - lastOffset, token);
076: lastOffset = lastKeyword = i;
077: token = Token.KEYWORD2;
078: break;
079: }
080: break;
081: case Token.KEYWORD1: // Inside a tag
082: backslash = false;
083: if (c == '>') {
084: addToken(i1 - lastOffset, token);
085: lastOffset = lastKeyword = i1;
086: token = Token.NULL;
087: }
088: break;
089: case Token.KEYWORD2: // Inside an entity
090: backslash = false;
091: if (c == ';') {
092: addToken(i1 - lastOffset, token);
093: lastOffset = lastKeyword = i1;
094: token = Token.NULL;
095: break;
096: }
097: break;
098: case Token.COMMENT1: // Inside a comment
099: backslash = false;
100: if (SyntaxUtilities
101: .regionMatches(false, line, i, "-->")) {
102: addToken(i + 3 - lastOffset, token);
103: i += 2;
104: lastOffset = lastKeyword = i + 1;
105: token = Token.NULL;
106: }
107: break;
108: case SCRIPT: // Inside a JavaScript or PHP
109: switch (c) {
110: case '<':
111: backslash = false;
112: doKeyword(line, i, c);
113: if (SyntaxUtilities.regionMatches(true, line, i1,
114: "/script>")) {
115: //Ending the script
116: addToken(i - lastOffset, Token.KEYWORD3);
117: addToken(9, Token.LABEL);
118: lastOffset = lastKeyword = (i += 8) + 1;
119: token = Token.NULL;
120: } else {
121: // < operator
122: addToken(i - lastOffset, Token.KEYWORD3);
123: addToken(1, Token.OPERATOR);
124: lastOffset = lastKeyword = i1;
125: }
126: break;
127: case '?':
128: backslash = false;
129: doKeyword(line, i, c);
130: if (array[i1] == '>') {
131: //Ending the script
132: addToken(i - lastOffset, Token.KEYWORD3);
133: addToken(2, Token.LABEL);
134: lastOffset = lastKeyword = (i += 1) + 1;
135: token = Token.NULL;
136: } else {
137: //? operator
138: addToken(i - lastOffset, Token.KEYWORD3);
139: addToken(1, Token.OPERATOR);
140: lastOffset = lastKeyword = i1;
141: }
142: break;
143: case '"':
144: if (backslash)
145: backslash = false;
146: else {
147: doKeyword(line, i, c);
148: addToken(i - lastOffset, Token.KEYWORD3);
149: lastOffset = lastKeyword = i;
150: token = Token.LITERAL1;
151: }
152: break;
153: case '\'':
154: if (backslash)
155: backslash = false;
156: else {
157: doKeyword(line, i, c);
158: addToken(i - lastOffset, Token.KEYWORD3);
159: lastOffset = lastKeyword = i;
160: token = Token.LITERAL2;
161: }
162: break;
163: case '#':
164: doKeyword(line, i, c);
165: addToken(i - lastOffset, Token.KEYWORD3);
166: addToken(length - i, Token.COMMENT2);
167: lastOffset = lastKeyword = length;
168: break loop;
169: case '/':
170: backslash = false;
171: doKeyword(line, i, c);
172: if (length - i > 1) /*This is the same as if(length > i + 1) */
173: {
174: addToken(i - lastOffset, Token.KEYWORD3);
175: lastOffset = lastKeyword = i;
176: if (array[i1] == '/') {
177: addToken(length - i, Token.COMMENT2);
178: lastOffset = lastKeyword = length;
179: break loop;
180: } else if (array[i1] == '*') {
181: token = Token.COMMENT2;
182: } else {
183: // / operator
184: addToken(i - lastOffset, Token.KEYWORD3);
185: addToken(1, Token.OPERATOR);
186: lastOffset = lastKeyword = i1;
187: }
188: } else {
189: // / operator
190: addToken(i - lastOffset, Token.KEYWORD3);
191: addToken(1, Token.OPERATOR);
192: lastOffset = lastKeyword = i1;
193: }
194: break;
195: default:
196: backslash = false;
197: if (!Character.isLetterOrDigit(c) && c != '_'
198: && c != '$') {
199: doKeyword(line, i, c);
200: if (c != ' ') {
201: //assume non alphanumeric characters are operators
202: addToken(i - lastOffset, Token.KEYWORD3);
203: addToken(1, Token.OPERATOR);
204: lastOffset = lastKeyword = i1;
205: }
206: }
207: break;
208: }
209: break;
210: case Token.LITERAL1: // Script "..."
211: if (backslash)
212: backslash = false;
213: else if (c == '"') {
214: addToken(i1 - lastOffset, Token.LITERAL1);
215: lastOffset = lastKeyword = i1;
216: token = SCRIPT;
217: }
218: break;
219: case Token.LITERAL2: // Script '...'
220: if (backslash)
221: backslash = false;
222: else if (c == '\'') {
223: addToken(i1 - lastOffset, Token.LITERAL1);
224: lastOffset = lastKeyword = i1;
225: token = SCRIPT;
226: }
227: break;
228: case Token.COMMENT2: // Inside a Script comment
229: backslash = false;
230: if (c == '*' && length - i > 1 && array[i1] == '/') {
231: addToken(i + 2 - lastOffset, Token.COMMENT2);
232: i += 1;
233: lastOffset = lastKeyword = i + 1;
234: token = SCRIPT;
235: }
236: break;
237: default:
238: throw new InternalError("Invalid state: " + token);
239: }
240: }
241:
242: switch (token) {
243: case Token.LITERAL1:
244: addToken(length - lastOffset, Token.LITERAL1);
245: break;
246: case Token.LITERAL2:
247: addToken(length - lastOffset, Token.LITERAL2);
248: break;
249: case Token.KEYWORD2:
250: addToken(length - lastOffset, Token.INVALID);
251: token = Token.NULL;
252: break;
253: case SCRIPT:
254: doKeyword(line, length, '\0');
255: addToken(length - lastOffset, Token.KEYWORD3);
256: break;
257: default:
258: addToken(length - lastOffset, token);
259: break;
260: }
261:
262: return token;
263: }
264:
265: // private members
266: private static KeywordMap keywords;
267: private int lastOffset;
268: private int lastKeyword;
269:
270: static {
271: keywords = new KeywordMap(false);
272: keywords.add("function", Token.KEYWORD2);
273: keywords.add("class", Token.KEYWORD2);
274: keywords.add("var", Token.KEYWORD2);
275: keywords.add("require", Token.KEYWORD2);
276: keywords.add("include", Token.KEYWORD2);
277: keywords.add("else", Token.KEYWORD1);
278: keywords.add("elseif", Token.KEYWORD1);
279: keywords.add("do", Token.KEYWORD1);
280: keywords.add("for", Token.KEYWORD1);
281: keywords.add("if", Token.KEYWORD1);
282: keywords.add("endif", Token.KEYWORD1);
283: keywords.add("in", Token.KEYWORD1);
284: keywords.add("new", Token.KEYWORD1);
285: keywords.add("return", Token.KEYWORD1);
286: keywords.add("while", Token.KEYWORD1);
287: keywords.add("endwhile", Token.KEYWORD1);
288: keywords.add("with", Token.KEYWORD1);
289: keywords.add("break", Token.KEYWORD1);
290: keywords.add("switch", Token.KEYWORD1);
291: keywords.add("case", Token.KEYWORD1);
292: keywords.add("continue", Token.KEYWORD1);
293: keywords.add("default", Token.KEYWORD1);
294: keywords.add("echo", Token.KEYWORD1);
295: keywords.add("false", Token.KEYWORD1);
296: keywords.add("this", Token.KEYWORD1);
297: keywords.add("true", Token.KEYWORD1);
298: keywords.add("array", Token.KEYWORD1);
299: keywords.add("extends", Token.KEYWORD1);
300: }
301:
302: private boolean doKeyword(Segment line, int i, char c) {
303: int i1 = i + 1;
304:
305: int len = i - lastKeyword;
306: byte id = keywords.lookup(line, lastKeyword, len);
307: if (id != Token.NULL) {
308: if (lastKeyword != lastOffset)
309: addToken(lastKeyword - lastOffset, Token.KEYWORD3);
310: addToken(len, id);
311: lastOffset = i;
312: }
313: lastKeyword = i1;
314: return false;
315: }
316: }
|