gnulib: update
[bison.git] / src / location.h
blobaf21c70f5faa371ac8eb4f64edb5b787668630da
1 /* Locations for Bison
3 Copyright (C) 2002, 2004-2015, 2018-2021 Free Software Foundation,
4 Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
21 #ifndef LOCATION_H_
22 # define LOCATION_H_
24 # include <stdbool.h>
25 # include <stdio.h>
26 # include <string.h> /* strcmp */
28 # include "uniqstr.h"
30 /* A boundary between two characters. */
31 typedef struct
33 /* The name of the file that contains the boundary. */
34 uniqstr file;
36 /* If positive, the line (starting at 1) that contains the boundary.
37 If this is INT_MAX, the line number has overflowed.
39 Meaningless and not displayed if nonpositive.
41 int line;
43 /* If positive, the column (starting at 1) just after the boundary.
44 This is neither a byte count, nor a character count; it is a
45 (visual) column count. If this is INT_MAX, the column number has
46 overflowed.
48 Meaningless and not displayed if nonpositive. */
49 int column;
51 /* If nonnegative, the byte number (starting at 0) in the current
52 line. Not displayed (unless --trace=location). */
53 int byte;
55 } boundary;
57 /* Set the position of \a p. */
58 static inline void
59 boundary_set (boundary *p, const char *f, int l, int c, int b)
61 p->file = f;
62 p->line = l;
63 p->column = c;
64 p->byte = b;
67 /* Return -1, 0, 1, depending whether a is before, equal, or
68 after b. */
69 static inline int
70 boundary_cmp (boundary a, boundary b)
72 /* Locations with no file first. */
73 int res =
74 a.file && b.file ? strcmp (a.file, b.file)
75 : a.file ? 1
76 : b.file ? -1
77 : 0;
78 if (!res)
79 res = a.line - b.line;
80 if (!res)
81 res = a.column - b.column;
82 return res;
85 /* Return nonzero if A and B are equal boundaries. */
86 static inline bool
87 equal_boundaries (boundary a, boundary b)
89 return (a.column == b.column
90 && a.line == b.line
91 && UNIQSTR_EQ (a.file, b.file));
94 /* A location, that is, a region of source code. */
95 typedef struct
97 /* Boundary just before the location starts. */
98 boundary start;
100 /* Boundary just after the location ends. */
101 boundary end;
103 } location;
105 # define GRAM_LTYPE location
107 # define EMPTY_LOCATION_INIT {{NULL, 0, 0, 0}, {NULL, 0, 0, 0}}
108 extern location const empty_loc;
110 /* Set *LOC and adjust scanner cursor to account for token TOKEN of
111 size SIZE. */
112 void location_compute (location *loc,
113 boundary *cur, char const *token, size_t size);
115 /* Print location to file.
116 Return number of actually printed characters.
117 Warning: uses quotearg's slot 3. */
118 int location_print (location loc, FILE *out);
120 /* Prepare the use of location_caret. */
121 void caret_init (void);
123 /* Free any allocated resources and close any open file handles that are
124 left-over by the usage of location_caret. */
125 void caret_free (void);
127 /* If -fcaret is enabled, quote the line containing LOC onto OUT.
128 Highlight the part of LOC with the color STYLE. */
129 void location_caret (location loc, const char* style, FILE *out);
131 /* If -fcaret is enabled, display a suggestion of replacement for LOC
132 with S. To call after location_caret. */
133 void location_caret_suggestion (location loc, const char *s, FILE *out);
135 /* Return -1, 0, 1, depending whether a is before, equal, or
136 after b. */
137 static inline int
138 location_cmp (location a, location b)
140 int res = boundary_cmp (a.start, b.start);
141 if (!res)
142 res = boundary_cmp (a.end, b.end);
143 return res;
146 /* Whether this is the empty location. */
147 bool location_empty (location loc);
149 /* STR must be formatted as 'file:line.column@byte' or 'file:line.column',
150 it will be modified. */
151 void boundary_set_from_string (boundary *bound, char *str);
153 #endif /* ! defined LOCATION_H_ */