PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/wml_backend/p9_slice/SliceTermParser.pm

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