001: package net.xoetrope.builder.editor.syntaxhighlight;
002:
003: /*
004: * TeXTokenMarker.java - TeX/LaTeX/AMS-TeX token marker
005: * Copyright (C) 1998 Slava Pestov
006: *
007: * You may use and modify this package for any purpose. Redistribution is
008: * permitted, in both source and binary form, provided that this notice
009: * remains intact in all source distributions of this package.
010: */
011:
012: import javax.swing.text.Segment;
013:
014: /**
015: * TeX token marker.
016: *
017: * @author Slava Pestov
018: * @version $Id: TeXTokenMarker.java,v 1.22 2005/01/05 17:20:50 luano Exp $
019: */
020: public class TeXTokenMarker extends TokenMarker {
021: // public members
022: public static final byte BDFORMULA = Token.INTERNAL_FIRST;
023: public static final byte EDFORMULA = (byte) (Token.INTERNAL_FIRST + 1);
024:
025: public byte markTokensImpl(byte token, Segment line, int lineIndex) {
026: char[] array = line.array;
027: int offset = line.offset;
028: int lastOffset = offset;
029: int length = line.count + offset;
030: boolean backslash = false;
031: loop: for (int i = offset; i < length; i++) {
032: int i1 = (i + 1);
033:
034: char c = array[i];
035: // if a backslash is followed immediately
036: // by a non-alpha character, the command at
037: // the non-alpha char. If we have a backslash,
038: // some text, and then a non-alpha char,
039: // the command ends before the non-alpha char.
040: if (Character.isLetter(c)) {
041: backslash = false;
042: } else {
043: if (backslash) {
044: // \<non alpha>
045: // we skip over this character,
046: // hence the `continue'
047: backslash = false;
048: if (token == Token.KEYWORD2 || token == EDFORMULA)
049: token = Token.KEYWORD2;
050: addToken(i1 - lastOffset, token);
051: lastOffset = i1;
052: if (token == Token.KEYWORD1)
053: token = Token.NULL;
054: continue;
055: } else {
056: //\blah<non alpha>
057: // we leave the character in
058: // the stream, and it's not
059: // part of the command token
060: if (token == BDFORMULA || token == EDFORMULA)
061: token = Token.KEYWORD2;
062: addToken(i - lastOffset, token);
063: if (token == Token.KEYWORD1)
064: token = Token.NULL;
065: lastOffset = i;
066: }
067: }
068: switch (c) {
069: case '%':
070: if (backslash) {
071: backslash = false;
072: break;
073: }
074: addToken(i - lastOffset, token);
075: addToken(length - i, Token.COMMENT1);
076: lastOffset = length;
077: break loop;
078: case '\\':
079: backslash = true;
080: if (token == Token.NULL) {
081: token = Token.KEYWORD1;
082: addToken(i - lastOffset, Token.NULL);
083: lastOffset = i;
084: }
085: break;
086: case '$':
087: backslash = false;
088: if (token == Token.NULL) // singe $
089: {
090: token = Token.KEYWORD2;
091: addToken(i - lastOffset, Token.NULL);
092: lastOffset = i;
093: } else if (token == Token.KEYWORD1) // \...$
094: {
095: token = Token.KEYWORD2;
096: addToken(i - lastOffset, Token.KEYWORD1);
097: lastOffset = i;
098: } else if (token == Token.KEYWORD2) // $$aaa
099: {
100: if (i - lastOffset == 1 && array[i - 1] == '$') {
101: token = BDFORMULA;
102: break;
103: }
104: token = Token.NULL;
105: addToken(i1 - lastOffset, Token.KEYWORD2);
106: lastOffset = i1;
107: } else if (token == BDFORMULA) // $$aaa$
108: {
109: token = EDFORMULA;
110: } else if (token == EDFORMULA) // $$aaa$$
111: {
112: token = Token.NULL;
113: addToken(i1 - lastOffset, Token.KEYWORD2);
114: lastOffset = i1;
115: }
116: break;
117: }
118: }
119: if (lastOffset != length)
120: addToken(length - lastOffset, token == BDFORMULA
121: || token == EDFORMULA ? Token.KEYWORD2 : token);
122: return (token != Token.KEYWORD1 ? token : Token.NULL);
123: }
124: }
|