Debian Codegraph @

gcc/cobol/lexio.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of the Symas Corporation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <algorithm>
33 #include <cassert>
34 #include <cctype>
35 #include <cstdlib>
36 #include <cstring>
37
38 #include <sys/mman.h>
39
40 #ifndef _LEXIO_H_
41 #define _LEXIO_H_
42
43 #define SPACE ' '
44
45 bool lexer_echo();
46 bool is_reference_format();
47
48 static inline bool isquote( char ch ) {
49 return ch == '\'' || ch == '"';
50 }
51
52 static inline void
53 erase_source( char *src, char *esrc ) {
54 std::replace_if(src, esrc,
55 [](char ch) { return ch != '\n'; },
56 SPACE );
57 }
58
59 /*
60 * Column number as in Cobol, with 1 at the start of the line.
61 * 0: free-format, but comment lines may start with '*'.
62 * N: columns less than N treated as space.
63 * '/' or '*' in N starts a comment
64 * 'D' starts a debug line
65 * '-' is a line-continuation indicator
66 * Others ignored.
67 * Right margin is enforced if it is greater than left margin.
68 */
69 struct bytespan_t {
70 char *data, *eodata;
71
72 bytespan_t() : data( nullptr), eodata(nullptr) {}
73
74 bytespan_t( char *data, char *eodata )
75 : data(data), eodata(eodata)
76 {
77 if( eodata < data ) {
78 this->eodata = data + strlen(data);
79 }
80 assert( this->data <= this->eodata );
81 }
82 size_t size() const { return eodata - data; }
83
84 bool in_string( ) const {
85 char open = '\0';
86
87 for( char *q = data; (q = std::find_if(q, eodata, isquote)) != eodata; q++) {
88 if( !open ) {
89 open = *q; // first quote opens
90 continue;
91 }
92 if( open == *q && q + 1 < eodata && q[0] == q[1] ) { // doubled
93 q++;
94 continue;
95 }
96 if( open == *q ) open = '\0'; // closing quote must match
97 }
98 return isquote(open);
99 }
100
101 char * append( const char *input, const char *eoinput );
102
103 bytespan_t&
104 update( char *line, char *eoline, size_t right_margin ) {
105 *this = bytespan_t(line, eoline);
106 if( right_margin && data + right_margin < eodata ) {
107 erase_source(data + right_margin, eodata);
108 eodata = data + right_margin;
109 }
110 eodata = std::find(data, eodata, '\n');
111 return *this;
112 }
113 };
114
115 // YYLTYPE supplied by cbldiag.h. Borrowed from parse.h as generated by Bison.
116
117 struct filespan_t : public bytespan_t {
118 char *cur, *eol, *quote;
119 private:
120 size_t iline, icol;
121 size_t line_quote72;
122 static char empty_file[8];
123 public:
124 filespan_t()
125 : cur(data), eol(data), quote(NULL), iline(0), icol(0), line_quote72(0)
126 {}
127 filespan_t(void *p, size_t len)
128 : bytespan_t( static_cast<char*>(p), static_cast<char*>(p) + len )
129 , cur(data), eol(data), quote(NULL), iline(0), icol(0), line_quote72(0)
130 {}
131
132 size_t lineno() const { return iline; }
133 size_t colno() const { return icol; }
134
135 void lineno_reset() { iline = 0; }
136 size_t colno( size_t icol ) { return this->icol = icol; }
137
138 bool nada() const { return data == empty_file; }
139 void use_nada() {
140 assert(!data);
141 cur = eol = data = empty_file;
142 eol = eodata = empty_file + sizeof(empty_file) - 1;
143 }
144
145 const char *ccur() const { return cur; }
146
147 /*
148 * "If an alphanumeric or national literal that is to be continued on
149 * the next line has as its last character a quotation mark in
150 * column 72, the continuation line must start with two consecutive
151 * quotation marks."
152 */
153 bool was_quote72() const { return iline == line_quote72 + 1; }
154
155 size_t next_line() {
156 // Before advancing, mark the current line as ending in a quote, if true.
157 if( is_reference_format() && 72 <= line_length() ) {
158 if( isquote(cur[71]) ) { line_quote72 = iline; }
159 }
160
161 cur = eol;
162 assert(data <= cur && cur <= eodata);
163 if( cur == eodata ) return 0;
164
165 eol = std::find(cur, eodata, '\n');
166
167 if( eol < eodata ) {
168 ++eol;
169 ++iline;
170 icol = 0;
171 }
172 return eol - cur;
173 }
174
175 size_t line_length() const { return eol - cur; }
176
177 static size_t tab_check( const char *src, const char *esrc );
178
179 bool is_blank_line() const {
180 auto p = std::find_if( cur, eol, []( char ch ) { return !fisspace(ch); } );
181 return p == eol;
182 }
183
184 YYLTYPE as_location() const {
185 YYLTYPE loc;
186
187 loc.first_line = loc.last_line = 1 + iline;
188 loc.first_column = loc.last_column = 1 + icol;
189 return loc;
190 }
191
192 };
193
194 #if USE_STD_REGEX
195 # include <regex>
196 #else
197 # include "dts.h"
198 using dts::csub_match;
199 using dts::cmatch;
200 using dts::regex;
201 using dts::regex_search;
202 #endif
203
204 struct span_t {
205 protected:
206 void verify() const {
207 if( !p ) {
208 dbgmsg("span_t::span_t: p is NULL");
209 } else if( ! (p <= pend) ) {
210 dbgmsg("span_t::span_t: p %p > pend %p", p, pend);
211 }
212 assert(p && p <= pend);
213 }
214 span_t& trim() {
215 while( p < pend && isblank(p[0]) ) p++;
216 while( p < pend - 1 && isblank(pend[-1]) ) pend--;
217 return *this;
218 }
219 public:
220 const char *p, *pend;
221 span_t() : p(NULL), pend(NULL) {}
222
223 span_t( size_t len, const char *data ) : p(data), pend(data + len) {
224 verify();
225 }
226 span_t( const char *data, const char *eodata ) : p(data), pend(eodata) {
227 verify();
228 }
229 // cppcheck-suppress operatorEqRetRefThis
230 span_t& operator=( const csub_match& cm ) {
231 p = cm.first;
232 pend = cm.second;
233 return p && pend ? trim() : *this;
234 }
235
236 int size() const { return pend - p; }
237
238 size_t nlines() const { return p && pend? std::count(p, pend, '\n') : 0; }
239
240 span_t dup() const {
241 auto output = new char[size() + 1];
242 auto eout = std::copy(p, pend, output);
243 *eout = '\0';
244 return span_t(output, eout);
245 }
246 const char * has_nul() const {
247 auto p_l = std::find(this->p, pend, '\0');
248 return p_l != pend? p_l : NULL;
249 }
250
251 bool at_eol() const {
252 return p < pend && '\n' == pend[-1];
253 }
254 const char * optional_eol() const {
255 return at_eol() ? "" : "\n";
256 }
257 };
258
259 struct replace_t {
260 struct span_t before, after;
261 replace_t() : before(span_t()), after(span_t()) {}
262 replace_t( span_t before, span_t after )
263 : before(before), after(after)
264 {}
265 replace_t& reset() {
266 before = after = span_t();
267 return *this;
268 }
269 };
270
271 #include <cstdio>
272 #include <list>
273
274 class cdftext {
275 static filespan_t free_form_reference_format( int fd );
276 static void process_file( filespan_t, int output, bool second_pass = false );
277
278 static filespan_t map_file( int fd );
279
280 static void echo_input( int input, const char filename[] );
281
282 static int open_input( const char filename[] );
283 static int open_output();
284
285 static std::list<span_t> segment_line( filespan_t& mfile );
286
287 public:
288 static FILE * lex_open( const char filename[] );
289 };
290
291 std::list<replace_t> free_form_reference_format( filespan_t mfile );
292
293 #endif