View Javadoc

    1   /*
    2    * MakefileTokenMarker.java - Makefile token marker
    3    * Copyright (C) 1998, 1999 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   * Makefile token marker.
    16   *
    17   * @author Slava Pestov
    18   * @version $Id: MakefileTokenMarker.java,v 1.18 1999/12/13 03:40:30 sp Exp $
    19   */
    20  public class MakefileTokenMarker extends TokenMarker
    21  {
    22  	// public members
    23  	public byte markTokensImpl(byte token, Segment line, int lineIndex)
    24  	{
    25  		char[] array = line.array;
    26  		int offset = line.offset;
    27  		int lastOffset = offset;
    28  		int length = line.count + offset;
    29  		boolean backslash = false;
    30  loop:		for(int i = offset; i < length; i++)
    31  		{
    32  			int i1 = (i+1);
    33
    34  			char c = array[i];
    35  			if(c == '//')
    36  			{
    37  				backslash = !backslash;
    38  				continue;
    39  			}
    40
    41  			switch(token)
    42  			{
    43  			case Token.NULL:
    44  				switch(c)
    45  				{
    46  				case ':': case '=': case ' ': case '\t':
    47  					backslash = false;
    48  					if(lastOffset == offset)
    49  					{
    50  						addToken(i1 - lastOffset,Token.KEYWORD1);
    51  						lastOffset = i1;
    52  					}
    53  					break;
    54  				case '#':
    55  					if(backslash)
    56  						backslash = false;
    57  					else
    58  					{
    59  						addToken(i - lastOffset,token);
    60  						addToken(length - i,Token.COMMENT1);
    61  						lastOffset = length;
    62  						break loop;
    63  					}
    64  					break;
    65  				case '$':
    66  					if(backslash)
    67  						backslash = false;
    68  					else if(lastOffset != offset)
    69  					{
    70  						addToken(i - lastOffset,token);
    71  						lastOffset = i;
    72  						if(length - i > 1)
    73  						{
    74  							char c1 = array[i1];
    75  							if(c1 == '(' || c1 == '{')
    76  								token = Token.KEYWORD2;
    77  							else
    78  							{
    79  								addToken(2,Token.KEYWORD2);
    80  								lastOffset += 2;
    81  								i++;
    82  							}
    83  						}
    84  					}
    85  					break;
    86  				case '"':
    87  					if(backslash)
    88  						backslash = false;
    89  					else
    90  					{
    91  						addToken(i - lastOffset,token);
    92  						token = Token.LITERAL1;
    93  						lastOffset = i;
    94  					}
    95  					break;
    96  				case '\'':
    97  					if(backslash)
    98  						backslash = false;
    99  					else
    100 					{
    101 						addToken(i - lastOffset,token);
    102 						token = Token.LITERAL2;
    103 						lastOffset = i;
    104 					}
    105 					break;
    106 				default:
    107 					backslash = false;
    108 					break;
    109 				}
    110 			case Token.KEYWORD2:
    111 				backslash = false;
    112 				if(c == ')' || c == '}')
    113 				{
    114 					addToken(i1 - lastOffset,token);
    115 					token = Token.NULL;
    116 					lastOffset = i1;
    117 				}
    118 				break;
    119 			case Token.LITERAL1:
    120 				if(backslash)
    121 					backslash = false;
    122 				else if(c == '"')
    123 				{
    124 					addToken(i1 - lastOffset,token);
    125 					token = Token.NULL;
    126 					lastOffset = i1;
    127 				}
    128 				else
    129 					backslash = false;
    130 				break;
    131 			case Token.LITERAL2:
    132 				if(backslash)
    133 					backslash = false;
    134 				else if(c == '\'')
    135 				{
    136 					addToken(i1 - lastOffset,Token.LITERAL1);
    137 					token = Token.NULL;
    138 					lastOffset = i1;
    139 				}
    140 				else
    141 					backslash = false;
    142 				break;
    143 			}
    144 		}
    145 		switch(token)
    146 		{
    147 		case Token.KEYWORD2:
    148 			addToken(length - lastOffset,Token.INVALID);
    149 			token = Token.NULL;
    150 			break;
    151 		case Token.LITERAL2:
    152 			addToken(length - lastOffset,Token.LITERAL1);
    153 			break;
    154 		default:
    155 			addToken(length - lastOffset,token);
    156 			break;
    157 		}
    158 		return token;
    159 	}
    160 }