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