| 1 |
|
| 2 |
|
| 3 |
. |
| 4 |
. |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
import re, sys |
| 23 |
|
| 24 |
|
| 25 |
section0 = [ |
| 26 |
"/* This file is generated automatically by `mergelex.py'. */\n"] |
| 27 |
|
| 28 |
|
| 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 |
|
| 47 |
section2 = '''\ |
| 48 |
%% |
| 49 |
<<EOF>> { return 1; } |
| 50 |
'''.splitlines(True) |
| 51 |
|
| 52 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 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 |
|
| 90 |
|
| 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 |
|
| 104 |
|
| 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 |
|
| 121 |
|
| 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 |
|
| 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 |
|
| 144 |
|
| 145 |
if section == 3: |
| 146 |
section3.append(line) |
| 147 |
continue |
| 148 |
|
| 149 |
|
| 150 |
|
| 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) |