]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/regexcst.pl
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / i18n / regexcst.pl
CommitLineData
b75a7d8f
A
1#!/usr/bin/perl
2# ********************************************************************
3# * COPYRIGHT:
4# * Copyright (c) 2002-2003, International Business Machines Corporation and
5# * others. All Rights Reserved.
6# ********************************************************************
7#
8# regexcst.pl
9# Compile the regular expression paser state table data into initialized C data.
10# Usage:
11# cd icu/source/i18n
12# perl regexcst.pl < regexcst.txt > regexcst.h
13#
14# The output file, regexcst.h, is included by some of the .cpp regex
15# implementation files. This perl script is NOT run as part
16# of a normal ICU build. It is run by hand when needed, and the
17# regexcst.h generated file is put back into cvs.
18#
19# See regexcst.txt for a description of the input format for this script.
20#
21# This script is derived from rbbicst.pl, which peforms the same function
22# for the Rule Based Break Iterator Rule Parser. Perhaps they could be
23# merged?
24#
25#*********************************************************************
26# Copyright (C) 2002 International Business Machines Corporation *
27# and others. All rights reserved. *
28#*********************************************************************
29
30
31$num_states = 1; # Always the state number for the line being compiled.
32$line_num = 0; # The line number in the input file.
33
34$states{"pop"} = 255; # Add the "pop" to the list of defined state names.
35 # This prevents any state from being labelled with "pop",
36 # and resolves references to "pop" in the next state field.
37
38line_loop: while (<>) {
39 chomp();
40 $line = $_;
41 @fields = split();
42 $line_num++;
43
44 # Remove # comments, which are any fields beginning with a #, plus all
45 # that follow on the line.
46 for ($i=0; $i<@fields; $i++) {
47 if ($fields[$i] =~ /^#/) {
48 @fields = @fields[0 .. $i-1];
49 last;
50 }
51 }
52 # ignore blank lines, and those with no fields left after stripping comments..
53 if (@fields == 0) {
54 next;
55 }
56
57 #
58 # State Label: handling.
59 # Does the first token end with a ":"? If so, it's the name of a state.
60 # Put in a hash, together with the current state number,
61 # so that we can later look up the number from the name.
62 #
63 if (@fields[0] =~ /.*:$/) {
64 $state_name = @fields[0];
65 $state_name =~ s/://; # strip off the colon from the state name.
66
67 if ($states{$state_name} != 0) {
68 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
69 }
70 $states{$state_name} = $num_states;
71 $stateNames[$num_states] = $state_name;
72
73 # if the label was the only thing on this line, go on to the next line,
74 # otherwise assume that a state definition is on the same line and fall through.
75 if (@fields == 1) {
76 next line_loop;
77 }
78 shift @fields; # shift off label field in preparation
79 # for handling the rest of the line.
80 }
81
82 #
83 # State Transition line.
84 # syntax is this,
85 # character [n] target-state [^push-state] [function-name]
86 # where
87 # [something] is an optional something
88 # character is either a single quoted character e.g. '['
89 # or a name of a character class, e.g. white_space
90 #
91
92 $state_line_num[$num_states] = $line_num; # remember line number with each state
93 # so we can make better error messages later.
94 #
95 # First field, character class or literal character for this transition.
96 #
97 if ($fields[0] =~ /^'.'$/) {
98 # We've got a quoted literal character.
99 $state_literal_chars[$num_states] = $fields[0];
100 $state_literal_chars[$num_states] =~ s/'//g;
101 } else {
102 # We've got the name of a character class.
103 $state_char_class[$num_states] = $fields[0];
104 if ($fields[0] =~ /[\W]/) {
105 print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
106 print " scanning $fields[0]\n";
107 exit(-1);
108 }
109 }
110 shift @fields;
111
112 #
113 # do the 'n' flag
114 #
115 $state_flag[$num_states] = "FALSE";
116 if ($fields[0] eq "n") {
117 $state_flag[$num_states] = "TRUE";
118 shift @fields;
119 }
120
121 #
122 # do the destination state.
123 #
124 $state_dest_state[$num_states] = $fields[0];
125 if ($fields[0] eq "") {
126 print " rbbicsts: at line $line_num, destination state missing.\n";
127 exit(-1);
128 }
129 shift @fields;
130
131 #
132 # do the push state, if present.
133 #
134 if ($fields[0] =~ /^\^/) {
135 $fields[0] =~ s/^\^//;
136 $state_push_state[$num_states] = $fields[0];
137 if ($fields[0] eq "" ) {
138 print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
139 exit(-1);
140 }
141 shift @fields;
142 }
143
144 #
145 # Lastly, do the optional action name.
146 #
147 if ($fields[0] ne "") {
148 $state_func_name[$num_states] = $fields[0];
149 shift @fields;
150 }
151
152 #
153 # There should be no fields left on the line at this point.
154 #
155 if (@fields > 0) {
156 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
157 print " scanning $fields[0]\n";
158 }
159 $num_states++;
160}
161
162#
163# We've read in the whole file, now go back and output the
164# C source code for the state transition table.
165#
166# We read all states first, before writing anything, so that the state numbers
167# for the destination states are all available to be written.
168#
169
170#
171# Make hashes for the names of the character classes and
172# for the names of the actions that appeared.
173#
174for ($state=1; $state < $num_states; $state++) {
175 if ($state_char_class[$state] ne "") {
176 if ($charClasses{$state_char_class[$state]} == 0) {
177 $charClasses{$state_char_class[$state]} = 1;
178 }
179 }
180 if ($state_func_name[$state] eq "") {
181 $state_func_name[$state] = "doNOP";
182 }
183 if ($actions{$state_action_name[$state]} == 0) {
184 $actions{$state_func_name[$state]} = 1;
185 }
186}
187
188#
189# Check that all of the destination states have been defined
190#
191#
192$states{"exit"} = 0; # Predefined state name, terminates state machine.
193for ($state=1; $state<$num_states; $state++) {
194 if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
195 print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
196 $errors++;
197 }
198 if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
199 print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
200 $errors++;
201 }
202}
203
204die if ($errors>0);
205
206print "//---------------------------------------------------------------------------------\n";
207print "//\n";
208print "// Generated Header File. Do not edit by hand.\n";
209print "// This file contains the state table for the ICU Regular Expression Pattern Parser\n";
210print "// It is generated by the Perl script \"regexcst.pl\" from\n";
211print "// the rule parser state definitions file \"regexcst.txt\".\n";
212print "//\n";
213print "// Copyright (C) 2002-2003 International Business Machines Corporation \n";
214print "// and others. All rights reserved. \n";
215print "//\n";
216print "//---------------------------------------------------------------------------------\n";
217print "#ifndef RBBIRPT_H\n";
218print "#define RBBIRPT_H\n";
219print "\n";
220print "U_NAMESPACE_BEGIN\n";
221
222#
223# Emit the constants for indicies of Unicode Sets
224# Define one constant for each of the character classes encountered.
225# At the same time, store the index corresponding to the set name back into hash.
226#
227print "//\n";
228print "// Character classes for regex pattern scanning.\n";
229print "//\n";
230$i = 128; # State Table values for Unicode char sets range from 128-250.
231 # Sets "default", "quoted", etc. get special handling.
232 # They have no corresponding UnicodeSet object in the state machine,
233 # but are handled by special case code. So we emit no reference
234 # to a UnicodeSet object to them here.
235foreach $setName (keys %charClasses) {
236 if ($setName eq "default") {
237 $charClasses{$setName} = 255;}
238 elsif ($setName eq "quoted") {
239 $charClasses{$setName} = 254;}
240 elsif ($setName eq "eof") {
241 $charClasses{$setName} = 253;}
242 else {
243 # Normal character class. Fill in array with a ptr to the corresponding UnicodeSet in the state machine.
244 print " static const uint8_t kRuleSet_$setName = $i;\n";
245 $charClasses{$setName} = $i;
246 $i++;
247 }
248}
249print "\n\n";
250
251#
252# Emit the enum for the actions to be performed.
253#
254print "enum Regex_PatternParseAction {\n";
255foreach $act (keys %actions) {
256 print " $act,\n";
257}
258print " rbbiLastAction};\n\n";
259
260#
261# Emit the struct definition for transtion table elements.
262#
263print "//-------------------------------------------------------------------------------\n";
264print "//\n";
265print "// RegexTableEl represents the structure of a row in the transition table\n";
266print "// for the pattern parser state machine.\n";
267print "//-------------------------------------------------------------------------------\n";
268print "struct RegexTableEl {\n";
269print " Regex_PatternParseAction fAction;\n";
270print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
271print " // 128-255: character class index\n";
272print " uint8_t fNextState; // 0-250: normal next-state numbers\n";
273print " // 255: pop next-state from stack.\n";
274print " uint8_t fPushState;\n";
275print " UBool fNextChar;\n";
276print "};\n\n";
277
278#
279# emit the state transition table
280#
281print "static const struct RegexTableEl gRuleParseStateTable[] = {\n";
282print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
283for ($state=1; $state < $num_states; $state++) {
284 print " , {$state_func_name[$state],";
285 if ($state_literal_chars[$state] ne "") {
286 $c = $state_literal_chars[$state];
287 printf(" %d /* $c */,", ord($c)); # use numeric value, so EBCDIC machines are ok.
288 }else {
289 print " $charClasses{$state_char_class[$state]},";
290 }
291 print " $states{$state_dest_state[$state]},";
292
293 # The push-state field is optional. If omitted, fill field with a zero, which flags
294 # the state machine that there is no push state.
295 if ($state_push_state[$state] eq "") {
296 print "0, ";
297 } else {
298 print " $states{$state_push_state[$state]},";
299 }
300 print " $state_flag[$state]} ";
301
302 # Put out a C++ comment showing the number (index) of this state row,
303 # and, if this is the first row of the table for this state, the state name.
304 print " // $state ";
305 if ($stateNames[$state] ne "") {
306 print " $stateNames[$state]";
307 }
308 print "\n";
309};
310print " };\n";
311
312
313#
314# emit a mapping array from state numbers to state names.
315#
316# This array is used for producing debugging output from the pattern parser.
317#
318print "static const char * const RegexStateNames[] = {";
319for ($state=0; $state<$num_states; $state++) {
320 if ($stateNames[$state] ne "") {
321 print " \"$stateNames[$state]\",\n";
322 } else {
323 print " 0,\n";
324 }
325}
326print " 0};\n\n";
327
328print "U_NAMESPACE_END\n";
329print "#endif\n";
330
331
332