]> andersk Git - splint.git/blob - src/scan.c
*** empty log message ***
[splint.git] / src / scan.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 ** scan.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 # include "signature.h"
48 # include "signature2.h"
49 # include "scan.h"
50 # include "scanline.h"
51
52 static /*@exposed@*/ ltoken LSLScanLookAhead (void);
53 static inputStream scanFile;      /* file to scan */
54 static o_ltoken TokenList[MAXLINE]; /* available tokens */
55 static int nextToken;             /* next available token */
56 static int lastToken;             /* next available slot */
57
58 static /*@dependent@*/ /*@null@*/ char *line;   /* input text */
59 static unsigned int lineNumber; /* current line number */
60
61 unsigned int lsllex (YYSTYPE *lval)
62 {
63   /* This is important!  Bison expects this */
64   /* splint doesn't know the type of YYSTYPE, so we need to ignore warnings here */
65   /*@i1@*/ lval->ltok = LSLScanNextToken ();
66   /*@i1@*/ return (ltoken_getCode (lval->ltok));
67 }
68
69 ltoken LSLScanNextToken (void)
70 {
71   if (nextToken < lastToken)
72     {   
73       ltoken res = TokenList[nextToken];
74       TokenList[nextToken] = ltoken_undefined;
75       nextToken++;
76       /*@-dependenttrans@*/
77       return res; /* Its the only reference now. */
78       /*@=dependenttrans@*/
79
80     }
81   else
82     {
83       lastToken = 0;            
84       lineNumber++;
85
86       line = inputStream_nextLine (scanFile);       
87       
88       if (line != (char *) 0)
89         {
90           lscanLine (line);     /* tokenize */
91           nextToken = 0;
92           return LSLScanNextToken ();   
93         }
94       else
95         {
96           return LSLScanEofToken ();
97         }
98     }
99 }
100
101 static /*@exposed@*/ ltoken
102 LSLScanLookAhead (void)
103 {
104   if (nextToken < lastToken)
105     {           
106       return TokenList[nextToken];
107     }
108   else
109     {
110       lastToken = 0;            
111       line = inputStream_nextLine (scanFile);
112
113       if (line != (char *) 0)
114         {
115           lscanLine (line);     
116           nextToken = 0;        
117           return LSLScanLookAhead ();   
118         }
119       else
120         {
121           /* 
122           ** This is a real memory leak.  Its only a few bytes
123           ** per file though, and lsl files are hardly ever used.
124           */
125
126           /*@-onlytrans@*/ 
127           return LSLScanEofToken ();
128           /*@=onlytrans@*/
129         }
130     }
131 }
132
133 void
134 LSLScanFreshToken (ltoken tok)
135 {
136   if (lastToken < MAXLINE)
137     {                           
138       TokenList[lastToken++] = ltoken_copy (tok);       
139     }
140   else
141     {
142       llfatalbug (message ("LSLScanFreshToken: out of range: %s", 
143                            cstring_fromChars (lsymbol_toChars (ltoken_getText (tok)))));
144     }
145 }
146
147 /*@exposed@*/ inputStream LSLScanSource (void)
148 {
149   return scanFile;
150 }
151
152
153 void
154 LSLScanInit (void)
155 {
156 }
157
158 void
159 LSLScanReset (inputStream s)
160 {
161   scanFile = s;
162   lastToken = 0;
163   nextToken = lastToken + 1;    /* force call to scanline   */
164   lineNumber = 0;
165 }
166
167 void
168 LSLScanCleanup (void)
169 {
170 }
This page took 0.072334 seconds and 5 git commands to generate.