lexer.h: Add WARN_UNUSED_RESULT to lex_force functions.
[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 "libpspp/compiler.h"
27 #include "libpspp/prompt.h"
28
29 struct lexer;
30
31 /* The syntax mode for which a syntax file is intended. */
32 enum lex_syntax_mode
33   {
34     LEX_SYNTAX_AUTO,            /* Try to guess intent. */
35     LEX_SYNTAX_INTERACTIVE,     /* Interactive mode. */
36     LEX_SYNTAX_BATCH            /* Batch mode. */
37   };
38
39 /* Handling of errors. */
40 enum lex_error_mode
41   {
42     LEX_ERROR_TERMINAL,        /* Discard input line and continue reading. */
43     LEX_ERROR_CONTINUE,        /* Continue to next command, except for
44                                   cascading failures. */
45     LEX_ERROR_STOP             /* Stop processing. */
46   };
47
48 /* Reads a single syntax file as a stream of bytes encoded in UTF-8.
49
50    Not opaque. */
51 struct lex_reader
52   {
53     const struct lex_reader_class *class;
54     enum lex_syntax_mode syntax;
55     enum lex_error_mode error;
56     char *encoding;
57     char *file_name;            /* NULL if not associated with a file. */
58     int line_number;            /* 1-based initial line number, 0 if none. */
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 lex_syntax_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 /* Files. */
99 void lex_include (struct lexer *, struct lex_reader *);
100 void lex_append (struct lexer *, struct lex_reader *);
101
102 /* Advancing. */
103 void lex_get (struct lexer *);
104
105 /* Token testing functions. */
106 bool lex_is_number (struct lexer *);
107 double lex_number (struct lexer *);
108 bool lex_is_integer (struct lexer *);
109 long lex_integer (struct lexer *);
110 bool lex_is_string (struct lexer *);
111
112 /* Token testing functions with lookahead. */
113 bool lex_next_is_number (struct lexer *, int n);
114 double lex_next_number (struct lexer *, int n);
115 bool lex_next_is_integer (struct lexer *, int n);
116 long lex_next_integer (struct lexer *, int n);
117 bool lex_next_is_string (struct lexer *, int n);
118
119 /* Token matching functions. */
120 bool lex_match (struct lexer *, enum token_type);
121 bool lex_match_id (struct lexer *, const char *);
122 bool lex_match_id_n (struct lexer *, const char *, size_t n);
123 bool lex_match_int (struct lexer *, int);
124 bool lex_match_phrase (struct lexer *, const char *s);
125
126 /* Forcible matching functions. */
127 bool lex_force_match (struct lexer *, enum token_type) WARN_UNUSED_RESULT;
128 bool lex_force_match_id (struct lexer *, const char *) WARN_UNUSED_RESULT;
129 bool lex_force_int (struct lexer *) WARN_UNUSED_RESULT;
130 bool lex_force_num (struct lexer *) WARN_UNUSED_RESULT;
131 bool lex_force_id (struct lexer *) WARN_UNUSED_RESULT;
132 bool lex_force_string (struct lexer *) WARN_UNUSED_RESULT;
133 bool lex_force_string_or_id (struct lexer *) WARN_UNUSED_RESULT;
134
135 /* Token accessors. */
136 enum token_type lex_token (const struct lexer *);
137 double lex_tokval (const struct lexer *);
138 const char *lex_tokcstr (const struct lexer *);
139 struct substring lex_tokss (const struct lexer *);
140
141 /* Looking ahead. */
142 const struct token *lex_next (const struct lexer *, int n);
143 enum token_type lex_next_token (const struct lexer *, int n);
144 const char *lex_next_tokcstr (const struct lexer *, int n);
145 double lex_next_tokval (const struct lexer *, int n);
146 struct substring lex_next_tokss (const struct lexer *, int n);
147
148 /* Current position. */
149 int lex_get_first_line_number (const struct lexer *, int n);
150 int lex_get_last_line_number (const struct lexer *, int n);
151 int lex_get_first_column (const struct lexer *, int n);
152 int lex_get_last_column (const struct lexer *, int n);
153 const char *lex_get_file_name (const struct lexer *);
154 const char *lex_get_encoding (const struct lexer *);
155
156 /* Issuing errors. */
157 void lex_error (struct lexer *, const char *, ...) PRINTF_FORMAT (2, 3);
158 void lex_next_error (struct lexer *, int n0, int n1, const char *, ...)
159   PRINTF_FORMAT (4, 5);
160 int lex_end_of_command (struct lexer *);
161
162 void lex_error_expecting (struct lexer *, const char *, ...) SENTINEL(0);
163
164 void lex_sbc_only_once (const char *);
165 void lex_sbc_missing (const char *);
166
167 void lex_spec_only_once (struct lexer *, const char *subcommand,
168                          const char *specification);
169 void lex_spec_missing (struct lexer *, const char *subcommand,
170                        const char *specification);
171
172 void lex_error_valist (struct lexer *, const char *, va_list)
173   PRINTF_FORMAT (2, 0);
174 void lex_next_error_valist (struct lexer *lexer, int n0, int n1,
175                             const char *format, va_list)
176   PRINTF_FORMAT (4, 0);
177
178 /* Error handling. */
179 enum lex_syntax_mode lex_get_syntax_mode (const struct lexer *);
180 enum lex_error_mode lex_get_error_mode (const struct lexer *);
181 void lex_discard_rest_of_command (struct lexer *);
182 void lex_interactive_reset (struct lexer *);
183 void lex_discard_noninteractive (struct lexer *);
184
185 #endif /* lexer.h */