intl/icu/source/common/rbbicst.pl

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #**************************************************************************
michael@0 2 # Copyright (C) 2002-2005 International Business Machines Corporation *
michael@0 3 # and others. All rights reserved. *
michael@0 4 #**************************************************************************
michael@0 5 #
michael@0 6 # rbbicst Compile the RBBI rule paser state table data into initialized C data.
michael@0 7 # Usage:
michael@0 8 # cd icu/source/common
michael@0 9 # perl rbbicst.pl < rbbirpt.txt > rbbirpt.h
michael@0 10 # perl rbbicst.pl -j < rbbirpt.txt > RBBIRuleParseTable.java
michael@0 11 #
michael@0 12 # The output file, rbbrpt.h, is included by some of the .cpp rbbi
michael@0 13 # implementation files. This perl script is NOT run as part
michael@0 14 # of a normal ICU build. It is run by hand when needed, and the
michael@0 15 # rbbirpt.h generated file is put back into cvs.
michael@0 16 #
michael@0 17 # See rbbirpt.txt for a description of the input format for this script.
michael@0 18 #
michael@0 19
michael@0 20 if ($ARGV[0] eq "-j") {
michael@0 21 $javaOutput = 1;
michael@0 22 shift @ARGV;
michael@0 23 }
michael@0 24
michael@0 25
michael@0 26 $num_states = 1; # Always the state number for the line being compiled.
michael@0 27 $line_num = 0; # The line number in the input file.
michael@0 28
michael@0 29 $states{"pop"} = 255; # Add the "pop" to the list of defined state names.
michael@0 30 # This prevents any state from being labelled with "pop",
michael@0 31 # and resolves references to "pop" in the next state field.
michael@0 32
michael@0 33 line_loop: while (<>) {
michael@0 34 chomp();
michael@0 35 $line = $_;
michael@0 36 @fields = split();
michael@0 37 $line_num++;
michael@0 38
michael@0 39 # Remove # comments, which are any fields beginning with a #, plus all
michael@0 40 # that follow on the line.
michael@0 41 for ($i=0; $i<@fields; $i++) {
michael@0 42 if ($fields[$i] =~ /^#/) {
michael@0 43 @fields = @fields[0 .. $i-1];
michael@0 44 last;
michael@0 45 }
michael@0 46 }
michael@0 47 # ignore blank lines, and those with no fields left after stripping comments..
michael@0 48 if (@fields == 0) {
michael@0 49 next;
michael@0 50 }
michael@0 51
michael@0 52 #
michael@0 53 # State Label: handling.
michael@0 54 # Does the first token end with a ":"? If so, it's the name of a state.
michael@0 55 # Put in a hash, together with the current state number,
michael@0 56 # so that we can later look up the number from the name.
michael@0 57 #
michael@0 58 if (@fields[0] =~ /.*:$/) {
michael@0 59 $state_name = @fields[0];
michael@0 60 $state_name =~ s/://; # strip off the colon from the state name.
michael@0 61
michael@0 62 if ($states{$state_name} != 0) {
michael@0 63 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
michael@0 64 }
michael@0 65 $states{$state_name} = $num_states;
michael@0 66 $stateNames[$num_states] = $state_name;
michael@0 67
michael@0 68 # if the label was the only thing on this line, go on to the next line,
michael@0 69 # otherwise assume that a state definition is on the same line and fall through.
michael@0 70 if (@fields == 1) {
michael@0 71 next line_loop;
michael@0 72 }
michael@0 73 shift @fields; # shift off label field in preparation
michael@0 74 # for handling the rest of the line.
michael@0 75 }
michael@0 76
michael@0 77 #
michael@0 78 # State Transition line.
michael@0 79 # syntax is this,
michael@0 80 # character [n] target-state [^push-state] [function-name]
michael@0 81 # where
michael@0 82 # [something] is an optional something
michael@0 83 # character is either a single quoted character e.g. '['
michael@0 84 # or a name of a character class, e.g. white_space
michael@0 85 #
michael@0 86
michael@0 87 $state_line_num[$num_states] = $line_num; # remember line number with each state
michael@0 88 # so we can make better error messages later.
michael@0 89 #
michael@0 90 # First field, character class or literal character for this transition.
michael@0 91 #
michael@0 92 if ($fields[0] =~ /^'.'$/) {
michael@0 93 # We've got a quoted literal character.
michael@0 94 $state_literal_chars[$num_states] = $fields[0];
michael@0 95 $state_literal_chars[$num_states] =~ s/'//g;
michael@0 96 } else {
michael@0 97 # We've got the name of a character class.
michael@0 98 $state_char_class[$num_states] = $fields[0];
michael@0 99 if ($fields[0] =~ /[\W]/) {
michael@0 100 print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
michael@0 101 print " scanning $fields[0]\n";
michael@0 102 exit(-1);
michael@0 103 }
michael@0 104 }
michael@0 105 shift @fields;
michael@0 106
michael@0 107 #
michael@0 108 # do the 'n' flag
michael@0 109 #
michael@0 110 $state_flag[$num_states] = $javaOutput? "false" : "FALSE";
michael@0 111 if ($fields[0] eq "n") {
michael@0 112 $state_flag[$num_states] = $javaOutput? "true": "TRUE";
michael@0 113 shift @fields;
michael@0 114 }
michael@0 115
michael@0 116 #
michael@0 117 # do the destination state.
michael@0 118 #
michael@0 119 $state_dest_state[$num_states] = $fields[0];
michael@0 120 if ($fields[0] eq "") {
michael@0 121 print " rbbicsts: at line $line_num, destination state missing.\n";
michael@0 122 exit(-1);
michael@0 123 }
michael@0 124 shift @fields;
michael@0 125
michael@0 126 #
michael@0 127 # do the push state, if present.
michael@0 128 #
michael@0 129 if ($fields[0] =~ /^\^/) {
michael@0 130 $fields[0] =~ s/^\^//;
michael@0 131 $state_push_state[$num_states] = $fields[0];
michael@0 132 if ($fields[0] eq "" ) {
michael@0 133 print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
michael@0 134 exit(-1);
michael@0 135 }
michael@0 136 shift @fields;
michael@0 137 }
michael@0 138
michael@0 139 #
michael@0 140 # Lastly, do the optional action name.
michael@0 141 #
michael@0 142 if ($fields[0] ne "") {
michael@0 143 $state_func_name[$num_states] = $fields[0];
michael@0 144 shift @fields;
michael@0 145 }
michael@0 146
michael@0 147 #
michael@0 148 # There should be no fields left on the line at this point.
michael@0 149 #
michael@0 150 if (@fields > 0) {
michael@0 151 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
michael@0 152 print " scanning $fields[0]\n";
michael@0 153 }
michael@0 154 $num_states++;
michael@0 155 }
michael@0 156
michael@0 157 #
michael@0 158 # We've read in the whole file, now go back and output the
michael@0 159 # C source code for the state transition table.
michael@0 160 #
michael@0 161 # We read all states first, before writing anything, so that the state numbers
michael@0 162 # for the destination states are all available to be written.
michael@0 163 #
michael@0 164
michael@0 165 #
michael@0 166 # Make hashes for the names of the character classes and
michael@0 167 # for the names of the actions that appeared.
michael@0 168 #
michael@0 169 for ($state=1; $state < $num_states; $state++) {
michael@0 170 if ($state_char_class[$state] ne "") {
michael@0 171 if ($charClasses{$state_char_class[$state]} == 0) {
michael@0 172 $charClasses{$state_char_class[$state]} = 1;
michael@0 173 }
michael@0 174 }
michael@0 175 if ($state_func_name[$state] eq "") {
michael@0 176 $state_func_name[$state] = "doNOP";
michael@0 177 }
michael@0 178 if ($actions{$state_action_name[$state]} == 0) {
michael@0 179 $actions{$state_func_name[$state]} = 1;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 #
michael@0 184 # Check that all of the destination states have been defined
michael@0 185 #
michael@0 186 #
michael@0 187 $states{"exit"} = 0; # Predefined state name, terminates state machine.
michael@0 188 for ($state=1; $state<$num_states; $state++) {
michael@0 189 if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
michael@0 190 print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
michael@0 191 $errors++;
michael@0 192 }
michael@0 193 if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
michael@0 194 print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
michael@0 195 $errors++;
michael@0 196 }
michael@0 197 }
michael@0 198
michael@0 199 die if ($errors>0);
michael@0 200
michael@0 201 #
michael@0 202 # Assign numbers to each of the character classes classes used.
michael@0 203 # Sets are numbered from 128 - 250
michael@0 204 # The values 0-127 in the state table are used for matching
michael@0 205 # individual ASCII characters (the only thing that can appear in the rules.)
michael@0 206 # The "set" names appearing in the code below (default, etc.) need special
michael@0 207 # handling because they do not correspond to a normal set of characters,
michael@0 208 # but trigger special handling by code in the state machine.
michael@0 209 #
michael@0 210 $i = 128;
michael@0 211 foreach $setName (sort keys %charClasses) {
michael@0 212 if ($setName eq "default") {
michael@0 213 $charClasses{$setName} = 255;}
michael@0 214 elsif ($setName eq "escaped") {
michael@0 215 $charClasses{$setName} = 254;}
michael@0 216 elsif ($setName eq "escapedP") {
michael@0 217 $charClasses{$setName} = 253;}
michael@0 218 elsif ($setName eq "eof") {
michael@0 219 $charClasses{$setName} = 252;}
michael@0 220 else {
michael@0 221 # Normal (single) character class. Number them.
michael@0 222 $charClasses{$setName} = $i;
michael@0 223 $i++;
michael@0 224 }
michael@0 225 }
michael@0 226
michael@0 227
michael@0 228 my ($sec, $min, $hour, , $day, $mon, $year, $wday, $yday, $isdst) = localtime;
michael@0 229 $year += 1900;
michael@0 230
michael@0 231 if ($javaOutput) {
michael@0 232 print "/*\n";
michael@0 233 print " *******************************************************************************\n";
michael@0 234 print " * Copyright (C) 2003-$year,\n";
michael@0 235 print " * International Business Machines Corporation and others. All Rights Reserved.\n";
michael@0 236 print " *******************************************************************************\n";
michael@0 237 print " */\n";
michael@0 238 print " \n";
michael@0 239 print "package com.ibm.icu.text;\n";
michael@0 240 print " \n";
michael@0 241 print "/**\n";
michael@0 242 print " * Generated Java File. Do not edit by hand.\n";
michael@0 243 print " * This file contains the state table for the ICU Rule Based Break Iterator\n";
michael@0 244 print " * rule parser.\n";
michael@0 245 print " * It is generated by the Perl script \"rbbicst.pl\" from\n";
michael@0 246 print " * the rule parser state definitions file \"rbbirpt.txt\".\n";
michael@0 247 print " * \@internal \n";
michael@0 248 print " *\n";
michael@0 249 print " */\n";
michael@0 250
michael@0 251 print "class RBBIRuleParseTable\n";
michael@0 252 print "{\n";
michael@0 253
michael@0 254 #
michael@0 255 # Emit the constants for the actions to be performed.
michael@0 256 #
michael@0 257 $n = 1;
michael@0 258 foreach $act (sort keys %actions) {
michael@0 259 print " static final short $act = $n;\n";
michael@0 260 $n++;
michael@0 261 }
michael@0 262 print " \n";
michael@0 263
michael@0 264 #
michael@0 265 # Emit constants for char class names
michael@0 266 #
michael@0 267 foreach $setName (sort keys %charClasses) {
michael@0 268 print " static final short kRuleSet_$setName = $charClasses{$setName};\n";
michael@0 269 }
michael@0 270 print "\n\n";
michael@0 271
michael@0 272
michael@0 273 print " static class RBBIRuleTableElement { \n";
michael@0 274 print " short fAction; \n";
michael@0 275 print " short fCharClass; \n";
michael@0 276 print " short fNextState; \n";
michael@0 277 print " short fPushState; \n";
michael@0 278 print " boolean fNextChar; \n";
michael@0 279 print " String fStateName; \n";
michael@0 280 print " RBBIRuleTableElement(short a, int cc, int ns, int ps, boolean nc, String sn) { \n";
michael@0 281 print " fAction = a; \n";
michael@0 282 print " fCharClass = (short)cc; \n";
michael@0 283 print " fNextState = (short)ns; \n";
michael@0 284 print " fPushState = (short)ps; \n";
michael@0 285 print " fNextChar = nc; \n";
michael@0 286 print " fStateName = sn; \n";
michael@0 287 print " } \n";
michael@0 288 print " }; \n";
michael@0 289 print " \n";
michael@0 290
michael@0 291
michael@0 292 print " static RBBIRuleTableElement[] gRuleParseStateTable = { \n ";
michael@0 293 print " new RBBIRuleTableElement(doNOP, 0, 0,0, true, null ) // 0 \n"; #output the unused state 0.
michael@0 294 for ($state=1; $state < $num_states; $state++) {
michael@0 295 print " , new RBBIRuleTableElement($state_func_name[$state],";
michael@0 296 if ($state_literal_chars[$state] ne "") {
michael@0 297 $c = $state_literal_chars[$state];
michael@0 298 print("'$c', ");
michael@0 299 }else {
michael@0 300 print " $charClasses{$state_char_class[$state]},";
michael@0 301 }
michael@0 302 print " $states{$state_dest_state[$state]},";
michael@0 303
michael@0 304 # The push-state field is optional. If omitted, fill field with a zero, which flags
michael@0 305 # the state machine that there is no push state.
michael@0 306 if ($state_push_state[$state] eq "") {
michael@0 307 print "0, ";
michael@0 308 } else {
michael@0 309 print " $states{$state_push_state[$state]},";
michael@0 310 }
michael@0 311 print " $state_flag[$state], ";
michael@0 312
michael@0 313 # if this is the first row of the table for this state, put out the state name.
michael@0 314 if ($stateNames[$state] ne "") {
michael@0 315 print " \"$stateNames[$state]\") ";
michael@0 316 } else {
michael@0 317 print " null ) ";
michael@0 318 }
michael@0 319
michael@0 320 # Put out a comment showing the number (index) of this state row,
michael@0 321 print " // $state ";
michael@0 322 print "\n";
michael@0 323 }
michael@0 324 print " };\n";
michael@0 325
michael@0 326 print "}; \n";
michael@0 327
michael@0 328 }
michael@0 329 else
michael@0 330 {
michael@0 331 #
michael@0 332 # C++ Output ...
michael@0 333 #
michael@0 334
michael@0 335
michael@0 336 print "//---------------------------------------------------------------------------------\n";
michael@0 337 print "//\n";
michael@0 338 print "// Generated Header File. Do not edit by hand.\n";
michael@0 339 print "// This file contains the state table for the ICU Rule Based Break Iterator\n";
michael@0 340 print "// rule parser.\n";
michael@0 341 print "// It is generated by the Perl script \"rbbicst.pl\" from\n";
michael@0 342 print "// the rule parser state definitions file \"rbbirpt.txt\".\n";
michael@0 343 print "//\n";
michael@0 344 print "// Copyright (C) 2002-$year International Business Machines Corporation \n";
michael@0 345 print "// and others. All rights reserved. \n";
michael@0 346 print "//\n";
michael@0 347 print "//---------------------------------------------------------------------------------\n";
michael@0 348 print "#ifndef RBBIRPT_H\n";
michael@0 349 print "#define RBBIRPT_H\n";
michael@0 350 print "\n";
michael@0 351 print "U_NAMESPACE_BEGIN\n";
michael@0 352
michael@0 353 #
michael@0 354 # Emit the constants for indicies of Unicode Sets
michael@0 355 # Define one constant for each of the character classes encountered.
michael@0 356 # At the same time, store the index corresponding to the set name back into hash.
michael@0 357 #
michael@0 358 print "//\n";
michael@0 359 print "// Character classes for RBBI rule scanning.\n";
michael@0 360 print "//\n";
michael@0 361 foreach $setName (sort keys %charClasses) {
michael@0 362 if ($charClasses{$setName} < 250) {
michael@0 363 # Normal character class.
michael@0 364 print " static const uint8_t kRuleSet_$setName = $charClasses{$setName};\n";
michael@0 365 }
michael@0 366 }
michael@0 367 print "\n\n";
michael@0 368
michael@0 369 #
michael@0 370 # Emit the enum for the actions to be performed.
michael@0 371 #
michael@0 372 print "enum RBBI_RuleParseAction {\n";
michael@0 373 foreach $act (sort keys %actions) {
michael@0 374 print " $act,\n";
michael@0 375 }
michael@0 376 print " rbbiLastAction};\n\n";
michael@0 377
michael@0 378 #
michael@0 379 # Emit the struct definition for transtion table elements.
michael@0 380 #
michael@0 381 print "//-------------------------------------------------------------------------------\n";
michael@0 382 print "//\n";
michael@0 383 print "// RBBIRuleTableEl represents the structure of a row in the transition table\n";
michael@0 384 print "// for the rule parser state machine.\n";
michael@0 385 print "//-------------------------------------------------------------------------------\n";
michael@0 386 print "struct RBBIRuleTableEl {\n";
michael@0 387 print " RBBI_RuleParseAction fAction;\n";
michael@0 388 print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
michael@0 389 print " // 128-255: character class index\n";
michael@0 390 print " uint8_t fNextState; // 0-250: normal next-stat numbers\n";
michael@0 391 print " // 255: pop next-state from stack.\n";
michael@0 392 print " uint8_t fPushState;\n";
michael@0 393 print " UBool fNextChar;\n";
michael@0 394 print "};\n\n";
michael@0 395
michael@0 396 #
michael@0 397 # emit the state transition table
michael@0 398 #
michael@0 399 print "static const struct RBBIRuleTableEl gRuleParseStateTable[] = {\n";
michael@0 400 print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
michael@0 401 for ($state=1; $state < $num_states; $state++) {
michael@0 402 print " , {$state_func_name[$state],";
michael@0 403 if ($state_literal_chars[$state] ne "") {
michael@0 404 $c = $state_literal_chars[$state];
michael@0 405 printf(" %d /* $c */,", ord($c)); # use numeric value, so EBCDIC machines are ok.
michael@0 406 }else {
michael@0 407 print " $charClasses{$state_char_class[$state]},";
michael@0 408 }
michael@0 409 print " $states{$state_dest_state[$state]},";
michael@0 410
michael@0 411 # The push-state field is optional. If omitted, fill field with a zero, which flags
michael@0 412 # the state machine that there is no push state.
michael@0 413 if ($state_push_state[$state] eq "") {
michael@0 414 print "0, ";
michael@0 415 } else {
michael@0 416 print " $states{$state_push_state[$state]},";
michael@0 417 }
michael@0 418 print " $state_flag[$state]} ";
michael@0 419
michael@0 420 # Put out a C++ comment showing the number (index) of this state row,
michael@0 421 # and, if this is the first row of the table for this state, the state name.
michael@0 422 print " // $state ";
michael@0 423 if ($stateNames[$state] ne "") {
michael@0 424 print " $stateNames[$state]";
michael@0 425 }
michael@0 426 print "\n";
michael@0 427 };
michael@0 428 print " };\n";
michael@0 429
michael@0 430
michael@0 431 #
michael@0 432 # emit a mapping array from state numbers to state names.
michael@0 433 #
michael@0 434 # This array is used for producing debugging output from the rule parser.
michael@0 435 #
michael@0 436 print "#ifdef RBBI_DEBUG\n";
michael@0 437 print "static const char * const RBBIRuleStateNames[] = {";
michael@0 438 for ($state=0; $state<$num_states; $state++) {
michael@0 439 if ($stateNames[$state] ne "") {
michael@0 440 print " \"$stateNames[$state]\",\n";
michael@0 441 } else {
michael@0 442 print " 0,\n";
michael@0 443 }
michael@0 444 }
michael@0 445 print " 0};\n";
michael@0 446 print "#endif\n\n";
michael@0 447
michael@0 448 print "U_NAMESPACE_END\n";
michael@0 449 print "#endif\n";
michael@0 450 }
michael@0 451
michael@0 452
michael@0 453

mercurial