lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / lexer / lexer.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 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 #ifndef LEXER_H
18 #define LEXER_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22
23 #include "data/identifier.h"
24 #include "data/variable.h"
25 #include "libpspp/compiler.h"
26 #include "libpspp/prompt.h"
27
28 struct lexer;
29
30 /* The syntax mode for which a syntax file is intended. */
31 enum lex_syntax_mode
32   {
33     LEX_SYNTAX_AUTO,            /* Try to guess intent. */
34     LEX_SYNTAX_INTERACTIVE,     /* Interactive mode. */
35     LEX_SYNTAX_BATCH            /* Batch mode. */
36   };
37
38 /* Handling of errors. */
39 enum lex_error_mode
40   {
41     LEX_ERROR_INTERACTIVE,     /* Always continue to next command. */
42     LEX_ERROR_CONTINUE,        /* Continue to next command, except for
43                                   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 lex_syntax_mode syntax;
54     enum lex_error_mode error;
55     char *file_name;            /* NULL if not associated with a file. */
56     int line_number;            /* 1-based initial line number, 0 if none. */
57   };
58
59 /* An implementation of a lex_reader. */
60 struct lex_reader_class
61   {
62     /* Reads up to N bytes of data from READER into N.  Returns the positive
63        number of bytes read if successful, or zero at end of input or on
64        error.
65
66        STYLE provides a hint to interactive readers as to what kind of syntax
67        is being read right now. */
68     size_t (*read) (struct lex_reader *reader, char *buf, size_t n,
69                     enum prompt_style style);
70
71     /* Closes and destroys READER, releasing any allocated storage.
72
73        The caller will free the 'file_name' member of READER, so the
74        implementation should not do so. */
75     void (*close) (struct lex_reader *reader);
76   };
77
78 /* Helper functions for lex_reader. */
79 void lex_reader_init (struct lex_reader *, const struct lex_reader_class *);
80 void lex_reader_set_file_name (struct lex_reader *, const char *file_name);
81
82 /* Creating various kinds of lex_readers. */
83 struct lex_reader *lex_reader_for_file (const char *file_name,
84                                         const char *encoding,
85                                         enum lex_syntax_mode syntax,
86                                         enum lex_error_mode error);
87 struct lex_reader *lex_reader_for_string (const char *);
88 struct lex_reader *lex_reader_for_format (const char *, ...)
89   PRINTF_FORMAT (1, 2);
90 struct lex_reader *lex_reader_for_substring_nocopy (struct substring);
91
92 /* Initialization. */
93 struct lexer *lex_create (void);
94 void lex_destroy (struct lexer *);
95
96 /* Files. */
97 void lex_include (struct lexer *, struct lex_reader *);
98 void lex_append (struct lexer *, struct lex_reader *);
99
100 /* Advancing. */
101 void lex_get (struct lexer *);
102
103 /* Token testing functions. */
104 bool lex_is_number (struct lexer *);
105 double lex_number (struct lexer *);
106 bool lex_is_integer (struct lexer *);
107 long lex_integer (struct lexer *);
108 bool lex_is_string (struct lexer *);
109
110 /* Token testing functions with lookahead. */
111 bool lex_next_is_number (struct lexer *, int n);
112 double lex_next_number (struct lexer *, int n);
113 bool lex_next_is_integer (struct lexer *, int n);
114 long lex_next_integer (struct lexer *, int n);
115 bool lex_next_is_string (struct lexer *, int n);
116
117 /* Token matching functions. */
118 bool lex_match (struct lexer *, enum token_type);
119 bool lex_match_id (struct lexer *, const char *);
120 bool lex_match_id_n (struct lexer *, const char *, size_t n);
121 bool lex_match_int (struct lexer *, int);
122 bool lex_match_phrase (struct lexer *, const char *s);
123
124 /* Forcible matching functions. */
125 bool lex_force_match (struct lexer *, enum token_type);
126 bool lex_force_match_id (struct lexer *, const char *);
127 bool lex_force_int (struct lexer *);
128 bool lex_force_num (struct lexer *);
129 bool lex_force_id (struct lexer *);
130 bool lex_force_string (struct lexer *);
131
132 /* Token accessors. */
133 enum token_type lex_token (const struct lexer *);
134 double lex_tokval (const struct lexer *);
135 const char *lex_tokcstr (const struct lexer *);
136 struct substring lex_tokss (const struct lexer *);
137
138 /* Looking ahead. */
139 const struct token *lex_next (const struct lexer *, int n);
140 enum token_type lex_next_token (const struct lexer *, int n);
141 const char *lex_next_tokcstr (const struct lexer *, int n);
142 double lex_next_tokval (const struct lexer *, int n);
143 struct substring lex_next_tokss (const struct lexer *, int n);
144
145 /* Current position. */
146 int lex_get_first_line_number (const struct lexer *, int n);
147 int lex_get_last_line_number (const struct lexer *, int n);
148 int lex_get_first_column (const struct lexer *, int n);
149 int lex_get_last_column (const struct lexer *, int n);
150 const char *lex_get_file_name (const struct lexer *);
151
152 /* Issuing errors. */
153 void lex_error (struct lexer *, const char *, ...) PRINTF_FORMAT (2, 3);
154 void lex_next_error (struct lexer *, int n0, int n1, const char *, ...)
155   PRINTF_FORMAT (4, 5);
156 int lex_end_of_command (struct lexer *);
157
158 void lex_sbc_only_once (const char *);
159 void lex_sbc_missing (struct lexer *, const char *);
160
161 void lex_error_valist (struct lexer *, const char *, va_list)
162   PRINTF_FORMAT (2, 0);
163 void lex_next_error_valist (struct lexer *lexer, int n0, int n1,
164                             const char *format, va_list)
165   PRINTF_FORMAT (4, 0);
166
167 /* Error handling. */
168 enum lex_syntax_mode lex_get_syntax_mode (const struct lexer *);
169 enum lex_error_mode lex_get_error_mode (const struct lexer *);
170 void lex_discard_rest_of_command (struct lexer *);
171 void lex_interactive_reset (struct lexer *);
172 void lex_discard_noninteractive (struct lexer *);
173
174 #endif /* lexer.h */