PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wml_backend/p9_slice/SliceTerm.pm

https://bitbucket.org/shlomif/website-meta-language
Perl | 469 lines | 439 code | 14 blank | 16 comment | 82 complexity | 6bb9c264012e4d7d3b5c2803bbde3d75 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.0
  1. # @(#)yaccpar 1.8 (Berkeley) 01/20/91 (JAKE-P5BP-0.6 04/26/98)
  2. package SliceTermParser;
  3. ;##
  4. ;## slice_term.y -- YACC parser for slice terms
  5. ;## Copyright (c) 1997-2002 Ralf S. Engelschall.
  6. ;## Copyright (c) 1999-2002 Denis Barbier.
  7. ;##
  8. package SliceTermParser;
  9. $SLICE=257;
  10. $YYERRCODE=256;
  11. @yylhs = ( -1,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. 0, 0, 0,
  14. );
  15. @yylen = ( 2,
  16. 1, 2, 2, 2, 3, 3, 3, 3, 3, 3,
  17. 3, 3, 3,
  18. );
  19. @yydefred = ( 0,
  20. 0, 0, 0, 0, 0, 2, 3, 4, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 13, 0, 0,
  22. 0, 0, 0, 0, 9, 10,
  23. );
  24. @yydgoto = ( 5,
  25. );
  26. @yysindex = ( -33,
  27. -60, -33, -33, -33, -7, 0, 0, 0, -12, -33,
  28. -33, -33, -33, -33, -33, -33, -33, 0, -31, -31,
  29. 24, 24, -36, -36, 0, 0,
  30. );
  31. @yyrindex = ( 0,
  32. 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33. 0, 0, 0, 0, 0, 0, 0, 0, 15, 17,
  34. 9, 14, 3, 8, 0, 0,
  35. );
  36. @yygindex = ( 135,
  37. );
  38. $YYTABLESIZE=224;
  39. @yytable = ( 2,
  40. 17, 1, 5, 6, 0, 17, 4, 6, 11, 0,
  41. 0, 13, 0, 12, 7, 0, 8, 0, 0, 0,
  42. 0, 0, 0, 0, 17, 0, 0, 0, 18, 17,
  43. 13, 0, 11, 0, 0, 13, 0, 11, 1, 0,
  44. 0, 0, 1, 5, 1, 5, 1, 5, 6, 11,
  45. 6, 11, 6, 11, 12, 7, 12, 8, 12, 7,
  46. 17, 8, 15, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 16, 0, 0, 0, 0, 16, 10,
  48. 0, 15, 0, 0, 10, 12, 15, 0, 14, 0,
  49. 0, 0, 3, 1, 5, 1, 5, 16, 0, 6,
  50. 11, 6, 16, 0, 12, 12, 7, 14, 8, 12,
  51. 0, 1, 14, 0, 0, 0, 0, 15, 1, 5,
  52. 0, 1, 5, 0, 6, 11, 0, 6, 0, 0,
  53. 12, 0, 0, 16, 0, 0, 7, 8, 9, 0,
  54. 0, 0, 0, 14, 19, 20, 21, 22, 23, 24,
  55. 25, 26, 0, 0, 0, 0, 0, 0, 0, 0,
  56. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  59. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  60. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  61. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 1,
  63. );
  64. @yycheck = ( 33,
  65. 37, 0, 0, 64, -1, 37, 40, 0, 0, -1,
  66. -1, 43, -1, 0, 0, -1, 0, -1, -1, -1,
  67. -1, -1, -1, -1, 37, -1, -1, -1, 41, 37,
  68. 43, -1, 45, -1, -1, 43, -1, 45, 37, -1,
  69. -1, -1, 41, 41, 43, 43, 45, 45, 41, 41,
  70. 43, 43, 45, 45, 41, 41, 43, 41, 45, 45,
  71. 37, 45, 94, -1, -1, -1, -1, -1, -1, -1,
  72. -1, -1, -1, 110, -1, -1, -1, -1, 110, 92,
  73. -1, 94, -1, -1, 92, 117, 94, -1, 120, -1,
  74. -1, -1, 126, 92, 92, 94, 94, 110, -1, 92,
  75. 92, 94, 110, -1, 117, 92, 92, 120, 92, 117,
  76. -1, 110, 120, -1, -1, -1, -1, 94, 117, 117,
  77. -1, 120, 120, -1, 117, 117, -1, 120, -1, -1,
  78. 117, -1, -1, 110, -1, -1, 2, 3, 4, -1,
  79. -1, -1, -1, 120, 10, 11, 12, 13, 14, 15,
  80. 16, 17, -1, -1, -1, -1, -1, -1, -1, -1,
  81. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  82. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  83. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  84. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  85. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  86. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  87. -1, -1, -1, 257,
  88. );
  89. $YYFINAL=5;
  90. #ifndef YYDEBUG
  91. #define YYDEBUG 0
  92. #endif
  93. $YYMAXTOKEN=257;
  94. #if YYDEBUG
  95. @yyname = (
  96. "end-of-file",'','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',
  97. "'!'",'','','',"'%'",'','',"'('","')'",'',"'+'",'',"'-'",'','','','','','','','','','','','','','','',
  98. '','','',"'\@'",'','','','','','','','','','','','','','','','','','','','','','','','','','','',"'\\\\'",'',
  99. "'^'",'','','','','','','','','','','','','','','',"'n'",'','','','','','',"'u'",'','',"'x'",'','','','','',
  100. "'~'",'','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',
  101. '','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',
  102. '','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','',
  103. '','','','','','','','','','','','','',"SLICE",
  104. );
  105. @yyrule = (
  106. "\$accept : expr",
  107. "expr : SLICE",
  108. "expr : SLICE '\@'",
  109. "expr : '!' expr",
  110. "expr : '~' expr",
  111. "expr : expr 'x' expr",
  112. "expr : expr '^' expr",
  113. "expr : expr '\\\\' expr",
  114. "expr : expr '-' expr",
  115. "expr : expr 'n' expr",
  116. "expr : expr '%' expr",
  117. "expr : expr 'u' expr",
  118. "expr : expr '+' expr",
  119. "expr : '(' expr ')'",
  120. );
  121. #endif
  122. sub yyclearin {
  123. my $p;
  124. ($p) = @_;
  125. $p->{yychar} = -1;
  126. }
  127. sub yyerrok {
  128. my $p;
  129. ($p) = @_;
  130. $p->{yyerrflag} = 0;
  131. }
  132. sub new {
  133. my $p = bless {}, $_[0];
  134. $p->{yylex} = $_[1];
  135. $p->{yyerror} = $_[2];
  136. $p->{yydebug} = $_[3];
  137. return $p;
  138. }
  139. sub YYERROR {
  140. my $p;
  141. ($p) = @_;
  142. ++$p->{yynerrs};
  143. $p->yy_err_recover;
  144. }
  145. sub yy_err_recover {
  146. my $p;
  147. ($p) = @_;
  148. if ($p->{yyerrflag} < 3)
  149. {
  150. $p->{yyerrflag} = 3;
  151. while (1)
  152. {
  153. if (($p->{yyn} = $yysindex[$p->{yyss}->[$p->{yyssp}]]) &&
  154. ($p->{yyn} += $YYERRCODE) >= 0 &&
  155. $p->{yyn} <= $#yycheck &&
  156. $yycheck[$p->{yyn}] == $YYERRCODE)
  157. {
  158. warn("yydebug: state " .
  159. $p->{yyss}->[$p->{yyssp}] .
  160. ", error recovery shifting to state" .
  161. $yytable[$p->{yyn}] . "\n")
  162. if $p->{yydebug};
  163. $p->{yyss}->[++$p->{yyssp}] =
  164. $p->{yystate} = $yytable[$p->{yyn}];
  165. $p->{yyvs}->[++$p->{yyvsp}] = $p->{yylval};
  166. next yyloop;
  167. }
  168. else
  169. {
  170. warn("yydebug: error recovery discarding state ".
  171. $p->{yyss}->[$p->{yyssp}]. "\n")
  172. if $p->{yydebug};
  173. return(undef) if $p->{yyssp} <= 0;
  174. --$p->{yyssp};
  175. --$p->{yyvsp};
  176. }
  177. }
  178. }
  179. else
  180. {
  181. return (undef) if $p->{yychar} == 0;
  182. if ($p->{yydebug})
  183. {
  184. $p->{yys} = '';
  185. if ($p->{yychar} <= $YYMAXTOKEN) { $p->{yys} =
  186. $yyname[$p->{yychar}]; }
  187. if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; }
  188. warn("yydebug: state " . $p->{yystate} .
  189. ", error recovery discards " .
  190. "token " . $p->{yychar} . "(" .
  191. $p->{yys} . ")\n");
  192. }
  193. $p->{yychar} = -1;
  194. next yyloop;
  195. }
  196. 0;
  197. } # yy_err_recover
  198. sub yyparse {
  199. my $p;
  200. my $s;
  201. ($p, $s) = @_;
  202. if ($p->{yys} = $ENV{'YYDEBUG'})
  203. {
  204. $p->{yydebug} = int($1) if $p->{yys} =~ /^(\d)/;
  205. }
  206. $p->{yynerrs} = 0;
  207. $p->{yyerrflag} = 0;
  208. $p->{yychar} = (-1);
  209. $p->{yyssp} = 0;
  210. $p->{yyvsp} = 0;
  211. $p->{yyss}->[$p->{yyssp}] = $p->{yystate} = 0;
  212. yyloop: while(1)
  213. {
  214. yyreduce: {
  215. last yyreduce if ($p->{yyn} = $yydefred[$p->{yystate}]);
  216. if ($p->{yychar} < 0)
  217. {
  218. if ((($p->{yychar}, $p->{yylval}) =
  219. &{$p->{yylex}}($s)) < 0) { $p->{yychar} = 0; }
  220. if ($p->{yydebug})
  221. {
  222. $p->{yys} = '';
  223. if ($p->{yychar} <= $#yyname)
  224. { $p->{yys} = $yyname[$p->{yychar}]; }
  225. if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; };
  226. warn("yydebug: state " . $p->{yystate} .
  227. ", reading " . $p->{yychar} . " (" .
  228. $p->{yys} . ")\n");
  229. }
  230. }
  231. if (($p->{yyn} = $yysindex[$p->{yystate}]) &&
  232. ($p->{yyn} += $p->{yychar}) >= 0 &&
  233. $p->{yyn} <= $#yycheck &&
  234. $yycheck[$p->{yyn}] == $p->{yychar})
  235. {
  236. warn("yydebug: state " . $p->{yystate} .
  237. ", shifting to state " .
  238. $yytable[$p->{yyn}] . "\n") if $p->{yydebug};
  239. $p->{yyss}->[++$p->{yyssp}] = $p->{yystate} =
  240. $yytable[$p->{yyn}];
  241. $p->{yyvs}->[++$p->{yyvsp}] = $p->{yylval};
  242. $p->{yychar} = (-1);
  243. --$p->{yyerrflag} if $p->{yyerrflag} > 0;
  244. next yyloop;
  245. }
  246. if (($p->{yyn} = $yyrindex[$p->{yystate}]) &&
  247. ($p->{yyn} += $p->{'yychar'}) >= 0 &&
  248. $p->{yyn} <= $#yycheck &&
  249. $yycheck[$p->{yyn}] == $p->{yychar})
  250. {
  251. $p->{yyn} = $yytable[$p->{yyn}];
  252. last yyreduce;
  253. }
  254. if (! $p->{yyerrflag}) {
  255. &{$p->{yyerror}}('syntax error', $s);
  256. ++$p->{yynerrs};
  257. }
  258. return(undef) if $p->yy_err_recover;
  259. } # yyreduce
  260. warn("yydebug: state " . $p->{yystate} .
  261. ", reducing by rule " .
  262. $p->{yyn} . " (" . $yyrule[$p->{yyn}] .
  263. ")\n") if $p->{yydebug};
  264. $p->{yym} = $yylen[$p->{yyn}];
  265. $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}+1-$p->{yym}];
  266. if ($p->{yyn} == 1) {
  267. { $p->{yyval} = newvar($p->{yyvs}->[$p->{yyvsp}-0]); push(@OUT, "my ".$p->{yyval}." = \$CFG->{SLICE}->{SET}->{OBJ}->{'".$p->{yyvs}->[$p->{yyvsp}-0]."'}->Clone;"); }
  268. }
  269. if ($p->{yyn} == 2) {
  270. { $p->{yyval} = newvar($p->{yyvs}->[$p->{yyvsp}-1]); push(@OUT, "my ".$p->{yyval}." = \$CFG->{SLICE}->{SET}->{OBJ}->{'NOV_".$p->{yyvs}->[$p->{yyvsp}-1]."'}->Clone;"); }
  271. }
  272. if ($p->{yyn} == 3) {
  273. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-0]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-0]."->Complement(".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  274. }
  275. if ($p->{yyn} == 4) {
  276. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-0]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-0]."->Complement(".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  277. }
  278. if ($p->{yyn} == 5) {
  279. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->ExclusiveOr(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  280. }
  281. if ($p->{yyn} == 6) {
  282. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->ExclusiveOr(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  283. }
  284. if ($p->{yyn} == 7) {
  285. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Difference(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  286. }
  287. if ($p->{yyn} == 8) {
  288. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Difference(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  289. }
  290. if ($p->{yyn} == 9) {
  291. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Intersection(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  292. }
  293. if ($p->{yyn} == 10) {
  294. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Intersection(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  295. }
  296. if ($p->{yyn} == 11) {
  297. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Union(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  298. }
  299. if ($p->{yyn} == 12) {
  300. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-2]; push(@OUT, $p->{yyvs}->[$p->{yyvsp}-2]."->Union(".$p->{yyvs}->[$p->{yyvsp}-2].",".$p->{yyvs}->[$p->{yyvsp}-0].");"); }
  301. }
  302. if ($p->{yyn} == 13) {
  303. { $p->{yyval} = $p->{yyvs}->[$p->{yyvsp}-1]; }
  304. }
  305. $p->{yyssp} -= $p->{yym};
  306. $p->{yystate} = $p->{yyss}->[$p->{yyssp}];
  307. $p->{yyvsp} -= $p->{yym};
  308. $p->{yym} = $yylhs[$p->{yyn}];
  309. if ($p->{yystate} == 0 && $p->{yym} == 0)
  310. {
  311. warn("yydebug: after reduction, shifting from state 0 ",
  312. "to state $YYFINAL\n") if $p->{yydebug};
  313. $p->{yystate} = $YYFINAL;
  314. $p->{yyss}->[++$p->{yyssp}] = $YYFINAL;
  315. $p->{yyvs}->[++$p->{yyvsp}] = $p->{yyval};
  316. if ($p->{yychar} < 0)
  317. {
  318. if ((($p->{yychar}, $p->{yylval}) =
  319. &{$p->{yylex}}($s)) < 0) { $p->{yychar} = 0; }
  320. if ($p->{yydebug})
  321. {
  322. $p->{yys} = '';
  323. if ($p->{yychar} <= $#yyname)
  324. { $p->{yys} = $yyname[$p->{yychar}]; }
  325. if (!$p->{yys}) { $p->{yys} = 'illegal-symbol'; }
  326. warn("yydebug: state $YYFINAL, reading " .
  327. $p->{yychar} . " (" . $p->{yys} . ")\n");
  328. }
  329. }
  330. return ($p->{yyvs}->[1]) if $p->{yychar} == 0;
  331. next yyloop;
  332. }
  333. if (($p->{yyn} = $yygindex[$p->{yym}]) &&
  334. ($p->{yyn} += $p->{yystate}) >= 0 &&
  335. $p->{yyn} <= $#yycheck &&
  336. $yycheck[$p->{yyn}] == $p->{yystate})
  337. {
  338. $p->{yystate} = $yytable[$p->{yyn}];
  339. } else {
  340. $p->{yystate} = $yydgoto[$p->{yym}];
  341. }
  342. warn("yydebug: after reduction, shifting from state " .
  343. $p->{yyss}->[$p->{yyssp}] . " to state " .
  344. $p->{yystate} . "\n") if $p->{yydebug};
  345. $p->{yyss}[++$p->{yyssp}] = $p->{yystate};
  346. $p->{yyvs}[++$p->{yyvsp}] = $p->{yyval};
  347. } # yyloop
  348. } # yyparse
  349. # create new set variable
  350. $tmpcnt = 0;
  351. sub newvar {
  352. my ($name) = @_;
  353. my ($tmp);
  354. if ($main::CFG->{SLICE}->{SET}->{OBJ}->{"$name"} eq '') {
  355. main::printwarning("no such slice '$name'\n") if $undef;
  356. # The $undef string is caught by caller, it is used
  357. # to trap warnings depending on the -y command line flag.
  358. die $undef."\n" if $undef > 1;
  359. $main::CFG->{SLICE}->{SET}->{OBJ}->{"$name"} =
  360. $main::CFG->{SLICE}->{SET}->{OBJ}->{DEF0}->Clone;
  361. }
  362. $tmp = sprintf("\$T%03d", $tmpcnt++);
  363. return $tmp;
  364. }
  365. # the lexical scanner
  366. sub yylex {
  367. local (*s) = @_;
  368. my ($c, $val);
  369. # ignore whitespaces
  370. $s =~ s|^\s+||;
  371. # recognize end of string
  372. return 0 if ($s eq '');
  373. # found a token
  374. if ($s =~ s|^([_A-Z0-9*{}]+)||) {
  375. $val = $1;
  376. # if its a wildcarded slice name we have
  377. # to construct the slice union on-the-fly
  378. if ($val =~ m|\*|) {
  379. my $pat = $val;
  380. $pat =~ s|\*|\.\*|g;
  381. # treat special *{...} sequence
  382. $excl = '';
  383. while ($pat =~ s|^(.*?)\.\*\{([_A-Z0-9]+)\}(.*)$|$1\.\*$3|) {
  384. my $temp = $1 . $2 . $3;
  385. $temp =~ s|\.\*\{[_A-Z0-9]+\}|\.\*|g;
  386. $excl .= "return 1 if m/^$temp\$/;";
  387. }
  388. $sub_excl = eval "sub { \$_ = shift; $excl; return 0}";
  389. my $slice;
  390. my @slices = ();
  391. foreach $slice (keys(%{$main::CFG->{SLICE}->{SET}->{ASC}})) {
  392. if ($slice =~ m|^$pat$|) {
  393. push(@slices, $slice) unless &$sub_excl($slice);
  394. }
  395. }
  396. if ($#slices == 0) {
  397. $val = $slices[0];
  398. }
  399. elsif ($#slices > 0) {
  400. $s = join('u', @slices).')'.$s;
  401. return ord('(');
  402. }
  403. else {
  404. main::printwarning("no existing slice matches `$val'\n") if $wildcard;
  405. # The $wildcard string is caught by caller, it is used
  406. # to trap warnings depending on the -y command line flag.
  407. die $wildcard."\n" if $wildcard > 1;
  408. }
  409. }
  410. return ($SLICE, $val);
  411. }
  412. # else give back one plain character
  413. $c = substr($s, 0, 1);
  414. $s = substr($s, 1);
  415. return ord($c);
  416. }
  417. # and error function
  418. sub yyerror {
  419. my ($msg, $s) = @_;
  420. die "$msg at $s.\n";
  421. }
  422. #
  423. # The top-level function which gets called by the user
  424. #
  425. # ($cmds, $var) = SliceTerm::Parse($term, $status);
  426. #
  427. package SliceTerm;
  428. sub Parse {
  429. local($str, $status) = @_;
  430. my($p, $var, $cmds);
  431. @SliceTermParser::OUT = ();
  432. $SliceTermParser::undef = $status->{u};
  433. $SliceTermParser::wildcard = $status->{w};
  434. $p = SliceTermParser->new(\&SliceTermParser::yylex, \&SliceTermParser::yyerror, 0);
  435. eval {$var = $p->yyparse(*str);};
  436. if ($@ =~ s/^(\d)$//) {
  437. main::error("Execution stopped\n") if $1 > 2;
  438. return ();
  439. }
  440. $cmds = join("\n", @SliceTermParser::OUT) . "\n";
  441. return ($cmds, $var);
  442. }
  443. package main;
  444. 1;
  445. ##EOF##
  446. 1;