00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef __MMX_SOURCE_TRACK_HPP
00014 #define __MMX_SOURCE_TRACK_HPP
00015 #include <basix/string.hpp>
00016 #include <basix/identifiers.hpp>
00017
00019
00020 namespace mmx {
00021
00022
00023
00024
00025
00026
00027 class source_position {
00028 MMX_ALLOCATORS
00029 public:
00030
00031 nat position;
00032 nat line;
00033 nat column;
00034 source_position (void) : position (0), line (0), column (0) {}
00035 source_position (nat p, nat l, nat c):
00036 position (p), line (l), column (c) {}
00037 source_position (const source_position& p):
00038 position (p.position), line (p.line), column (p.column) {}
00039 inline source_position& operator= (const source_position& p) {
00040 position = p.position;
00041 line = p.line;
00042 column = p.column;
00043 return *this;
00044 }
00045 };
00046
00047 inline source_position
00048 min (const source_position& p1, const source_position& p2) {
00049 return p1.position < p2.position? p1: p2;
00050 };
00051
00052 inline source_position
00053 max (const source_position& p1, const source_position& p2) {
00054 return p1.position > p2.position? p1: p2;
00055 };
00056
00057
00058
00059
00060
00061 class source_location {
00062 MMX_ALLOCATORS
00063 public:
00064 generic obj;
00065 string file_name;
00066 nat input_number;
00067 source_position begin;
00068 source_position end;
00069
00070 inline source_location () : obj (), file_name (""),
00071 input_number (0), begin (), end () {}
00072 inline source_location (generic g, string f, nat i,
00073 const source_position& b,
00074 const source_position& e) :
00075 obj (g), file_name (f),
00076 input_number (i), begin (b), end(e) {}
00077 inline source_location (const source_location& l) :
00078 obj (l.obj), file_name (l.file_name),
00079 input_number (l.input_number),
00080 begin (l.begin), end (l.end) {}
00081 inline source_location& operator= (const source_location& l) {
00082 obj = l.obj;
00083 file_name = l.file_name;
00084 input_number = l.input_number;
00085 begin = l.begin;
00086 end = l.end;
00087 return *this;
00088 }
00089 };
00090
00091 inline bool
00092 is_nil (const source_location& l) {
00093 return (l.end.line == 0 && l.end.position == 0);
00094 };
00095
00096 inline syntactic
00097 flatten (const source_location& l) {
00098 return flatten (gen (GEN_TUPLE, l.obj, l.file_name, l.input_number,
00099 gen (GEN_TUPLE, l.begin.line, l.begin.column),
00100 gen (GEN_TUPLE, l.end.line, l.end.column)));
00101 }
00102
00103
00104
00105
00106
00107 void store_interactive_number (nat n);
00108
00109
00110 nat get_interactive_number (void);
00111
00112
00113 void store_interactive_source (const string& data,
00114 nat n= get_interactive_number ());
00115
00116
00117 string get_interactive_source (nat i);
00118
00119
00120 void store_file_source (const string& file_name, const string& data);
00121
00122
00123 string get_file_source (const string& file_name);
00124
00125
00126 string get_source (const string& file_name, nat input_number, nat line);
00127
00128
00129
00130
00131
00132
00133
00134
00135 void source_insert (const generic& g, const source_location& l);
00136
00137
00138 void source_delete (const generic& g);
00139
00140
00141 generic source_link (const generic& g, const generic& h, const generic& kind);
00142
00143
00144
00145
00146 generic source_extend (const generic& g1, const generic& g2);
00147
00148
00149
00150 generic source_assign (const generic& g1, const generic& g2);
00151
00152
00153
00154 inline generic
00155 source_extend (const generic& g1, const generic& g2, const generic& g3) {
00156 return source_extend (source_extend (g1, g2), g3);
00157 }
00158
00159
00160
00161
00162
00163 void source_locate (const generic& g, source_location& l);
00164
00165
00166
00167
00168
00169 source_location source_locate (const generic& g);
00170
00171
00172 bool source_has_exact (const generic& g);
00173
00174
00175 bool source_exists (const generic& g);
00176
00177
00178 string source_file (const generic& g);
00179
00180
00181 int source_line (const generic& g, const bool& last);
00182
00183
00184 int source_column (const generic& g, const bool& last);
00185
00186
00187 string source_begin (const generic& g);
00188
00189
00190 string source_end (const generic& g);
00191
00192
00193 string source_string (const generic& g);
00194
00195
00196 string source_string_unindented (const generic& g);
00197
00198
00199 string source_underlined (const generic& g);
00200
00201
00202 string source_error (const string& msg, const generic& where);
00203
00204
00205 string source_exception (const exception& e);
00206
00207
00208 }
00209 #endif // __MMX_SOURCE_TRACK_HPP