2 // Author: Sergey Chaban (serge@wildwestsoftware.com)
6 using System
.Globalization
;
12 internal class NumberHelper
: StringHelperBase
{
14 private ILToken result
;
18 /// <param name="host"></param>
19 public NumberHelper (ILTokenizer host
) : base (host
)
27 result
= ILToken
.Invalid
.Clone() as ILToken
;
32 /// <returns></returns>
33 public override bool Start (char ch
)
35 bool res
= (Char
.IsDigit (ch
) || ch
== '-' || (ch
== '.' && Char
.IsDigit ((char) host
.Reader
.Peek ())));
42 return (e
>= '0' && e
<= '9') || (e
>= 'A' && e
<= 'F') || (e
>= 'a' && e
<= 'f');
47 return ((ch
== '+') || (ch
== '-'));
52 return ((ch
== 'e') || (ch
== 'E'));
57 /// <returns></returns>
58 public override string Build ()
60 ILReader reader
= host
.Reader
;
61 reader
.MarkLocation ();
62 StringBuilder num_builder
= new StringBuilder ();
67 bool dec_found
= false;
69 NumberStyles nstyles
= NumberStyles
.AllowExponent
| NumberStyles
.AllowDecimalPoint
|
70 NumberStyles
.AllowLeadingSign
;
73 peek
= reader
.Peek ();
76 if (ch
== '0' && (peek
== 'x' || peek
== 'X'))
79 if (is_sign (reader
.Peek ()))
80 num_builder
.Append ((char) reader
.Read ());
84 peek
= reader
.Peek ();
85 num_builder
.Append ((char) ch
);
89 throw new ILTokenizingException (reader
.Location
, num_builder
.ToString ());
96 !(peek
== '.' && !dec_found
) && !is_e (peek
) &&
97 !(is_sign (peek
) && is_real
)) {
102 num
= num_builder
.ToString ();
104 // Check for hexbytes
105 if (num
.Length
== 2) {
106 if (Char
.IsLetter (num
[0]) || Char
.IsLetter (num
[1])) {
107 result
.token
= Token
.HEXBYTE
;
108 result
.val
= Byte
.Parse (num
, NumberStyles
.HexNumber
);
113 if (ch
== '.' && peek
== '.') {
114 num
= num
.Substring (0, num
.Length
-1);
117 } else if (ch
== '.') {
121 if (!dec_found
&& !is_real
) {
123 long i
= Int64
.Parse (num
, nstyles
);
124 result
.token
= Token
.INT64
;
132 long i
= (long) UInt64
.Parse (num
, nstyles
);
133 result
.token
= Token
.INT64
;
142 double d
= Double
.Parse (num
, nstyles
, NumberFormatInfo
.InvariantInfo
);
143 result
.token
= Token
.FLOAT64
;
146 reader
.Unread (num
.ToCharArray ());
147 reader
.RestoreLocation ();
150 throw new ILTokenizingException (reader
.Location
, num_builder
.ToString ());
155 public string BuildHex ()
157 ILReader reader
= host
.Reader
;
158 reader
.MarkLocation ();
159 StringBuilder num_builder
= new StringBuilder ();
160 NumberStyles nstyles
= NumberStyles
.HexNumber
;
168 throw new ILTokenizingException (reader
.Location
, ((char) ch
).ToString ());
172 if (ch
!= 'x' && ch
!= 'X')
173 throw new ILTokenizingException (reader
.Location
, "0" + (char) ch
);
177 peek
= reader
.Peek ();
178 num_builder
.Append ((char) ch
);
180 if (!is_hex ((char) peek
))
183 if (num_builder
.Length
== 32)
184 throw new ILTokenizingException (reader
.Location
, num_builder
.ToString ());
188 num
= num_builder
.ToString ();
191 long i
= (long) UInt64
.Parse (num
, nstyles
);
192 //if (i < Int32.MinValue || i > Int32.MaxValue) {
193 result
.token
= Token
.INT64
;
196 // result.token = Token.INT32;
197 // result.val = (int) i;
201 reader
.Unread (num
.ToCharArray ());
202 reader
.RestoreLocation ();
205 throw new ILTokenizingException (reader
.Location
, tnum
);
212 public ILToken ResultToken
{