scan: New library for high-level PSPP syntax lexical analysis.
[pspp-builds.git] / src / libpspp / msg-locator.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "libpspp/msg-locator.h"
20
21 #include <stdlib.h>
22
23 #include "libpspp/assertion.h"
24 #include "libpspp/cast.h"
25 #include "libpspp/message.h"
26 #include "libpspp/getl.h"
27
28 #include "gl/xalloc.h"
29
30 /* File locator stack. */
31 static const struct msg_locator **file_loc;
32
33 static int nfile_loc, mfile_loc;
34
35 void
36 msg_locator_done (void)
37 {
38   free(file_loc);
39   file_loc = NULL;
40   nfile_loc = mfile_loc = 0;
41 }
42
43
44 /* File locator stack functions. */
45
46 /* Pushes F onto the stack of file locations. */
47 void
48 msg_push_msg_locator (const struct msg_locator *loc)
49 {
50   if (nfile_loc >= mfile_loc)
51     {
52       if (mfile_loc == 0)
53         mfile_loc = 8;
54       else
55         mfile_loc *= 2;
56
57       file_loc = xnrealloc (file_loc, mfile_loc, sizeof *file_loc);
58     }
59
60   file_loc[nfile_loc++] = loc;
61 }
62
63 /* Pops F off the stack of file locations.
64    Argument F is only used for verification that that is actually the
65    item on top of the stack. */
66 void
67 msg_pop_msg_locator (const struct msg_locator *loc)
68 {
69   assert (nfile_loc >= 0 && file_loc[nfile_loc - 1] == loc);
70   nfile_loc--;
71 }
72
73 /* Puts the current file and line number into LOC, or NULL and -1 if
74    none. */
75 void
76 get_msg_location (const struct source_stream *ss, struct msg_locator *loc)
77 {
78   if (nfile_loc)
79     {
80       *loc = *file_loc[nfile_loc - 1];
81     }
82   else
83     {
84       loc->file_name = CONST_CAST (char *, getl_source_name (ss));
85       loc->line_number = getl_source_location (ss);
86     }
87 }