improve macro error messages
[pspp] / src / language / lexer / lexer.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011, 2013, 2014 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 #ifndef LEXER_H
18 #define LEXER_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <unistd.h>
23
24 #include "data/identifier.h"
25 #include "data/variable.h"
26 #include "language/lexer/segment.h"
27 #include "libpspp/cast.h"
28 #include "libpspp/compiler.h"
29 #include "libpspp/prompt.h"
30
31 struct lexer;
32 struct macro;
33
34 /* Handling of errors. */
35 enum lex_error_mode
36   {
37     LEX_ERROR_TERMINAL,        /* Discard input line and continue reading. */
38     LEX_ERROR_CONTINUE,        /* Continue to next command, except for
39                                   cascading failures. */
40     LEX_ERROR_STOP             /* Stop processing. */
41   };
42
43 /* Reads a single syntax file as a stream of bytes encoded in UTF-8.
44
45    Not opaque. */
46 struct lex_reader
47   {
48     const struct lex_reader_class *class;
49     enum segmenter_mode syntax;
50     enum lex_error_mode error;
51     char *encoding;
52     char *file_name;            /* NULL if not associated with a file. */
53     int line_number;            /* 1-based initial line number, 0 if none. */
54     bool eof;
55   };
56
57 /* An implementation of a lex_reader. */
58 struct lex_reader_class
59   {
60     /* Reads up to N bytes of data from READER into N.  Returns the positive
61        number of bytes read if successful, or zero at end of input or on
62        error.
63
64        STYLE provides a hint to interactive readers as to what kind of syntax
65        is being read right now. */
66     size_t (*read) (struct lex_reader *reader, char *buf, size_t n,
67                     enum prompt_style style);
68
69     /* Closes and destroys READER, releasing any allocated storage.
70
71        The caller will free the 'file_name' member of READER, so the
72        implementation should not do so. */
73     void (*destroy) (struct lex_reader *reader);
74   };
75
76 /* Helper functions for lex_reader. */
77 void lex_reader_init (struct lex_reader *, const struct lex_reader_class *);
78 void lex_reader_set_file_name (struct lex_reader *, const char *file_name);
79
80 /* Creating various kinds of lex_readers. */
81 struct lex_reader *lex_reader_for_file (const char *file_name,
82                                         const char *encoding,
83                                         enum segmenter_mode syntax,
84                                         enum lex_error_mode error);
85 struct lex_reader *lex_reader_for_string (const char *, const char *encoding);
86 struct lex_reader *lex_reader_for_format (const char *, const char *, ...)
87   PRINTF_FORMAT (1, 3);
88 struct lex_reader *lex_reader_for_substring_nocopy (struct substring, const char *encoding);
89
90 /* Initialization. */
91 struct lexer *lex_create (void);
92 void lex_destroy (struct lexer *);
93
94 /* Macros. */
95 void lex_define_macro (struct lexer *, struct macro *);
96
97 /* Files. */
98 void lex_include (struct lexer *, struct lex_reader *);
99 void lex_append (struct lexer *, struct lex_reader *);
100
101 /* Advancing. */
102 void lex_get (struct lexer *);
103
104 /* Token testing functions. */
105 bool lex_is_number (const struct lexer *);
106 double lex_number (const struct lexer *);
107 bool lex_is_integer (const struct lexer *);
108 long lex_integer (const struct lexer *);
109 bool lex_is_string (const struct lexer *);
110
111 /* Token testing functions with lookahead. */
112 bool lex_next_is_number (const struct lexer *, int n);
113 double lex_next_number (const struct lexer *, int n);
114 bool lex_next_is_integer (const struct lexer *, int n);
115 long lex_next_integer (const struct lexer *, int n);
116 bool lex_next_is_string (const struct lexer *, int n);
117
118 /* Token matching functions. */
119 bool lex_match (struct lexer *, enum token_type);
120 bool lex_match_id (struct lexer *, const char *);
121 bool lex_match_id_n (struct lexer *, const char *, size_t n);
122 bool lex_match_int (struct lexer *, int);
123 bool lex_match_phrase (struct lexer *, const char *s);
124
125 /* Forcible matching functions. */
126 bool lex_force_match (struct lexer *, enum token_type) WARN_UNUSED_RESULT;
127 bool lex_force_match_id (struct lexer *, const char *) WARN_UNUSED_RESULT;
128 bool lex_force_int (struct lexer *) WARN_UNUSED_RESULT;
129 bool lex_force_int_range (struct lexer *, const char *name,
130                           long min, long max) WARN_UNUSED_RESULT;
131 bool lex_force_num (struct lexer *) WARN_UNUSED_RESULT;
132 bool lex_force_id (struct lexer *) WARN_UNUSED_RESULT;
133 bool lex_force_string (struct lexer *) WARN_UNUSED_RESULT;
134 bool lex_force_string_or_id (struct lexer *) WARN_UNUSED_RESULT;
135
136 /* Token accessors. */
137 enum token_type lex_token (const struct lexer *);
138 double lex_tokval (const struct lexer *);
139 const char *lex_tokcstr (const struct lexer *);
140 struct substring lex_tokss (const struct lexer *);
141
142 /* Looking ahead. */
143 const struct token *lex_next (const struct lexer *, int n);
144 enum token_type lex_next_token (const struct lexer *, int n);
145 const char *lex_next_tokcstr (const struct lexer *, int n);
146 double lex_next_tokval (const struct lexer *, int n);
147 struct substring lex_next_tokss (const struct lexer *, int n);
148
149 /* Token representation. */
150 char *lex_next_representation (const struct lexer *, int n0, int n1);
151 bool lex_next_is_from_macro (const struct lexer *, int n);
152
153 /* Current position. */
154 int lex_get_first_line_number (const struct lexer *, int n);
155 int lex_get_last_line_number (const struct lexer *, int n);
156 int lex_get_first_column (const struct lexer *, int n);
157 int lex_get_last_column (const struct lexer *, int n);
158 const char *lex_get_file_name (const struct lexer *);
159 const char *lex_get_encoding (const struct lexer *);
160
161 /* Issuing errors. */
162 void lex_error (struct lexer *, const char *, ...) PRINTF_FORMAT (2, 3);
163 void lex_next_error (struct lexer *, int n0, int n1, const char *, ...)
164   PRINTF_FORMAT (4, 5);
165 int lex_end_of_command (struct lexer *);
166
167 void lex_error_expecting (struct lexer *, ...) SENTINEL(0);
168 #define lex_error_expecting(...) \
169   lex_error_expecting(__VA_ARGS__, NULL_SENTINEL)
170 void lex_error_expecting_valist (struct lexer *, va_list);
171 void lex_error_expecting_array (struct lexer *, const char **, size_t n);
172
173 void lex_sbc_only_once (const char *);
174 void lex_sbc_missing (const char *);
175
176 void lex_spec_only_once (struct lexer *, const char *subcommand,
177                          const char *specification);
178 void lex_spec_missing (struct lexer *, const char *subcommand,
179                        const char *specification);
180
181 void lex_error_valist (struct lexer *, const char *, va_list)
182   PRINTF_FORMAT (2, 0);
183 void lex_next_error_valist (struct lexer *lexer, int n0, int n1,
184                             const char *format, va_list)
185   PRINTF_FORMAT (4, 0);
186
187 /* Error handling. */
188 enum segmenter_mode lex_get_syntax_mode (const struct lexer *);
189 enum lex_error_mode lex_get_error_mode (const struct lexer *);
190 void lex_discard_rest_of_command (struct lexer *);
191 void lex_interactive_reset (struct lexer *);
192 void lex_discard_noninteractive (struct lexer *);
193
194 #endif /* lexer.h */