]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/regexcst.pl
ICU-57131.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / regexcst.pl
CommitLineData
b75a7d8f
A
1#!/usr/bin/perl
2# ********************************************************************
3# * COPYRIGHT:
2ca993e8 4# * Copyright (c) 2002-2016, International Business Machines Corporation and
b75a7d8f
A
5# * others. All Rights Reserved.
6# ********************************************************************
7#
8# regexcst.pl
9# Compile the regular expression paser state table data into initialized C data.
10# Usage:
11# cd icu/source/i18n
12# perl regexcst.pl < regexcst.txt > regexcst.h
13#
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.
18#
19# See regexcst.txt for a description of the input format for this script.
20#
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
23# merged?
24#
b75a7d8f
A
25
26
27$num_states = 1; # Always the state number for the line being compiled.
28$line_num = 0; # The line number in the input file.
29
30$states{"pop"} = 255; # Add the "pop" to the list of defined state names.
31 # This prevents any state from being labelled with "pop",
32 # and resolves references to "pop" in the next state field.
33
34line_loop: while (<>) {
35 chomp();
36 $line = $_;
37 @fields = split();
38 $line_num++;
39
40 # Remove # comments, which are any fields beginning with a #, plus all
41 # that follow on the line.
42 for ($i=0; $i<@fields; $i++) {
43 if ($fields[$i] =~ /^#/) {
44 @fields = @fields[0 .. $i-1];
45 last;
46 }
47 }
48 # ignore blank lines, and those with no fields left after stripping comments..
49 if (@fields == 0) {
50 next;
51 }
52
53 #
54 # State Label: handling.
55 # Does the first token end with a ":"? If so, it's the name of a state.
56 # Put in a hash, together with the current state number,
57 # so that we can later look up the number from the name.
58 #
59 if (@fields[0] =~ /.*:$/) {
60 $state_name = @fields[0];
61 $state_name =~ s/://; # strip off the colon from the state name.
62
63 if ($states{$state_name} != 0) {
64 print " rbbicst: at line $line-num duplicate definition of state $state_name\n";
65 }
66 $states{$state_name} = $num_states;
67 $stateNames[$num_states] = $state_name;
68
69 # if the label was the only thing on this line, go on to the next line,
70 # otherwise assume that a state definition is on the same line and fall through.
71 if (@fields == 1) {
72 next line_loop;
73 }
74 shift @fields; # shift off label field in preparation
75 # for handling the rest of the line.
76 }
77
78 #
79 # State Transition line.
80 # syntax is this,
81 # character [n] target-state [^push-state] [function-name]
82 # where
83 # [something] is an optional something
84 # character is either a single quoted character e.g. '['
85 # or a name of a character class, e.g. white_space
86 #
87
88 $state_line_num[$num_states] = $line_num; # remember line number with each state
89 # so we can make better error messages later.
90 #
91 # First field, character class or literal character for this transition.
92 #
93 if ($fields[0] =~ /^'.'$/) {
94 # We've got a quoted literal character.
95 $state_literal_chars[$num_states] = $fields[0];
96 $state_literal_chars[$num_states] =~ s/'//g;
97 } else {
98 # We've got the name of a character class.
99 $state_char_class[$num_states] = $fields[0];
100 if ($fields[0] =~ /[\W]/) {
101 print " rbbicsts: at line $line_num, bad character literal or character class name.\n";
102 print " scanning $fields[0]\n";
103 exit(-1);
104 }
105 }
106 shift @fields;
107
108 #
109 # do the 'n' flag
110 #
111 $state_flag[$num_states] = "FALSE";
112 if ($fields[0] eq "n") {
113 $state_flag[$num_states] = "TRUE";
114 shift @fields;
115 }
116
117 #
118 # do the destination state.
119 #
120 $state_dest_state[$num_states] = $fields[0];
121 if ($fields[0] eq "") {
122 print " rbbicsts: at line $line_num, destination state missing.\n";
123 exit(-1);
124 }
125 shift @fields;
126
127 #
128 # do the push state, if present.
129 #
130 if ($fields[0] =~ /^\^/) {
131 $fields[0] =~ s/^\^//;
132 $state_push_state[$num_states] = $fields[0];
133 if ($fields[0] eq "" ) {
134 print " rbbicsts: at line $line_num, expected state after ^ (no spaces).\n";
135 exit(-1);
136 }
137 shift @fields;
138 }
139
140 #
141 # Lastly, do the optional action name.
142 #
143 if ($fields[0] ne "") {
144 $state_func_name[$num_states] = $fields[0];
145 shift @fields;
146 }
147
148 #
149 # There should be no fields left on the line at this point.
150 #
151 if (@fields > 0) {
152 print " rbbicsts: at line $line_num, unexpected extra stuff on input line.\n";
153 print " scanning $fields[0]\n";
154 }
155 $num_states++;
156}
157
158#
159# We've read in the whole file, now go back and output the
160# C source code for the state transition table.
161#
162# We read all states first, before writing anything, so that the state numbers
163# for the destination states are all available to be written.
164#
165
166#
167# Make hashes for the names of the character classes and
168# for the names of the actions that appeared.
169#
170for ($state=1; $state < $num_states; $state++) {
171 if ($state_char_class[$state] ne "") {
172 if ($charClasses{$state_char_class[$state]} == 0) {
173 $charClasses{$state_char_class[$state]} = 1;
174 }
175 }
176 if ($state_func_name[$state] eq "") {
177 $state_func_name[$state] = "doNOP";
178 }
179 if ($actions{$state_action_name[$state]} == 0) {
180 $actions{$state_func_name[$state]} = 1;
181 }
182}
183
184#
185# Check that all of the destination states have been defined
186#
187#
188$states{"exit"} = 0; # Predefined state name, terminates state machine.
189for ($state=1; $state<$num_states; $state++) {
190 if ($states{$state_dest_state[$state]} == 0 && $state_dest_state[$state] ne "exit") {
191 print "Error at line $state_line_num[$state]: target state \"$state_dest_state[$state]\" is not defined.\n";
192 $errors++;
193 }
194 if ($state_push_state[$state] ne "" && $states{$state_push_state[$state]} == 0) {
195 print "Error at line $state_line_num[$state]: target state \"$state_push_state[$state]\" is not defined.\n";
196 $errors++;
197 }
198}
199
200die if ($errors>0);
201
202print "//---------------------------------------------------------------------------------\n";
203print "//\n";
204print "// Generated Header File. Do not edit by hand.\n";
205print "// This file contains the state table for the ICU Regular Expression Pattern Parser\n";
206print "// It is generated by the Perl script \"regexcst.pl\" from\n";
207print "// the rule parser state definitions file \"regexcst.txt\".\n";
208print "//\n";
2ca993e8 209print "// Copyright (C) 2002-2016 International Business Machines Corporation \n";
b75a7d8f
A
210print "// and others. All rights reserved. \n";
211print "//\n";
212print "//---------------------------------------------------------------------------------\n";
213print "#ifndef RBBIRPT_H\n";
214print "#define RBBIRPT_H\n";
215print "\n";
2ca993e8
A
216print "#include \"unicode/utypes.h\"\n";
217print "\n";
b75a7d8f
A
218print "U_NAMESPACE_BEGIN\n";
219
220#
221# Emit the constants for indicies of Unicode Sets
222# Define one constant for each of the character classes encountered.
223# At the same time, store the index corresponding to the set name back into hash.
224#
225print "//\n";
226print "// Character classes for regex pattern scanning.\n";
227print "//\n";
228$i = 128; # State Table values for Unicode char sets range from 128-250.
229 # Sets "default", "quoted", etc. get special handling.
230 # They have no corresponding UnicodeSet object in the state machine,
231 # but are handled by special case code. So we emit no reference
232 # to a UnicodeSet object to them here.
233foreach $setName (keys %charClasses) {
234 if ($setName eq "default") {
235 $charClasses{$setName} = 255;}
236 elsif ($setName eq "quoted") {
237 $charClasses{$setName} = 254;}
238 elsif ($setName eq "eof") {
239 $charClasses{$setName} = 253;}
240 else {
241 # Normal character class. Fill in array with a ptr to the corresponding UnicodeSet in the state machine.
242 print " static const uint8_t kRuleSet_$setName = $i;\n";
243 $charClasses{$setName} = $i;
244 $i++;
245 }
246}
247print "\n\n";
248
249#
250# Emit the enum for the actions to be performed.
251#
252print "enum Regex_PatternParseAction {\n";
253foreach $act (keys %actions) {
254 print " $act,\n";
255}
256print " rbbiLastAction};\n\n";
257
258#
259# Emit the struct definition for transtion table elements.
260#
261print "//-------------------------------------------------------------------------------\n";
262print "//\n";
263print "// RegexTableEl represents the structure of a row in the transition table\n";
264print "// for the pattern parser state machine.\n";
265print "//-------------------------------------------------------------------------------\n";
266print "struct RegexTableEl {\n";
267print " Regex_PatternParseAction fAction;\n";
268print " uint8_t fCharClass; // 0-127: an individual ASCII character\n";
269print " // 128-255: character class index\n";
270print " uint8_t fNextState; // 0-250: normal next-state numbers\n";
271print " // 255: pop next-state from stack.\n";
272print " uint8_t fPushState;\n";
273print " UBool fNextChar;\n";
274print "};\n\n";
275
276#
277# emit the state transition table
278#
279print "static const struct RegexTableEl gRuleParseStateTable[] = {\n";
280print " {doNOP, 0, 0, 0, TRUE}\n"; # State 0 is a dummy. Real states start with index = 1.
281for ($state=1; $state < $num_states; $state++) {
282 print " , {$state_func_name[$state],";
283 if ($state_literal_chars[$state] ne "") {
284 $c = $state_literal_chars[$state];
285 printf(" %d /* $c */,", ord($c)); # use numeric value, so EBCDIC machines are ok.
286 }else {
287 print " $charClasses{$state_char_class[$state]},";
288 }
289 print " $states{$state_dest_state[$state]},";
290
291 # The push-state field is optional. If omitted, fill field with a zero, which flags
292 # the state machine that there is no push state.
293 if ($state_push_state[$state] eq "") {
294 print "0, ";
295 } else {
296 print " $states{$state_push_state[$state]},";
297 }
298 print " $state_flag[$state]} ";
299
300 # Put out a C++ comment showing the number (index) of this state row,
301 # and, if this is the first row of the table for this state, the state name.
302 print " // $state ";
303 if ($stateNames[$state] ne "") {
304 print " $stateNames[$state]";
305 }
306 print "\n";
307};
308print " };\n";
309
310
311#
312# emit a mapping array from state numbers to state names.
313#
314# This array is used for producing debugging output from the pattern parser.
315#
316print "static const char * const RegexStateNames[] = {";
317for ($state=0; $state<$num_states; $state++) {
318 if ($stateNames[$state] ne "") {
319 print " \"$stateNames[$state]\",\n";
320 } else {
321 print " 0,\n";
322 }
323}
324print " 0};\n\n";
325
326print "U_NAMESPACE_END\n";
327print "#endif\n";
328
329
330