3 * Smarty Internal Plugin Configfilelexer
4 * This is the lexer to break the config file source into tokens
12 * Smarty Internal Plugin Configfilelexer
14 class Smarty_Internal_Configfilelexer
25 public $yyTracePrompt;
26 public $state_name = array(1 => 'START', 2 => 'VALUE', 3 => 'NAKED_STRING_VALUE', 4 => 'COMMENT', 5 => 'SECTION', 6 => 'TRIPPLE');
27 public $smarty_token_names = array( // Text for parser error messages
30 function __construct($data, $compiler)
32 // set instance object
33 self::instance($this);
34 $this->data = $data . "\n"; //now all lines are \n-terminated
37 $this->compiler = $compiler;
38 $this->smarty = $compiler->smarty;
41 public static function &instance($new_instance = null)
43 static $instance = null;
44 if (isset($new_instance) && is_object($new_instance)) {
45 $instance = $new_instance;
50 public function PrintTrace()
52 $this->yyTraceFILE = fopen('php://output', 'w');
53 $this->yyTracePrompt = '<br>';
56 private $_yy_state = 1;
57 private $_yy_stack = array();
59 public function yylex()
61 return $this->{'yylex' . $this->_yy_state}();
64 public function yypushstate($state)
66 if ($this->yyTraceFILE) {
67 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);
69 array_push($this->_yy_stack, $this->_yy_state);
70 $this->_yy_state = $state;
71 if ($this->yyTraceFILE) {
72 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);
76 public function yypopstate()
78 if ($this->yyTraceFILE) {
79 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);
81 $this->_yy_state = array_pop($this->_yy_stack);
82 if ($this->yyTraceFILE) {
83 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);
87 public function yybegin($state)
89 $this->_yy_state = $state;
90 if ($this->yyTraceFILE) {
91 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);
95 public function yylex1()
107 if ($this->counter >= strlen($this->data)) {
108 return false; // end of input
110 $yy_global_pattern = "/\G(#|;)|\G(\\[)|\G(\\])|\G(=)|\G([ \t\r]+)|\G(\n)|\G([0-9]*[a-zA-Z_]\\w*)|\G([\S\s])/iS";
113 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
114 $yysubmatches = $yymatches;
115 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
116 if (!count($yymatches)) {
117 throw new Exception('Error: lexing failed because a rule matched' .
118 ' an empty string. Input "' . substr($this->data,
119 $this->counter, 5) . '... state START');
121 next($yymatches); // skip global match
122 $this->token = key($yymatches); // token number
123 if ($tokenMap[$this->token]) {
124 // extract sub-patterns for passing to lex function
125 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
126 $tokenMap[$this->token]);
128 $yysubmatches = array();
130 $this->value = current($yymatches); // token value
131 $r = $this->{'yy_r1_' . $this->token}($yysubmatches);
133 $this->counter += strlen($this->value);
134 $this->line += substr_count($this->value, "\n");
137 } elseif ($r === true) {
138 // we have changed state
139 // process this token in the new state
140 return $this->yylex();
141 } elseif ($r === false) {
142 $this->counter += strlen($this->value);
143 $this->line += substr_count($this->value, "\n");
144 if ($this->counter >= strlen($this->data)) {
145 return false; // end of input
151 throw new Exception('Unexpected input at line' . $this->line .
152 ': ' . $this->data[$this->counter]);
160 function yy_r1_1($yy_subpatterns)
163 $this->token = Smarty_Internal_Configfileparser::TPC_COMMENTSTART;
164 $this->yypushstate(self::COMMENT);
167 function yy_r1_2($yy_subpatterns)
170 $this->token = Smarty_Internal_Configfileparser::TPC_OPENB;
171 $this->yypushstate(self::SECTION);
174 function yy_r1_3($yy_subpatterns)
177 $this->token = Smarty_Internal_Configfileparser::TPC_CLOSEB;
180 function yy_r1_4($yy_subpatterns)
183 $this->token = Smarty_Internal_Configfileparser::TPC_EQUAL;
184 $this->yypushstate(self::VALUE);
187 function yy_r1_5($yy_subpatterns)
193 function yy_r1_6($yy_subpatterns)
196 $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
199 function yy_r1_7($yy_subpatterns)
202 $this->token = Smarty_Internal_Configfileparser::TPC_ID;
205 function yy_r1_8($yy_subpatterns)
208 $this->token = Smarty_Internal_Configfileparser::TPC_OTHER;
211 public function yylex2()
224 if ($this->counter >= strlen($this->data)) {
225 return false; // end of input
227 $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";
230 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
231 $yysubmatches = $yymatches;
232 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
233 if (!count($yymatches)) {
234 throw new Exception('Error: lexing failed because a rule matched' .
235 ' an empty string. Input "' . substr($this->data,
236 $this->counter, 5) . '... state VALUE');
238 next($yymatches); // skip global match
239 $this->token = key($yymatches); // token number
240 if ($tokenMap[$this->token]) {
241 // extract sub-patterns for passing to lex function
242 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
243 $tokenMap[$this->token]);
245 $yysubmatches = array();
247 $this->value = current($yymatches); // token value
248 $r = $this->{'yy_r2_' . $this->token}($yysubmatches);
250 $this->counter += strlen($this->value);
251 $this->line += substr_count($this->value, "\n");
254 } elseif ($r === true) {
255 // we have changed state
256 // process this token in the new state
257 return $this->yylex();
258 } elseif ($r === false) {
259 $this->counter += strlen($this->value);
260 $this->line += substr_count($this->value, "\n");
261 if ($this->counter >= strlen($this->data)) {
262 return false; // end of input
268 throw new Exception('Unexpected input at line' . $this->line .
269 ': ' . $this->data[$this->counter]);
277 function yy_r2_1($yy_subpatterns)
283 function yy_r2_2($yy_subpatterns)
286 $this->token = Smarty_Internal_Configfileparser::TPC_FLOAT;
290 function yy_r2_3($yy_subpatterns)
293 $this->token = Smarty_Internal_Configfileparser::TPC_INT;
297 function yy_r2_4($yy_subpatterns)
300 $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_QUOTES;
301 $this->yypushstate(self::TRIPPLE);
304 function yy_r2_5($yy_subpatterns)
307 $this->token = Smarty_Internal_Configfileparser::TPC_SINGLE_QUOTED_STRING;
311 function yy_r2_6($yy_subpatterns)
314 $this->token = Smarty_Internal_Configfileparser::TPC_DOUBLE_QUOTED_STRING;
318 function yy_r2_7($yy_subpatterns)
321 if (!$this->smarty->config_booleanize || !in_array(strtolower($this->value), Array("true", "false", "on", "off", "yes", "no"))) {
323 $this->yypushstate(self::NAKED_STRING_VALUE);
324 return true; //reprocess in new state
326 $this->token = Smarty_Internal_Configfileparser::TPC_BOOL;
331 function yy_r2_8($yy_subpatterns)
334 $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
338 function yy_r2_9($yy_subpatterns)
341 $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
346 public function yylex3()
351 if ($this->counter >= strlen($this->data)) {
352 return false; // end of input
354 $yy_global_pattern = "/\G([^\n]+?(?=[ \t\r]*\n))/iS";
357 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
358 $yysubmatches = $yymatches;
359 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
360 if (!count($yymatches)) {
361 throw new Exception('Error: lexing failed because a rule matched' .
362 ' an empty string. Input "' . substr($this->data,
363 $this->counter, 5) . '... state NAKED_STRING_VALUE');
365 next($yymatches); // skip global match
366 $this->token = key($yymatches); // token number
367 if ($tokenMap[$this->token]) {
368 // extract sub-patterns for passing to lex function
369 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
370 $tokenMap[$this->token]);
372 $yysubmatches = array();
374 $this->value = current($yymatches); // token value
375 $r = $this->{'yy_r3_' . $this->token}($yysubmatches);
377 $this->counter += strlen($this->value);
378 $this->line += substr_count($this->value, "\n");
381 } elseif ($r === true) {
382 // we have changed state
383 // process this token in the new state
384 return $this->yylex();
385 } elseif ($r === false) {
386 $this->counter += strlen($this->value);
387 $this->line += substr_count($this->value, "\n");
388 if ($this->counter >= strlen($this->data)) {
389 return false; // end of input
395 throw new Exception('Unexpected input at line' . $this->line .
396 ': ' . $this->data[$this->counter]);
402 const NAKED_STRING_VALUE = 3;
404 function yy_r3_1($yy_subpatterns)
407 $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
411 public function yylex4()
418 if ($this->counter >= strlen($this->data)) {
419 return false; // end of input
421 $yy_global_pattern = "/\G([ \t\r]+)|\G([^\n]+?(?=[ \t\r]*\n))|\G(\n)/iS";
424 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
425 $yysubmatches = $yymatches;
426 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
427 if (!count($yymatches)) {
428 throw new Exception('Error: lexing failed because a rule matched' .
429 ' an empty string. Input "' . substr($this->data,
430 $this->counter, 5) . '... state COMMENT');
432 next($yymatches); // skip global match
433 $this->token = key($yymatches); // token number
434 if ($tokenMap[$this->token]) {
435 // extract sub-patterns for passing to lex function
436 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
437 $tokenMap[$this->token]);
439 $yysubmatches = array();
441 $this->value = current($yymatches); // token value
442 $r = $this->{'yy_r4_' . $this->token}($yysubmatches);
444 $this->counter += strlen($this->value);
445 $this->line += substr_count($this->value, "\n");
448 } elseif ($r === true) {
449 // we have changed state
450 // process this token in the new state
451 return $this->yylex();
452 } elseif ($r === false) {
453 $this->counter += strlen($this->value);
454 $this->line += substr_count($this->value, "\n");
455 if ($this->counter >= strlen($this->data)) {
456 return false; // end of input
462 throw new Exception('Unexpected input at line' . $this->line .
463 ': ' . $this->data[$this->counter]);
471 function yy_r4_1($yy_subpatterns)
477 function yy_r4_2($yy_subpatterns)
480 $this->token = Smarty_Internal_Configfileparser::TPC_NAKED_STRING;
483 function yy_r4_3($yy_subpatterns)
486 $this->token = Smarty_Internal_Configfileparser::TPC_NEWLINE;
490 public function yylex5()
496 if ($this->counter >= strlen($this->data)) {
497 return false; // end of input
499 $yy_global_pattern = "/\G(\\.)|\G(.*?(?=[\.=[\]\r\n]))/iS";
502 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
503 $yysubmatches = $yymatches;
504 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
505 if (!count($yymatches)) {
506 throw new Exception('Error: lexing failed because a rule matched' .
507 ' an empty string. Input "' . substr($this->data,
508 $this->counter, 5) . '... state SECTION');
510 next($yymatches); // skip global match
511 $this->token = key($yymatches); // token number
512 if ($tokenMap[$this->token]) {
513 // extract sub-patterns for passing to lex function
514 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
515 $tokenMap[$this->token]);
517 $yysubmatches = array();
519 $this->value = current($yymatches); // token value
520 $r = $this->{'yy_r5_' . $this->token}($yysubmatches);
522 $this->counter += strlen($this->value);
523 $this->line += substr_count($this->value, "\n");
526 } elseif ($r === true) {
527 // we have changed state
528 // process this token in the new state
529 return $this->yylex();
530 } elseif ($r === false) {
531 $this->counter += strlen($this->value);
532 $this->line += substr_count($this->value, "\n");
533 if ($this->counter >= strlen($this->data)) {
534 return false; // end of input
540 throw new Exception('Unexpected input at line' . $this->line .
541 ': ' . $this->data[$this->counter]);
549 function yy_r5_1($yy_subpatterns)
552 $this->token = Smarty_Internal_Configfileparser::TPC_DOT;
555 function yy_r5_2($yy_subpatterns)
558 $this->token = Smarty_Internal_Configfileparser::TPC_SECTION;
562 public function yylex6()
568 if ($this->counter >= strlen($this->data)) {
569 return false; // end of input
571 $yy_global_pattern = "/\G(\"\"\"(?=[ \t\r]*[\n#;]))|\G([\S\s])/iS";
574 if (preg_match($yy_global_pattern, $this->data, $yymatches, null, $this->counter)) {
575 $yysubmatches = $yymatches;
576 $yymatches = array_filter($yymatches, 'strlen'); // remove empty sub-patterns
577 if (!count($yymatches)) {
578 throw new Exception('Error: lexing failed because a rule matched' .
579 ' an empty string. Input "' . substr($this->data,
580 $this->counter, 5) . '... state TRIPPLE');
582 next($yymatches); // skip global match
583 $this->token = key($yymatches); // token number
584 if ($tokenMap[$this->token]) {
585 // extract sub-patterns for passing to lex function
586 $yysubmatches = array_slice($yysubmatches, $this->token + 1,
587 $tokenMap[$this->token]);
589 $yysubmatches = array();
591 $this->value = current($yymatches); // token value
592 $r = $this->{'yy_r6_' . $this->token}($yysubmatches);
594 $this->counter += strlen($this->value);
595 $this->line += substr_count($this->value, "\n");
598 } elseif ($r === true) {
599 // we have changed state
600 // process this token in the new state
601 return $this->yylex();
602 } elseif ($r === false) {
603 $this->counter += strlen($this->value);
604 $this->line += substr_count($this->value, "\n");
605 if ($this->counter >= strlen($this->data)) {
606 return false; // end of input
612 throw new Exception('Unexpected input at line' . $this->line .
613 ': ' . $this->data[$this->counter]);
621 function yy_r6_1($yy_subpatterns)
624 $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_QUOTES_END;
626 $this->yypushstate(self::START);
629 function yy_r6_2($yy_subpatterns)
632 $to = strlen($this->data);
633 preg_match("/\"\"\"[ \t\r]*[\n#;]/", $this->data, $match, PREG_OFFSET_CAPTURE, $this->counter);
634 if (isset($match[0][1])) {
637 $this->compiler->trigger_template_error("missing or misspelled literal closing tag");
639 $this->value = substr($this->data, $this->counter, $to - $this->counter);
640 $this->token = Smarty_Internal_Configfileparser::TPC_TRIPPLE_TEXT;