Old code
[mono.git] / mcs / mbas / location.cs
1 //\r
2 // location.cs: Keeps track of the location of source code entity\r
3 //\r
4 // Author:\r
5 //   Miguel de Icaza\r
6 //\r
7 // (C) 2001 Ximian, Inc.\r
8 //\r
9 \r
10 using System;\r
11 using System.Collections;\r
12 \r
13 namespace Mono.CSharp {\r
14         /// <summary>\r
15         ///   Keeps track of the location in the program\r
16         /// </summary>\r
17         ///\r
18         /// <remarks>\r
19         ///   This uses a compact representation and a couple of auxiliary\r
20         ///   structures to keep track of tokens to (file,line) mappings.\r
21         ///\r
22         ///   We could probably also keep track of columns by storing those\r
23         ///   in 8 bits (and say, map anything after char 255 to be `255+').\r
24         /// </remarks>\r
25         public struct Location {\r
26                 public int token; \r
27 \r
28                 static Hashtable map;\r
29                 static ArrayList list;\r
30                 static int global_count;\r
31                 static int module_base;\r
32 \r
33                 static Location ()\r
34                 {\r
35                         map = new Hashtable ();\r
36                         list = new ArrayList ();\r
37                         global_count = 0;\r
38                         module_base = 0;\r
39                 }\r
40         \r
41                 static public void Push (string name)\r
42                 {\r
43                         map.Remove (global_count);\r
44                         map.Add (global_count, name);\r
45                         list.Add (global_count);\r
46                         module_base = global_count;\r
47                 }\r
48                 \r
49                 public Location (int row)\r
50                 {\r
51                         if (row < 0)\r
52                                 token = -1;\r
53                         else {\r
54                                 token = module_base + row;\r
55                                 if (global_count < token)\r
56                                         global_count = token;\r
57                         }\r
58                 }\r
59 \r
60                 public override string ToString ()\r
61                 {\r
62                         return Name + ": (" + Row + ")";\r
63                 }\r
64                 \r
65                 /// <summary>\r
66                 ///   Whether the Location is Null\r
67                 /// </summary>\r
68                 static public bool IsNull (Location l)\r
69                 {\r
70                         return l.token == -1;\r
71                 }\r
72 \r
73                 static public Location Null {\r
74                         get {\r
75                                 return new Location (-1);\r
76                         }\r
77                 }\r
78 \r
79                 public string Name {\r
80                         get {\r
81                                 int best = 0;\r
82                                 \r
83                                 if (token < 0)\r
84                                         return "Internal";\r
85 \r
86                                 foreach (int b in list){\r
87                                         if (token > b)\r
88                                                 best = b;\r
89                                 }\r
90                                 return (string) map [best];\r
91                         }\r
92                 }\r
93 \r
94                 public int Row {\r
95                         get {\r
96                                 int best = 0;\r
97                                 \r
98                                 if (token < 0)\r
99                                         return 1;\r
100                                 \r
101                                 foreach (int b in list){\r
102                                         if (token > b)\r
103                                                 best = b;\r
104                                 }\r
105                                 return token - best;\r
106                         }\r
107                 }\r
108         }\r
109 }\r