2 * info.c: filename/linenumber information for parser/interpreter
4 * Copyright (C) 2007-2016 David Lutterkort
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Author: David Lutterkort <lutter@redhat.com>
32 struct string *make_string(char *str) {
33 struct string *string;
39 struct string *dup_string(const char *str) {
40 struct string *string;
43 string->str = strdup("");
45 string->str = strdup(str);
46 if (string->str == NULL)
47 unref(string, string);
51 void free_string(struct string *string) {
54 assert(string->ref == 0);
62 char *format_info(struct info *info) {
68 return strdup("(no file info)");
71 int fl = info->first_line, ll = info->last_line;
72 int fc = info->first_column, lc = info->last_column;
73 fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
78 r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc);
80 r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc);
83 r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc);
86 r = xasprintf(&result, "%s:", fname);
88 return (r == -1) ? NULL : result;
91 void print_info(FILE *out, struct info *info) {
93 fprintf(out, "(no file info):");
97 info->filename != NULL ? info->filename->str : "(unknown file)");
98 if (info->first_line > 0) {
99 if (info->first_line == info->last_line) {
100 if (info->first_column == info->last_column) {
101 fprintf(out, "%d.%d:", info->first_line, info->first_column);
103 fprintf(out, "%d.%d-.%d:", info->first_line,
104 info->first_column, info->last_column);
107 fprintf(out, "%d.%d-%d.%d:",
108 info->first_line, info->first_column,
109 info->last_line, info->last_column);
114 void free_info(struct info *info) {
117 assert(info->ref == 0);
118 unref(info->filename, string);
122 struct span *make_span(struct info *info) {
123 struct span *span = NULL;
124 if (ALLOC(span) < 0) {
127 /* UINT_MAX means span is not initialized yet */
128 span->span_start = UINT_MAX;
129 span->filename = ref(info->filename);
133 void free_span(struct span *span) {
136 unref(span->filename, string);
140 void print_span(struct span *span) {
143 printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n",
145 span->label_start, span->label_end,
146 span->value_start, span->value_end,
147 span->span_start, span->span_end);
150 void update_span(struct span *node_info, int x, int y) {
151 if (node_info == NULL)
153 if (node_info->span_start == UINT_MAX) {
154 node_info->span_start = x;
155 node_info->span_end = y;
157 if (node_info->span_start > x)
158 node_info->span_start = x;
159 if (node_info->span_end < y)
160 node_info->span_end = y;
166 * indent-tabs-mode: nil