if ($ARGV[0] eq "-j") {
$javaOutput = 1;
shift @ARGV;
}
$num_states = 1; $line_num = 0;
$states{"pop"} = 255;
line_loop: while (<>) {
chomp();
$line = $_;
@fields = split();
$line_num++;
for ($i=0; $i<@fields; $i++) {
if ($fields[$i] =~ /^ @fields = @fields[0 .. $i-1];
last;
}
}
if (@fields == 0) {
next;
}
if (@fields[0] =~ /.*:$/) {
$state_name = @fields[0];
$state_name =~ s/://; # strip off the colon from the state name.
if ($states{$state_name} != 0) {
print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
}
$states{$state_name} = $num_states;
$stateNames[$num_states] = $state_name;
if (@fields == 1) {
next line_loop;
}
shift @fields; }
$state_line_num[$num_states] = $line_num; if ($fields[0] =~ /^'.'$/) {
$state_literal_chars[$num_states] = $fields[0];
$state_literal_chars[$num_states] =~ s/'//g;
} else {
# We've got the name of a character class.
$state_char_class[$num_states] = $fields[0];
if ($fields[0] =~ /[\W]/) {
print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
print " scanning $fields[0]\n";
exit(-1);
}
}
shift @fields;
$state_flag[$num_states] = "FALSE";
if ($fields[0] eq "n") {
$state_flag[$num_states] = "TRUE";
shift @fields;
}
$state_dest_state[$num_states] = $fields[0];
if ($fields[0] eq "") {
print " rbbicsts: at line $line_num, destination state missing.\n";
exit(-1);
}
shift @fields;
if ($fields[0] =~ /^\^/) {
$fields[0] =~ s/^\^//;
$state_push_state[$num_states] = $fields[0];
if ($fields[0] eq "" ) {
print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
exit(-1);
}
shift @fields;
}
if ($fields[0] ne "") {
$state_func_name[$num_states] = $fields[0];
shift @fields;
}
if (@fields > 0) {
print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
print " scanning $fields[0]\n";
}
$num_states++;
}
for ($state=1; $state < $num_states; $state++) {
if ($state_char_class[$state] ne "") {
if ($charClasses{$state_char_class[$state]} == 0) {
$charClasses{$state_char_class[$state]} = 1;
}
}
if ($state_func_name[$state] eq "") {
$state_func_name[$state] = "doNOP";
}
if ($actions{$state_action_name[$state]} == 0) {
$actions{$state_func_name[$state]} = 1;
}
}
$states{"exit"} = 0; for ($state=1; $state<$num_states; $state++) {
if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
$errors++;
}
if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
$errors++;
}
}
die if ($errors>0);
my ($sec, $min, $hour, , $day, $mon, $year, $wday, $yday, $isdst) = localtime;
$year += 1900;
if ($javaOutput) {
print "/*\n";
print " *******************************************************************************\n";
print " * Copyright (C) 2003-$year,\n";
print " * International Business Machines Corporation and others. All Rights Reserved.\n";
print " *******************************************************************************\n";
print " */\n";
print " \n";
print "package com.ibm.icu.text;\n";
print " \n";
print "/**\n";
print " * Generated Java File. Do not edit by hand.\n";
print " * This file contains the state table for the ICU Rule Based Break Iterator\n";
print " * rule parser.\n";
print " * It is generated by the Perl script \"rbbicst.pl\" from\n";
print " * the rule parser state definitions file \"rbbirpt.txt\".\n";
print " *\n";
print " */\n";
print "public class RuleBasedBreakIteratorStateTable\n";
print "{\n";
$n = 1;
foreach $act (keys %actions) {
print " public static final int $act = $n;\n";
$n++;
}
print " \n";
print " public static final String[] gRuleParseStateTable = {\n";
printf(" \"\\u%04.4x\\u%04.4x\\u%04.4x\\u%04.4x\\u%04.4x\"\n", doNOP, 0, 0, 0, 1);
for ($state=1; $state < $num_states; $state++) {
printf(" , \"\\u%04.4x", $state_func_name[$state]);
if ($state_literal_chars[$state] ne "") {
printf("\\u%04.4x", $state_func_name[$state]);
}else {
printf("\\u%04.4x", $charClasses{$state_char_class[$state]});
}
printf("\\u%04.4x", $states{$state_dest_state[$state]});
if ($state_push_state[$state] eq "") {
print "\\u0000";
} else {
printf("\\u%04.4x", $states{$state_push_state[$state]});
}
printf("\\u%04.4x", $state_flag[$state]);
if ($stateNames[$state] ne "") {
printf("%-20s", $stateNames[$state]."\"");
} else {
printf("%-20s", "\"");
}
print " // $state ";
print "\n";
};
print " };\n";
print "}\n";
}
else
{
print "//---------------------------------------------------------------------------------\n";
print "//\n";
print "// Generated Header File. Do not edit by hand.\n";
print "// This file contains the state table for the ICU Rule Based Break Iterator\n";
print "// rule parser.\n";
print "// It is generated by the Perl script \"rbbicst.pl\" from\n";
print "// the rule parser state definitions file \"rbbirpt.txt\".\n";
print "//\n";
print "// Copyright (C) 2002-$year International Business Machines Corporation \n";
print "// and others. All rights reserved. \n";
print "//\n";
print "//---------------------------------------------------------------------------------\n";
print "#ifndef RBBIRPT_H\n";
print "#define RBBIRPT_H\n";
print "\n";
print "U_NAMESPACE_BEGIN\n";
print "//\n";
print "// Character classes for RBBI rule scanning.\n";
print "//\n";
$i = 128; foreach $setName (keys %charClasses) {
if ($setName eq "default") {
$charClasses{$setName} = 255;}
elsif ($setName eq "escaped") {
$charClasses{$setName} = 254;}
elsif ($setName eq "escapedP") {
$charClasses{$setName} = 253;}
elsif ($setName eq "eof") {
$charClasses{$setName} = 252;}
else {
print " static const uint8_t kRuleSet_$setName = $i;\n";
$charClasses{$setName} = $i;
$i++;
}
}
print "\n\n";
print "enum RBBI_RuleParseAction {\n";
foreach $act (keys %actions) {
print " $act,\n";
}
print " rbbiLastAction};\n\n";
print "//-------------------------------------------------------------------------------\n";
print "//\n";
print "// RBBIRuleTableEl represents the structure of a row in the transition table\n";
print "// for the rule parser state machine.\n";
print "//-------------------------------------------------------------------------------\n";
print "struct RBBIRuleTableEl {\n";
print " RBBI_RuleParseAction fAction;\n";
print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
print " // 128-255: character class index\n";
print " uint8_t fNextState; // 0-250: normal next-stat numbers\n";
print " // 255: pop next-state from stack.\n";
print " uint8_t fPushState;\n";
print " UBool fNextChar;\n";
print "};\n\n";
print "static const struct RBBIRuleTableEl gRuleParseStateTable[] = {\n";
print " {doNOP, 0, 0, 0, TRUE}\n"; for ($state=1; $state < $num_states; $state++) {
print " , {$state_func_name[$state],";
if ($state_literal_chars[$state] ne "") {
$c = $state_literal_chars[$state];
printf(" %d /* $c */,", ord($c)); }else {
print " $charClasses{$state_char_class[$state]},";
}
print " $states{$state_dest_state[$state]},";
if ($state_push_state[$state] eq "") {
print "0, ";
} else {
print " $states{$state_push_state[$state]},";
}
print " $state_flag[$state]} ";
print " // $state ";
if ($stateNames[$state] ne "") {
print " $stateNames[$state]";
}
print "\n";
};
print " };\n";
print "#ifdef RBBI_DEBUG\n";
print "static const char * const RBBIRuleStateNames[] = {";
for ($state=0; $state<$num_states; $state++) {
if ($stateNames[$state] ne "") {
print " \"$stateNames[$state]\",\n";
} else {
print " 0,\n";
}
}
print " 0};\n";
print "#endif\n\n";
print "U_NAMESPACE_END\n";
print "#endif\n";
}