PageRenderTime 41ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Brainy/sysplugins/smarty_internal_configfilelexer.php

https://gitlab.com/TouchyPunchy/brainy
PHP | 647 lines | 534 code | 76 blank | 37 comment | 60 complexity | 73773ecd5fe7d554722688d6fe4a6443 MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Configfilelexer
  4. *
  5. * This is the lexer to break the config file source into tokens
  6. * @package Brainy
  7. * @subpackage Config
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Smarty Internal Plugin Configfilelexer
  12. */
  13. class Smarty_Internal_Configfilelexer
  14. {
  15. public $data;
  16. public $counter;
  17. public $token;
  18. public $value;
  19. public $node;
  20. public $line;
  21. private $state = 1;
  22. public $yyTraceFILE;
  23. public $yyTracePrompt;
  24. public $state_name = array (1 => 'START', 2 => 'VALUE', 3 => 'NAKED_STRING_VALUE', 4 => 'COMMENT', 5 => 'SECTION', 6 => 'TRIPPLE');
  25. public $smarty_token_names = array ( // Text for parser error messages
  26. );
  27. function __construct($data, $smarty)
  28. {
  29. // set instance object
  30. self::instance($this);
  31. $this->data = $data . "\n"; //now all lines are \n-terminated
  32. $this->counter = 0;
  33. $this->line = 1;
  34. $this->smarty = $smarty;
  35. $this->mbstring_overload = ini_get('mbstring.func_overload') & 2;
  36. }
  37. public static function &instance($new_instance = null)
  38. {
  39. static $instance = null;
  40. if (isset($new_instance) && is_object($new_instance))
  41. $instance = $new_instance;
  42. return $instance;
  43. }
  44. public function PrintTrace()
  45. {
  46. $this->yyTraceFILE = fopen('php://output', 'w');
  47. $this->yyTracePrompt = '<br>';
  48. }
  49. private $_yy_state = 1;
  50. private $_yy_stack = array();
  51. public function yylex()
  52. {
  53. return $this->{'yylex' . $this->_yy_state}();
  54. }
  55. public function yypushstate($state)
  56. {
  57. if ($this->yyTraceFILE) {
  58. fprintf($this->yyTraceFILE, "%sState push %s\n", $this->yyTracePrompt, isset($this->state_name[$this->_yy_state]) ? $this->state_name[$this->_yy_state] : $this->_yy_state);
  59. }
  60. array_push($this->_yy_stack, $this->_yy_state);
  61. $this->_yy_state = $state;
  62. if ($this->yyTraceFILE) {
  63. fprintf($this->yyTraceFILE, "%snew State %s\n", $this->yyTracePrompt, isset($this->state_name[$this->_yy_state]) ? $this->state_name[$this->_yy_state] : $this->_yy_state);
  64. }
  65. }
  66. public function yypopstate()
  67. {
  68. if ($this->yyTraceFILE) {
  69. fprintf($this->yyTraceFILE, "%sState pop %s\n", $this->yyTracePrompt, isset($this->state_name[$this->_yy_state]) ? $this->state_name[$this->_yy_state] : $this->_yy_state);
  70. }
  71. $this->_yy_state = array_pop($this->_yy_stack);
  72. if ($this->yyTraceFILE) {
  73. fprintf($this->yyTraceFILE, "%snew State %s\n", $this->yyTracePrompt, isset($this->state_name[$this->_yy_state]) ? $this->state_name[$this->_yy_state] : $this->_yy_state);
  74. }
  75. }
  76. public function yybegin($state)
  77. {
  78. $this->_yy_state = $state;
  79. if ($this->yyTraceFILE) {
  80. fprintf($this->yyTraceFILE, "%sState set %s\n", $this->yyTracePrompt, isset($this->state_name[$this->_yy_state]) ? $this->state_name[$this->_yy_state] : $this->_yy_state);
  81. }
  82. }
  83. public function yylex1()
  84. {
  85. $tokenMap = array (
  86. 1 => 0,
  87. 2 => 0,
  88. 3 => 0,
  89. 4 => 0,
  90. 5 => 0,
  91. 6 => 0,
  92. 7 => 0,
  93. 8 => 0,
  94. );
  95. if ($this->counter >= strlen($this->data)) {
  96. return false; // end of input
  97. }
  98. $yy_global_pattern = "/\G(#|;)|\G(\\[)|\G(\\])|\G(=)|\G([ \t\r]+)|\G(\n)|\G([0-9]*[a-zA-Z_]\\w*)|\G([\S\s])/iS";
  99. do {
  100. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  101. $yysubmatches = $yymatches;
  102. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  103. if (!count($yymatches)) {
  104. throw new Exception('Error: lexing failed because a rule matched' .
  105. ' an empty string. Input "' . substr($this->data,
  106. $this->counter, 5) . '... state START');
  107. }
  108. next($yymatches); // skip global match
  109. $this->token = key($yymatches); // token number
  110. if ($tokenMap[$this->token]) {
  111. // extract sub-patterns for passing to lex function
  112. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  113. $tokenMap[$this->token]);
  114. } else {
  115. $yysubmatches = array();
  116. }
  117. $this->value = current($yymatches); // token value
  118. $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
  119. if ($r === null) {
  120. $this->counter += strlen($this->value);
  121. $this->line += substr_count($this->value, "\n");
  122. // accept this token
  123. return true;
  124. } elseif ($r === true) {
  125. // we have changed state
  126. // process this token in the new state
  127. return $this->yylex();
  128. } elseif ($r === false) {
  129. $this->counter += strlen($this->value);
  130. $this->line += substr_count($this->value, "\n");
  131. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  132. return false; // end of input
  133. }
  134. // skip this token
  135. continue;
  136. } } else {
  137. throw new Exception('Unexpected input at line' . $this->line .
  138. ': ' . $this->data[$this->counter]);
  139. }
  140. break;
  141. } while (true);
  142. } // end function
  143. const START = 1;
  144. function yy_r1_1($yy_subpatterns)
  145. {
  146. $this->token = Smarty_Internal_Configfileparser::TPC_COMMENTSTART;
  147. $this->yypushstate(self::COMMENT);
  148. }
  149. function yy_r1_2($yy_subpatterns)
  150. {
  151. $this->token = Smarty_Internal_Configfileparser::TPC_OPENB;
  152. $this->yypushstate(self::SECTION);
  153. }
  154. function yy_r1_3($yy_subpatterns)
  155. {
  156. $this->token = Smarty_Internal_Configfileparser::TPC_CLOSEB;
  157. }
  158. function yy_r1_4($yy_subpatterns)
  159. {
  160. $this->token = Smarty_Internal_Configfileparser::TPC_EQUAL;
  161. $this->yypushstate(self::VALUE);
  162. }
  163. function yy_r1_5($yy_subpatterns)
  164. {
  165. return false;
  166. }
  167. function yy_r1_6($yy_subpatterns)
  168. {
  169. $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
  170. }
  171. function yy_r1_7($yy_subpatterns)
  172. {
  173. $this->token = Smarty_Internal_Configfileparser::TPC_ID;
  174. }
  175. function yy_r1_8($yy_subpatterns)
  176. {
  177. $this->token = Smarty_Internal_Configfileparser::TPC_OTHER;
  178. }
  179. public function yylex2()
  180. {
  181. $tokenMap = array (
  182. 1 => 0,
  183. 2 => 0,
  184. 3 => 0,
  185. 4 => 0,
  186. 5 => 0,
  187. 6 => 0,
  188. 7 => 0,
  189. 8 => 0,
  190. 9 => 0,
  191. );
  192. if ($this->counter >= strlen($this->data)) {
  193. return false; // end of input
  194. }
  195. $yy_global_pattern = "/\G([ \t\r]+)|\G(\\d+\\.\\d+(?=[ \t\r]*[\n#;]))|\G(\\d+(?=[ \t\r]*[\n#;]))|\G(\"\"\")|\G('[^'\\\\]*(?:\\\\.[^'\\\\]*)*'(?=[ \t\r]*[\n#;]))|\G(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"(?=[ \t\r]*[\n#;]))|\G([a-zA-Z]+(?=[ \t\r]*[\n#;]))|\G([^\n]+?(?=[ \t\r]*\n))|\G(\n)/iS";
  196. do {
  197. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  198. $yysubmatches = $yymatches;
  199. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  200. if (!count($yymatches)) {
  201. throw new Exception('Error: lexing failed because a rule matched' .
  202. ' an empty string. Input "' . substr($this->data,
  203. $this->counter, 5) . '... state VALUE');
  204. }
  205. next($yymatches); // skip global match
  206. $this->token = key($yymatches); // token number
  207. if ($tokenMap[$this->token]) {
  208. // extract sub-patterns for passing to lex function
  209. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  210. $tokenMap[$this->token]);
  211. } else {
  212. $yysubmatches = array();
  213. }
  214. $this->value = current($yymatches); // token value
  215. $r = $this->{'yy_r2_' . $this->token}($yysubmatches);
  216. if ($r === null) {
  217. $this->counter += strlen($this->value);
  218. $this->line += substr_count($this->value, "\n");
  219. // accept this token
  220. return true;
  221. } elseif ($r === true) {
  222. // we have changed state
  223. // process this token in the new state
  224. return $this->yylex();
  225. } elseif ($r === false) {
  226. $this->counter += strlen($this->value);
  227. $this->line += substr_count($this->value, "\n");
  228. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  229. return false; // end of input
  230. }
  231. // skip this token
  232. continue;
  233. } } else {
  234. throw new Exception('Unexpected input at line' . $this->line .
  235. ': ' . $this->data[$this->counter]);
  236. }
  237. break;
  238. } while (true);
  239. } // end function
  240. const VALUE = 2;
  241. function yy_r2_1($yy_subpatterns)
  242. {
  243. return false;
  244. }
  245. function yy_r2_2($yy_subpatterns)
  246. {
  247. $this->token = Smarty_Internal_Configfileparser::TPC_FLOAT;
  248. $this->yypopstate();
  249. }
  250. function yy_r2_3($yy_subpatterns)
  251. {
  252. $this->token = Smarty_Internal_Configfileparser::TPC_INT;
  253. $this->yypopstate();
  254. }
  255. function yy_r2_4($yy_subpatterns)
  256. {
  257. $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_QUOTES;
  258. $this->yypushstate(self::TRIPPLE);
  259. }
  260. function yy_r2_5($yy_subpatterns)
  261. {
  262. $this->token = Smarty_Internal_Configfileparser::TPC_SINGLE_QUOTED_STRING;
  263. $this->yypopstate();
  264. }
  265. function yy_r2_6($yy_subpatterns)
  266. {
  267. $this->token = Smarty_Internal_Configfileparser::TPC_DOUBLE_QUOTED_STRING;
  268. $this->yypopstate();
  269. }
  270. function yy_r2_7($yy_subpatterns)
  271. {
  272. if (!$this->smarty->config_booleanize || !in_array(strtolower($this->value), Array("true", "false", "on", "off", "yes", "no")) ) {
  273. $this->yypopstate();
  274. $this->yypushstate(self::NAKED_STRING_VALUE);
  275. return true; //reprocess in new state
  276. } else {
  277. $this->token = Smarty_Internal_Configfileparser::TPC_BOOL;
  278. $this->yypopstate();
  279. }
  280. }
  281. function yy_r2_8($yy_subpatterns)
  282. {
  283. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  284. $this->yypopstate();
  285. }
  286. function yy_r2_9($yy_subpatterns)
  287. {
  288. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  289. $this->value = "";
  290. $this->yypopstate();
  291. }
  292. public function yylex3()
  293. {
  294. $tokenMap = array (
  295. 1 => 0,
  296. );
  297. if ($this->counter >= strlen($this->data)) {
  298. return false; // end of input
  299. }
  300. $yy_global_pattern = "/\G([^\n]+?(?=[ \t\r]*\n))/iS";
  301. do {
  302. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  303. $yysubmatches = $yymatches;
  304. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  305. if (!count($yymatches)) {
  306. throw new Exception('Error: lexing failed because a rule matched' .
  307. ' an empty string. Input "' . substr($this->data,
  308. $this->counter, 5) . '... state NAKED_STRING_VALUE');
  309. }
  310. next($yymatches); // skip global match
  311. $this->token = key($yymatches); // token number
  312. if ($tokenMap[$this->token]) {
  313. // extract sub-patterns for passing to lex function
  314. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  315. $tokenMap[$this->token]);
  316. } else {
  317. $yysubmatches = array();
  318. }
  319. $this->value = current($yymatches); // token value
  320. $r = $this->{'yy_r3_' . $this->token}($yysubmatches);
  321. if ($r === null) {
  322. $this->counter += strlen($this->value);
  323. $this->line += substr_count($this->value, "\n");
  324. // accept this token
  325. return true;
  326. } elseif ($r === true) {
  327. // we have changed state
  328. // process this token in the new state
  329. return $this->yylex();
  330. } elseif ($r === false) {
  331. $this->counter += strlen($this->value);
  332. $this->line += substr_count($this->value, "\n");
  333. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  334. return false; // end of input
  335. }
  336. // skip this token
  337. continue;
  338. } } else {
  339. throw new Exception('Unexpected input at line' . $this->line .
  340. ': ' . $this->data[$this->counter]);
  341. }
  342. break;
  343. } while (true);
  344. } // end function
  345. const NAKED_STRING_VALUE = 3;
  346. function yy_r3_1($yy_subpatterns)
  347. {
  348. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  349. $this->yypopstate();
  350. }
  351. public function yylex4()
  352. {
  353. $tokenMap = array (
  354. 1 => 0,
  355. 2 => 0,
  356. 3 => 0,
  357. );
  358. if ($this->counter >= strlen($this->data)) {
  359. return false; // end of input
  360. }
  361. $yy_global_pattern = "/\G([ \t\r]+)|\G([^\n]+?(?=[ \t\r]*\n))|\G(\n)/iS";
  362. do {
  363. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  364. $yysubmatches = $yymatches;
  365. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  366. if (!count($yymatches)) {
  367. throw new Exception('Error: lexing failed because a rule matched' .
  368. ' an empty string. Input "' . substr($this->data,
  369. $this->counter, 5) . '... state COMMENT');
  370. }
  371. next($yymatches); // skip global match
  372. $this->token = key($yymatches); // token number
  373. if ($tokenMap[$this->token]) {
  374. // extract sub-patterns for passing to lex function
  375. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  376. $tokenMap[$this->token]);
  377. } else {
  378. $yysubmatches = array();
  379. }
  380. $this->value = current($yymatches); // token value
  381. $r = $this->{'yy_r4_' . $this->token}($yysubmatches);
  382. if ($r === null) {
  383. $this->counter += strlen($this->value);
  384. $this->line += substr_count($this->value, "\n");
  385. // accept this token
  386. return true;
  387. } elseif ($r === true) {
  388. // we have changed state
  389. // process this token in the new state
  390. return $this->yylex();
  391. } elseif ($r === false) {
  392. $this->counter += strlen($this->value);
  393. $this->line += substr_count($this->value, "\n");
  394. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  395. return false; // end of input
  396. }
  397. // skip this token
  398. continue;
  399. } } else {
  400. throw new Exception('Unexpected input at line' . $this->line .
  401. ': ' . $this->data[$this->counter]);
  402. }
  403. break;
  404. } while (true);
  405. } // end function
  406. const COMMENT = 4;
  407. function yy_r4_1($yy_subpatterns)
  408. {
  409. return false;
  410. }
  411. function yy_r4_2($yy_subpatterns)
  412. {
  413. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  414. }
  415. function yy_r4_3($yy_subpatterns)
  416. {
  417. $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
  418. $this->yypopstate();
  419. }
  420. public function yylex5()
  421. {
  422. $tokenMap = array (
  423. 1 => 0,
  424. 2 => 0,
  425. );
  426. if ($this->counter >= strlen($this->data)) {
  427. return false; // end of input
  428. }
  429. $yy_global_pattern = "/\G(\\.)|\G(.*?(?=[\.=[\]\r\n]))/iS";
  430. do {
  431. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  432. $yysubmatches = $yymatches;
  433. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  434. if (!count($yymatches)) {
  435. throw new Exception('Error: lexing failed because a rule matched' .
  436. ' an empty string. Input "' . substr($this->data,
  437. $this->counter, 5) . '... state SECTION');
  438. }
  439. next($yymatches); // skip global match
  440. $this->token = key($yymatches); // token number
  441. if ($tokenMap[$this->token]) {
  442. // extract sub-patterns for passing to lex function
  443. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  444. $tokenMap[$this->token]);
  445. } else {
  446. $yysubmatches = array();
  447. }
  448. $this->value = current($yymatches); // token value
  449. $r = $this->{'yy_r5_' . $this->token}($yysubmatches);
  450. if ($r === null) {
  451. $this->counter += strlen($this->value);
  452. $this->line += substr_count($this->value, "\n");
  453. // accept this token
  454. return true;
  455. } elseif ($r === true) {
  456. // we have changed state
  457. // process this token in the new state
  458. return $this->yylex();
  459. } elseif ($r === false) {
  460. $this->counter += strlen($this->value);
  461. $this->line += substr_count($this->value, "\n");
  462. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  463. return false; // end of input
  464. }
  465. // skip this token
  466. continue;
  467. } } else {
  468. throw new Exception('Unexpected input at line' . $this->line .
  469. ': ' . $this->data[$this->counter]);
  470. }
  471. break;
  472. } while (true);
  473. } // end function
  474. const SECTION = 5;
  475. function yy_r5_1($yy_subpatterns)
  476. {
  477. $this->token = Smarty_Internal_Configfileparser::TPC_DOT;
  478. }
  479. function yy_r5_2($yy_subpatterns)
  480. {
  481. $this->token = Smarty_Internal_Configfileparser::TPC_SECTION;
  482. $this->yypopstate();
  483. }
  484. public function yylex6()
  485. {
  486. $tokenMap = array (
  487. 1 => 0,
  488. 2 => 0,
  489. );
  490. if ($this->counter >= strlen($this->data)) {
  491. return false; // end of input
  492. }
  493. $yy_global_pattern = "/\G(\"\"\"(?=[ \t\r]*[\n#;]))|\G([\S\s])/iS";
  494. do {
  495. if (preg_match($yy_global_pattern,$this->data, $yymatches, null, $this->counter)) {
  496. $yysubmatches = $yymatches;
  497. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  498. if (!count($yymatches)) {
  499. throw new Exception('Error: lexing failed because a rule matched' .
  500. ' an empty string. Input "' . substr($this->data,
  501. $this->counter, 5) . '... state TRIPPLE');
  502. }
  503. next($yymatches); // skip global match
  504. $this->token = key($yymatches); // token number
  505. if ($tokenMap[$this->token]) {
  506. // extract sub-patterns for passing to lex function
  507. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  508. $tokenMap[$this->token]);
  509. } else {
  510. $yysubmatches = array();
  511. }
  512. $this->value = current($yymatches); // token value
  513. $r = $this->{'yy_r6_' . $this->token}($yysubmatches);
  514. if ($r === null) {
  515. $this->counter += strlen($this->value);
  516. $this->line += substr_count($this->value, "\n");
  517. // accept this token
  518. return true;
  519. } elseif ($r === true) {
  520. // we have changed state
  521. // process this token in the new state
  522. return $this->yylex();
  523. } elseif ($r === false) {
  524. $this->counter += strlen($this->value);
  525. $this->line += substr_count($this->value, "\n");
  526. if ($this->counter >= ($this->mbstring_overload ? mb_strlen($this->data,'latin1'): strlen($this->data))) {
  527. return false; // end of input
  528. }
  529. // skip this token
  530. continue;
  531. } } else {
  532. throw new Exception('Unexpected input at line' . $this->line .
  533. ': ' . $this->data[$this->counter]);
  534. }
  535. break;
  536. } while (true);
  537. } // end function
  538. const TRIPPLE = 6;
  539. function yy_r6_1($yy_subpatterns)
  540. {
  541. $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_QUOTES_END;
  542. $this->yypopstate();
  543. $this->yypushstate(self::START);
  544. }
  545. function yy_r6_2($yy_subpatterns)
  546. {
  547. if ($this->mbstring_overload) {
  548. $to = mb_strlen($this->data,'latin1');
  549. } else {
  550. $to = strlen($this->data);
  551. }
  552. preg_match("/\"\"\"[ \t\r]*[\n#;]/",$this->data,$match,PREG_OFFSET_CAPTURE,$this->counter);
  553. if (isset($match[0][1])) {
  554. $to = $match[0][1];
  555. } else {
  556. $this->compiler->trigger_template_error ("missing or misspelled literal closing tag");
  557. }
  558. if ($this->mbstring_overload) {
  559. $this->value = mb_substr($this->data,$this->counter,$to-$this->counter,'latin1');
  560. } else {
  561. $this->value = substr($this->data,$this->counter,$to-$this->counter);
  562. }
  563. $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_TEXT;
  564. }
  565. }