Debian Codegraph @

src/mergelex.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # Conversion of files between different charsets and surfaces.
3 # Copyright © 1990-2019 Free Software Foundation, Inc.
4 # François Pinard <[email protected]>, 1990.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
9 # 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 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, see <https://www.gnu.org/licenses/>.
18
19 # This Python script merges several Flex sources intended for Recode.
20 # It requires Flex 2.5 or later.
21
22 import re, sys
23
24 # Initial comments.
25 section0 = [
26 "/* This file is generated automatically by `mergelex.py'. */\n"]
27
28 # Flex and C declarations.
29 section1 = '''\
30
31 %option nounput
32 %option noyywrap
33 %{
34 #include "common.h"
35 static RECODE_CONST_REQUEST request;
36 static RECODE_SUBTASK subtask;
37
38 #define YY_INPUT(buf, result, max_size) \
39 { \
40 int c = recode_get_byte (subtask); \
41 result = (c == EOF) ? YY_NULL : (buf[0] = c, 1); \
42 }
43 %}
44 '''.splitlines(True)
45
46 # Flex rules.
47 section2 = '''\
48 %%
49 <<EOF>> { return 1; }
50 '''.splitlines(True)
51
52 # Rest of C code.
53 section3 = ['%%\n']
54
55 within_C_code = False
56 definitions = {}
57 section = 3
58
59 while True:
60 line = sys.stdin.readline()
61 if not line:
62 break
63
64 # A `/* Step name: NAME. */' line indicates the start of a new file.
65 # Generate an interface routine. The step name is saved for broketed
66 # prefixes in rules.
67
68 match = re.match(r'/\* Step name: (.*)\. \*/', line)
69 if match:
70 section = 1
71 step_name = match.group(1)
72 section3.append('''\
73
74 static bool
75 transform_%s (RECODE_SUBTASK subtask_argument)
76 {
77 subtask = subtask_argument;
78 request = subtask->task->request;
79 yy_init = 0;
80 yyin = subtask->input.file;
81 yyout = subtask->output.file;
82 BEGIN %s;
83 return yylex ();
84 }
85 '''
86 % (step_name, step_name))
87 continue
88
89 # In section 1, any %% in column 1 signals the beginning of lex rules.
90 # Remove all white lines.
91
92 if section == 1:
93 if re.match('^[ \t]*$', line):
94 continue
95 if line[:2] == '%%':
96 section1.append('%%x %s\n' % step_name)
97 section = 2
98 section2.append('\n'
99 '<%s>{\n'
100 % step_name)
101 continue
102
103 # A %{ comment in column 1 signals the start of a C code section ended
104 # by a %} line.
105
106 if line[:2] == '%{':
107 within_C_code = True
108 section1.append('\n')
109 section1.append(line)
110 continue
111 if line[:2] == '%}':
112 section1.append(line)
113 section1.append('\n')
114 within_C_code = False
115 continue
116 if within_C_code:
117 section1.append(line)
118 continue
119
120 # Section 1 declarations are studied and copied, once each.
121 # Conflicting declaractions are reported at beginning of output.
122
123 if section == 1 and line[0] not in (' ', '\t', '\n'):
124 key, rest = line.split(None, 1)
125 if key in definitions:
126 if definitions[key] != line:
127 sys.stderr.write("** Conflicting definition: %s" % line)
128 else:
129 definitions[key] = line
130 section1.append(line)
131 continue
132
133 # In section 2, a %% in column 1 starts section 3.
134
135 if section == 2:
136 if line[:2] == '%%':
137 section2.append('}\n')
138 section = 3
139 else:
140 section2.append(line)
141 continue
142
143 # Section 3 contains C code, all copied verbatim.
144
145 if section == 3:
146 section3.append(line)
147 continue
148
149 # At end, output all temporary buffers. This produces the generated
150 # interfaces: one routine for each step name.
151
152 if section == 2:
153 section2.append('}\n')
154
155 sys.stdout.writelines(section0)
156 sys.stdout.writelines(section1)
157 sys.stdout.writelines(section2)
158 sys.stdout.writelines(section3)