| 1 |
/* NumberParser.java */ |
| 2 |
|
| 3 |
|
| 4 |
/* |
| 5 |
This parser is used to check the syntax of numbers read by our |
| 6 |
formatted read routines. |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
options { |
| 11 |
STATIC = false; |
| 12 |
DEBUG_PARSER = false; |
| 13 |
DEBUG_TOKEN_MANAGER = false; |
| 14 |
DEBUG_LOOKAHEAD = false; |
| 15 |
} |
| 16 |
|
| 17 |
PARSER_BEGIN(NumberParser) |
| 18 |
package org.j_paine.formatter; |
| 19 |
|
| 20 |
class NumberParser |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
PARSER_END(NumberParser) |
| 25 |
|
| 26 |
|
| 27 |
TOKEN : |
| 28 |
{ |
| 29 |
< INTEGER_LITERAL: |
| 30 |
<DECIMAL_LITERAL> |
| 31 |
> |
| 32 |
| |
| 33 |
< #DECIMAL_LITERAL: |
| 34 |
"0" |
| 35 |
| ["1"-"9"] (["0"-"9"])* |
| 36 |
> |
| 37 |
| |
| 38 |
< LOGICAL_LITERAL: |
| 39 |
"T" | "F" |
| 40 |
> |
| 41 |
// We don't allow leading zeroes in integers, as these |
| 42 |
// might indicate typing errors in the data. |
| 43 |
| |
| 44 |
< FLOATING_POINT_LITERAL: |
| 45 |
(["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? |
| 46 |
| "." (["0"-"9"])+ (<EXPONENT>)? |
| 47 |
| (["0"-"9"])+ <EXPONENT> |
| 48 |
| (["0"-"9"])+ (<EXPONENT>)? |
| 49 |
> |
| 50 |
| |
| 51 |
< #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ > |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
int Float(): |
| 56 |
{ int start = 0; |
| 57 |
} |
| 58 |
{ |
| 59 |
( " " {start++;} )* |
| 60 |
[ "-" | "+" ] |
| 61 |
( <INTEGER_LITERAL> | <FLOATING_POINT_LITERAL> ) |
| 62 |
<EOF> |
| 63 |
{ return start; } |
| 64 |
} |
| 65 |
// This is the syntax of numbers we want a real format to accept. |
| 66 |
// The <EOF> makes sure that trailing non-numeric characters |
| 67 |
// (even spaces) are reported as an error. |
| 68 |
// Returns an integer which is the number of spaces to skip before |
| 69 |
// the number starts. |
| 70 |
|
| 71 |
|
| 72 |
int Integer(): |
| 73 |
{ int start = 0; |
| 74 |
} |
| 75 |
{ |
| 76 |
( " " {start++;} )* |
| 77 |
[ "-" | "+" ] |
| 78 |
<INTEGER_LITERAL> |
| 79 |
<EOF> |
| 80 |
{ return start; } |
| 81 |
} |
| 82 |
// This is the syntax of numbers we want an integer format to |
| 83 |
// accept. |
| 84 |
// Returns an integer which is the number of spaces to skip before |
| 85 |
// the number starts. |
| 86 |
|
| 87 |
int Boolean(): |
| 88 |
{ int start = 0; |
| 89 |
} |
| 90 |
{ |
| 91 |
( " " {start++;} )* |
| 92 |
<LOGICAL_LITERAL> |
| 93 |
<EOF> |
| 94 |
{ return start; } |
| 95 |
} |