]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/rbbicst.pl
1 #**************************************************************************
2 # Copyright (C) 2016 and later: Unicode, Inc. and others.
3 # License & terms of use: http://www.unicode.org/copyright.html#License
4 #**************************************************************************
5 #**************************************************************************
6 # Copyright (C) 2002-2016 International Business Machines Corporation
7 # and others. All rights reserved.
8 #**************************************************************************
10 # rbbicst Compile the RBBI rule paser state table data into initialized C data.
12 # cd icu/source/common
13 # perl rbbicst.pl < rbbirpt.txt > rbbirpt.h
14 # perl rbbicst.pl -j < rbbirpt.txt > RBBIRuleParseTable.java
16 # The output file, rbbrpt.h, is included by some of the .cpp rbbi
17 # implementation files. This perl script is NOT run as part
18 # of a normal ICU build. It is run by hand when needed, and the
19 # rbbirpt.h generated file is put back into cvs.
21 # See rbbirpt.txt for a description of the input format for this script.
24 if ($ARGV[0] eq "-j") {
30 $num_states = 1; # Always the state number for the line being compiled.
31 $line_num = 0; # The line number in the input file.
33 $states{"pop"} = 255; # Add the "pop" to the list of defined state names.
34 # This prevents any state from being labelled with "pop",
35 # and resolves references to "pop" in the next state field.
37 line_loop
: while (<>) {
43 # Remove # comments, which are any fields beginning with a #, plus all
44 # that follow on the line.
45 for ($i=0; $i<@fields; $i++) {
46 if ($fields[$i] =~ /^#/) {
47 @fields = @fields[0 .. $i-1];
51 # ignore blank lines, and those with no fields left after stripping comments..
57 # State Label: handling.
58 # Does the first token end with a ":"? If so, it's the name of a state.
59 # Put in a hash, together with the current state number,
60 # so that we can later look up the number from the name.
62 if (@fields[0] =~ /.*:$/) {
63 $state_name = @fields[0];
64 $state_name =~ s/://; # strip off the colon from the state name.
66 if ($states{$state_name} != 0) {
67 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
69 $states{$state_name} = $num_states;
70 $stateNames[$num_states] = $state_name;
72 # if the label was the only thing on this line, go on to the next line,
73 # otherwise assume that a state definition is on the same line and fall through.
77 shift @fields; # shift off label field in preparation
78 # for handling the rest of the line.
82 # State Transition line.
84 # character [n] target-state [^push-state] [function-name]
86 # [something] is an optional something
87 # character is either a single quoted character e.g. '['
88 # or a name of a character class, e.g. white_space
91 $state_line_num[$num_states] = $line_num; # remember line number with each state
92 # so we can make better error messages later.
94 # First field, character class or literal character for this transition.
96 if ($fields[0] =~ /^'.'$/) {
97 # We've got a quoted literal character.
98 $state_literal_chars[$num_states] = $fields[0];
99 $state_literal_chars[$num_states] =~ s/'//g;
101 # We've got the name of a character class.
102 $state_char_class[$num_states] = $fields[0];
103 if ($fields[0] =~ /[\W]/) {
104 print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
105 print " scanning $fields[0]\n";
114 $state_flag[$num_states] = $javaOutput? "false" : "FALSE";
115 if ($fields[0] eq "n") {
116 $state_flag[$num_states] = $javaOutput? "true": "TRUE";
121 # do the destination state.
123 $state_dest_state[$num_states] = $fields[0];
124 if ($fields[0] eq "") {
125 print " rbbicsts: at line $line_num, destination state missing.\n";
131 # do the push state, if present.
133 if ($fields[0] =~ /^\^/) {
134 $fields[0] =~ s/^\^//;
135 $state_push_state[$num_states] = $fields[0];
136 if ($fields[0] eq "" ) {
137 print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
144 # Lastly, do the optional action name.
146 if ($fields[0] ne "") {
147 $state_func_name[$num_states] = $fields[0];
152 # There should be no fields left on the line at this point.
155 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
156 print " scanning $fields[0]\n";
162 # We've read in the whole file, now go back and output the
163 # C source code for the state transition table.
165 # We read all states first, before writing anything, so that the state numbers
166 # for the destination states are all available to be written.
170 # Make hashes for the names of the character classes and
171 # for the names of the actions that appeared.
173 for ($state=1; $state < $num_states; $state++) {
174 if ($state_char_class[$state] ne "") {
175 if ($charClasses{$state_char_class[$state]} == 0) {
176 $charClasses{$state_char_class[$state]} = 1;
179 if ($state_func_name[$state] eq "") {
180 $state_func_name[$state] = "doNOP";
182 if ($actions{$state_action_name[$state]} == 0) {
183 $actions{$state_func_name[$state]} = 1;
188 # Check that all of the destination states have been defined
191 $states{"exit"} = 0; # Predefined state name, terminates state machine.
192 for ($state=1; $state<$num_states; $state++) {
193 if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
194 print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
197 if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
198 print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
206 # Assign numbers to each of the character classes classes used.
207 # Sets are numbered from 128 - 250
208 # The values 0-127 in the state table are used for matching
209 # individual ASCII characters (the only thing that can appear in the rules.)
210 # The "set" names appearing in the code below (default, etc.) need special
211 # handling because they do not correspond to a normal set of characters,
212 # but trigger special handling by code in the state machine.
215 foreach $setName (sort keys %charClasses) {
216 if ($setName eq "default") {
217 $charClasses{$setName} = 255;}
218 elsif ($setName eq "escaped") {
219 $charClasses{$setName} = 254;}
220 elsif ($setName eq "escapedP") {
221 $charClasses{$setName} = 253;}
222 elsif ($setName eq "eof") {
223 $charClasses{$setName} = 252;}
225 # Normal (single) character class. Number them.
226 $charClasses{$setName} = $i;
232 my ($sec, $min, $hour, , $day, $mon, $year, $wday, $yday, $isdst) = localtime;
237 print " *******************************************************************************\n";
238 print " * Copyright (C) 2003-$year,\n";
239 print " * International Business Machines Corporation and others. All Rights Reserved.\n";
240 print " *******************************************************************************\n";
243 print "package com.ibm.icu.text;\n";
246 print " * Generated Java File. Do not edit by hand.\n";
247 print " * This file contains the state table for the ICU Rule Based Break Iterator\n";
248 print " * rule parser.\n";
249 print " * It is generated by the Perl script \"rbbicst.pl\" from\n";
250 print " * the rule parser state definitions file \"rbbirpt.txt\".\n";
251 print " * \@internal \n";
255 print "class RBBIRuleParseTable\n";
259 # Emit the constants for the actions to be performed.
262 foreach $act (sort keys %actions) {
263 print " static final short $act = $n;\n";
269 # Emit constants for char class names
271 foreach $setName (sort keys %charClasses) {
272 print " static final short kRuleSet_$setName = $charClasses{$setName};\n";
277 print " static class RBBIRuleTableElement { \n";
278 print " short fAction; \n";
279 print " short fCharClass; \n";
280 print " short fNextState; \n";
281 print " short fPushState; \n";
282 print " boolean fNextChar; \n";
283 print " String fStateName; \n";
284 print " RBBIRuleTableElement(short a, int cc, int ns, int ps, boolean nc, String sn) { \n";
285 print " fAction = a; \n";
286 print " fCharClass = (short)cc; \n";
287 print " fNextState = (short)ns; \n";
288 print " fPushState = (short)ps; \n";
289 print " fNextChar = nc; \n";
290 print " fStateName = sn; \n";
296 print " static RBBIRuleTableElement[] gRuleParseStateTable = { \n ";
297 print " new RBBIRuleTableElement(doNOP, 0, 0,0, true, null ) // 0 \n"; #output the unused state 0.
298 for ($state=1; $state < $num_states; $state++) {
299 print " , new RBBIRuleTableElement($state_func_name[$state],";
300 if ($state_literal_chars[$state] ne "") {
301 $c = $state_literal_chars[$state];
304 print " $charClasses{$state_char_class[$state]},";
306 print " $states{$state_dest_state[$state]},";
308 # The push-state field is optional. If omitted, fill field with a zero, which flags
309 # the state machine that there is no push state.
310 if ($state_push_state[$state] eq "") {
313 print " $states{$state_push_state[$state]},";
315 print " $state_flag[$state], ";
317 # if this is the first row of the table for this state, put out the state name.
318 if ($stateNames[$state] ne "") {
319 print " \"$stateNames[$state]\") ";
324 # Put out a comment showing the number (index) of this state row,
340 print "//---------------------------------------------------------------------------------\n";
342 print "// Generated Header File. Do not edit by hand.\n";
343 print "// This file contains the state table for the ICU Rule Based Break Iterator\n";
344 print "// rule parser.\n";
345 print "// It is generated by the Perl script \"rbbicst.pl\" from\n";
346 print "// the rule parser state definitions file \"rbbirpt.txt\".\n";
348 print "// Copyright (C) 2002-$year International Business Machines Corporation \n";
349 print "// and others. All rights reserved. \n";
351 print "//---------------------------------------------------------------------------------\n";
352 print "#ifndef RBBIRPT_H\n";
353 print "#define RBBIRPT_H\n";
355 print "#include \"unicode/utypes.h\"\n";
357 print "U_NAMESPACE_BEGIN\n";
360 # Emit the constants for indicies of Unicode Sets
361 # Define one constant for each of the character classes encountered.
362 # At the same time, store the index corresponding to the set name back into hash.
365 print "// Character classes for RBBI rule scanning.\n";
367 foreach $setName (sort keys %charClasses) {
368 if ($charClasses{$setName} < 250) {
369 # Normal character class.
370 print " static const uint8_t kRuleSet_$setName = $charClasses{$setName};\n";
376 # Emit the enum for the actions to be performed.
378 print "enum RBBI_RuleParseAction {\n";
379 foreach $act (sort keys %actions) {
382 print " rbbiLastAction};\n\n";
385 # Emit the struct definition for transtion table elements.
387 print "//-------------------------------------------------------------------------------\n";
389 print "// RBBIRuleTableEl represents the structure of a row in the transition table\n";
390 print "// for the rule parser state machine.\n";
391 print "//-------------------------------------------------------------------------------\n";
392 print "struct RBBIRuleTableEl {\n";
393 print " RBBI_RuleParseAction fAction;\n";
394 print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
395 print " // 128-255: character class index\n";
396 print " uint8_t fNextState; // 0-250: normal next-stat numbers\n";
397 print " // 255: pop next-state from stack.\n";
398 print " uint8_t fPushState;\n";
399 print " UBool fNextChar;\n";
403 # emit the state transition table
405 print "static const struct RBBIRuleTableEl gRuleParseStateTable[] = {\n";
406 print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
407 for ($state=1; $state < $num_states; $state++) {
408 print " , {$state_func_name[$state],";
409 if ($state_literal_chars[$state] ne "") {
410 $c = $state_literal_chars[$state];
411 printf(" %d /* $c */,", ord($c)); # use numeric value, so EBCDIC machines are ok.
413 print " $charClasses{$state_char_class[$state]},";
415 print " $states{$state_dest_state[$state]},";
417 # The push-state field is optional. If omitted, fill field with a zero, which flags
418 # the state machine that there is no push state.
419 if ($state_push_state[$state] eq "") {
422 print " $states{$state_push_state[$state]},";
424 print " $state_flag[$state]} ";
426 # Put out a C++ comment showing the number (index) of this state row,
427 # and, if this is the first row of the table for this state, the state name.
429 if ($stateNames[$state] ne "") {
430 print " $stateNames[$state]";
438 # emit a mapping array from state numbers to state names.
440 # This array is used for producing debugging output from the rule parser.
442 print "#ifdef RBBI_DEBUG\n";
443 print "static const char * const RBBIRuleStateNames[] = {";
444 for ($state=0; $state<$num_states; $state++) {
445 if ($stateNames[$state] ne "") {
446 print " \"$stateNames[$state]\",\n";
454 print "U_NAMESPACE_END\n";