]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/rbbicst.pl
1 #**************************************************************************
2 # Copyright (C) 2002-2003 International Business Machines Corporation *
3 # and others. All rights reserved. *
4 #**************************************************************************
6 # rbbicst Compile the RBBI rule paser state table data into initialized C data.
9 # perl rbbicst.pl [-j] < rbbirpt.txt > rbbirpt.h
11 # The output file, rbbrpt.h, is included by some of the .cpp rbbi
12 # implementation files. This perl script is NOT run as part
13 # of a normal ICU build. It is run by hand when needed, and the
14 # rbbirpt.h generated file is put back into cvs.
16 # See rbbirpt.h for a description of the input format for this script.
19 if ($ARGV[0] eq "-j") {
25 $num_states = 1; # Always the state number for the line being compiled.
26 $line_num = 0; # The line number in the input file.
28 $states{"pop"} = 255; # Add the "pop" to the list of defined state names.
29 # This prevents any state from being labelled with "pop",
30 # and resolves references to "pop" in the next state field.
32 line_loop
: while (<>) {
38 # Remove # comments, which are any fields beginning with a #, plus all
39 # that follow on the line.
40 for ($i=0; $i<@fields; $i++) {
41 if ($fields[$i] =~ /^#/) {
42 @fields = @fields[0 .. $i-1];
46 # ignore blank lines, and those with no fields left after stripping comments..
52 # State Label: handling.
53 # Does the first token end with a ":"? If so, it's the name of a state.
54 # Put in a hash, together with the current state number,
55 # so that we can later look up the number from the name.
57 if (@fields[0] =~ /.*:$/) {
58 $state_name = @fields[0];
59 $state_name =~ s/://; # strip off the colon from the state name.
61 if ($states{$state_name} != 0) {
62 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
64 $states{$state_name} = $num_states;
65 $stateNames[$num_states] = $state_name;
67 # if the label was the only thing on this line, go on to the next line,
68 # otherwise assume that a state definition is on the same line and fall through.
72 shift @fields; # shift off label field in preparation
73 # for handling the rest of the line.
77 # State Transition line.
79 # character [n] target-state [^push-state] [function-name]
81 # [something] is an optional something
82 # character is either a single quoted character e.g. '['
83 # or a name of a character class, e.g. white_space
86 $state_line_num[$num_states] = $line_num; # remember line number with each state
87 # so we can make better error messages later.
89 # First field, character class or literal character for this transition.
91 if ($fields[0] =~ /^'.'$/) {
92 # We've got a quoted literal character.
93 $state_literal_chars[$num_states] = $fields[0];
94 $state_literal_chars[$num_states] =~ s/'//g;
96 # We've got the name of a character class.
97 $state_char_class[$num_states] = $fields[0];
98 if ($fields[0] =~ /[\W]/) {
99 print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
100 print " scanning $fields[0]\n";
109 $state_flag[$num_states] = "FALSE";
110 if ($fields[0] eq "n") {
111 $state_flag[$num_states] = "TRUE";
116 # do the destination state.
118 $state_dest_state[$num_states] = $fields[0];
119 if ($fields[0] eq "") {
120 print " rbbicsts: at line $line_num, destination state missing.\n";
126 # do the push state, if present.
128 if ($fields[0] =~ /^\^/) {
129 $fields[0] =~ s/^\^//;
130 $state_push_state[$num_states] = $fields[0];
131 if ($fields[0] eq "" ) {
132 print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
139 # Lastly, do the optional action name.
141 if ($fields[0] ne "") {
142 $state_func_name[$num_states] = $fields[0];
147 # There should be no fields left on the line at this point.
150 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
151 print " scanning $fields[0]\n";
157 # We've read in the whole file, now go back and output the
158 # C source code for the state transition table.
160 # We read all states first, before writing anything, so that the state numbers
161 # for the destination states are all available to be written.
165 # Make hashes for the names of the character classes and
166 # for the names of the actions that appeared.
168 for ($state=1; $state < $num_states; $state++) {
169 if ($state_char_class[$state] ne "") {
170 if ($charClasses{$state_char_class[$state]} == 0) {
171 $charClasses{$state_char_class[$state]} = 1;
174 if ($state_func_name[$state] eq "") {
175 $state_func_name[$state] = "doNOP";
177 if ($actions{$state_action_name[$state]} == 0) {
178 $actions{$state_func_name[$state]} = 1;
183 # Check that all of the destination states have been defined
186 $states{"exit"} = 0; # Predefined state name, terminates state machine.
187 for ($state=1; $state<$num_states; $state++) {
188 if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
189 print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
192 if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
193 print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
202 print " *******************************************************************************\n";
203 print " * Copyright (C) 2003,\n";
204 print " * International Business Machines Corporation and others. All Rights Reserved.\n";
205 print " *******************************************************************************\n";
207 print " * \$Source: /cvs/root/ICU/icuSources/common/rbbicst.pl,v $\n";
208 print " * \$Date: 2003/07/03 18:13:31 $\n";
209 print " * \$Revision: 1.1.1.2 $\n";
211 print " *******************************************************************************\n";
214 print "package com.ibm.icu.text;\n";
217 print " * Generated Java File. Do not edit by hand.\n";
218 print " * This file contains the state table for the ICU Rule Based Break Iterator\n";
219 print " * rule parser.\n";
220 print " * It is generated by the Perl script \"rbbicst.pl\" from\n";
221 print " * the rule parser state definitions file \"rbbirpt.txt\".\n";
225 print "public class RuleBasedBreakIteratorStateTable\n";
229 # Emit the constants for the actions to be performed.
232 foreach $act (keys %actions) {
233 print " public static final int $act = $n;\n";
238 # emit the state transition table
240 print " public static final String[] gRuleParseStateTable = {\n";
241 printf(" \"\\u%04.4x\\u%04.4x\\u%04.4x\\u%04.4x\\u%04.4x\"\n", doNOP
, 0, 0, 0, 1);
242 for ($state=1; $state < $num_states; $state++) {
243 printf(" , \"\\u%04.4x", $state_func_name[$state]);
244 # print " , {$state_func_name[$state],";
245 if ($state_literal_chars[$state] ne "") {
246 printf("\\u%04.4x", $state_func_name[$state]);
248 printf("\\u%04.4x", $charClasses{$state_char_class[$state]});
250 printf("\\u%04.4x", $states{$state_dest_state[$state]});
252 # The push-state field is optional. If omitted, fill field with a zero, which flags
253 # the state machine that there is no push state.
254 if ($state_push_state[$state] eq "") {
257 printf("\\u%04.4x", $states{$state_push_state[$state]});
259 printf("\\u%04.4x", $state_flag[$state]);
261 # For the first row of each state, append the state name.
262 # Used for debugging only.
263 if ($stateNames[$state] ne "") {
264 printf("%-20s", $stateNames[$state]."\"");
266 printf("%-20s", "\"");
269 # Put out a C++ comment showing the number (index) of this state row,
283 print "//---------------------------------------------------------------------------------\n";
285 print "// Generated Header File. Do not edit by hand.\n";
286 print "// This file contains the state table for the ICU Rule Based Break Iterator\n";
287 print "// rule parser.\n";
288 print "// It is generated by the Perl script \"rbbicst.pl\" from\n";
289 print "// the rule parser state definitions file \"rbbirpt.txt\".\n";
291 print "// Copyright (C) 2002 International Business Machines Corporation \n";
292 print "// and others. All rights reserved. \n";
294 print "//---------------------------------------------------------------------------------\n";
295 print "#ifndef RBBIRPT_H\n";
296 print "#define RBBIRPT_H\n";
298 print "U_NAMESPACE_BEGIN\n";
301 # Emit the constants for indicies of Unicode Sets
302 # Define one constant for each of the character classes encountered.
303 # At the same time, store the index corresponding to the set name back into hash.
306 print "// Character classes for RBBI rule scanning.\n";
308 $i = 128; # State Table values for Unicode char sets range from 128-250.
309 # Sets "default", "escaped", etc. get special handling.
310 # They have no corresponding UnicodeSet object in the state machine,
311 # but are handled by special case code. So we emit no reference
312 # to a UnicodeSet object to them here.
313 foreach $setName (keys %charClasses) {
314 if ($setName eq "default") {
315 $charClasses{$setName} = 255;}
316 elsif ($setName eq "escaped") {
317 $charClasses{$setName} = 254;}
318 elsif ($setName eq "escapedP") {
319 $charClasses{$setName} = 253;}
320 elsif ($setName eq "eof") {
321 $charClasses{$setName} = 252;}
323 # Normal character class. Fill in array with a ptr to the corresponding UnicodeSet in the state machine.
324 print " static const uint8_t kRuleSet_$setName = $i;\n";
325 $charClasses{$setName} = $i;
332 # Emit the enum for the actions to be performed.
334 print "enum RBBI_RuleParseAction {\n";
335 foreach $act (keys %actions) {
338 print " rbbiLastAction};\n\n";
341 # Emit the struct definition for transtion table elements.
343 print "//-------------------------------------------------------------------------------\n";
345 print "// RBBIRuleTableEl represents the structure of a row in the transition table\n";
346 print "// for the rule parser state machine.\n";
347 print "//-------------------------------------------------------------------------------\n";
348 print "struct RBBIRuleTableEl {\n";
349 print " RBBI_RuleParseAction fAction;\n";
350 print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
351 print " // 128-255: character class index\n";
352 print " uint8_t fNextState; // 0-250: normal next-stat numbers\n";
353 print " // 255: pop next-state from stack.\n";
354 print " uint8_t fPushState;\n";
355 print " UBool fNextChar;\n";
359 # emit the state transition table
361 print "static const struct RBBIRuleTableEl gRuleParseStateTable[] = {\n";
362 print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
363 for ($state=1; $state < $num_states; $state++) {
364 print " , {$state_func_name[$state],";
365 if ($state_literal_chars[$state] ne "") {
366 $c = $state_literal_chars[$state];
367 printf(" %d /* $c */,", ord($c)); # use numeric value, so EBCDIC machines are ok.
369 print " $charClasses{$state_char_class[$state]},";
371 print " $states{$state_dest_state[$state]},";
373 # The push-state field is optional. If omitted, fill field with a zero, which flags
374 # the state machine that there is no push state.
375 if ($state_push_state[$state] eq "") {
378 print " $states{$state_push_state[$state]},";
380 print " $state_flag[$state]} ";
382 # Put out a C++ comment showing the number (index) of this state row,
383 # and, if this is the first row of the table for this state, the state name.
385 if ($stateNames[$state] ne "") {
386 print " $stateNames[$state]";
394 # emit a mapping array from state numbers to state names.
396 # This array is used for producing debugging output from the rule parser.
398 print "static const char * const RBBIRuleStateNames[] = {";
399 for ($state=0; $state<$num_states; $state++) {
400 if ($stateNames[$state] ne "") {
401 print " \"$stateNames[$state]\",\n";
408 print "U_NAMESPACE_END\n";