1 /*-----------------------------------------------------------------------
2 
3 File  : cio_scanner.h
4 
5 Author: Stephan Schulz
6 
7 Contents
8 
9   Datatypes for the scanner: TokenType, TokenCell, TokenRepCell
10 
11   Copyright 1998, 1999 by the author.
12   This code is released under the GNU General Public Licence and
13   the GNU Lesser General Public License.
14   See the file COPYING in the main E directory for details..
15   Run "eprover -h" for contact information.
16 
17 Changes
18 
19 <1> Thu Aug 28 01:48:03 MET DST 1997
20     New
21 
22 -----------------------------------------------------------------------*/
23 
24 #ifndef CIO_SCANNER
25 
26 #define CIO_SCANNER
27 
28 #include <cio_streams.h>
29 #include <clb_stringtrees.h>
30 #include <ctype.h>
31 #include <limits.h>
32 
33 /*---------------------------------------------------------------------*/
34 /*                    Data type declarations                           */
35 /*---------------------------------------------------------------------*/
36 
37 
38 /* Possible token type. This used to be a nice enum, but that
39    restriced the set to no more than 32 distinct tokens. As a n
40    unsigned long long, we can accomodate at least 64 tokens.
41 
42    Note that it is possible to define additional classes consisting of
43    more than one particular token (see e.g. SkipToken). If this list
44    is extended, you also need to extend token_print_rep[] in
45    cio_scanner.c. */
46 
47 typedef unsigned long long TokenType;
48 
49 #define NoToken       1LL
50 #define WhiteSpace    (2*NoToken)
51 #define Comment       (2*WhiteSpace)
52 #define Ident         (2*Comment)
53 #define Idnum         (2*Ident)
54 #define SemIdent      (2*Idnum)
55 #define String        (2*SemIdent)
56 #define SQString      (2*String)
57 #define PosInt        (2*SQString)
58 #define OpenBracket   (2*PosInt)
59 #define CloseBracket  (2*OpenBracket)
60 #define OpenCurly     (2*CloseBracket)
61 #define CloseCurly    (2*OpenCurly)
62 #define OpenSquare    (2*CloseCurly)
63 #define CloseSquare   (2*OpenSquare)
64 #define LesserSign    (2*CloseSquare)
65 #define GreaterSign   (2*LesserSign)
66 #define EqualSign     (2*GreaterSign)
67 #define NegEqualSign  (2*EqualSign)
68 #define TildeSign     (2*NegEqualSign)
69 #define Exclamation   (2*TildeSign)
70 #define AllQuantor    (Exclamation)
71 #define QuestionMark  (2*Exclamation)
72 #define ExistQuantor  (QuestionMark)
73 #define Comma         (2*QuestionMark)
74 #define Semicolon     (2*Comma)
75 #define Colon         (2*Semicolon)
76 #define Hyphen        (2*Colon)
77 #define Plus          (2*Hyphen)
78 #define Mult          (2*Plus)
79 #define Fullstop      (2*Mult)
80 #define Dollar        (2*Fullstop)
81 #define Slash         (2*Dollar)
82 #define Pipe          (2*Slash)
83 #define FOFOr         (Pipe)
84 #define Ampersand     (2*Pipe)
85 #define FOFAnd        (Ampersand)
86 #define FOFLRImpl     (2*Ampersand)
87 #define FOFRLImpl     (2*FOFLRImpl)
88 #define FOFEquiv      (2*FOFRLImpl)
89 #define FOFXor        (2*FOFEquiv)
90 #define FOFNand       (2*FOFXor)
91 #define FOFNor        (2*FOFNand)
92 
93 
94 #define SkipToken     (WhiteSpace | Comment)
95 #define Identifier    (Ident | Idnum)
96 #define Name          (Identifier | String)
97 #define FOFBinOp      (FOFAnd|FOFOr|FOFLRImpl|FOFRLImpl|FOFEquiv|FOFXor|FOFNand|FOFNor)
98 #define FOFAssocOp    (FOFAnd|FOFOr)
99 
100 
101 
102 /* If your application parses multiple format you can use this to
103    distinguish them: */
104 
105 typedef enum
106 {
107    LOPFormat,
108    TPTPFormat,
109    TSTPFormat,
110    AutoFormat
111 }IOFormat;
112 
113 
114 typedef struct tokenrepcell
115 {
116    TokenType key;
117    char*     rep;
118 }TokenRepCell, *TokenRep_p;
119 
120 
121 typedef struct tokencell
122 {
123    TokenType     tok;         /* Type for AcceptTok(), TestTok() ...   */
124    DStr_p        literal;     /* Verbatim copy of input for the token  */
125    unsigned long numval;      /* Numerical value (if any) of the token */
126    DStr_p        comment;     /* Accumulated preceding comments        */
127    bool          skipped;     /* Was this token preceded by SkipSpace? */
128    DStr_p        source;      /* Ref. to the input stream source       */
129    StreamType    stream_type; /* File or string? */
130    long          line;        /* Position in this stream               */
131    long          column;      /*  "               "                    */
132 
133 }TokenCell, *Token_p;
134 
135 #define MAXTOKENLOOKAHEAD 4
136 
137 typedef struct scannercell
138 {
139    Stream_p    source;  /* Input stack from which to read */
140    DStr_p      default_dir; /* Directory we read from, if any */
141    IOFormat    format;
142    DStr_p      accu; /* Place for Multi-Token constructs or messages */
143    bool        ignore_comments; /* Comments can be skipped
144                                  * completely. If not set, comments
145                                  * are accumulated (but never
146                                  * delivered as tokens) */
147    char*       include_key; /* An Identifier,  e.g. "include" */
148    TokenCell   tok_sequence[MAXTOKENLOOKAHEAD]; /* Need help? Bozo! */
149    int         current; /* Pointer to current token in tok_sequence */
150    char*       include_pos; /* If created by "include", by which one? */
151 }ScannerCell, *Scanner_p;
152 
153 
154 /*---------------------------------------------------------------------*/
155 /*                Exported Functions and Variables                     */
156 /*---------------------------------------------------------------------*/
157 
158 #define TokenCellAlloc()      (TokenCell*)SizeMalloc(sizeof(TokenCell))
159 #define TokenCellFree(junk)   SizeFree(junk, sizeof(TokenCell))
160 #define ScannerCellAlloc()    (ScannerCell*)SizeMalloc(sizeof(ScannerCell))
161 #define ScannerCellFree(junk) SizeFree(junk, sizeof(ScannerCell))
162 
163 #define  Source(scanner)       (((scanner)->source)->source)
164 #define  SourceType(scanner)   (((scanner)->source)->stream_type)
165 #define  LookChar(scanner, look) StreamLookChar((scanner)->source, look)
166 #define  CurrChar(scanner)       StreamCurrChar((scanner)->source)
167 #define  CurrLine(scanner)       StreamCurrLine((scanner)->source)
168 #define  CurrColumn(scanner)     StreamCurrColumn((scanner)->source)
169 #define  NextChar(scanner)       StreamNextChar((scanner)->source)
170 
171 #define  ScannerGetFormat(scanner)        ((scanner)->format)
172 
173 #define  ScannerGetDefaultDir(scanner) DStrView((scanner)->default_dir)
174 
175 #define isstartidchar(ch)  (isalpha(ch) || (ch) == '_')
176 #define isidchar(ch)       (isalnum(ch) || (ch) == '_')
177 #define ischar(ch)         ((ch)!=EOF)
178 #define isstartcomment(ch) ((ch)=='#' || (ch)=='%')
179 
180 char*     PosRep(StreamType type, DStr_p file, long line, long column);
181 char*     TokenPosRep(Token_p token);
182 char*     DescribeToken(TokenType token);
183 void      PrintToken(FILE* out, Token_p token);
184 
185 Scanner_p CreateScanner(StreamType type, char *name, bool
186          ignore_comments, char *default_dir);
187 void      DestroyScanner(Scanner_p  junk);
188 
189 void      ScannerSetFormat(Scanner_p scanner, IOFormat fmt);
190 
191 
192 #define TOKENREALPOS(pos) ((pos) % MAXTOKENLOOKAHEAD)
193 #define AktToken(in) (&((in)->tok_sequence[(in)->current]))
194 #define AktTokenType(in) (AktToken(in)->tok)
195 #define LookToken(in,look) \
196     (&((in)->tok_sequence[TOKENREALPOS((in)->current+(look))]))
197 
198 bool TestTok(Token_p akt, TokenType toks);
199 bool TestId(Token_p akt, char* ids);
200 bool TestIdnum(Token_p akt, char* ids);
201 
202 #define TestInpTok(in, toks)  TestTok(AktToken(in), (toks))
203 #define TestInpId(in, ids)    TestId(AktToken(in), (ids))
204 #define TestInpIdnum(in, ids) TestIdnum(AktToken(in), (ids))
205 #define TestInpNoSkip(in)     (!(AktToken(in)->skipped))
206 #define TestInpTokNoSkip(in, toks) \
207         (TestInpNoSkip(in) && TestInpTok(in, toks))
208 
209 void AktTokenError(Scanner_p in, char* msg, bool syserr);
210 
211 void CheckInpTok(Scanner_p in, TokenType toks);
212 void CheckInpTokNoSkip(Scanner_p in, TokenType toks);
213 void CheckInpId(Scanner_p in, char* ids);
214 
215 
216 #define AcceptInpTok(in, toks) CheckInpTok((in), (toks));\
217                                NextToken(in)
218 #define AcceptInpTokNoSkip(in, toks) \
219                                CheckInpTokNoSkip((in), (toks));\
220                                NextToken(in)
221 #define AcceptInpId(in, ids)   CheckInpId((in), (ids));\
222                                NextToken(in)
223 
224 void NextToken(Scanner_p in);
225 
226 Scanner_p ScannerParseInclude(Scanner_p in, StrTree_p *name_selector,
227                               StrTree_p *skip_includes);
228 
229 #endif
230 
231 /*---------------------------------------------------------------------*/
232 /*                        End of File                                  */
233 /*---------------------------------------------------------------------*/
234 
235 
236 
237 
238 
239