]> andersk Git - splint.git/blob - src/scan.c
Merged code tree with Dave Evans's version. Many changes to numberous to list....
[splint.git] / src / scan.c
1 /*
2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 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 lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
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 "lclintMacros.nf"
46 # include "llbasic.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   lval->ltok = LSLScanNextToken ();
65   return (ltoken_getCode (lval->ltok));
66 }
67
68 ltoken LSLScanNextToken (void)
69 {
70   
71   if (nextToken < lastToken)
72     {   
73       /*@-retalias@*/
74             return TokenList[nextToken++];
75       /*@=retalias@*/
76     }
77   else
78     {
79       lastToken = 0;            
80       lineNumber++;
81
82       line = inputStream_nextLine (scanFile);       
83       
84       if (line != (char *) 0)
85         {
86           lscanLine (line);     /* tokenize */
87           nextToken = 0;
88           return LSLScanNextToken ();   
89         }
90       else
91         {
92           return LSLScanEofToken ();
93         }
94     }
95 }
96
97 static /*@exposed@*/ ltoken
98 LSLScanLookAhead (void)
99 {
100   if (nextToken < lastToken)
101     {           
102       return TokenList[nextToken];
103     }
104   else
105     {
106       lastToken = 0;            
107       line = inputStream_nextLine (scanFile);
108
109       if (line != (char *) 0)
110         {
111           lscanLine (line);     
112           nextToken = 0;        
113           return LSLScanLookAhead ();   
114         }
115       else
116         {
117           /* 
118           ** This is a real memory leak.  Its only a few bytes
119           ** per file though, and lsl files are hardly ever used.
120           */
121
122           /*@-onlytrans@*/ 
123           return LSLScanEofToken ();
124           /*@=onlytrans@*/
125         }
126     }
127 }
128
129 void
130 LSLScanFreshToken (ltoken tok)
131 {
132   if (lastToken < MAXLINE)
133     {                           
134       TokenList[lastToken++] = ltoken_copy (tok);       
135     }
136   else
137     {
138       llfatalbug (message ("LSLScanFreshToken: out of range: %s", 
139                            cstring_fromChars (lsymbol_toChars (ltoken_getText (tok)))));
140     }
141 }
142
143 /*@exposed@*/ inputStream LSLScanSource (void)
144 {
145   return scanFile;
146 }
147
148
149 void
150 LSLScanInit (void)
151 {
152 }
153
154 void
155 LSLScanReset (inputStream s)
156 {
157   scanFile = s;
158   lastToken = 0;
159   nextToken = lastToken + 1;    /* force call to scanline   */
160   lineNumber = 0;
161 }
162
163 void
164 LSLScanCleanup (void)
165 {
166 }
This page took 0.479363 seconds and 5 git commands to generate.