2 * info.c: filename/linenumber information for parser/interpreter
4 * Copyright (C) 2007-2015 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) {
66 int fl = info->first_line, ll = info->last_line;
67 int fc = info->first_column, lc = info->last_column;
68 fname = (info->filename != NULL) ? info->filename->str : "(unknown file)";
73 r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc);
75 r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc);
78 r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc);
81 r = xasprintf(&result, "%s:", fname);
83 return (r == -1) ? NULL : result;
86 void print_info(FILE *out, struct info *info) {
88 fprintf(out, "(no file info):");
92 info->filename != NULL ? info->filename->str : "(unknown file)");
93 if (info->first_line > 0) {
94 if (info->first_line == info->last_line) {
95 if (info->first_column == info->last_column) {
96 fprintf(out, "%d.%d:", info->first_line, info->first_column);
98 fprintf(out, "%d.%d-.%d:", info->first_line,
99 info->first_column, info->last_column);
102 fprintf(out, "%d.%d-%d.%d:",
103 info->first_line, info->first_column,
104 info->last_line, info->last_column);
109 void free_info(struct info *info) {
112 assert(info->ref == 0);
113 unref(info->filename, string);
117 struct span *make_span(struct info *info) {
118 struct span *span = NULL;
119 if (ALLOC(span) < 0) {
122 /* UINT_MAX means span is not initialized yet */
123 span->span_start = UINT_MAX;
124 span->filename = ref(info->filename);
128 void free_span(struct span *span) {
131 unref(span->filename, string);
135 void print_span(struct span *span) {
138 printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n",
140 span->label_start, span->label_end,
141 span->value_start, span->value_end,
142 span->span_start, span->span_end);
145 void update_span(struct span *node_info, int x, int y) {
146 if (node_info == NULL)
148 if (node_info->span_start == UINT_MAX) {
149 node_info->span_start = x;
150 node_info->span_end = y;
152 if (node_info->span_start > x)
153 node_info->span_start = x;
154 if (node_info->span_end < y)
155 node_info->span_end = y;
161 * indent-tabs-mode: nil