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>
35 struct string *make_string(char *str) {
36 struct string *string;
42 struct string *dup_string(const char *str) {
43 struct string *string;
46 string->str = strdup("");
48 string->str = strdup(str);
49 if (string->str == NULL)
50 unref(string, string);
54 void free_string(struct string *string) {
57 assert(string->ref == 0);
65 char *format_info(struct info *info) {
71 return strdup("(no file info)");
74 int fl = info->first_line, ll = info->last_line;
75 int fc = info->first_column, lc = info->last_column;
76 fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
81 r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc);
83 r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc);
86 r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc);
89 r = xasprintf(&result, "%s:", fname);
91 return (r == -1) ? NULL : result;
94 void print_info(FILE *out, struct info *info) {
96 fprintf(out, "(no file info):");
100 info->filename != NULL ? info->filename->str : "(unknown file)");
101 if (info->first_line > 0) {
102 if (info->first_line == info->last_line) {
103 if (info->first_column == info->last_column) {
104 fprintf(out, "%d.%d:", info->first_line, info->first_column);
106 fprintf(out, "%d.%d-.%d:", info->first_line,
107 info->first_column, info->last_column);
110 fprintf(out, "%d.%d-%d.%d:",
111 info->first_line, info->first_column,
112 info->last_line, info->last_column);
117 bool typecheck_p(const struct info *info) {
118 return (info->error->aug->flags & AUG_TYPE_CHECK) != 0;
121 void free_info(struct info *info) {
124 assert(info->ref == 0);
125 unref(info->filename, string);
129 struct span *make_span(struct info *info) {
130 struct span *span = NULL;
131 if (ALLOC(span) < 0) {
134 /* UINT_MAX means span is not initialized yet */
135 span->span_start = UINT_MAX;
136 span->filename = ref(info->filename);
140 void free_span(struct span *span) {
143 unref(span->filename, string);
147 void print_span(struct span *span) {
150 printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n",
152 span->label_start, span->label_end,
153 span->value_start, span->value_end,
154 span->span_start, span->span_end);
157 void update_span(struct span *node_info, int x, int y) {
158 if (node_info == NULL)
160 if (node_info->span_start == UINT_MAX) {
161 node_info->span_start = x;
162 node_info->span_end = y;
164 if (node_info->span_start > x)
165 node_info->span_start = x;
166 if (node_info->span_end < y)
167 node_info->span_end = y;
173 * indent-tabs-mode: nil