/thirdparts/smarty/sysplugins/smarty_internal_configfilelexer.php

https://github.com/laurentc/NApf · PHP · 548 lines · 424 code · 87 blank · 37 comment · 50 complexity · cc2463005ca979a362da787c93f2f973 MD5 · raw file

  1. <?php
  2. /**
  3. * Smarty Internal Plugin Configfilelexer
  4. *
  5. * This is the lexer to break the config common source into tokens
  6. * @package Smarty
  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 $smarty_token_names = array( // Text for parser error messages
  23. );
  24. function __construct($data, $smarty)
  25. {
  26. // set instance object
  27. self::instance($this);
  28. $this->data = $data . "\n"; //now all lines are \n-terminated
  29. $this->counter = 0;
  30. $this->line = 1;
  31. $this->smarty = $smarty;
  32. }
  33. public static function &instance($new_instance = null)
  34. {
  35. static $instance = null;
  36. if (isset($new_instance) && is_object($new_instance))
  37. $instance = $new_instance;
  38. return $instance;
  39. }
  40. private $_yy_state = 1;
  41. private $_yy_stack = array();
  42. function yylex()
  43. {
  44. return $this->{'yylex' . $this->_yy_state}();
  45. }
  46. function yypushstate($state)
  47. {
  48. array_push($this->_yy_stack, $this->_yy_state);
  49. $this->_yy_state = $state;
  50. }
  51. function yypopstate()
  52. {
  53. $this->_yy_state = array_pop($this->_yy_stack);
  54. }
  55. function yybegin($state)
  56. {
  57. $this->_yy_state = $state;
  58. }
  59. function yylex1()
  60. {
  61. $tokenMap = array(
  62. 1 => 0,
  63. 2 => 0,
  64. 3 => 0,
  65. 4 => 0,
  66. 5 => 0,
  67. 6 => 0,
  68. 7 => 0,
  69. );
  70. if ($this->counter >= strlen($this->data)) {
  71. return false; // end of input
  72. }
  73. $yy_global_pattern = "/^(#)|^(\\[)|^(\\])|^(=)|^([ \t\r]+)|^(\n)|^([0-9]*[a-zA-Z_]\\w*)/iS";
  74. do {
  75. if (preg_match($yy_global_pattern, substr($this->data, $this->counter), $yymatches)) {
  76. $yysubmatches = $yymatches;
  77. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  78. if (!count($yymatches)) {
  79. throw new Exception('Error: lexing failed because a rule matched' .
  80. 'an empty string. Input "' . substr($this->data,
  81. $this->counter, 5) . '... state START');
  82. }
  83. next($yymatches); // skip global match
  84. $this->token = key($yymatches); // token number
  85. if ($tokenMap[$this->token]) {
  86. // extract sub-patterns for passing to lex function
  87. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  88. $tokenMap[$this->token]);
  89. } else {
  90. $yysubmatches = array();
  91. }
  92. $this->value = current($yymatches); // token value
  93. $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
  94. if ($r === null) {
  95. $this->counter += strlen($this->value);
  96. $this->line += substr_count($this->value, "\n");
  97. // accept this token
  98. return true;
  99. } elseif ($r === true) {
  100. // we have changed state
  101. // process this token in the new state
  102. return $this->yylex();
  103. } elseif ($r === false) {
  104. $this->counter += strlen($this->value);
  105. $this->line += substr_count($this->value, "\n");
  106. if ($this->counter >= strlen($this->data)) {
  107. return false; // end of input
  108. }
  109. // skip this token
  110. continue;
  111. }
  112. } else {
  113. throw new Exception('Unexpected input at line' . $this->line .
  114. ': ' . $this->data[$this->counter]);
  115. }
  116. break;
  117. } while (true);
  118. } // end function
  119. const START = 1;
  120. function yy_r1_1($yy_subpatterns)
  121. {
  122. $this->token = Smarty_Internal_Configfileparser::TPC_COMMENTSTART;
  123. $this->yypushstate(self::COMMENT);
  124. }
  125. function yy_r1_2($yy_subpatterns)
  126. {
  127. $this->token = Smarty_Internal_Configfileparser::TPC_OPENB;
  128. $this->yypushstate(self::SECTION);
  129. }
  130. function yy_r1_3($yy_subpatterns)
  131. {
  132. $this->token = Smarty_Internal_Configfileparser::TPC_CLOSEB;
  133. }
  134. function yy_r1_4($yy_subpatterns)
  135. {
  136. $this->token = Smarty_Internal_Configfileparser::TPC_EQUAL;
  137. $this->yypushstate(self::VALUE);
  138. }
  139. function yy_r1_5($yy_subpatterns)
  140. {
  141. return false;
  142. }
  143. function yy_r1_6($yy_subpatterns)
  144. {
  145. $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
  146. }
  147. function yy_r1_7($yy_subpatterns)
  148. {
  149. $this->token = Smarty_Internal_Configfileparser::TPC_ID;
  150. }
  151. function yylex2()
  152. {
  153. $tokenMap = array(
  154. 1 => 0,
  155. 2 => 0,
  156. 3 => 0,
  157. 4 => 0,
  158. 5 => 0,
  159. 6 => 1,
  160. 8 => 0,
  161. 9 => 0,
  162. 10 => 0,
  163. );
  164. if ($this->counter >= strlen($this->data)) {
  165. return false; // end of input
  166. }
  167. $yy_global_pattern = "/^([ \t\r]+)|^(\\d+\\.\\d+(?=[ \t\r]*[\n#]))|^(\\d+(?=[ \t\r]*[\n#]))|^('[^'\\\\]*(?:\\\\.[^'\\\\]*)*'(?=[ \t\r]*[\n#]))|^(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"(?=[ \t\r]*[\n#]))|^(\"\"\"([^\"]|\\\\\"|\"{1,2}[^\"])*\"\"\"(?=[ \t\r]*[\n#]))|^([a-zA-Z]+(?=[ \t\r]*[\n#]))|^([^\n]+?(?=[ \t\r]*\n))|^(\n)/iS";
  168. do {
  169. if (preg_match($yy_global_pattern, substr($this->data, $this->counter), $yymatches)) {
  170. $yysubmatches = $yymatches;
  171. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  172. if (!count($yymatches)) {
  173. throw new Exception('Error: lexing failed because a rule matched' .
  174. 'an empty string. Input "' . substr($this->data,
  175. $this->counter, 5) . '... state VALUE');
  176. }
  177. next($yymatches); // skip global match
  178. $this->token = key($yymatches); // token number
  179. if ($tokenMap[$this->token]) {
  180. // extract sub-patterns for passing to lex function
  181. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  182. $tokenMap[$this->token]);
  183. } else {
  184. $yysubmatches = array();
  185. }
  186. $this->value = current($yymatches); // token value
  187. $r = $this->{'yy_r2_' . $this->token}($yysubmatches);
  188. if ($r === null) {
  189. $this->counter += strlen($this->value);
  190. $this->line += substr_count($this->value, "\n");
  191. // accept this token
  192. return true;
  193. } elseif ($r === true) {
  194. // we have changed state
  195. // process this token in the new state
  196. return $this->yylex();
  197. } elseif ($r === false) {
  198. $this->counter += strlen($this->value);
  199. $this->line += substr_count($this->value, "\n");
  200. if ($this->counter >= strlen($this->data)) {
  201. return false; // end of input
  202. }
  203. // skip this token
  204. continue;
  205. }
  206. } else {
  207. throw new Exception('Unexpected input at line' . $this->line .
  208. ': ' . $this->data[$this->counter]);
  209. }
  210. break;
  211. } while (true);
  212. } // end function
  213. const VALUE = 2;
  214. function yy_r2_1($yy_subpatterns)
  215. {
  216. return false;
  217. }
  218. function yy_r2_2($yy_subpatterns)
  219. {
  220. $this->token = Smarty_Internal_Configfileparser::TPC_FLOAT;
  221. $this->yypopstate();
  222. }
  223. function yy_r2_3($yy_subpatterns)
  224. {
  225. $this->token = Smarty_Internal_Configfileparser::TPC_INT;
  226. $this->yypopstate();
  227. }
  228. function yy_r2_4($yy_subpatterns)
  229. {
  230. $this->token = Smarty_Internal_Configfileparser::TPC_SINGLE_QUOTED_STRING;
  231. $this->yypopstate();
  232. }
  233. function yy_r2_5($yy_subpatterns)
  234. {
  235. $this->token = Smarty_Internal_Configfileparser::TPC_DOUBLE_QUOTED_STRING;
  236. $this->yypopstate();
  237. }
  238. function yy_r2_6($yy_subpatterns)
  239. {
  240. $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_DOUBLE_QUOTED_STRING;
  241. $this->yypopstate();
  242. }
  243. function yy_r2_8($yy_subpatterns)
  244. {
  245. if (!$this->smarty->config_booleanize || !in_array(strtolower($this->value), Array("true", "false", "on", "off", "yes", "no"))) {
  246. $this->yypopstate();
  247. $this->yypushstate(self::NAKED_STRING_VALUE);
  248. return true; //reprocess in new state
  249. } else {
  250. $this->token = Smarty_Internal_Configfileparser::TPC_BOOL;
  251. $this->yypopstate();
  252. }
  253. }
  254. function yy_r2_9($yy_subpatterns)
  255. {
  256. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  257. $this->yypopstate();
  258. }
  259. function yy_r2_10($yy_subpatterns)
  260. {
  261. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  262. $this->value = "";
  263. $this->yypopstate();
  264. }
  265. function yylex3()
  266. {
  267. $tokenMap = array(
  268. 1 => 0,
  269. );
  270. if ($this->counter >= strlen($this->data)) {
  271. return false; // end of input
  272. }
  273. $yy_global_pattern = "/^([^\n]+?(?=[ \t\r]*\n))/iS";
  274. do {
  275. if (preg_match($yy_global_pattern, substr($this->data, $this->counter), $yymatches)) {
  276. $yysubmatches = $yymatches;
  277. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  278. if (!count($yymatches)) {
  279. throw new Exception('Error: lexing failed because a rule matched' .
  280. 'an empty string. Input "' . substr($this->data,
  281. $this->counter, 5) . '... state NAKED_STRING_VALUE');
  282. }
  283. next($yymatches); // skip global match
  284. $this->token = key($yymatches); // token number
  285. if ($tokenMap[$this->token]) {
  286. // extract sub-patterns for passing to lex function
  287. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  288. $tokenMap[$this->token]);
  289. } else {
  290. $yysubmatches = array();
  291. }
  292. $this->value = current($yymatches); // token value
  293. $r = $this->{'yy_r3_' . $this->token}($yysubmatches);
  294. if ($r === null) {
  295. $this->counter += strlen($this->value);
  296. $this->line += substr_count($this->value, "\n");
  297. // accept this token
  298. return true;
  299. } elseif ($r === true) {
  300. // we have changed state
  301. // process this token in the new state
  302. return $this->yylex();
  303. } elseif ($r === false) {
  304. $this->counter += strlen($this->value);
  305. $this->line += substr_count($this->value, "\n");
  306. if ($this->counter >= strlen($this->data)) {
  307. return false; // end of input
  308. }
  309. // skip this token
  310. continue;
  311. }
  312. } else {
  313. throw new Exception('Unexpected input at line' . $this->line .
  314. ': ' . $this->data[$this->counter]);
  315. }
  316. break;
  317. } while (true);
  318. } // end function
  319. const NAKED_STRING_VALUE = 3;
  320. function yy_r3_1($yy_subpatterns)
  321. {
  322. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  323. $this->yypopstate();
  324. }
  325. function yylex4()
  326. {
  327. $tokenMap = array(
  328. 1 => 0,
  329. 2 => 0,
  330. 3 => 0,
  331. );
  332. if ($this->counter >= strlen($this->data)) {
  333. return false; // end of input
  334. }
  335. $yy_global_pattern = "/^([ \t\r]+)|^([^\n]+?(?=[ \t\r]*\n))|^(\n)/iS";
  336. do {
  337. if (preg_match($yy_global_pattern, substr($this->data, $this->counter), $yymatches)) {
  338. $yysubmatches = $yymatches;
  339. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  340. if (!count($yymatches)) {
  341. throw new Exception('Error: lexing failed because a rule matched' .
  342. 'an empty string. Input "' . substr($this->data,
  343. $this->counter, 5) . '... state COMMENT');
  344. }
  345. next($yymatches); // skip global match
  346. $this->token = key($yymatches); // token number
  347. if ($tokenMap[$this->token]) {
  348. // extract sub-patterns for passing to lex function
  349. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  350. $tokenMap[$this->token]);
  351. } else {
  352. $yysubmatches = array();
  353. }
  354. $this->value = current($yymatches); // token value
  355. $r = $this->{'yy_r4_' . $this->token}($yysubmatches);
  356. if ($r === null) {
  357. $this->counter += strlen($this->value);
  358. $this->line += substr_count($this->value, "\n");
  359. // accept this token
  360. return true;
  361. } elseif ($r === true) {
  362. // we have changed state
  363. // process this token in the new state
  364. return $this->yylex();
  365. } elseif ($r === false) {
  366. $this->counter += strlen($this->value);
  367. $this->line += substr_count($this->value, "\n");
  368. if ($this->counter >= strlen($this->data)) {
  369. return false; // end of input
  370. }
  371. // skip this token
  372. continue;
  373. }
  374. } else {
  375. throw new Exception('Unexpected input at line' . $this->line .
  376. ': ' . $this->data[$this->counter]);
  377. }
  378. break;
  379. } while (true);
  380. } // end function
  381. const COMMENT = 4;
  382. function yy_r4_1($yy_subpatterns)
  383. {
  384. return false;
  385. }
  386. function yy_r4_2($yy_subpatterns)
  387. {
  388. $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
  389. }
  390. function yy_r4_3($yy_subpatterns)
  391. {
  392. $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
  393. $this->yypopstate();
  394. }
  395. function yylex5()
  396. {
  397. $tokenMap = array(
  398. 1 => 0,
  399. 2 => 0,
  400. );
  401. if ($this->counter >= strlen($this->data)) {
  402. return false; // end of input
  403. }
  404. $yy_global_pattern = "/^(\\.)|^(.*?(?=[\.=[\]\r\n]))/iS";
  405. do {
  406. if (preg_match($yy_global_pattern, substr($this->data, $this->counter), $yymatches)) {
  407. $yysubmatches = $yymatches;
  408. $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
  409. if (!count($yymatches)) {
  410. throw new Exception('Error: lexing failed because a rule matched' .
  411. 'an empty string. Input "' . substr($this->data,
  412. $this->counter, 5) . '... state SECTION');
  413. }
  414. next($yymatches); // skip global match
  415. $this->token = key($yymatches); // token number
  416. if ($tokenMap[$this->token]) {
  417. // extract sub-patterns for passing to lex function
  418. $yysubmatches = array_slice($yysubmatches, $this->token + 1,
  419. $tokenMap[$this->token]);
  420. } else {
  421. $yysubmatches = array();
  422. }
  423. $this->value = current($yymatches); // token value
  424. $r = $this->{'yy_r5_' . $this->token}($yysubmatches);
  425. if ($r === null) {
  426. $this->counter += strlen($this->value);
  427. $this->line += substr_count($this->value, "\n");
  428. // accept this token
  429. return true;
  430. } elseif ($r === true) {
  431. // we have changed state
  432. // process this token in the new state
  433. return $this->yylex();
  434. } elseif ($r === false) {
  435. $this->counter += strlen($this->value);
  436. $this->line += substr_count($this->value, "\n");
  437. if ($this->counter >= strlen($this->data)) {
  438. return false; // end of input
  439. }
  440. // skip this token
  441. continue;
  442. }
  443. } else {
  444. throw new Exception('Unexpected input at line' . $this->line .
  445. ': ' . $this->data[$this->counter]);
  446. }
  447. break;
  448. } while (true);
  449. } // end function
  450. const SECTION = 5;
  451. function yy_r5_1($yy_subpatterns)
  452. {
  453. $this->token = Smarty_Internal_Configfileparser::TPC_DOT;
  454. }
  455. function yy_r5_2($yy_subpatterns)
  456. {
  457. $this->token = Smarty_Internal_Configfileparser::TPC_SECTION;
  458. $this->yypopstate();
  459. }
  460. }
  461. ?>