PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/jni/badvpn/lime/lime.php

https://bitbucket.org/madeye/shadowsocks-android
PHP | 910 lines | 701 code | 40 blank | 169 comment | 67 complexity | 8b264a11d1f46a48f32528c776dddaef MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. define('LIME_DIR', dirname(__FILE__));
  18. function emit($str) { fputs(STDERR, $str."\n"); }
  19. class Bug extends Exception {}
  20. function bug($gripe='Bug found.') { throw new Bug($gripe); }
  21. function bug_if($falacy, $gripe='Bug found.') { if ($falacy) throw new Bug($gripe); }
  22. function bug_unless($assertion, $gripe='Bug found.') { if (!$assertion) throw new Bug($gripe); }
  23. include_once(LIME_DIR.'/parse_engine.php');
  24. include_once(LIME_DIR.'/set.so.php');
  25. include_once(LIME_DIR.'/flex_token_stream.php');
  26. function lime_token_reference($pos) { return '$tokens['.$pos.']'; }
  27. function lime_token_reference_callback($foo) { return lime_token_reference($foo[1]-1); }
  28. class cf_action {
  29. function __construct($code) { $this->code=$code; }
  30. }
  31. class step {
  32. /*
  33. Base class for parse table instructions. The main idea is to make the
  34. subclasses responsible for conflict resolution among themselves. It also
  35. forms a sort of interface to the parse table.
  36. */
  37. function __construct($sym) {
  38. bug_unless($sym instanceof sym);
  39. $this->sym = $sym;
  40. }
  41. function glyph() { return $this->sym->name; }
  42. }
  43. class error extends step {
  44. function sane() { return false; }
  45. function instruction() { bug("This should not happen."); }
  46. function decide($that) { return $this; /* An error shall remain one. */ }
  47. }
  48. class shift extends step {
  49. function __construct($sym, $q) {
  50. parent::__construct($sym);
  51. $this->q = $q;
  52. }
  53. function sane() { return true; }
  54. function instruction() { return "s $this->q"; }
  55. function decide($that) {
  56. # shift-shift conflicts are impossible.
  57. # shift-accept conflicts are a bug.
  58. # so we can infer:
  59. bug_unless($that instanceof reduce);
  60. # That being said, the resolution is a matter of precedence.
  61. $shift_prec = $this->sym->right_prec;
  62. $reduce_prec = $that->rule->prec;
  63. # If we don't have defined precedence levels for both options,
  64. # then we default to shifting:
  65. if (!($shift_prec and $reduce_prec)) return $this;
  66. # Otherwise, use the step with higher precedence.
  67. if ($shift_prec > $reduce_prec) return $this;
  68. if ($reduce_prec > $shift_prec) return $that;
  69. # The "nonassoc" works by giving equal precedence to both options,
  70. # which means to put an error instruction in the parse table.
  71. return new error($this->sym);
  72. }
  73. }
  74. class reduce extends step {
  75. function __construct($sym, $rule) {
  76. bug_unless($rule instanceof rule);
  77. parent::__construct($sym);
  78. $this->rule = $rule;
  79. }
  80. function sane() { return true; }
  81. function instruction() { return 'r '.$this->rule->id; }
  82. function decide($that) {
  83. # This means that the input grammar has a reduce-reduce conflict.
  84. # Such things are considered an error in the input.
  85. throw new RRC($this, $that);
  86. #exit(1);
  87. # BISON would go with the first encountered reduce thus:
  88. # return $this;
  89. }
  90. }
  91. class accept extends step {
  92. function __construct($sym) { parent::__construct($sym); }
  93. function sane() { return true; }
  94. function instruction() { return 'a '.$this->sym->name; }
  95. }
  96. class RRC extends Exception {
  97. function __construct($a, $b) {
  98. parent::__construct("Reduce-Reduce Conflict");
  99. $this->a = $a;
  100. $this->b = $b;
  101. }
  102. function make_noise() {
  103. emit(sprintf(
  104. "Reduce-Reduce Conflict:\n%s\n%s\nLookahead is (%s)",
  105. $this->a->rule->text(),
  106. $this->b->rule->text(),
  107. $this->a->glyph()
  108. ));
  109. }
  110. }
  111. class state {
  112. function __construct($id, $key, $close) {
  113. $this->id = $id;
  114. $this->key = $key;
  115. $this->close = $close; # config key -> object
  116. ksort($this->close);
  117. $this->action = array();
  118. }
  119. function dump() {
  120. echo " * ".$this->id.' / '.$this->key."\n";
  121. foreach ($this->close as $config) $config->dump();
  122. }
  123. function add_shift($sym, $state) {
  124. $this->add_instruction(new shift($sym, $state->id));
  125. }
  126. function add_reduce($sym, $rule) {
  127. $this->add_instruction(new reduce($sym, $rule));
  128. }
  129. function add_accept($sym) {
  130. $this->add_instruction(new accept($sym));
  131. }
  132. function add_instruction($step) {
  133. bug_unless($step instanceof step);
  134. $this->action[] = $step;
  135. }
  136. function find_reductions($lime) {
  137. # rightmost configurations followset yields reduce.
  138. foreach($this->close as $c) {
  139. if ($c->rightmost) {
  140. foreach ($c->follow->all() as $glyph) $this->add_reduce($lime->sym($glyph), $c->rule);
  141. }
  142. }
  143. }
  144. function resolve_conflicts() {
  145. # For each possible lookahead, find one (and only one) step to take.
  146. $table = array();
  147. foreach ($this->action as $step) {
  148. $glyph = $step->glyph();
  149. if (isset($table[$glyph])) {
  150. # There's a conflict. The shifts all came first, which
  151. # simplifies the coding for the step->decide() methods.
  152. try {
  153. $table[$glyph] = $table[$glyph]->decide($step);
  154. } catch (RRC $e) {
  155. emit("State $this->id:");
  156. $e->make_noise();
  157. }
  158. } else {
  159. # This glyph is yet unprocessed, so the step at hand is
  160. # our best current guess at what the grammar indicates.
  161. $table[$glyph] = $step;
  162. }
  163. }
  164. # Now that we have the correct steps chosen, this routine is oddly
  165. # also responsible for turning that table into the form that will
  166. # eventually be passed to the parse engine. (So FIXME?)
  167. $out = array();
  168. foreach ($table as $glyph => $step) {
  169. if ($step->sane()) $out[$glyph] = $step->instruction();
  170. }
  171. return $out;
  172. }
  173. function segment_config() {
  174. # Filter $this->close into categories based on the symbol_after_the_dot.
  175. $f = array();
  176. foreach ($this->close as $c) {
  177. $p = $c->symbol_after_the_dot;
  178. if (!$p) continue;
  179. $f[$p->name][] = $c;
  180. }
  181. return $f;
  182. }
  183. }
  184. class sym {
  185. function __construct($name, $id) {
  186. $this->name=$name;
  187. $this->id=$id;
  188. $this->term = true; # Until proven otherwise.
  189. $this->rule = array();
  190. $this->config = array();
  191. $this->lambda = false;
  192. $this->first = new set();
  193. $this->left_prec = $this->right_prec = 0;
  194. }
  195. function summary() {
  196. $out = '';
  197. foreach ($this->rule as $rule) $out .= $rule->text()."\n";
  198. return $out;
  199. }
  200. }
  201. class rule {
  202. function __construct($id, $sym, $rhs, $code, $look, $replace) {
  203. $this->id = $id;
  204. $this->sym = $sym;
  205. $this->rhs = $rhs;
  206. $this->code = $code;
  207. $this->look = $look;
  208. bug_unless(is_int($look));
  209. $this->replace = $replace;
  210. #$this->prec_sym = $prec_sym;
  211. $this->prec = 0;
  212. $this->first = array();
  213. $this->epsilon = count($rhs);
  214. }
  215. function lhs_glyph() { return $this->sym->name; }
  216. function determine_precedence() {
  217. # We may eventually expand to allow explicit prec_symbol declarations.
  218. # Until then, we'll go with the rightmost terminal, which is what
  219. # BISON does. People probably expect that. The leftmost terminal
  220. # is a reasonable alternative behaviour, but I don't see the big
  221. # deal just now.
  222. #$prec_sym = $this->prec_sym;
  223. #if (!$prec_sym)
  224. $prec_sym = $this->rightmost_terminal();
  225. if (!$prec_sym) return;
  226. $this->prec = $prec_sym->left_prec;
  227. }
  228. private function rightmost_terminal() {
  229. $symbol = NULL;
  230. $rhs = $this->rhs;
  231. while ($rhs) {
  232. $symbol = array_pop($rhs);
  233. if ($symbol->term) break;
  234. }
  235. return $symbol;
  236. }
  237. function text() {
  238. $t = "($this->id) ".$this->lhs_glyph().' :=';
  239. foreach($this->rhs as $s) $t .= ' '.$s->name;
  240. return $t;
  241. }
  242. function table(lime_language $lang) {
  243. return array(
  244. 'symbol' => $this->lhs_glyph(),
  245. 'len' => $this->look,
  246. 'replace' => $this->replace,
  247. 'code' => $lang->fixup($this->code),
  248. 'text' => $this->text(),
  249. );
  250. }
  251. function lambda() {
  252. foreach ($this->rhs as $sym) if (!$sym->lambda) return false;
  253. return true;
  254. }
  255. function find_first() {
  256. $dot = count($this->rhs);
  257. $last = $this->first[$dot] = new set();
  258. while ($dot) {
  259. $dot--;
  260. $symbol_after_the_dot = $this->rhs[$dot];
  261. $first = $symbol_after_the_dot->first->all();
  262. bug_if(empty($first) and !$symbol_after_the_dot->lambda);
  263. $set = new set($first);
  264. if ($symbol_after_the_dot->lambda) {
  265. $set->union($last);
  266. if ($this->epsilon == $dot+1) $this->epsilon = $dot;
  267. }
  268. $last = $this->first[$dot] = $set;
  269. }
  270. }
  271. function teach_symbol_of_first_set() {
  272. $go = false;
  273. foreach ($this->rhs as $sym) {
  274. if ($this->sym->first->union($sym->first)) $go = true;
  275. if (!$sym->lambda) break;
  276. }
  277. return $go;
  278. }
  279. function lambda_from($dot) {
  280. return $this->epsilon <= $dot;
  281. }
  282. function leftmost($follow) {
  283. return new config($this, 0, $follow);
  284. }
  285. function dotted_text($dot) {
  286. $out = $this->lhs_glyph().' :=';
  287. $idx = -1;
  288. foreach($this->rhs as $idx => $s) {
  289. if ($idx == $dot) $out .= ' .';
  290. $out .= ' '.$s->name;
  291. }
  292. if ($dot > $idx) $out .= ' .';
  293. return $out;
  294. }
  295. }
  296. class config {
  297. function __construct($rule, $dot, $follow) {
  298. $this->rule=$rule;
  299. $this->dot = $dot;
  300. $this->key = "$rule->id.$dot";
  301. $this->rightmost = count($rule->rhs) <= $dot;
  302. $this->symbol_after_the_dot = $this->rightmost ? null : $rule->rhs[$dot];
  303. $this->_blink = array();
  304. $this->follow = new set($follow);
  305. $this->_flink= array();
  306. bug_unless($this->rightmost or count($rule));
  307. }
  308. function text() {
  309. $out = $this->rule->dotted_text($this->dot);
  310. $out .= ' [ '.implode(' ', $this->follow->all()).' ]';
  311. return $out;
  312. }
  313. function blink($config) {
  314. $this->_blink[] = $config;
  315. }
  316. function next() {
  317. bug_if($this->rightmost);
  318. $c = new config($this->rule, $this->dot+1, array());
  319. # Anything in the follow set for this config will also be in the next.
  320. # However, we link it backwards because we might wind up selecting a
  321. # pre-existing state, and the housekeeping is easier in the first half
  322. # of the program. We'll fix it before doing the propagation.
  323. $c->blink($this);
  324. return $c;
  325. }
  326. function copy_links_from($that) {
  327. foreach($that->_blink as $c) $this->blink($c);
  328. }
  329. function lambda() {
  330. return $this->rule->lambda_from($this->dot);
  331. }
  332. function simple_follow() {
  333. return $this->rule->first[$this->dot+1]->all();
  334. }
  335. function epsilon_follows() {
  336. return $this->rule->lambda_from($this->dot+1);
  337. }
  338. function fixlinks() {
  339. foreach ($this->_blink as $that) $that->_flink[] = $this;
  340. $this->blink = array();
  341. }
  342. function dump() {
  343. echo " * ";
  344. echo $this->key.' : ';
  345. echo $this->rule->dotted_text($this->dot);
  346. echo $this->follow->text();
  347. foreach ($this->_flink as $c) echo $c->key.' / ';
  348. echo "\n";
  349. }
  350. }
  351. class lime {
  352. var $parser_class = 'parser';
  353. function __construct() {
  354. $this->p_next = 1;
  355. $this->sym = array();
  356. $this->rule = array();
  357. $this->start_symbol_set = array();
  358. $this->state = array();
  359. $this->stop = $this->sym('#');
  360. #$err = $this->sym('error');
  361. $err->term = false;
  362. $this->lang = new lime_language_php();
  363. }
  364. function language() { return $this->lang; }
  365. function build_parser() {
  366. $this->add_start_rule();
  367. foreach ($this->rule as $r) $r->determine_precedence();
  368. $this->find_sym_lamdba();
  369. $this->find_sym_first();
  370. foreach ($this->rule as $rule) $rule->find_first();
  371. $initial = $this->find_states();
  372. $this->fixlinks();
  373. # $this->dump_configurations();
  374. $this->find_follow_sets();
  375. foreach($this->state as $s) $s->find_reductions($this);
  376. $i = $this->resolve_conflicts();
  377. $a = $this->rule_table();
  378. $qi = $initial->id;
  379. return $this->lang->ptab_to_class($this->parser_class, compact('a', 'qi', 'i'));
  380. }
  381. function rule_table() {
  382. $s = array();
  383. foreach ($this->rule as $i => $r) {
  384. $s[$i] = $r->table($this->lang);
  385. }
  386. return $s;
  387. }
  388. function add_rule($symbol, $rhs, $code) {
  389. $this->add_raw_rule($symbol, $rhs, $code, count($rhs), true);
  390. }
  391. function trump_up_bogus_lhs($real) {
  392. return "'$real'".count($this->rule);
  393. }
  394. function add_raw_rule($lhs, $rhs, $code, $look, $replace) {
  395. $sym = $this->sym($lhs);
  396. $sym->term=false;
  397. if (empty($rhs)) $sym->lambda = true;
  398. $rs = array();
  399. foreach ($rhs as $str) $rs[] = $this->sym($str);
  400. $rid = count($this->rule);
  401. $r = new rule($rid, $sym, $rs, $code, $look, $replace);
  402. $this->rule[$rid] = $r;
  403. $sym->rule[] = $r;
  404. }
  405. function sym($str) {
  406. if (!isset($this->sym[$str])) $this->sym[$str] = new sym($str, count($this->sym));
  407. return $this->sym[$str];
  408. }
  409. function summary() {
  410. $out = '';
  411. foreach ($this->sym as $sym) if (!$sym->term) $out .= $sym->summary();
  412. return $out;
  413. }
  414. private function find_sym_lamdba() {
  415. do {
  416. $go = false;
  417. foreach ($this->sym as $sym) if (!$sym->lambda) {
  418. foreach ($sym->rule as $rule) if ($rule->lambda()) {
  419. $go = true;
  420. $sym->lambda = true;
  421. }
  422. }
  423. } while ($go);
  424. }
  425. private function teach_terminals_first_set() {
  426. foreach ($this->sym as $sym) if ($sym->term) $sym->first->add($sym->name);
  427. }
  428. private function find_sym_first() {
  429. $this->teach_terminals_first_set();
  430. do {
  431. $go = false;
  432. foreach ($this->rule as $r) if ($r->teach_symbol_of_first_set()) $go = true;
  433. } while ($go);
  434. }
  435. function add_start_rule() {
  436. $rewrite = new lime_rewrite("'start'");
  437. $rhs = new lime_rhs();
  438. $rhs->add(new lime_glyph($this->deduce_start_symbol()->name, NULL));
  439. #$rhs->add(new lime_glyph($this->stop->name, NULL));
  440. $rewrite->add_rhs($rhs);
  441. $rewrite->update($this);
  442. }
  443. private function deduce_start_symbol() {
  444. $candidate = current($this->start_symbol_set);
  445. # Did the person try to set a start symbol at all?
  446. if (!$candidate) return $this->first_rule_lhs();
  447. # Do we actually have such a symbol on the left of a rule?
  448. if ($candidate->terminal) return $this->first_rule_lhs();
  449. # Ok, it's a decent choice. We need to return the symbol entry.
  450. return $this->sym($candidate);
  451. }
  452. private function first_rule_lhs() {
  453. reset($this->rule);
  454. $r = current($this->rule);
  455. return $r->sym;
  456. }
  457. function find_states() {
  458. /*
  459. Build an initial state. This is a recursive process which digs out
  460. the LR(0) state graph.
  461. */
  462. $start_glyph = "'start'";
  463. $sym = $this->sym($start_glyph);
  464. $basis = array();
  465. foreach($sym->rule as $rule) {
  466. $c = $rule->leftmost(array('#'));
  467. $basis[$c->key] = $c;
  468. }
  469. $initial = $this->get_state($basis);
  470. $initial->add_accept($sym);
  471. return $initial;
  472. }
  473. function get_state($basis) {
  474. $key = array_keys($basis);
  475. sort($key);
  476. $key = implode(' ', $key);
  477. if (isset($this->state[$key])) {
  478. # Copy all the links around...
  479. $state = $this->state[$key];
  480. foreach($basis as $config) $state->close[$config->key]->copy_links_from($config);
  481. return $state;
  482. } else {
  483. $close = $this->state_closure($basis);
  484. $this->state[$key] = $state = new state(count($this->state), $key, $close);
  485. $this->build_shifts($state);
  486. return $state;
  487. }
  488. }
  489. private function state_closure($q) {
  490. # $q is a list of config.
  491. $close = array();
  492. while ($config = array_pop($q)) {
  493. if (isset($close[$config->key])) {
  494. $close[$config->key]->copy_links_from($config);
  495. $close[$config->key]->follow->union($config->follow);
  496. continue;
  497. }
  498. $close[$config->key] = $config;
  499. $symbol_after_the_dot = $config->symbol_after_the_dot;
  500. if (!$symbol_after_the_dot) continue;
  501. if (! $symbol_after_the_dot->term) {
  502. foreach ($symbol_after_the_dot->rule as $r) {
  503. $station = $r->leftmost($config->simple_follow());
  504. if ($config->epsilon_follows()) $station->blink($config);
  505. $q[] = $station;
  506. }
  507. # The following turned out to be wrong. Don't do it.
  508. #if ($symbol_after_the_dot->lambda) {
  509. # $q[] = $config->next();
  510. #}
  511. }
  512. }
  513. return $close;
  514. }
  515. function build_shifts($state) {
  516. foreach ($state->segment_config() as $glyph => $segment) {
  517. $basis = array();
  518. foreach ($segment as $preshift) {
  519. $postshift = $preshift->next();
  520. $basis[$postshift->key] = $postshift;
  521. }
  522. $dest = $this->get_state($basis);
  523. $state->add_shift($this->sym($glyph), $dest);
  524. }
  525. }
  526. function fixlinks() {
  527. foreach ($this->state as $s) foreach ($s->close as $c) $c->fixlinks();
  528. }
  529. function find_follow_sets() {
  530. $q = array();
  531. foreach ($this->state as $s) foreach ($s->close as $c) $q[] = $c;
  532. while ($q) {
  533. $c = array_shift($q);
  534. foreach ($c->_flink as $d) {
  535. if ($d->follow->union($c->follow)) $q[] = $d;
  536. }
  537. }
  538. }
  539. private function set_assoc($ss, $l, $r) {
  540. $p = ($this->p_next++)*2;
  541. foreach ($ss as $glyph) {
  542. $s = $this->sym($glyph);
  543. $s->left_prec = $p+$l;
  544. $s->right_prec = $p+$r;
  545. }
  546. }
  547. function left_assoc($ss) { $this->set_assoc($ss, 1, 0); }
  548. function right_assoc($ss) { $this->set_assoc($ss, 0, 1); }
  549. function non_assoc($ss) { $this->set_assoc($ss, 0, 0); }
  550. private function resolve_conflicts() {
  551. # For each state, try to find one and only one
  552. # thing to do for any given lookahead.
  553. $i = array();
  554. foreach ($this->state as $s) $i[$s->id] = $s->resolve_conflicts();
  555. return $i;
  556. }
  557. function dump_configurations() {
  558. foreach ($this->state as $q) $q->dump();
  559. }
  560. function dump_first_sets() {
  561. foreach ($this->sym as $s) {
  562. echo " * ";
  563. echo $s->name.' : ';
  564. echo $s->first->text();
  565. echo "\n";
  566. }
  567. }
  568. function add_rule_with_actions($lhs, $rhs) {
  569. # First, make sure this thing is well-formed.
  570. if(!is_object(end($rhs))) $rhs[] = new cf_action('');
  571. # Now, split it into chunks based on the actions.
  572. $look = -1;
  573. $subrule = array();
  574. $subsymbol = '';
  575. while (count($rhs)) {
  576. $it = array_shift($rhs);
  577. $look ++;
  578. if (is_string($it)) {
  579. $subrule[] = $it;
  580. } else {
  581. $code = $it->code;
  582. # It's an action.
  583. # Is it the last one?
  584. if (count($rhs)) {
  585. # no.
  586. $subsymbol = $this->trump_up_bogus_lhs($lhs);
  587. $this->add_raw_rule($subsymbol, $subrule, $code, $look, false);
  588. $subrule = array($subsymbol);
  589. } else {
  590. # yes.
  591. $this->add_raw_rule($lhs, $subrule, $code, $look, true);
  592. }
  593. }
  594. }
  595. }
  596. function pragma($type, $args) {
  597. switch ($type) {
  598. case 'left':
  599. $this->left_assoc($args);
  600. break;
  601. case 'right':
  602. $this->right_assoc($args);
  603. break;
  604. case 'nonassoc':
  605. $this->non_assoc($args);
  606. break;
  607. case 'start':
  608. $this->start_symbol_set = $args;
  609. break;
  610. case 'class':
  611. $this->parser_class = $args[0];
  612. break;
  613. default:
  614. emit(sprintf("Bad Parser Pragma: (%s)", $type));
  615. exit(1);
  616. }
  617. }
  618. }
  619. class lime_language {}
  620. class lime_language_php extends lime_language {
  621. private function result_code($expr) { return "\$result = $expr;\n"; }
  622. function default_result() { return $this->result_code('reset($tokens)'); }
  623. function result_pos($pos) { return $this->result_code(lime_token_reference($pos)); }
  624. function bind($name, $pos) { return "\$$name =& \$tokens[$pos];\n"; }
  625. function fixup($code) {
  626. $code = preg_replace_callback('/\\$(\d+)/', 'lime_token_reference_callback', $code);
  627. $code = preg_replace('/\\$\\$/', '$result', $code);
  628. return $code;
  629. }
  630. function to_php($code) {
  631. return $code;
  632. }
  633. function ptab_to_class($parser_class, $ptab) {
  634. $code = "class $parser_class extends lime_parser {\n";
  635. $code .= 'var $qi = '.var_export($ptab['qi'], true).";\n";
  636. $code .= 'var $i = '.var_export($ptab['i'], true).";\n";
  637. $rc = array();
  638. $method = array();
  639. $rules = array();
  640. foreach($ptab['a'] as $k => $a) {
  641. $symbol = preg_replace('/[^\w]/', '', $a['symbol']);
  642. $rn = ++$rc[$symbol];
  643. $mn = "reduce_${k}_${symbol}_${rn}";
  644. $method[$k] = $mn;
  645. $comment = "#\n# $a[text]\n#\n";
  646. $php = $this->to_php($a['code']);
  647. $code .= "function $mn(".LIME_CALL_PROTOCOL.") {\n$comment$php\n}\n\n";
  648. unset($a['code']);
  649. unset($a['text']);
  650. $rules[$k] = $a;
  651. }
  652. $code .= 'var $method = '.var_export($method, true).";\n";
  653. $code .= 'var $a = '.var_export($rules, true).";\n";
  654. $code .= "}\n";
  655. #echo $code;
  656. return $code;
  657. }
  658. }
  659. class lime_rhs {
  660. function __construct() {
  661. /**
  662. Construct and add glyphs and actions in whatever order.
  663. Then, add this to a lime_rewrite.
  664. Don't call install_rule.
  665. The rewrite will do that for you when you "update" with it.
  666. */
  667. $this->rhs = array();
  668. }
  669. function add($slot) {
  670. bug_unless($slot instanceof lime_slot);
  671. $this->rhs[] = $slot;
  672. }
  673. function install_rule(lime $lime, $lhs) {
  674. # This is the part that has to break the rule into subrules if necessary.
  675. $rhs = $this->rhs;
  676. # First, make sure this thing is well-formed.
  677. if (!(end($rhs) instanceof lime_action)) $rhs[] = new lime_action('', NULL);
  678. # Now, split it into chunks based on the actions.
  679. $lang = $lime->language();
  680. $result_code = $lang->default_result();
  681. $look = -1;
  682. $subrule = array();
  683. $subsymbol = '';
  684. $preamble = '';
  685. while (count($rhs)) {
  686. $it = array_shift($rhs);
  687. $look ++;
  688. if ($it instanceof lime_glyph) {
  689. $subrule[] = $it->data;
  690. } elseif ($it instanceof lime_action) {
  691. $code = $it->data;
  692. # It's an action.
  693. # Is it the last one?
  694. if (count($rhs)) {
  695. # no.
  696. $subsymbol = $lime->trump_up_bogus_lhs($lhs);
  697. $action = $lang->default_result().$preamble.$code;
  698. $lime->add_raw_rule($subsymbol, $subrule, $action, $look, false);
  699. $subrule = array($subsymbol);
  700. } else {
  701. # yes.
  702. $action = $result_code.$preamble.$code;
  703. $lime->add_raw_rule($lhs, $subrule, $action, $look, true);
  704. }
  705. } else {
  706. impossible();
  707. }
  708. if ($it->name == '$') $result_code = $lang->result_pos($look);
  709. elseif ($it->name) $preamble .= $lang->bind($it->name, $look);
  710. }
  711. }
  712. }
  713. class lime_rewrite {
  714. function __construct($glyph) {
  715. /**
  716. Construct one of these with the name of the lhs.
  717. Add some rhs-es to it.
  718. Finally, "update" the lime you're building.
  719. */
  720. $this->glyph = $glyph;
  721. $this->rhs = array();
  722. }
  723. function add_rhs($rhs) {
  724. bug_unless($rhs instanceof lime_rhs);
  725. $this->rhs[] = $rhs;
  726. }
  727. function update(lime $lime) {
  728. foreach ($this->rhs as $rhs) {
  729. $rhs->install_rule($lime, $this->glyph);
  730. }
  731. }
  732. }
  733. class lime_slot {
  734. /**
  735. This keeps track of one position in an rhs.
  736. We specialize to handle actions and glyphs.
  737. If there is a name for the slot, we store it here.
  738. Later on, this structure will be consulted in the formation of
  739. actual production rules.
  740. */
  741. function __construct($data, $name) {
  742. $this->data = $data;
  743. $this->name = $name;
  744. }
  745. function preamble($pos) {
  746. if (strlen($this->name) > 0) {
  747. return "\$$this->name =& \$tokens[$pos];\n";
  748. }
  749. }
  750. }
  751. class lime_glyph extends lime_slot {}
  752. class lime_action extends lime_slot {}
  753. function lime_bootstrap() {
  754. /*
  755. This function isn't too terribly interesting to the casual observer.
  756. You're probably better off looking at parse_lime_grammar() instead.
  757. Ok, if you insist, I'll explain.
  758. The input to Lime is a CFG parser definition. That definition is
  759. written in some language. (The Lime language, to be exact.)
  760. Anyway, I have to parse the Lime language and compile it into a
  761. very complex data structure from which a parser is eventually
  762. built. What better way than to use Lime itself to parse its own
  763. language? Well, it's almost that simple, but not quite.
  764. The Lime language is fairly potent, but a restricted subset of
  765. its features was used to write a metagrammar. Then, I hand-translated
  766. that metagrammar into another form which is easy to snarf up.
  767. In the process of reading that simplified form, this function
  768. builds the same sort of data structure that later gets turned into
  769. a parser. The last step is to run the parser generation algorithm,
  770. eval() the resulting PHP code, and voila! With no hard work, I can
  771. suddenly read and comprehend the full range of the Lime language
  772. without ever having written an algorithm to do so. It feels like magic.
  773. */
  774. $bootstrap = LIME_DIR."/lime.bootstrap";
  775. $lime = new lime();
  776. $lime->parser_class = 'lime_metaparser';
  777. $rhs = array();
  778. bug_unless(is_readable($bootstrap));
  779. foreach(file($bootstrap) as $l) {
  780. $a = explode(":", $l, 2);
  781. if (count($a) == 2) {
  782. list($pattern, $code) = $a;
  783. $sl = new lime_rhs();
  784. $pattern = trim($pattern);
  785. if (strlen($pattern)>0) {
  786. foreach (explode(' ', $pattern) as $glyph) $sl->add(new lime_glyph($glyph, NULL));
  787. }
  788. $sl->add(new lime_action($code, NULL));
  789. $rhs[] = $sl;
  790. } else {
  791. $m = preg_match('/^to (\w+)$/', $l, $r);
  792. if ($m == 0) continue;
  793. $g = $r[1];
  794. $rw = new lime_rewrite($g);
  795. foreach($rhs as $b) $rw->add_rhs($b);
  796. $rw->update($lime);
  797. $rhs = array();
  798. }
  799. }
  800. $parser_code = $lime->build_parser();
  801. eval($parser_code);
  802. }
  803. class voodoo_scanner extends flex_scanner {
  804. /*
  805. The voodoo is in the way I do lexical processing on grammar definition
  806. files. They contain embedded bits of PHP, and it's important to keep
  807. track of things like strings, comments, and matched braces. It seemed
  808. like an ideal problem to solve with GNU flex, so I wrote a little
  809. scanner in flex and C to dig out the tokens for me. Of course, I need
  810. the tokens in PHP, so I designed a simple binary wrapper for them which
  811. also contains line-number information, guaranteed to help out if you
  812. write a grammar which surprises the parser in any manner.
  813. */
  814. function executable() { return LIME_DIR.'/lime_scan_tokens'; }
  815. }
  816. function parse_lime_grammar($path) {
  817. /*
  818. This is a good function to read because it teaches you how to interface
  819. with a Lime parser. I've tried to isolate out the bits that aren't
  820. instructive in that regard.
  821. */
  822. if (!class_exists('lime_metaparser')) lime_bootstrap();
  823. $parse_engine = new parse_engine(new lime_metaparser());
  824. $scanner = new voodoo_scanner($path);
  825. try {
  826. # The result of parsing a Lime grammar is a Lime object.
  827. $lime = $scanner->feed($parse_engine);
  828. # Calling its build_parser() method gets the output PHP code.
  829. return $lime->build_parser();
  830. } catch (parse_error $e) {
  831. die ($e->getMessage()." in $path line $scanner->lineno.\n");
  832. }
  833. }
  834. if ($_SERVER['argv']) {
  835. $code = '';
  836. array_shift($_SERVER['argv']); # Strip out the program name.
  837. foreach ($_SERVER['argv'] as $path) {
  838. $code .= parse_lime_grammar($path);
  839. }
  840. echo "<?php\n\n";
  841. ?>
  842. /*
  843. DON'T EDIT THIS FILE!
  844. This file was automatically generated by the Lime parser generator.
  845. The real source code you should be looking at is in one or more
  846. grammar files in the Lime format.
  847. THE ONLY REASON TO LOOK AT THIS FILE is to see where in the grammar
  848. file that your error happened, because there are enough comments to
  849. help you debug your grammar.
  850. If you ignore this warning, you're shooting yourself in the brain,
  851. not the foot.
  852. */
  853. <?php
  854. echo $code;
  855. }