DISPLAY MACROS: New command.
[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/message.h"
30 #include "libpspp/prompt.h"
31 #include "libpspp/str.h"
32
33 struct lexer;
34 struct lex_source;
35 struct macro;
36
37 /* Handling of errors. */
38 enum lex_error_mode
39   {
40     LEX_ERROR_TERMINAL,        /* Discard input line and continue reading. */
41     LEX_ERROR_CONTINUE,        /* Continue to next command, except for
42                                   cascading failures. */
43     LEX_ERROR_IGNORE,          /* Continue, even for cascading failures. */
44     LEX_ERROR_STOP,            /* Stop processing. */
45   };
46
47 /* Reads a single syntax file as a stream of bytes encoded in UTF-8.
48
49    Not opaque. */
50 struct lex_reader
51   {
52     const struct lex_reader_class *class;
53     enum segmenter_mode syntax;
54     enum lex_error_mode error;
55     char *encoding;
56     char *file_name;            /* NULL if not associated with a file. */
57     int line_number;            /* 1-based initial line number, 0 if none. */
58     bool eof;
59   };
60
61 /* An implementation of a lex_reader. */
62 struct lex_reader_class
63   {
64     /* Reads up to N bytes of data from READER into N.  Returns the positive
65        number of bytes read if successful, or zero at end of input or on
66        error.
67
68        STYLE provides a hint to interactive readers as to what kind of syntax
69        is being read right now. */
70     size_t (*read) (struct lex_reader *reader, char *buf, size_t n,
71                     enum prompt_style style);
72
73     /* Closes and destroys READER, releasing any allocated storage.
74
75        The caller will free the 'file_name' member of READER, so the
76        implementation should not do so. */
77     void (*destroy) (struct lex_reader *reader);
78   };
79
80 /* Helper functions for lex_reader. */
81 void lex_reader_init (struct lex_reader *, const struct lex_reader_class *);
82 void lex_reader_set_file_name (struct lex_reader *, const char *file_name);
83
84 /* Creating various kinds of lex_readers. */
85 struct lex_reader *lex_reader_for_file (const char *file_name,
86                                         const char *encoding,
87                                         enum segmenter_mode syntax,
88                                         enum lex_error_mode error);
89 struct lex_reader *lex_reader_for_string (const char *, const char *encoding);
90 struct lex_reader *lex_reader_for_format (const char *, const char *, ...)
91   PRINTF_FORMAT (1, 3);
92 struct lex_reader *lex_reader_for_substring_nocopy (struct substring, const char *encoding);
93
94 /* Initialization. */
95 struct lexer *lex_create (void);
96 void lex_destroy (struct lexer *);
97
98 /* Macros. */
99 void lex_define_macro (struct lexer *, struct macro *);
100 const struct macro_set *lex_get_macros (const struct lexer *);
101
102 /* Files. */
103 void lex_include (struct lexer *, struct lex_reader *);
104 void lex_append (struct lexer *, struct lex_reader *);
105
106 /* Advancing. */
107 void lex_get (struct lexer *);
108 void lex_get_n (struct lexer *, size_t n);
109
110 /* Token testing functions. */
111 bool lex_is_number (const struct lexer *);
112 double lex_number (const struct lexer *);
113 bool lex_is_integer (const struct lexer *);
114 long lex_integer (const struct lexer *);
115 bool lex_is_string (const struct lexer *);
116
117 /* Token testing functions with lookahead. */
118 bool lex_next_is_number (const struct lexer *, int n);
119 double lex_next_number (const struct lexer *, int n);
120 bool lex_next_is_integer (const struct lexer *, int n);
121 long lex_next_integer (const struct lexer *, int n);
122 bool lex_next_is_string (const struct lexer *, int n);
123
124 /* Token matching functions. */
125 bool lex_match (struct lexer *, enum token_type);
126 bool lex_match_id (struct lexer *, const char *);
127 bool lex_match_id_n (struct lexer *, const char *, size_t n);
128 bool lex_match_int (struct lexer *, int);
129 bool lex_at_phrase (struct lexer *, const char *s);
130 bool lex_match_phrase (struct lexer *, const char *s);
131 bool lex_force_match_phrase (struct lexer *, const char *s);
132
133 /* Forcible matching functions. */
134 bool lex_force_match (struct lexer *, enum token_type) WARN_UNUSED_RESULT;
135 bool lex_force_match_id (struct lexer *, const char *) WARN_UNUSED_RESULT;
136 bool lex_force_int (struct lexer *) WARN_UNUSED_RESULT;
137 bool lex_force_int_range (struct lexer *, const char *name,
138                           long min, long max) WARN_UNUSED_RESULT;
139 bool lex_force_num (struct lexer *) WARN_UNUSED_RESULT;
140 bool lex_force_num_range_closed (struct lexer *, const char *name,
141                                  double min, double max) WARN_UNUSED_RESULT;
142 bool lex_force_num_range_halfopen (struct lexer *, const char *name,
143                                    double min, double max) WARN_UNUSED_RESULT;
144 bool lex_force_num_range_open (struct lexer *, const char *name,
145                                double min, double max) WARN_UNUSED_RESULT;
146 bool lex_force_id (struct lexer *) WARN_UNUSED_RESULT;
147 bool lex_force_string (struct lexer *) WARN_UNUSED_RESULT;
148 bool lex_force_string_or_id (struct lexer *) WARN_UNUSED_RESULT;
149
150 /* Token accessors. */
151 enum token_type lex_token (const struct lexer *);
152 double lex_tokval (const struct lexer *);
153 const char *lex_tokcstr (const struct lexer *);
154 struct substring lex_tokss (const struct lexer *);
155
156 /* Looking ahead. */
157 const struct token *lex_next (const struct lexer *, int n);
158 enum token_type lex_next_token (const struct lexer *, int n);
159 const char *lex_next_tokcstr (const struct lexer *, int n);
160 double lex_next_tokval (const struct lexer *, int n);
161 struct substring lex_next_tokss (const struct lexer *, int n);
162
163 /* Looking at the current command, including lookahead and lookbehind. */
164 int lex_ofs (const struct lexer *);
165 int lex_max_ofs (const struct lexer *);
166 const struct token *lex_ofs_token (const struct lexer *, int ofs);
167 struct msg_location *lex_ofs_location (const struct lexer *, int ofs0, int ofs1);
168 struct msg_point lex_ofs_start_point (const struct lexer *, int ofs);
169 struct msg_point lex_ofs_end_point (const struct lexer *, int ofs);
170
171 /* Token representation. */
172 char *lex_next_representation (const struct lexer *, int n0, int n1);
173 char *lex_ofs_representation (const struct lexer *, int ofs0, int ofs1);
174 bool lex_next_is_from_macro (const struct lexer *, int n);
175
176 /* Current position. */
177 const char *lex_get_file_name (const struct lexer *);
178 struct msg_location *lex_get_location (const struct lexer *, int n0, int n1);
179 const char *lex_get_encoding (const struct lexer *);
180 const struct lex_source *lex_source (const struct lexer *);
181
182 /* Issuing errors and warnings. */
183 void lex_error (struct lexer *, const char *, ...) PRINTF_FORMAT (2, 3);
184 void lex_next_error (struct lexer *, int n0, int n1, const char *, ...)
185   PRINTF_FORMAT (4, 5);
186 void lex_ofs_error (struct lexer *, int ofs0, int ofs1, const char *, ...)
187   PRINTF_FORMAT (4, 5);
188
189 void lex_msg (struct lexer *, enum msg_class, const char *, ...)
190   PRINTF_FORMAT (3, 4);
191 void lex_next_msg (struct lexer *, enum msg_class, int n0, int n1,
192                    const char *, ...)
193   PRINTF_FORMAT (5, 6);
194 void lex_ofs_msg (struct lexer *, enum msg_class, int ofs0, int ofs1,
195                   const char *, ...)
196   PRINTF_FORMAT (5, 6);
197 void lex_ofs_msg_valist (struct lexer *lexer, enum msg_class,
198                          int ofs0, int ofs1, const char *format, va_list)
199   PRINTF_FORMAT (5, 0);
200
201 int lex_end_of_command (struct lexer *);
202
203 void lex_error_expecting (struct lexer *, ...) SENTINEL(0);
204 #define lex_error_expecting(...) \
205   lex_error_expecting(__VA_ARGS__, NULL_SENTINEL)
206 void lex_error_expecting_valist (struct lexer *, va_list);
207 void lex_error_expecting_array (struct lexer *, const char **, size_t n);
208
209 void lex_sbc_only_once (struct lexer *, const char *);
210 void lex_sbc_missing (struct lexer *, const char *);
211
212 void lex_spec_only_once (struct lexer *, const char *subcommand,
213                          const char *specification);
214 void lex_spec_missing (struct lexer *, const char *subcommand,
215                        const char *specification);
216
217 /* Error handling. */
218 enum segmenter_mode lex_get_syntax_mode (const struct lexer *);
219 enum lex_error_mode lex_get_error_mode (const struct lexer *);
220 void lex_discard_rest_of_command (struct lexer *);
221 void lex_interactive_reset (struct lexer *);
222 void lex_discard_noninteractive (struct lexer *);
223
224 /* Source code access. */
225 void lex_set_message_handler (struct lexer *,
226                               void (*output_msg) (const struct msg *,
227                                                   struct lexer *));
228 struct lex_source *lex_source_ref (const struct lex_source *);
229 void lex_source_unref (struct lex_source *);
230 struct substring lex_source_get_line (const struct lex_source *, int line);
231
232 #endif /* lexer.h */