]> andersk Git - splint.git/blob - src/lclscan.c
bf968b69c21115556a6876f8c78aaa547532407b
[splint.git] / src / lclscan.c
1 /*
2 ** Splint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2003 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on splint: info@splint.org
21 ** To report a bug: splint-bug@splint.org
22 ** For more information: http://www.splint.org
23 */
24 /*
25 ** lclscan.c
26 **
27 ** Deliver tokens one at a time
28 **
29 **      METHOD:
30 **      The input arrives via LSLScanFreshToken ().
31 **      The output is reported via LSLScanNextToken ().
32 **
33 **      The tokens are built in module ScanLine.
34 **      The tokens are delivered from this module.
35 **      Meantimes, they are saved in a static array.
36 **
37 **      The tokenizing is split off from the delivery of tokens
38 **      to facilitate incremental scanning at a later date.
39 **      The essential is that scanline () can be called or not
40 **      if the input text is dirty or not.  Clean lines cause
41 **      tokens to be played out from the saved token list (not
42 **      yet implemented in this version).
43 */
44
45 # include "splintMacros.nf"
46 # include "basic.h"
47
48 /*@-redecl@*/ /* from llgrammar.y */
49 extern bool g_inTypeDef;
50 /*@=redecl@*/
51
52 /*@ignore@*/
53 # include "llgrammar2.h" /* hack to force real include */
54 /*@end@*/
55
56 # include "lclscan.h"
57 # include "scanline.h"
58 # include "lclscanline.h"
59 # include "lcltokentable.h"
60
61 static inputStream scanFile;    /* file to scan         */
62 static o_ltoken TokenList[MAXLINE];     /* available tokens     */
63 static bool restore = FALSE;      /* wasn't static! */
64 static YYSTYPE restoretok;
65 static int nextToken;           /* next available token */
66 static int lastToken;           /* next available slot  */
67
68 static /*@dependent@*/ /*@null@*/ char *line;  /* input text */
69 static unsigned int lineNumber;                /* current line number */
70
71 ltokenCode yllex (void)
72   /*@globals killed restoretok@*/ 
73 {
74   lsymbol tokenSym;
75
76   if (restore)
77     {
78       yllval = restoretok;
79       restore = FALSE;
80     }
81   else
82     {
83       yllval.ltok = ltoken_copy (LCLScanNextToken ());
84     }
85
86   tokenSym = ltoken_getText (yllval.ltok);
87
88   if (ltoken_getCode (yllval.ltok) == simpleId)
89     {
90       if (g_inTypeDef)
91         {
92           ltoken_setCode (yllval.ltok, LLT_TYPEDEF_NAME);
93           LCLUpdateToken (LLT_TYPEDEF_NAME, tokenSym, 
94                           ltoken_isStateDefined (yllval.ltok));
95         }
96       else
97         {
98          /* or if it is already declared as a type, so
99             typedef int foo; typedef foo bar;      works*/
100           if (symtable_exists (g_symtab, tokenSym))
101             {
102               if (typeInfo_exists (symtable_typeInfo (g_symtab, tokenSym)))
103                 {
104                   ltoken_setCode (yllval.ltok, LLT_TYPEDEF_NAME);
105                   LCLUpdateToken (LLT_TYPEDEF_NAME, tokenSym, 
106                                   ltoken_isStateDefined (yllval.ltok));
107                 }
108             }
109         }
110     }
111
112   return (ltoken_getCode (yllval.ltok));
113 }
114
115 /* useful for scanning LCL init files and LSL init files ? */
116
117 /*@dependent@*/ ltoken
118 LCLScanNextToken (void)
119 {
120   ltoken ret;
121
122   if (nextToken < lastToken)
123     {                   
124       ret = TokenList[nextToken++];
125     }
126   else
127     {
128       lastToken = 0;
129       lineNumber++;
130       line = inputStream_nextLine (scanFile);   
131
132       if (line != (char *) 0)
133         {
134           
135           LCLScanLine (line);   
136           nextToken = 0;
137           ret = LCLScanNextToken ();    
138           return ret;
139         }
140       else
141         {
142           ret = LCLScanEofToken ();
143         }
144     }
145
146
147     return ret;
148 }
149
150 static /*@exposed@*/ /*@dependent@*/ ltoken
151 LCLScanLookAhead (void)
152 {
153   if (nextToken < lastToken)
154     {                   
155       return TokenList[nextToken];
156     }
157   else
158     {
159       lastToken = 0;    
160       line = inputStream_nextLine (scanFile);
161       if (line != (char *) 0)
162         {
163           LCLScanLine (line);   
164           nextToken = 0;        
165           return LCLScanLookAhead ();
166         }
167       else
168         {
169           return LCLScanEofToken ();    
170         }
171     }
172 }
173
174 void
175 LCLScanFreshToken (/*@only@*/ ltoken tok)
176 {
177   if (lastToken < MAXLINE)
178     {           
179       TokenList[lastToken++] = tok;
180     }
181   else
182     {
183       llbugexitlit ("LCLScanFreshToken: out of range");
184     }
185 }
186
187 inputStream LCLScanSource (void)
188 {
189   return scanFile;
190 }
191
192
193 void
194 LCLScanInit (void)
195 {
196 }
197
198 void
199 LCLScanReset (inputStream  s)
200 {
201   scanFile = s;
202   lastToken = 0;
203   nextToken = lastToken + 1;    
204   lineNumber = 0;
205 }
206
207 void
208 LCLScanCleanup (void)
209 {
210 }
211
212
This page took 0.052909 seconds and 3 git commands to generate.