View Javadoc

    1   /*
    2    * TeXTokenMarker.java - TeX/LaTeX/AMS-TeX token marker
    3    * Copyright (C) 1998 Slava Pestov
    4    *
    5    * You may use and modify this package for any purpose. Redistribution is
    6    * permitted, in both source and binary form, provided that this notice
    7    * remains intact in all source distributions of this package.
    8    */
    9
    10  package org.syntax.jedit.tokenmarker;
    11
    12  import javax.swing.text.Segment;
    13
    14  /***
    15   * TeX token marker.
    16   *
    17   * @author Slava Pestov
    18   * @version $Id: TeXTokenMarker.java,v 1.16 1999/12/13 03:40:30 sp Exp $
    19   */
    20  public class TeXTokenMarker extends TokenMarker
    21  {
    22  	// public members
    23  	public static final byte BDFORMULA = Token.INTERNAL_FIRST;
    24  	public static final byte EDFORMULA = (byte)(Token.INTERNAL_FIRST + 1);
    25
    26  	public byte markTokensImpl(byte token, Segment line, int lineIndex)
    27  	{
    28  		char[] array = line.array;
    29  		int offset = line.offset;
    30  		int lastOffset = offset;
    31  		int length = line.count + offset;
    32  		boolean backslash = false;
    33  loop:		for(int i = offset; i < length; i++)
    34  		{
    35  			int i1 = (i+1);
    36
    37  			char c = array[i];
    38  			// if a backslash is followed immediately
    39  			// by a non-alpha character, the command at
    40  			// the non-alpha char. If we have a backslash,
    41  			// some text, and then a non-alpha char,
    42  			// the command ends before the non-alpha char.
    43  			if(Character.isLetter(c))
    44  			{
    45  				backslash = false;
    46  			}
    47  			else
    48  			{
    49  				if(backslash)
    50  				{
    51  					// \<non alpha>
    52  					// we skip over this character,
    53  					// hence the `continue'
    54  					backslash = false;
    55  					if(token == Token.KEYWORD2 || token == EDFORMULA)
    56  						token = Token.KEYWORD2;
    57  					addToken(i1 - lastOffset,token);
    58  					lastOffset = i1;
    59  					if(token == Token.KEYWORD1)
    60  						token = Token.NULL;
    61  					continue;
    62  				}
    63  				else
    64  				{
    65  					//\blah<non alpha>
    66  					// we leave the character in
    67  					// the stream, and it's not
    68  					// part of the command token
    69  					if(token == BDFORMULA || token == EDFORMULA)
    70  						token = Token.KEYWORD2;
    71  					addToken(i - lastOffset,token);
    72  					if(token == Token.KEYWORD1)
    73  						token = Token.NULL;
    74  					lastOffset = i;
    75  				}
    76  			}
    77  			switch(c)
    78  			{
    79  			case '%':
    80  				if(backslash)
    81  				{
    82  					backslash = false;
    83  					break;
    84  				}
    85  				addToken(i - lastOffset,token);
    86  				addToken(length - i,Token.COMMENT1);
    87  				lastOffset = length;
    88  				break loop;
    89  			case '//':
    90  				backslash = true;
    91  				if(token == Token.NULL)
    92  				{
    93  					token = Token.KEYWORD1;
    94  					addToken(i - lastOffset,Token.NULL);
    95  					lastOffset = i;
    96  				}
    97  				break;
    98  			case '$':
    99  				backslash = false;
    100 				if(token == Token.NULL) // singe $
    101 				{
    102 					token = Token.KEYWORD2;
    103 					addToken(i - lastOffset,Token.NULL);
    104 					lastOffset = i;
    105 				}
    106 				else if(token == Token.KEYWORD1) // \...$
    107 				{
    108 					token = Token.KEYWORD2;
    109 					addToken(i - lastOffset,Token.KEYWORD1);
    110 					lastOffset = i;
    111 				}
    112 				else if(token == Token.KEYWORD2) // $$aaa
    113 				{
    114 					if(i - lastOffset == 1 && array[i-1] == '$')
    115 					{
    116 						token = BDFORMULA;
    117 						break;
    118 					}
    119 					token = Token.NULL;
    120 					addToken(i1 - lastOffset,Token.KEYWORD2);
    121 					lastOffset = i1;
    122 				}
    123 				else if(token == BDFORMULA) // $$aaa$
    124 				{
    125 					token = EDFORMULA;
    126 				}
    127 				else if(token == EDFORMULA) // $$aaa$$
    128 				{
    129 					token = Token.NULL;
    130 					addToken(i1 - lastOffset,Token.KEYWORD2);
    131 					lastOffset = i1;
    132 				}
    133 				break;
    134 			}
    135 		}
    136 		if(lastOffset != length)
    137 			addToken(length - lastOffset,token == BDFORMULA
    138 				|| token == EDFORMULA ? Token.KEYWORD2 :
    139 				token);
    140 		return (token != Token.KEYWORD1 ? token : Token.NULL);
    141 	}
    142 }