* MethodTable.cs: Add method to Check if all methods have been defined, set reference...
[mono.git] / mcs / ilasm / scanner / NumberHelper.cs
1 // NumberHelper.cs\r
2 // Author: Sergey Chaban (serge@wildwestsoftware.com)\r
3 \r
4 using System;\r
5 using System.Globalization;\r
6 \r
7 namespace Mono.ILASM {\r
8 \r
9         /// <summary>\r
10         /// </summary>\r
11         internal class NumberHelper : StringHelperBase {\r
12 \r
13                 private ILToken result;\r
14 \r
15                 /// <summary>\r
16                 /// </summary>\r
17                 /// <param name="host"></param>\r
18                 public NumberHelper (ILTokenizer host) : base (host)\r
19                 {\r
20                         Reset ();\r
21                 }\r
22 \r
23 \r
24                 private void Reset ()\r
25                 {\r
26                         result = ILToken.Invalid.Clone() as ILToken;\r
27                 }\r
28 \r
29                 /// <summary>\r
30                 /// </summary>\r
31                 /// <returns></returns>\r
32                 public override bool Start (char ch)\r
33                 {\r
34                         bool res = (Char.IsDigit (ch) || ch == '-' || (ch == '.' && Char.IsDigit ((char) host.Reader.Peek ())));\r
35                         Reset ();\r
36                         return res;\r
37                 }\r
38 \r
39                 /// <summary>\r
40                 /// </summary>\r
41                 /// <returns></returns>\r
42                 public override string Build ()\r
43                 {\r
44                         ILReader reader = host.Reader;\r
45                         reader.MarkLocation ();\r
46                         string num = reader.ReadToWhitespace ();\r
47 \r
48                         NumberStyles nstyles = NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint;\r
49 \r
50                         try {\r
51                                 if (num.IndexOf ('.') != -1) {\r
52                                         double d = Double.Parse (num, nstyles, NumberFormatInfo.InvariantInfo);\r
53                                         result.token = Token.FLOAT64;\r
54                                         result.val = d;\r
55                                 } else {\r
56                                         long i = Int64.Parse (num, nstyles);\r
57                                         if (i < Int32.MinValue || i > Int32.MaxValue) {\r
58                                                 result.token = Token.INT64;\r
59                                                 result.val = i;\r
60                                         } else {\r
61                                                 result.token = Token.INT32;\r
62                                                 result.val = (int) i;\r
63                                         }\r
64                                 }\r
65                         } catch {\r
66                                 reader.Unread (num.ToCharArray ());\r
67                                 reader.RestoreLocation ();\r
68                                 num = String.Empty;\r
69                                 Reset ();\r
70                                 throw new ILSyntaxError ("Bad number format!");\r
71                         }\r
72                         return num;\r
73                 }\r
74 \r
75 \r
76                 /// <summary>\r
77                 /// </summary>\r
78                 public ILToken ResultToken {\r
79                         get {\r
80                                 return result;\r
81                         }\r
82                 }\r
83 \r
84 \r
85         }\r
86 \r
87 }\r