]>
Commit | Line | Data |
---|---|---|
616915dd | 1 | /* |
11db3170 | 2 | ** Splint - annotation-assisted static program checker |
c59f5181 | 3 | ** Copyright (C) 1994-2003 University of Virginia, |
616915dd | 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 | ** | |
155af98d | 20 | ** For information on splint: info@splint.org |
21 | ** To report a bug: splint-bug@splint.org | |
11db3170 | 22 | ** For more information: http://www.splint.org |
616915dd | 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 | ||
1b8ae690 | 45 | # include "splintMacros.nf" |
b73d1009 | 46 | # include "basic.h" |
616915dd | 47 | # include "signature.h" |
48 | # include "signature2.h" | |
49 | # include "scan.h" | |
50 | # include "scanline.h" | |
51 | ||
616915dd | 52 | static /*@exposed@*/ ltoken LSLScanLookAhead (void); |
28bf4b0b | 53 | static inputStream scanFile; /* file to scan */ |
616915dd | 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 | { | |
616915dd | 70 | if (nextToken < lastToken) |
71 | { | |
cd7d9b17 | 72 | ltoken res = TokenList[nextToken]; |
73 | TokenList[nextToken] = ltoken_undefined; | |
74 | nextToken++; | |
75 | /*@-dependenttrans@*/ | |
76 | return res; /* Its the only reference now. */ | |
77 | /*@=dependenttrans@*/ | |
78 | ||
616915dd | 79 | } |
80 | else | |
81 | { | |
82 | lastToken = 0; | |
83 | lineNumber++; | |
84 | ||
28bf4b0b | 85 | line = inputStream_nextLine (scanFile); |
616915dd | 86 | |
87 | if (line != (char *) 0) | |
88 | { | |
28bf4b0b | 89 | lscanLine (line); /* tokenize */ |
616915dd | 90 | nextToken = 0; |
91 | return LSLScanNextToken (); | |
92 | } | |
93 | else | |
94 | { | |
28bf4b0b | 95 | return LSLScanEofToken (); |
616915dd | 96 | } |
97 | } | |
98 | } | |
99 | ||
100 | static /*@exposed@*/ ltoken | |
101 | LSLScanLookAhead (void) | |
102 | { | |
103 | if (nextToken < lastToken) | |
104 | { | |
105 | return TokenList[nextToken]; | |
106 | } | |
107 | else | |
108 | { | |
109 | lastToken = 0; | |
28bf4b0b | 110 | line = inputStream_nextLine (scanFile); |
616915dd | 111 | |
112 | if (line != (char *) 0) | |
113 | { | |
114 | lscanLine (line); | |
115 | nextToken = 0; | |
116 | return LSLScanLookAhead (); | |
117 | } | |
118 | else | |
119 | { | |
120 | /* | |
121 | ** This is a real memory leak. Its only a few bytes | |
122 | ** per file though, and lsl files are hardly ever used. | |
123 | */ | |
124 | ||
125 | /*@-onlytrans@*/ | |
126 | return LSLScanEofToken (); | |
127 | /*@=onlytrans@*/ | |
128 | } | |
129 | } | |
130 | } | |
131 | ||
132 | void | |
133 | LSLScanFreshToken (ltoken tok) | |
134 | { | |
135 | if (lastToken < MAXLINE) | |
136 | { | |
137 | TokenList[lastToken++] = ltoken_copy (tok); | |
138 | } | |
139 | else | |
140 | { | |
141 | llfatalbug (message ("LSLScanFreshToken: out of range: %s", | |
142 | cstring_fromChars (lsymbol_toChars (ltoken_getText (tok))))); | |
143 | } | |
144 | } | |
145 | ||
28bf4b0b | 146 | /*@exposed@*/ inputStream LSLScanSource (void) |
616915dd | 147 | { |
148 | return scanFile; | |
149 | } | |
150 | ||
151 | ||
152 | void | |
153 | LSLScanInit (void) | |
154 | { | |
155 | } | |
156 | ||
157 | void | |
28bf4b0b | 158 | LSLScanReset (inputStream s) |
616915dd | 159 | { |
160 | scanFile = s; | |
161 | lastToken = 0; | |
162 | nextToken = lastToken + 1; /* force call to scanline */ | |
163 | lineNumber = 0; | |
164 | } | |
165 | ||
166 | void | |
167 | LSLScanCleanup (void) | |
168 | { | |
169 | } |