866321b0c84c5c61e8dec03b29fc3901554db3b6
[pspp] / src / language / lexer / scan.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2011, 2013 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 SCAN_H
18 #define SCAN_H 1
19
20 #include "language/lexer/segment.h"
21 #include "libpspp/str.h"
22
23 struct token;
24
25 /* PSPP syntax scanning.
26
27    PSPP divides traditional "lexical analysis" or "tokenization" into two
28    phases: a lower-level phase called "segmentation" and a higher-level phase
29    called "scanning".  segment.h provides declarations for the segmentation
30    phase.  This header file contains declarations for the scanning phase.
31
32    Scanning accepts as input a stream of segments, which are UTF-8 strings each
33    labeled with a segment type.  It outputs a stream of "scan tokens", which
34    are the same as the tokens used by the PSPP parser with a few additional
35    types.
36 */
37
38 #define SCAN_TYPES                              \
39     SCAN_TYPE(BAD_HEX_LENGTH)                   \
40     SCAN_TYPE(BAD_HEX_DIGIT)                    \
41                                                 \
42     SCAN_TYPE(BAD_UNICODE_LENGTH)               \
43     SCAN_TYPE(BAD_UNICODE_DIGIT)                \
44     SCAN_TYPE(BAD_UNICODE_CODE_POINT)           \
45                                                 \
46     SCAN_TYPE(EXPECTED_QUOTE)                   \
47     SCAN_TYPE(EXPECTED_EXPONENT)                \
48     SCAN_TYPE(UNEXPECTED_CHAR)                  \
49                                                 \
50     SCAN_TYPE(SKIP)
51
52 /* Types of scan tokens.
53
54    Scan token types are a superset of enum token_type.  Only the additional
55    scan token types are defined here, so see the definition of enum token_type
56    for the others. */
57 enum scan_type
58   {
59 #define SCAN_TYPE(TYPE) SCAN_##TYPE,
60     SCAN_FIRST = 255,
61     SCAN_TYPES
62     SCAN_LAST
63 #undef SCAN_TYPE
64   };
65
66 const char *scan_type_to_string (enum scan_type);
67 bool is_scan_type (enum scan_type);
68
69 /* A scanner.  Opaque. */
70 struct scanner
71   {
72     unsigned char state;
73     unsigned char substate;
74   };
75
76 /* scanner_push() return type. */
77 enum scan_result
78   {
79     /* Complete token. */
80     SCAN_DONE,                  /* Token successfully scanned. */
81     SCAN_MORE,                  /* More segments needed to scan token. */
82
83     /* Incomplete token. */
84     SCAN_BACK,                  /* Done, but go back to saved position too. */
85     SCAN_SAVE                   /* Need more segments, and save position. */
86   };
87
88 void scanner_init (struct scanner *, struct token *);
89 enum scan_result scanner_push (struct scanner *, enum segment_type,
90                                struct substring, struct token *);
91 \f
92 /* A simplified lexer for handling syntax in a string. */
93
94 struct string_lexer
95   {
96     const char *input;
97     size_t length;
98     size_t offset;
99     struct segmenter segmenter;
100   };
101
102 void string_lexer_init (struct string_lexer *, const char *input,
103                         size_t length, enum segmenter_mode);
104 bool string_lexer_next (struct string_lexer *, struct token *);
105
106 #endif /* scan.h */