PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/third_party/mozjs/extract/intl/icu/source/i18n/regexcst.pl

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