]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/regexcst.pl
2 # ********************************************************************
4 # * Copyright (c) 2002-2003, International Business Machines Corporation and
5 # * others. All Rights Reserved.
6 # ********************************************************************
9 # Compile the regular expression paser state table data into initialized C data.
12 # perl regexcst.pl < regexcst.txt > regexcst.h
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.
19 # See regexcst.txt for a description of the input format for this script.
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
25 #*********************************************************************
26 # Copyright (C) 2002 International Business Machines Corporation *
27 # and others. All rights reserved. *
28 #*********************************************************************
31 $num_states = 1; # Always the state number for the line being compiled.
32 $line_num = 0; # The line number in the input file.
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.
38 line_loop
: while (<>) {
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];
52 # ignore blank lines, and those with no fields left after stripping comments..
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.
63 if (@fields[0] =~ /.*:$/) {
64 $state_name = @fields[0];
65 $state_name =~ s/://; # strip off the colon from the state name.
67 if ($states{$state_name} != 0) {
68 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
70 $states{$state_name} = $num_states;
71 $stateNames[$num_states] = $state_name;
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.
78 shift @fields; # shift off label field in preparation
79 # for handling the rest of the line.
83 # State Transition line.
85 # character [n] target-state [^push-state] [function-name]
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
92 $state_line_num[$num_states] = $line_num; # remember line number with each state
93 # so we can make better error messages later.
95 # First field, character class or literal character for this transition.
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;
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";
115 $state_flag[$num_states] = "FALSE";
116 if ($fields[0] eq "n") {
117 $state_flag[$num_states] = "TRUE";
122 # do the destination state.
124 $state_dest_state[$num_states] = $fields[0];
125 if ($fields[0] eq "") {
126 print " rbbicsts: at line $line_num, destination state missing.\n";
132 # do the push state, if present.
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";
145 # Lastly, do the optional action name.
147 if ($fields[0] ne "") {
148 $state_func_name[$num_states] = $fields[0];
153 # There should be no fields left on the line at this point.
156 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
157 print " scanning $fields[0]\n";
163 # We've read in the whole file, now go back and output the
164 # C source code for the state transition table.
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.
171 # Make hashes for the names of the character classes and
172 # for the names of the actions that appeared.
174 for ($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;
180 if ($state_func_name[$state] eq "") {
181 $state_func_name[$state] = "doNOP";
183 if ($actions{$state_action_name[$state]} == 0) {
184 $actions{$state_func_name[$state]} = 1;
189 # Check that all of the destination states have been defined
192 $states{"exit"} = 0; # Predefined state name, terminates state machine.
193 for ($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";
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";
206 print "//---------------------------------------------------------------------------------\n";
208 print "// Generated Header File. Do not edit by hand.\n";
209 print "// This file contains the state table for the ICU Regular Expression Pattern Parser\n";
210 print "// It is generated by the Perl script \"regexcst.pl\" from\n";
211 print "// the rule parser state definitions file \"regexcst.txt\".\n";
213 print "// Copyright (C) 2002-2003 International Business Machines Corporation \n";
214 print "// and others. All rights reserved. \n";
216 print "//---------------------------------------------------------------------------------\n";
217 print "#ifndef RBBIRPT_H\n";
218 print "#define RBBIRPT_H\n";
220 print "U_NAMESPACE_BEGIN\n";
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.
228 print "// Character classes for regex pattern scanning.\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.
235 foreach $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;}
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;
252 # Emit the enum for the actions to be performed.
254 print "enum Regex_PatternParseAction {\n";
255 foreach $act (keys %actions) {
258 print " rbbiLastAction};\n\n";
261 # Emit the struct definition for transtion table elements.
263 print "//-------------------------------------------------------------------------------\n";
265 print "// RegexTableEl represents the structure of a row in the transition table\n";
266 print "// for the pattern parser state machine.\n";
267 print "//-------------------------------------------------------------------------------\n";
268 print "struct RegexTableEl {\n";
269 print " Regex_PatternParseAction fAction;\n";
270 print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
271 print " // 128-255: character class index\n";
272 print " uint8_t fNextState; // 0-250: normal next-state numbers\n";
273 print " // 255: pop next-state from stack.\n";
274 print " uint8_t fPushState;\n";
275 print " UBool fNextChar;\n";
279 # emit the state transition table
281 print "static const struct RegexTableEl gRuleParseStateTable[] = {\n";
282 print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
283 for ($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.
289 print " $charClasses{$state_char_class[$state]},";
291 print " $states{$state_dest_state[$state]},";
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 "") {
298 print " $states{$state_push_state[$state]},";
300 print " $state_flag[$state]} ";
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.
305 if ($stateNames[$state] ne "") {
306 print " $stateNames[$state]";
314 # emit a mapping array from state numbers to state names.
316 # This array is used for producing debugging output from the pattern parser.
318 print "static const char * const RegexStateNames[] = {";
319 for ($state=0; $state<$num_states; $state++) {
320 if ($stateNames[$state] ne "") {
321 print " \"$stateNames[$state]\",\n";
328 print "U_NAMESPACE_END\n";