]>
Commit | Line | Data |
---|---|---|
0c15323d | 1 | /* Locations for Bison |
7d424de1 | 2 | |
7d6bad19 | 3 | Copyright (C) 2002, 2004-2013 Free Software Foundation, Inc. |
0c15323d AD |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
f16b0819 | 7 | This program is free software: you can redistribute it and/or modify |
0c15323d | 8 | it under the terms of the GNU General Public License as published by |
f16b0819 PE |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
0c15323d | 11 | |
f16b0819 | 12 | This program is distributed in the hope that it will be useful, |
0c15323d AD |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
f16b0819 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
0c15323d AD |
19 | |
20 | #ifndef LOCATION_H_ | |
21 | # define LOCATION_H_ | |
22 | ||
dca81a78 PE |
23 | # include "uniqstr.h" |
24 | ||
8f759107 PE |
25 | /* A boundary between two characters. */ |
26 | typedef struct | |
0c15323d | 27 | { |
8f759107 | 28 | /* The name of the file that contains the boundary. */ |
dca81a78 | 29 | uniqstr file; |
8f759107 | 30 | |
92822aff JD |
31 | /* If nonnegative, the (origin-1) line that contains the boundary. |
32 | If this is INT_MAX, the line number has overflowed. | |
33 | ||
34 | Meaningless and not displayed if negative. | |
35 | */ | |
8f759107 PE |
36 | int line; |
37 | ||
56c5eca9 AD |
38 | /* If nonnegative, the (origin-1) column just after the boundary. |
39 | This is neither a byte count, nor a character count; it is a | |
40 | column count. If this is INT_MAX, the column number has | |
41 | overflowed. | |
42 | ||
43 | Meaningless and not displayed if negative. | |
44 | */ | |
8f759107 PE |
45 | int column; |
46 | ||
47 | } boundary; | |
48 | ||
e9071366 AD |
49 | /* Set the position of \a a. */ |
50 | static inline void | |
51 | boundary_set (boundary *b, const char *f, int l, int c) | |
52 | { | |
db06f0ce PE |
53 | b->file = f; |
54 | b->line = l; | |
55 | b->column = c; | |
e9071366 AD |
56 | } |
57 | ||
12cf133f AD |
58 | /* Return -1, 0, 1, depending whether a is before, equal, or |
59 | after b. */ | |
60 | static inline int | |
61 | boundary_cmp (boundary a, boundary b) | |
62 | { | |
63 | int res = strcmp (a.file, b.file); | |
64 | if (!res) | |
65 | res = a.line - b.line; | |
66 | if (!res) | |
67 | res = a.column - b.column; | |
68 | return res; | |
69 | } | |
70 | ||
8f759107 PE |
71 | /* Return nonzero if A and B are equal boundaries. */ |
72 | static inline bool | |
73 | equal_boundaries (boundary a, boundary b) | |
74 | { | |
75 | return (a.column == b.column | |
e9690142 JD |
76 | && a.line == b.line |
77 | && UNIQSTR_EQ (a.file, b.file)); | |
8f759107 PE |
78 | } |
79 | ||
80 | /* A location, that is, a region of source code. */ | |
81 | typedef struct | |
82 | { | |
83 | /* Boundary just before the location starts. */ | |
84 | boundary start; | |
85 | ||
86 | /* Boundary just after the location ends. */ | |
87 | boundary end; | |
88 | ||
69b8cc09 | 89 | } location; |
8f759107 | 90 | |
a99ec53e | 91 | # define GRAM_LTYPE location |
0c15323d | 92 | |
a99ec53e | 93 | # define EMPTY_LOCATION_INIT {{NULL, 0, 0}, {NULL, 0, 0}} |
69b8cc09 | 94 | extern location const empty_location; |
8f759107 | 95 | |
e9071366 AD |
96 | /* Set *LOC and adjust scanner cursor to account for token TOKEN of |
97 | size SIZE. */ | |
98 | void location_compute (location *loc, | |
e9690142 | 99 | boundary *cur, char const *token, size_t size); |
e9071366 | 100 | |
ec6dc437 AD |
101 | /* Print location to file. |
102 | Return number of actually printed characters. | |
103 | Warning: uses quotearg's slot 3. */ | |
b805eca7 | 104 | unsigned location_print (location loc, FILE *out); |
8f759107 | 105 | |
3f5d1b2c TR |
106 | /* Free any allocated ressources and close any open file handles that are |
107 | left-over by the usage of location_caret. */ | |
108 | void cleanup_caret (void); | |
109 | ||
110 | /* Output to OUT the line and caret corresponding to location LOC. */ | |
b805eca7 | 111 | void location_caret (location loc, FILE *out); |
3f5d1b2c | 112 | |
12cf133f AD |
113 | /* Return -1, 0, 1, depending whether a is before, equal, or |
114 | after b. */ | |
115 | static inline int | |
116 | location_cmp (location a, location b) | |
117 | { | |
118 | int res = boundary_cmp (a.start, b.start); | |
119 | if (!res) | |
120 | res = boundary_cmp (a.end, b.end); | |
121 | return res; | |
122 | } | |
123 | ||
45eebca4 | 124 | /* LOC_STR must be formatted as 'file:line.column', it will be modified. */ |
3fc65ead JD |
125 | void boundary_set_from_string (boundary *bound, char *loc_str); |
126 | ||
8f759107 | 127 | #endif /* ! defined LOCATION_H_ */ |