scan: New library for high-level PSPP syntax lexical analysis.
[pspp-builds.git] / src / libpspp / getl.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 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 GETL_H
18 #define GETL_H 1
19
20 #include <stdbool.h>
21 #include "libpspp/ll.h"
22
23 struct string;
24
25 struct getl_source;
26
27 /* Syntax rules that apply to a given source line. */
28 enum syntax_mode
29   {
30     /* Each line that begins in column 1 starts a new command.  A
31        `+' or `-' in column 1 is ignored to allow visual
32        indentation of new commands.  Continuation lines must be
33        indented from the left margin.  A period at the end of a
34        line does end a command, but it is optional. */
35     GETL_BATCH,
36
37     /* Each command must end in a period or in a blank line. */
38     GETL_INTERACTIVE
39   };
40
41 enum error_mode
42   {
43     /* When errors are encountered, report the error and continue to
44        the next command. */
45     ERRMODE_CONTINUE,
46
47     /* When errors are encountered, abort the current stream. */
48     ERRMODE_STOP
49   };
50
51 /* An abstract base class for objects which act as line buffers for the
52    PSPP.  Ie anything which might contain content for the lexer */
53 struct getl_interface
54   {
55     /* Returns true if the interface is interactive, that is, if
56        it prompts a human user.  This property is independent of
57        the syntax mode returned by the read member function. */
58     bool  (*interactive) (const struct getl_interface *);
59
60     /* Read a line the intended syntax mode from the interface.
61        Returns true if succesful, false on failure or at end of
62        input. */
63     bool  (*read)  (struct getl_interface *,
64                     struct string *);
65
66     /* Close and destroy the interface */
67     void  (*close) (struct getl_interface *);
68
69     /* Filter for current and all included sources, which may
70        modify the line.  Usually null.  */
71     void  (*filter) (struct getl_interface *,
72                      struct string *line);
73
74     /* Returns the name of the source */
75     const char * (*name) (const struct getl_interface *);
76
77     /* Returns the current location within the source */
78     int (*location) (const struct getl_interface *);
79   };
80
81 struct source_stream;
82
83 struct source_stream *create_source_stream (void);
84
85 enum syntax_mode source_stream_current_syntax_mode
86    (const struct source_stream *);
87
88
89 enum error_mode source_stream_current_error_mode
90    (const struct source_stream *);
91
92
93 void destroy_source_stream (struct source_stream *);
94
95 void getl_clear_include_path (struct source_stream *);
96 void getl_add_include_dir (struct source_stream *, const char *);
97 char **getl_include_path (const struct source_stream *);
98
99 void getl_abort_noninteractive (struct source_stream *);
100 bool getl_is_interactive (const struct source_stream *);
101
102 bool getl_read_line (struct source_stream *, struct string *);
103
104 void getl_append_source (struct source_stream *, struct getl_interface *s,
105                          enum syntax_mode, enum error_mode) ;
106
107 void getl_include_source (struct source_stream *, struct getl_interface *s,
108                           enum syntax_mode, enum error_mode) ;
109
110 const char * getl_source_name (const struct source_stream *);
111 int getl_source_location (const struct source_stream *);
112
113 #endif /* line-buffer.h */