001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * TeXTokenMarker.java
028: *
029: */
030:
031: package org.syntax.jedit.tokenmarker;
032:
033: import javax.swing.text.Segment;
034:
035: /**
036: * TeX token marker.
037: *
038: * @author Slava Pestov
039: * @version $Id: TeXTokenMarker.java 1167 2008-01-15 18:49:05Z gtoffoli $
040: */
041: public class TeXTokenMarker extends TokenMarker {
042: // public members
043: public static final byte BDFORMULA = Token.INTERNAL_FIRST;
044: public static final byte EDFORMULA = (byte) (Token.INTERNAL_FIRST + 1);
045:
046: public byte markTokensImpl(byte token, Segment line, int lineIndex) {
047: char[] array = line.array;
048: int offset = line.offset;
049: int lastOffset = offset;
050: int length = line.count + offset;
051: boolean backslash = false;
052: loop: for (int i = offset; i < length; i++) {
053: int i1 = (i + 1);
054:
055: char c = array[i];
056: // if a backslash is followed immediately
057: // by a non-alpha character, the command at
058: // the non-alpha char. If we have a backslash,
059: // some text, and then a non-alpha char,
060: // the command ends before the non-alpha char.
061: if (Character.isLetter(c)) {
062: backslash = false;
063: } else {
064: if (backslash) {
065: // \<non alpha>
066: // we skip over this character,
067: // hence the `continue'
068: backslash = false;
069: if (token == Token.KEYWORD2 || token == EDFORMULA)
070: token = Token.KEYWORD2;
071: addToken(i1 - lastOffset, token);
072: lastOffset = i1;
073: if (token == Token.KEYWORD1)
074: token = Token.NULL;
075: continue;
076: } else {
077: //\blah<non alpha>
078: // we leave the character in
079: // the stream, and it's not
080: // part of the command token
081: if (token == BDFORMULA || token == EDFORMULA)
082: token = Token.KEYWORD2;
083: addToken(i - lastOffset, token);
084: if (token == Token.KEYWORD1)
085: token = Token.NULL;
086: lastOffset = i;
087: }
088: }
089: switch (c) {
090: case '%':
091: if (backslash) {
092: backslash = false;
093: break;
094: }
095: addToken(i - lastOffset, token);
096: addToken(length - i, Token.COMMENT1);
097: lastOffset = length;
098: break loop;
099: case '\\':
100: backslash = true;
101: if (token == Token.NULL) {
102: token = Token.KEYWORD1;
103: addToken(i - lastOffset, Token.NULL);
104: lastOffset = i;
105: }
106: break;
107: case '$':
108: backslash = false;
109: if (token == Token.NULL) // singe $
110: {
111: token = Token.KEYWORD2;
112: addToken(i - lastOffset, Token.NULL);
113: lastOffset = i;
114: } else if (token == Token.KEYWORD1) // \...$
115: {
116: token = Token.KEYWORD2;
117: addToken(i - lastOffset, Token.KEYWORD1);
118: lastOffset = i;
119: } else if (token == Token.KEYWORD2) // $$aaa
120: {
121: if (i - lastOffset == 1 && array[i - 1] == '$') {
122: token = BDFORMULA;
123: break;
124: }
125: token = Token.NULL;
126: addToken(i1 - lastOffset, Token.KEYWORD2);
127: lastOffset = i1;
128: } else if (token == BDFORMULA) // $$aaa$
129: {
130: token = EDFORMULA;
131: } else if (token == EDFORMULA) // $$aaa$$
132: {
133: token = Token.NULL;
134: addToken(i1 - lastOffset, Token.KEYWORD2);
135: lastOffset = i1;
136: }
137: break;
138: }
139: }
140: if (lastOffset != length)
141: addToken(length - lastOffset, token == BDFORMULA
142: || token == EDFORMULA ? Token.KEYWORD2 : token);
143: return (token != Token.KEYWORD1 ? token : Token.NULL);
144: }
145: }
|