Revision 18765

View differences:

trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/ParseException.java
1
/* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2
package org.gvsig.symbology.fmap.labeling.parse2;
3

  
4
/**
5
 * This exception is thrown when parse errors are encountered.
6
 * You can explicitly create objects of this exception type by
7
 * calling the method generateParseException in the generated
8
 * parser.
9
 *
10
 * You can modify this class to customize your error reporting
11
 * mechanisms so long as you retain the public fields.
12
 */
13
public @SuppressWarnings("all") class ParseException extends Exception {
14

  
15
  /**
16
   * This constructor is used by the method "generateParseException"
17
   * in the generated parser.  Calling this constructor generates
18
   * a new object of this type with the fields "currentToken",
19
   * "expectedTokenSequences", and "tokenImage" set.  The boolean
20
   * flag "specialConstructor" is also set to true to indicate that
21
   * this constructor was used to create this object.
22
   * This constructor calls its super class with the empty string
23
   * to force the "toString" method of parent class "Throwable" to
24
   * print the error message in the form:
25
   *     ParseException: <result of getMessage>
26
   */
27
  public ParseException(Token currentTokenVal,
28
                        int[][] expectedTokenSequencesVal,
29
                        String[] tokenImageVal
30
                       )
31
  {
32
    super("");
33
    specialConstructor = true;
34
    currentToken = currentTokenVal;
35
    expectedTokenSequences = expectedTokenSequencesVal;
36
    tokenImage = tokenImageVal;
37
  }
38

  
39
  /**
40
   * The following constructors are for use by you for whatever
41
   * purpose you can think of.  Constructing the exception in this
42
   * manner makes the exception behave in the normal way - i.e., as
43
   * documented in the class "Throwable".  The fields "errorToken",
44
   * "expectedTokenSequences", and "tokenImage" do not contain
45
   * relevant information.  The JavaCC generated code does not use
46
   * these constructors.
47
   */
48

  
49
  public ParseException() {
50
    super();
51
    specialConstructor = false;
52
  }
53

  
54
  public ParseException(String message) {
55
    super(message);
56
    specialConstructor = false;
57
  }
58

  
59
  /**
60
   * This variable determines which constructor was used to create
61
   * this object and thereby affects the semantics of the
62
   * "getMessage" method (see below).
63
   */
64
  protected boolean specialConstructor;
65

  
66
  /**
67
   * This is the last token that has been consumed successfully.  If
68
   * this object has been created due to a parse error, the token
69
   * followng this token will (therefore) be the first error token.
70
   */
71
  public Token currentToken;
72

  
73
  /**
74
   * Each entry in this array is an array of integers.  Each array
75
   * of integers represents a sequence of tokens (by their ordinal
76
   * values) that is expected at this point of the parse.
77
   */
78
  public int[][] expectedTokenSequences;
79

  
80
  /**
81
   * This is a reference to the "tokenImage" array of the generated
82
   * parser within which the parse error occurred.  This array is
83
   * defined in the generated ...Constants interface.
84
   */
85
  public String[] tokenImage;
86

  
87
  /**
88
   * This method has the standard behavior when this object has been
89
   * created using the standard constructors.  Otherwise, it uses
90
   * "currentToken" and "expectedTokenSequences" to generate a parse
91
   * error message and returns it.  If this object has been created
92
   * due to a parse error, and you do not catch it (it gets thrown
93
   * from the parser), then this method is called during the printing
94
   * of the final stack trace, and hence the correct error message
95
   * gets displayed.
96
   */
97
  public String getMessage() {
98
    if (!specialConstructor) {
99
      return super.getMessage();
100
    }
101
    StringBuffer expected = new StringBuffer();
102
    int maxSize = 0;
103
    for (int i = 0; i < expectedTokenSequences.length; i++) {
104
      if (maxSize < expectedTokenSequences[i].length) {
105
        maxSize = expectedTokenSequences[i].length;
106
      }
107
      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
108
        expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
109
      }
110
      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
111
        expected.append("...");
112
      }
113
      expected.append(eol).append("    ");
114
    }
115
    String retval = "Encountered \"";
116
    Token tok = currentToken.next;
117
    for (int i = 0; i < maxSize; i++) {
118
      if (i != 0) retval += " ";
119
      if (tok.kind == 0) {
120
        retval += tokenImage[0];
121
        break;
122
      }
123
      retval += add_escapes(tok.image);
124
      tok = tok.next; 
125
    }
126
    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
127
    retval += "." + eol;
128
    if (expectedTokenSequences.length == 1) {
129
      retval += "Was expecting:" + eol + "    ";
130
    } else {
131
      retval += "Was expecting one of:" + eol + "    ";
132
    }
133
    retval += expected.toString();
134
    return retval;
135
  }
136

  
137
  /**
138
   * The end of line string for this machine.
139
   */
140
  protected String eol = System.getProperty("line.separator", "\n");
141
 
142
  /**
143
   * Used to convert raw characters to their escaped version
144
   * when these raw version cannot be used as part of an ASCII
145
   * string literal.
146
   */
147
  protected String add_escapes(String str) {
148
      StringBuffer retval = new StringBuffer();
149
      char ch;
150
      for (int i = 0; i < str.length(); i++) {
151
        switch (str.charAt(i))
152
        {
153
           case 0 :
154
              continue;
155
           case '\b':
156
              retval.append("\\b");
157
              continue;
158
           case '\t':
159
              retval.append("\\t");
160
              continue;
161
           case '\n':
162
              retval.append("\\n");
163
              continue;
164
           case '\f':
165
              retval.append("\\f");
166
              continue;
167
           case '\r':
168
              retval.append("\\r");
169
              continue;
170
           case '\"':
171
              retval.append("\\\"");
172
              continue;
173
           case '\'':
174
              retval.append("\\\'");
175
              continue;
176
           case '\\':
177
              retval.append("\\\\");
178
              continue;
179
           default:
180
              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
181
                 String s = "0000" + Integer.toString(ch, 16);
182
                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
183
              } else {
184
                 retval.append(ch);
185
              }
186
              continue;
187
        }
188
      }
189
      return retval.toString();
190
   }
191

  
192
}
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/LabelExpressionParser.jj
1
/**
2
 * JavaCC file
3
 */
4
 
5
options {
6
  JDK_VERSION = "1.5";
7
  STATIC = false;
8
}
9
PARSER_BEGIN(LabelExpressionParser)
10
package org.gvsig.symbology.fmap.labeling.parse;
11

  
12
import org.apache.log4j.Logger;
13
import com.hardcode.gdbms.engine.instruction.EvaluationException;
14
import com.hardcode.gdbms.engine.values.*;
15
import java.io.*;
16
import java.util.*;
17
import org.gvsig.symbology.fmap.labeling.lang.Symbol;
18
import org.gvsig.symbology.fmap.labeling.lang.Function;
19

  
20

  
21
public class LabelExpressionParser {  
22
	private static Hashtable<String, Function> function_table = new Hashtable<String, Function>(); 
23

  
24
	private Hashtable<String, Symbol> symbol_table = new Hashtable<String, Symbol>();
25
	private Stack stack = new Stack();
26
	
27
	public static void addFunction(Class functionClass) {
28
		try {
29
			Function f = (Function) functionClass.newInstance();
30
			function_table.put(f.getName(), f);
31
		} catch (Exception e) {
32
			Logger.getLogger(LabelExpressionParser.class).error("couldn't install function '"+functionClass.getName(), e);
33
		};
34
	}
35
	
36
	public Object pop() {
37
		return stack.pop();
38
	}
39
	
40
	public static void main(String args[]) throws ParseException {
41

  
42
           LabelExpressionParser parser = new LabelExpressionParser(System.in);
43
           
44
           parser.putSymbol(new Symbol(
45
           						"TIPNOMCALL", 
46
           						Symbol.DATA_TYPE_STRING,
47
           						ValueFactory.createValue("Yaba daba doo...")
48
           						)
49
           					); 
50
           parser.putSymbol(new Symbol(
51
           						"LONGITUD", 
52
           						Symbol.DATA_TYPE_FLOATING,
53
           						ValueFactory.createValue(4.0)
54
           						)
55
           					); 
56
           while (true) {
57
        	try {
58
				String[] texts = parser.LabelExpression();
59
				for (int i = 0; i < texts.length; i++) {
60
					System.out.println("Text at field "+i+": "+texts[i]);
61
				}
62
        	} catch (ParseException e) {
63
        		System.err.println(e.getMessage());
64
        		System.exit(-1);
65
        	}
66
           }
67
	}
68
	
69
	public void putSymbol(Symbol sym) {
70
		symbol_table.put(sym.getName(), sym);
71
	}
72
}
73

  
74
class LabelField {
75
	String text;
76
	int pos;
77
	
78
	public LabelField(String text, int pos) {
79
		this.text = text;
80
		this.pos = pos;
81
	}
82
}
83

  
84
class LabelFieldComparator implements Comparator<LabelField> {
85
	public int compare(LabelField o1, LabelField o2) {
86
		if (o1 != null && o2 == null) return -1;
87
		if (o1 == null && o2 != null) return  1;
88
		if (o1 == null && o2 == null) return  0;
89
		if (o1.pos <= o2.pos) return -1;
90
		else return 1;
91
	}
92
}
93
PARSER_END(LabelExpressionParser)
94

  
95
SKIP :
96
{
97
 	" "
98
|	"\r"
99
|	"\t"
100
|	"\n"
101
}
102

  
103

  
104
TOKEN : /* OPERATORS */
105
{
106
	< PLUS: "+" >
107
|	< MINUS: "-" >
108
|	< MULTIPLY: "*" >
109
|	< DIVIDE: "/" >
110
|	< GT: ">" >
111
|	< LT: "<" >
112
|	< NOT: "!" >
113
|	< EQ: "==" >
114
|	< LE: "<=" >
115
|	< GE: ">=" >
116
|	< NE: "!=" >
117
|	< OR: "||" >
118
|	< AND: "&&" >
119
}
120
TOKEN : /* MISC */
121
{
122
	< #DOT: "." >
123
|	< OPEN_SQ_BRACKETS : "[" >
124
|	< CLOSE_SQ_BRACKETS : "]" >
125
|	< OPEN_PARENTHESIS : "(" >
126
|	< CLOSE_PARENTHESIS : ")" >
127
|	< DOUBLE_QUOTE : "\"" >	
128
|	< COMMA : "," >
129
|	< EOEXPR : ";" >
130
}
131
TOKEN : /* CONSTANTS AND IDENTIFIERS */
132
{
133
	< BOOLEAN: "true" | "false" >	
134
|	< IDENTIFIER: ( <LETTER> ) ( <LETTER>|<DIGIT> )* >
135
|	< #LETTER: ( ["_","$","%"] | ( ["a"-"z"] ) | ( ["A"-"Z"] ) ) >
136
}
137
TOKEN : /* NUMBER */
138
{
139
    < #DIGIT: ["0"-"9"] >
140
|	< INTEGER: ( <DIGIT> )+ >
141
|	< FLOATING_POINT: (<DIGIT>)+ "." (<DIGIT>)* (<EXPONENT>)? (["f","F","d","D"])?
142
      | <DOT> (<DIGIT>)+ (<EXPONENT>)? (["f","F","d","D"])?
143
      | (<DIGIT>)+ <EXPONENT> (["f","F","d","D"])?
144
      | (<DIGIT>)+ (<EXPONENT>)? ["f","F","d","D"] >
145
|	< #EXPONENT: ["e","E"] (["+","-"])? (<DIGIT>)+ >
146
}
147
TOKEN: /* STRING */
148
{
149
	< STRING: ( <DOUBLE_QUOTE> ( <CHAR_STRING> )* <DOUBLE_QUOTE> )>
150
|	< CHAR_STRING : ~[ "\"", ";","\n","\r"] >
151
}
152

  
153

  
154
TOKEN : /* LABELING TOKENS */
155
{
156
	< FIELD_IDENTIFIER: <OPEN_SQ_BRACKETS> <CHAR_STRING> <CLOSE_SQ_BRACKETS> >
157
}
158

  
159
/*
160
 * The grammar.
161
 */
162
 
163
/* LABEL EXPRESSION */
164
String[] LabelExpression() :
165
{
166
	ArrayList<String> labelFields = new ArrayList<String>();
167
	String f;
168
}
169
{
170
	f = FieldExpression() { labelFields.add(f); }
171
	(
172
		<COMMA>
173
		f = FieldExpression() { labelFields.add(f); }
174
	)*  <EOEXPR>
175
	{ 
176
		return labelFields.toArray(new String[0]);
177
	}
178
|	<EOEXPR> { return new String[0]; }
179
}
180

  
181
/* FIELD EXPRESSIONS */
182
String FieldExpression() :
183
{
184
	Token t;
185
	String value;
186
}
187
{
188
		Expression() { value = stack.pop().toString(); }
189
		( Expression() { value += stack.pop().toString(); } ) *
190
		{ return value; }
191
}
192

  
193
void Expression(): {}
194
{
195
	AndExpression() ( <OR> AndExpression() {
196
		try {
197
			boolean a = (Boolean) stack.pop();
198
			boolean b = (Boolean) stack.pop();
199
			stack.push(ValueFactory.createValue(a || b));
200
		} catch (ClassCastException ex) {
201
			throw new SemanticException(SemanticException.TYPE_MISMATCH);
202
		}
203
	}
204
	)*
205
}
206

  
207
void AndExpression() : {}
208
{
209
	EqComparisonExpression() ( <AND> EqComparisonExpression() {
210
		try {
211
			boolean a = (Boolean) stack.pop();
212
			boolean b = (Boolean) stack.pop();
213
			stack.push(new Boolean(b && a));
214
		} catch (ClassCastException ex) {
215
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
216
				"Trying to perform logical AND with non-boolean data types");
217
		}
218
	})*
219
}
220

  
221
void EqComparisonExpression(): 
222
{
223
	Token t;
224
}
225
{
226
	ComparisonExpression() ( ( t = <EQ> | t = <NE> ) ComparisonExpression() {
227
		try {
228
			Object A = stack.pop();
+			Object B = stack.pop();
+			boolean aInt = A.getClass().equals(Integer.class);
+			boolean bInt = B.getClass().equals(Integer.class);
+			if (aInt &&	bInt) {
+				int a = (Integer) A;
+				int b = (Integer) B;
+				if (t.kind == EQ)
+					stack.push(new Boolean(b==a));
+				else
+					stack.push(new Boolean(b!=a));
+			} else {
+				double a = (!aInt) ? (Double) A : new Double(A.toString());
+				double b = (!bInt) ? (Double) B : new Double(B.toString());
229
				if (t.kind == EQ)
230
					stack.push(new Boolean(b==a));
231
				else
232
					stack.push(new Boolean(b!=a));
233
			}
234
		} catch (ClassCastException ex) {
235
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
236
				"Trying to perform comparison '"+t.image+"' with non-numeric data types");
237
		}
238
	})*
239
}
240

  
241
void ComparisonExpression() :
242
{
243
	Token t;
244
}
245
{
246
	AdditiveExpression() ((t = <LT> | t = <GT> | t = <LE> | t = <GE> ) AdditiveExpression() {
247
		try {
248
			Object A = stack.pop();
+			Object B = stack.pop();
+			boolean aInt = A.getClass().equals(Integer.class);
+			boolean bInt = B.getClass().equals(Integer.class);
+			if (aInt &&	bInt) {
+				int a = (Integer) A;
+				int b = (Integer) B;
+				if (t.kind == LT)
+					stack.push(new Boolean(b < a));
+				else if (t.kind == GT)
+					stack.push(new Boolean(b > a));
+				else if (t.kind == LE)
+					stack.push(new Boolean(b <= a));
+				else if (t.kind == GE)
+					stack.push(new Boolean(b >= a));
+			} else {
+				 
+				double a = (!aInt) ? (Double) A : new Double(A.toString());
+				double b = (!bInt) ? (Double) B : new Double(B.toString());
249
				if (t.kind == LT)
250
					stack.push(new Boolean(b < a));
251
				else if (t.kind == GT)
252
					stack.push(new Boolean(b > a));
253
				else if (t.kind == LE)
254
					stack.push(new Boolean(b <= a));
255
				else if (t.kind == GE)
256
					stack.push(new Boolean(b >= a));
257
			}
258
			
259
		} catch (ClassCastException ex) {
260
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
261
				"Trying to perform relational '"+t.image+"' with non-numeric data types");
262
		}
263
	})*
264
}
265

  
266
void AdditiveExpression(): 
267
{
268
	Token t;
269
}
270
{
271
	MultiplicativeExpression() ( ( t = <PLUS> | t = <MINUS> ) MultiplicativeExpression() {
272
		try {
273
			Object A = stack.pop();
+			Object B = stack.pop();
+			boolean aInt = A.getClass().equals(Integer.class);
+			boolean bInt = B.getClass().equals(Integer.class);
+			if (aInt &&	bInt) {
+				int a = (Integer) A;
+				int b = (Integer) B;
+				if (t.kind == PLUS)
+					stack.push(new Integer(b + a));
+				else
+					stack.push(new Integer(b - a));
+			} else {
+				 
+				double a = (!aInt) ? (Double) A : new Double(A.toString());
+				double b = (!bInt) ? (Double) B : new Double(B.toString());
274
				if (t.kind == PLUS)
275
					stack.push(new Double(b + a));
276
				else
277
					stack.push(new Double(b - a));
278
			}
279
		} catch (ClassCastException ex) {
280
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
281
				"Trying to perform ADD '"+t.image+"' with non-numeric data types");
282
		}
283
	})*
284
}
285

  
286
void MultiplicativeExpression(): 
287
{
288
	Token t;
289
}
290
{
291
	ArithmeticUnaryExpression() ( ( t = <MULTIPLY> | t = <DIVIDE> ) ArithmeticUnaryExpression() {
292
		try {
293
			Object A = stack.pop();
+			Object B = stack.pop();
+			boolean aInt = A.getClass().equals(Integer.class);
+			boolean bInt = B.getClass().equals(Integer.class);
+			if (aInt &&	bInt) {
+				int a = (Integer) A;
+				int b = (Integer) B;
+				if (t.kind == PLUS)
+					stack.push(new Integer(b * a));
+				else
+					stack.push(new Integer(b / a));
+			} else {
+				
+				double a = (!aInt) ? (Double) A : new Double(A.toString());
+				double b = (!bInt) ? (Double) B : new Double(B.toString());
294
				if (t.kind == MULTIPLY)
295
					stack.push(new Double(b * a));
296
				else
297
					stack.push(new Double(b / a));
298
			}
299
		} catch (ClassCastException ex) {
300
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
301
				"Trying to perform MULT '"+t.image+"' with non-numeric data types");
302
		}
303
	})*
304
}
305

  
306
void ArithmeticUnaryExpression(): 
307
{
308
	Token t;
309
}
310
{
311
	 ( t = <PLUS> | t = <MINUS> ) ArithmeticUnaryExpression() {
312
		try {
313
			Object A = stack.pop();
314
			if (A.getClass().equals(Integer.class)) {
315
				int a = (Integer) A;
316
				if (t.kind == PLUS)
317
					stack.push(new Integer(a));
318
				else
319
					stack.push(new Integer(-a));
320
			} else {
321
				 
322
				double a = (Double) A;
323
				if (t.kind == PLUS)
324
					stack.push(new Double(a));
325
				else
326
					stack.push(new Double(-a));
327
			}
328
		} catch (ClassCastException ex) {
329
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
330
				"Trying to perform UNARY '"+t.image+"' with non-numeric data types");
331
		}
332
	}
333
| BooleanUnaryExpression()
334
}
335

  
336
void BooleanUnaryExpression(): 
337
{
338
	Token t;
339
}
340
{
341
	 t = <NOT> ArithmeticUnaryExpression() {
342
		try {
343
			boolean a = (Boolean) stack.pop();
344
			stack.push(new Boolean(!a));
345
		} catch (ClassCastException ex) {
346
			throw new SemanticException(SemanticException.TYPE_MISMATCH,
347
				"Trying to perform UNARY '"+t.image+"' with non-numeric data types");
348
		}
349
	}
350
| PrimitiveExpression()
351
}
352

  
353
void PrimitiveExpression():
354
{
355
	Token t;
356
}
357
{
358
	Literal()
359
|	LOOKAHEAD(2)
360
	Function()
361
|   <OPEN_SQ_BRACKETS> t = <IDENTIFIER> <CLOSE_SQ_BRACKETS>
362
	{ String name = t.image.trim();  }
363
	{ Symbol sym = symbol_table.get(name); }
364
	{ if (sym == null) 
365
		throw new SemanticException(SemanticException.UNDEFINED_SYMBOL,
366
				"Symbol "+name+" undefined"); }
367
	{ switch (sym.getDataType()) {
368
		case Symbol.DATA_TYPE_INT: 
369
			stack.push(new Integer(((Value) sym.getValue()).toString()));
370
			break;
371
		case Symbol.DATA_TYPE_FLOATING: 
372
			stack.push(new Double(((Value) sym.getValue()).toString())); 
373
			break;
374
		case Symbol.DATA_TYPE_STRING:
375
			stack.push(((StringValue) sym.getValue()).toString());
376
			break;
377
		case Symbol.DATA_TYPE_NULL:
378
			stack.push(null);
379
			break;
380
		case Symbol.DATA_TYPE_BOOLEAN:
381
			stack.push(new Boolean(((Value) sym.getValue()).toString()));
382
			break;
383
		case Symbol.DATA_TYPE_DATE:
384
			stack.push(((DateValue) sym.getValue()).getValue());
385
			break;
386
		default:
387
		throw new SemanticException(SemanticException.TYPE_MISMATCH,
388
				"Unknown type for "+name);
389
	  }
390
	}
391
|	<OPEN_PARENTHESIS> Expression() <CLOSE_PARENTHESIS>
392
}
393

  
394
void Function():
395
{
396
	Token t;
397
	int argCount;
398
	String name;
399
}
400
{
401
	t = <IDENTIFIER> {name = t.image;} argCount = Arguments() 
402
		{ Function f = function_table.get(name); 
403
		  if (f == null) 
404
			throw new SemanticException(SemanticException.UNDEFINED_SYMBOL,
405
				"Symbol "+name+" undefined"); 
406
//		  if (sym.getSymbolType() == Symbol.SYMBOL_TYPE_FUNCTION) {
407
//		 	Function f = (Function) sym.getValue();
408
		 	if (f.getArgumentCount() != argCount) {
409
			 	throw new SemanticException(SemanticException.TYPE_MISMATCH,
410
		 	    	"Function '"+name+"' requires "+f.getArgumentCount()+", "+argCount+" were provided.");
411
		 	}
412
		 	ArrayList<Object> argList = new ArrayList<Object>();
413
		 	for (int i = 0; i <= argCount-1; i++) {
414
			 	argList.add(0, stack.pop());
415
		 	} 
416
		 	try {
417
		 		stack.push(f.evaluate(argList.toArray(new Object[0])));
418
			} catch (EvaluationException evEx) {
419
				throw new Error("Aquesta excepció no deuria de llançar-se així, deuria de ser una extensió de parse exception (o bé repensar-se el funcinament!!!");
420
			}
421
		 	
422
//	  	  } else {
+//			throw new SemanticException(SemanticException.TYPE_MISMATCH,
+//					"Tye symbol '"+name+"' is not a function");
+//	  	  }
423
		}
424
}
425

  
426
void Literal() : 
427
{
428
	Token t;
429
}
430

  
431
{
432
	t = <INTEGER> 		 { stack.push(new Integer(t.image)); }
433
|	t = <FLOATING_POINT> { stack.push(new Double(t.image));  }
434
|	t = <BOOLEAN>		 { stack.push(new Boolean(t.image)); }
435
|	t = <STRING> 		 { stack.push(t.image.substring(1, t.image.length()-1)); }
436
}
437

  
438
int Arguments():
439
{
440
	int argCount = 0;
441
}
442
{
443
	<OPEN_PARENTHESIS> ( Expression()	{ argCount++; }
444
	(	<COMMA> Expression() 			{ argCount++; }
445
	)*
446
	) <CLOSE_PARENTHESIS>
447
	{ return argCount; }
448
}
449

  
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/Token.java
1
/* Generated By:JavaCC: Do not edit this line. Token.java Version 3.0 */
2
package org.gvsig.symbology.fmap.labeling.parse2;
3

  
4
/**
5
 * Describes the input token stream.
6
 */
7

  
8
public @SuppressWarnings("all") class Token {
9

  
10
  /**
11
   * An integer that describes the kind of this token.  This numbering
12
   * system is determined by JavaCCParser, and a table of these numbers is
13
   * stored in the file ...Constants.java.
14
   */
15
  public int kind;
16

  
17
  /**
18
   * beginLine and beginColumn describe the position of the first character
19
   * of this token; endLine and endColumn describe the position of the
20
   * last character of this token.
21
   */
22
  public int beginLine, beginColumn, endLine, endColumn;
23

  
24
  /**
25
   * The string image of the token.
26
   */
27
  public String image;
28

  
29
  /**
30
   * A reference to the next regular (non-special) token from the input
31
   * stream.  If this is the last token from the input stream, or if the
32
   * token manager has not read tokens beyond this one, this field is
33
   * set to null.  This is true only if this token is also a regular
34
   * token.  Otherwise, see below for a description of the contents of
35
   * this field.
36
   */
37
  public Token next;
38

  
39
  /**
40
   * This field is used to access special tokens that occur prior to this
41
   * token, but after the immediately preceding regular (non-special) token.
42
   * If there are no such special tokens, this field is set to null.
43
   * When there are more than one such special token, this field refers
44
   * to the last of these special tokens, which in turn refers to the next
45
   * previous special token through its specialToken field, and so on
46
   * until the first special token (whose specialToken field is null).
47
   * The next fields of special tokens refer to other special tokens that
48
   * immediately follow it (without an intervening regular token).  If there
49
   * is no such token, this field is null.
50
   */
51
  public Token specialToken;
52

  
53
  /**
54
   * Returns the image.
55
   */
56
  public String toString()
57
  {
58
     return image;
59
  }
60

  
61
  /**
62
   * Returns a new Token object, by default. However, if you want, you
63
   * can create and return subclass objects based on the value of ofKind.
64
   * Simply add the cases to the switch for all those special cases.
65
   * For example, if you have a subclass of Token called IDToken that
66
   * you want to create if ofKind is ID, simlpy add something like :
67
   *
68
   *    case MyParserConstants.ID : return new IDToken();
69
   *
70
   * to the following switch statement. Then you can cast matchedToken
71
   * variable to the appropriate type and use it in your lexical actions.
72
   */
73
  public static final Token newToken(int ofKind)
74
  {
75
     switch(ofKind)
76
     {
77
       default : return new Token();
78
     }
79
  }
80

  
81
}
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/TokenMgrError.java
1
/* Generated By:JavaCC: Do not edit this line. TokenMgrError.java Version 3.0 */
2
package org.gvsig.symbology.fmap.labeling.parse2;
3

  
4
public @SuppressWarnings("all") class TokenMgrError extends Error
5
{
6
   /*
7
    * Ordinals for various reasons why an Error of this type can be thrown.
8
    */
9

  
10
   /**
11
    * Lexical error occurred.
12
    */
13
   static final int LEXICAL_ERROR = 0;
14

  
15
   /**
16
    * An attempt was made to create a second instance of a static token manager.
17
    */
18
   static final int STATIC_LEXER_ERROR = 1;
19

  
20
   /**
21
    * Tried to change to an invalid lexical state.
22
    */
23
   static final int INVALID_LEXICAL_STATE = 2;
24

  
25
   /**
26
    * Detected (and bailed out of) an infinite loop in the token manager.
27
    */
28
   static final int LOOP_DETECTED = 3;
29

  
30
   /**
31
    * Indicates the reason why the exception is thrown. It will have
32
    * one of the above 4 values.
33
    */
34
   int errorCode;
35

  
36
   /**
37
    * Replaces unprintable characters by their escaped (or unicode escaped)
38
    * equivalents in the given string
39
    */
40
   protected static final String addEscapes(String str) {
41
      StringBuffer retval = new StringBuffer();
42
      char ch;
43
      for (int i = 0; i < str.length(); i++) {
44
        switch (str.charAt(i))
45
        {
46
           case 0 :
47
              continue;
48
           case '\b':
49
              retval.append("\\b");
50
              continue;
51
           case '\t':
52
              retval.append("\\t");
53
              continue;
54
           case '\n':
55
              retval.append("\\n");
56
              continue;
57
           case '\f':
58
              retval.append("\\f");
59
              continue;
60
           case '\r':
61
              retval.append("\\r");
62
              continue;
63
           case '\"':
64
              retval.append("\\\"");
65
              continue;
66
           case '\'':
67
              retval.append("\\\'");
68
              continue;
69
           case '\\':
70
              retval.append("\\\\");
71
              continue;
72
           default:
73
              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
74
                 String s = "0000" + Integer.toString(ch, 16);
75
                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
76
              } else {
77
                 retval.append(ch);
78
              }
79
              continue;
80
        }
81
      }
82
      return retval.toString();
83
   }
84

  
85
   /**
86
    * Returns a detailed message for the Error when it is thrown by the
87
    * token manager to indicate a lexical error.
88
    * Parameters : 
89
    *    EOFSeen     : indicates if EOF caused the lexical error
90
    *    curLexState : lexical state in which this error occurred
91
    *    errorLine   : line number when the error occurred
92
    *    errorColumn : column number when the error occurred
93
    *    errorAfter  : prefix that was seen before this error occurred
94
    *    curchar     : the offending character
95
    * Note: You can customize the lexical error message by modifying this method.
96
    */
97
   protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {
98
      return("Lexical error at line " +
99
           errorLine + ", column " +
100
           errorColumn + ".  Encountered: " +
101
           (EOFSeen ? "<EOF> " : ("\"" + addEscapes(String.valueOf(curChar)) + "\"") + " (" + (int)curChar + "), ") +
102
           "after : \"" + addEscapes(errorAfter) + "\"");
103
   }
104

  
105
   /**
106
    * You can also modify the body of this method to customize your error messages.
107
    * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not
108
    * of end-users concern, so you can return something like : 
109
    *
110
    *     "Internal Error : Please file a bug report .... "
111
    *
112
    * from this method for such cases in the release version of your parser.
113
    */
114
   public String getMessage() {
115
      return super.getMessage();
116
   }
117

  
118
   /*
119
    * Constructors of various flavors follow.
120
    */
121

  
122
   public TokenMgrError() {
123
   }
124

  
125
   public TokenMgrError(String message, int reason) {
126
      super(message);
127
      errorCode = reason;
128
   }
129

  
130
   public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {
131
      this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);
132
   }
133
}
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/SemanticException.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.symbology.fmap.labeling.parse2;
42

  
43
public class SemanticException extends ParseException {
44
	public static final int TYPE_MISMATCH = -1;
45
	public static final int UNDEFINED_SYMBOL = -2;
46
	
47
	private int type = 0;
48
	
49
	public SemanticException(int exceptionType) {
50
		this(exceptionType, "Unknown.");
51
	}
52
	
53
	public SemanticException(int exceptionType, String message) {
54
		super(message);
55
		type = exceptionType;
56
	}
57
	
58
}
trunk/extensions/extSymbology/src/org/gvsig/symbology/fmap/labeling/parse2/SimpleCharStream.java
1
/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
2
package org.gvsig.symbology.fmap.labeling.parse2;
3

  
4
/**
5
 * An implementation of interface CharStream, where the stream is assumed to
6
 * contain only ASCII characters (without unicode processing).
7
 */
8

  
9
public @SuppressWarnings("all") class SimpleCharStream
10
{
11
  public static final boolean staticFlag = false;
12
  int bufsize;
13
  int available;
14
  int tokenBegin;
15
  public int bufpos = -1;
16
  protected int bufline[];
17
  protected int bufcolumn[];
18

  
19
  protected int column = 0;
20
  protected int line = 1;
21

  
22
  protected boolean prevCharIsCR = false;
23
  protected boolean prevCharIsLF = false;
24

  
25
  protected java.io.Reader inputStream;
26

  
27
  protected char[] buffer;
28
  protected int maxNextCharInd = 0;
29
  protected int inBuf = 0;
30
  protected int tabSize = 8;
31

  
32
  protected void setTabSize(int i) { tabSize = i; }
33
  protected int getTabSize(int i) { return tabSize; }
34

  
35

  
36
  protected void ExpandBuff(boolean wrapAround)
37
  {
38
     char[] newbuffer = new char[bufsize + 2048];
39
     int newbufline[] = new int[bufsize + 2048];
40
     int newbufcolumn[] = new int[bufsize + 2048];
41

  
42
     try
43
     {
44
        if (wrapAround)
45
        {
46
           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
47
           System.arraycopy(buffer, 0, newbuffer,
48
                                             bufsize - tokenBegin, bufpos);
49
           buffer = newbuffer;
50

  
51
           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
52
           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
53
           bufline = newbufline;
54

  
55
           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
56
           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
57
           bufcolumn = newbufcolumn;
58

  
59
           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
60
        }
61
        else
62
        {
63
           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
64
           buffer = newbuffer;
65

  
66
           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
67
           bufline = newbufline;
68

  
69
           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
70
           bufcolumn = newbufcolumn;
71

  
72
           maxNextCharInd = (bufpos -= tokenBegin);
73
        }
74
     }
75
     catch (Throwable t)
76
     {
77
        throw new Error(t.getMessage());
78
     }
79

  
80

  
81
     bufsize += 2048;
82
     available = bufsize;
83
     tokenBegin = 0;
84
  }
85

  
86
  protected void FillBuff() throws java.io.IOException
87
  {
88
     if (maxNextCharInd == available)
89
     {
90
        if (available == bufsize)
91
        {
92
           if (tokenBegin > 2048)
93
           {
94
              bufpos = maxNextCharInd = 0;
95
              available = tokenBegin;
96
           }
97
           else if (tokenBegin < 0)
98
              bufpos = maxNextCharInd = 0;
99
           else
100
              ExpandBuff(false);
101
        }
102
        else if (available > tokenBegin)
103
           available = bufsize;
104
        else if ((tokenBegin - available) < 2048)
105
           ExpandBuff(true);
106
        else
107
           available = tokenBegin;
108
     }
109

  
110
     int i;
111
     try {
112
        if ((i = inputStream.read(buffer, maxNextCharInd,
113
                                    available - maxNextCharInd)) == -1)
114
        {
115
           inputStream.close();
116
           throw new java.io.IOException();
117
        }
118
        else
119
           maxNextCharInd += i;
120
        return;
121
     }
122
     catch(java.io.IOException e) {
123
        --bufpos;
124
        backup(0);
125
        if (tokenBegin == -1)
126
           tokenBegin = bufpos;
127
        throw e;
128
     }
129
  }
130

  
131
  public char BeginToken() throws java.io.IOException
132
  {
133
     tokenBegin = -1;
134
     char c = readChar();
135
     tokenBegin = bufpos;
136

  
137
     return c;
138
  }
139

  
140
  protected void UpdateLineColumn(char c)
141
  {
142
     column++;
143

  
144
     if (prevCharIsLF)
145
     {
146
        prevCharIsLF = false;
147
        line += (column = 1);
148
     }
149
     else if (prevCharIsCR)
150
     {
151
        prevCharIsCR = false;
152
        if (c == '\n')
153
        {
154
           prevCharIsLF = true;
155
        }
156
        else
157
           line += (column = 1);
158
     }
159

  
160
     switch (c)
161
     {
162
        case '\r' :
163
           prevCharIsCR = true;
164
           break;
165
        case '\n' :
166
           prevCharIsLF = true;
167
           break;
168
        case '\t' :
169
           column--;
170
           column += (tabSize - (column % tabSize));
171
           break;
172
        default :
173
           break;
174
     }
175

  
176
     bufline[bufpos] = line;
177
     bufcolumn[bufpos] = column;
178
  }
179

  
180
  public char readChar() throws java.io.IOException
181
  {
182
     if (inBuf > 0)
183
     {
184
        --inBuf;
185

  
186
        if (++bufpos == bufsize)
187
           bufpos = 0;
188

  
189
        return buffer[bufpos];
190
     }
191

  
192
     if (++bufpos >= maxNextCharInd)
193
        FillBuff();
194

  
195
     char c = buffer[bufpos];
196

  
197
     UpdateLineColumn(c);
198
     return c;
199
  }
200

  
201
  /**
202
   * @deprecated 
203
   * @see #getEndColumn
204
   */
205

  
206
  public int getColumn() {
207
     return bufcolumn[bufpos];
208
  }
209

  
210
  /**
211
   * @deprecated 
212
   * @see #getEndLine
213
   */
214

  
215
  public int getLine() {
216
     return bufline[bufpos];
217
  }
218

  
219
  public int getEndColumn() {
220
     return bufcolumn[bufpos];
221
  }
222

  
223
  public int getEndLine() {
224
     return bufline[bufpos];
225
  }
226

  
227
  public int getBeginColumn() {
228
     return bufcolumn[tokenBegin];
229
  }
230

  
231
  public int getBeginLine() {
232
     return bufline[tokenBegin];
233
  }
234

  
235
  public void backup(int amount) {
236

  
237
    inBuf += amount;
238
    if ((bufpos -= amount) < 0)
239
       bufpos += bufsize;
240
  }
241

  
242
  public SimpleCharStream(java.io.Reader dstream, int startline,
243
  int startcolumn, int buffersize)
244
  {
245
    inputStream = dstream;
246
    line = startline;
247
    column = startcolumn - 1;
248

  
249
    available = bufsize = buffersize;
250
    buffer = new char[buffersize];
251
    bufline = new int[buffersize];
252
    bufcolumn = new int[buffersize];
253
  }
254

  
255
  public SimpleCharStream(java.io.Reader dstream, int startline,
256
                          int startcolumn)
257
  {
258
     this(dstream, startline, startcolumn, 4096);
259
  }
260

  
261
  public SimpleCharStream(java.io.Reader dstream)
262
  {
263
     this(dstream, 1, 1, 4096);
264
  }
265
  public void ReInit(java.io.Reader dstream, int startline,
266
  int startcolumn, int buffersize)
267
  {
268
    inputStream = dstream;
269
    line = startline;
270
    column = startcolumn - 1;
271

  
272
    if (buffer == null || buffersize != buffer.length)
273
    {
274
      available = bufsize = buffersize;
275
      buffer = new char[buffersize];
276
      bufline = new int[buffersize];
277
      bufcolumn = new int[buffersize];
278
    }
279
    prevCharIsLF = prevCharIsCR = false;
280
    tokenBegin = inBuf = maxNextCharInd = 0;
281
    bufpos = -1;
282
  }
283

  
284
  public void ReInit(java.io.Reader dstream, int startline,
285
                     int startcolumn)
286
  {
287
     ReInit(dstream, startline, startcolumn, 4096);
288
  }
289

  
290
  public void ReInit(java.io.Reader dstream)
291
  {
292
     ReInit(dstream, 1, 1, 4096);
293
  }
294
  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
295
  int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
296
  {
297
     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
298
  }
299

  
300
  public SimpleCharStream(java.io.InputStream dstream, int startline,
301
  int startcolumn, int buffersize)
302
  {
303
     this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
304
  }
305

  
306
  public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
307
                          int startcolumn) throws java.io.UnsupportedEncodingException
308
  {
309
     this(dstream, encoding, startline, startcolumn, 4096);
310
  }
311

  
312
  public SimpleCharStream(java.io.InputStream dstream, int startline,
313
                          int startcolumn)
314
  {
315
     this(dstream, startline, startcolumn, 4096);
316
  }
317

  
318
  public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
319
  {
320
     this(dstream, encoding, 1, 1, 4096);
321
  }
322

  
323
  public SimpleCharStream(java.io.InputStream dstream)
324
  {
325
     this(dstream, 1, 1, 4096);
326
  }
327

  
328
  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
329
                          int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
330
  {
331
     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
332
  }
333

  
334
  public void ReInit(java.io.InputStream dstream, int startline,
335
                          int startcolumn, int buffersize)
336
  {
337
     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
338
  }
339

  
340
  public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
341
  {
342
     ReInit(dstream, encoding, 1, 1, 4096);
343
  }
344

  
345
  public void ReInit(java.io.InputStream dstream)
346
  {
347
     ReInit(dstream, 1, 1, 4096);
348
  }
349
  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
350
                     int startcolumn) throws java.io.UnsupportedEncodingException
351
  {
352
     ReInit(dstream, encoding, startline, startcolumn, 4096);
353
  }
354
  public void ReInit(java.io.InputStream dstream, int startline,
355
                     int startcolumn)
356
  {
357
     ReInit(dstream, startline, startcolumn, 4096);
358
  }
359
  public String GetImage()
360
  {
361
     if (bufpos >= tokenBegin)
362
        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
363
     else
364
        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
365
                              new String(buffer, 0, bufpos + 1);
366
  }
367

  
368
  public char[] GetSuffix(int len)
369
  {
370
     char[] ret = new char[len];
371

  
372
     if ((bufpos + 1) >= len)
373
        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
374
     else
375
     {
376
        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
377
                                                          len - bufpos - 1);
378
        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
379
     }
380

  
381
     return ret;
382
  }
383

  
384
  public void Done()
385
  {
386
     buffer = null;
387
     bufline = null;
388
     bufcolumn = null;
389
  }
390

  
391
  /**
392
   * Method to adjust line and column numbers for the start of a token.
393
   */
394
  public void adjustBeginLineColumn(int newLine, int newCol)
395
  {
396
     int start = tokenBegin;
397
     int len;
398

  
399
     if (bufpos >= tokenBegin)
400
     {
401
        len = bufpos - tokenBegin + inBuf + 1;
402
     }
403
     else
404
     {
405
        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
406
     }
407

  
408
     int i = 0, j = 0, k = 0;
409
     int nextColDiff = 0, columnDiff = 0;
410

  
411
     while (i < len &&
412
            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
413
     {
414
        bufline[j] = newLine;
415
        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
416
        bufcolumn[j] = newCol + columnDiff;
417
        columnDiff = nextColDiff;
418
        i++;
419
     } 
420

  
421
     if (i < len)
422
     {
423
        bufline[j] = newLine++;
424
        bufcolumn[j] = newCol + columnDiff;
425

  
426
        while (i++ < len)
427
        {
428
           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
429
              bufline[j] = newLine++;
430
           else
431
              bufline[j] = newLine;
432
        }
433
     }
434

  
435
     line = bufline[j];
436
     column = bufcolumn[j];
437
  }
438

  
439
}

Also available in: Unified diff