3 * ARC2 SPARQL-enhanced Turtle Parser
5 * @author Benjamin Nowack
6 * @license <http://arc.semsol.org/license>
7 * @homepage <http://arc.semsol.org/>
12 ARC2::inc('RDFParser');
14 class ARC2_TurtleParser extends ARC2_RDFParser {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_TurtleParser($a = '', &$caller) {
21 $this->__construct($a, $caller);
24 function __init() {/* reader */
27 $this->xml = 'http://www.w3.org/XML/1998/namespace';
28 $this->rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
29 $this->xsd = 'http://www.w3.org/2001/XMLSchema#';
30 $this->nsp = array($this->xml => 'xml', $this->rdf => 'rdf', $this->xsd => 'xsd');
31 $this->unparsed_code = '';
32 $this->max_parsing_loops = $this->v('turtle_max_parsing_loops', 500, $this->a);
37 function x($re, $v, $options = 'si') {
38 $v = preg_replace('/^[\xA0\xC2]+/', ' ', $v);
39 while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $v, $m)) {/* comment removal */
42 return ARC2::x($re, $v, $options);
43 //$this->unparsed_code = ($sub_r && count($sub_r)) ? $sub_r[count($sub_r) - 1] : '';
46 function createBnodeID(){
48 return '_:' . $this->bnode_prefix . $this->bnode_id;
54 if ($this->skip_dupes) {
55 $h = md5(serialize($t));
56 if (!isset($this->added_triples[$h])) {
57 $this->triples[$this->t_count] = $t;
59 $this->added_triples[$h] = true;
63 $this->triples[$this->t_count] = $t;
70 function getTriples() {
71 return $this->v('triples', array());
74 function countTriples() {
75 return $this->t_count;
80 function getUnparsedCode() {
81 return $this->v('unparsed_code', '');
86 function setDefaultPrefixes() {
87 $this->prefixes = array(
88 'rdf:' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
89 'rdfs:' => 'http://www.w3.org/2000/01/rdf-schema#',
90 'owl:' => 'http://www.w3.org/2002/07/owl#',
91 'xsd:' => 'http://www.w3.org/2001/XMLSchema#',
93 if ($ns = $this->v('ns', array(), $this->a)) {
94 foreach ($ns as $p => $u) $this->prefixes[$p . ':'] = $u;
99 function parse($path, $data = '', $iso_fallback = false) {
100 $this->setDefaultPrefixes();
102 if (!$this->v('reader')) {
104 $this->reader = & new ARC2_Reader($this->a, $this);
106 $this->reader->setAcceptHeader('Accept: application/x-turtle; q=0.9, */*; q=0.1');
107 $this->reader->activate($path, $data);
108 $this->base = $this->v1('base', $this->reader->base, $this->a);
109 $this->r = array('vars' => array());
112 $more_triples = array();
117 while ($d = $this->reader->readStream(0)) {
122 if (!$prologue_done) {
124 if ((list($sub_r, $sub_v) = $this->xPrologue($sub_v)) && $sub_r) {
126 $sub_v .= $this->reader->readStream(0, 128);
127 /* we might have missed the final DOT in the previous prologue loop */
128 if ($sub_r = $this->x('\.', $sub_v)) $sub_v = $sub_r[1];
129 if ($this->x("\@?(base|prefix)", $sub_v)) {/* more prologue to come, use outer loop */
137 if ($prologue_done && (list($sub_r, $sub_v, $more_triples, $sub_v2) = $this->xTriplesBlock($sub_v)) && is_array($sub_r)) {
140 foreach ($sub_r as $t) {
147 if ($loops > $this->max_parsing_loops) {/* most probably a parser or code bug, might also be a huge object value, though */
148 $this->addError('too many loops: ' . $loops . '. Could not parse "' . substr($buffer, 0, 200) . '..."');
152 foreach ($more_triples as $t) {
155 $sub_v = count($more_triples) ? $sub_v2 : $sub_v;
157 $this->unparsed_code = $buffer;
158 $this->reader->closeStream();
159 unset($this->reader);
160 /* remove trailing comments */
161 while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) $this->unparsed_code = $m[2];
162 if ($this->unparsed_code && !$this->getErrors()) {
163 $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30));
164 if (trim($rest)) $this->addError('Could not parse "' . $rest . '"');
166 return $this->done();
169 function xPrologue($v) {
171 if (!$this->t_count) {
172 if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) {
173 $this->base = $sub_r;
176 while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) {
177 $this->prefixes[$sub_r['prefix']] = $sub_r['uri'];
181 return array($r, $v);
186 function xBaseDecl($v) {
187 if ($r = $this->x("\@?base\s+", $v)) {
188 if ((list($r, $sub_v) = $this->xIRI_REF($r[1])) && $r) {
189 if ($sub_r = $this->x('\.', $sub_v)) {
192 return array($r, $sub_v);
200 function xPrefixDecl($v) {
201 if ($r = $this->x("\@?prefix\s+", $v)) {
202 if ((list($r, $sub_v) = $this->xPNAME_NS($r[1])) && $r) {
204 if((list($r, $sub_v) = $this->xIRI_REF($sub_v)) && $r) {
205 $uri = $this->calcURI($r, $this->base);
206 if ($sub_r = $this->x('\.', $sub_v)) {
209 return array(array('prefix' => $prefix, 'uri_ref' => $r, 'uri' => $uri), $sub_v);
218 function xTriplesBlock($v) {
226 if ($state == 1) {/* expecting subject */
227 $t = array('type' => 'triple', 's' => '', 'p' => '', 'o' => '', 's_type' => '', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => '');
228 if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
229 $t['s'] = $sub_r['value'];
230 $t['s_type'] = $sub_r['type'];
233 if ($sub_r = $this->x('(\}|\.)', $sub_v)) {
234 if ($t['s_type'] == 'placeholder') {
238 $this->addError('"' . $sub_r[1]. '" after subject found.');
242 elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) {
243 $t['s'] = $sub_r['id'];
244 $t['s_type'] = $sub_r['type'];
245 $pre_r = array_merge($pre_r, $sub_r['triples']);
248 if ($sub_r = $this->x('\.', $sub_v)) {
249 $this->addError('DOT after subject found.');
252 elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) {
253 $t['s'] = $sub_r['id'];
254 $t['s_type'] = $sub_r['type'];
255 $pre_r = array_merge($pre_r, $sub_r['triples']);
259 elseif ($sub_r = $this->x('\.', $sub_v)) {
260 $this->addError('Subject expected, DOT found.' . $sub_v);
263 if ($state == 2) {/* expecting predicate */
264 if ($sub_r = $this->x('a\s+', $sub_v)) {
266 $t['p'] = $this->rdf . 'type';
267 $t['p_type'] = 'uri';
271 elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
272 if ($sub_r['type'] == 'bnode') {
273 $this->addError('Blank node used as triple predicate');
275 $t['p'] = $sub_r['value'];
276 $t['p_type'] = $sub_r['type'];
280 elseif ($sub_r = $this->x('\.', $sub_v)) {
283 elseif ($sub_r = $this->x('\}', $sub_v)) {
285 $r = array_merge($r, $pre_r);
290 if ($state == 3) {/* expecting object */
291 if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
292 $t['o'] = $sub_r['value'];
293 $t['o_type'] = $sub_r['type'];
294 $t['o_lang'] = $this->v('lang', '', $sub_r);
295 $t['o_datatype'] = $this->v('datatype', '', $sub_r);
300 elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) {
301 $t['o'] = $sub_r['id'];
302 $t['o_type'] = $sub_r['type'];
303 $pre_r = array_merge($pre_r, array($t), $sub_r['triples']);
307 elseif ((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) {
308 $t['o'] = $sub_r['id'];
309 $t['o_type'] = $sub_r['type'];
310 $pre_r = array_merge($pre_r, array($t), $sub_r['triples']);
315 if ($state == 4) {/* expecting . or ; or , or } */
316 if ($sub_r = $this->x('\.', $sub_v)) {
319 $r = array_merge($r, $pre_r);
324 elseif ($sub_r = $this->x('\;', $sub_v)) {
329 elseif ($sub_r = $this->x('\,', $sub_v)) {
333 if ($sub_r = $this->x('\}', $sub_v)) {
334 $this->addError('Object expected, } found.');
337 if ($sub_r = $this->x('(\}|\{|OPTIONAL|FILTER|GRAPH)', $sub_v)) {
339 $r = array_merge($r, $pre_r);
345 return count($r) ? array($r, $buffer, $pre_r, $sub_v) : array(0, $buffer, $pre_r, $sub_v);
350 function xBlankNodePropertyList($v) {
351 if ($sub_r = $this->x('\[', $v)) {
353 $s = $this->createBnodeID();
354 $r = array('id' => $s, 'type' => 'bnode', 'triples' => array());
355 $t = array('type' => 'triple', 's' => $s, 'p' => '', 'o' => '', 's_type' => 'bnode', 'p_type' => '', 'o_type' => '', 'o_datatype' => '', 'o_lang' => '');
360 if ($state == 2) {/* expecting predicate */
361 if ($sub_r = $this->x('a\s+', $sub_v)) {
363 $t['p'] = $this->rdf . 'type';
364 $t['p_type'] = 'uri';
368 elseif ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
369 $t['p'] = $sub_r['value'];
370 $t['p_type'] = $sub_r['type'];
375 if ($state == 3) {/* expecting object */
376 if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
377 $t['o'] = $sub_r['value'];
378 $t['o_type'] = $sub_r['type'];
379 $t['o_lang'] = $this->v('lang', '', $sub_r);
380 $t['o_datatype'] = $this->v('datatype', '', $sub_r);
381 $r['triples'][] = $t;
385 elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) {
386 $t['o'] = $sub_r['id'];
387 $t['o_type'] = $sub_r['type'];
388 $r['triples'] = array_merge($r['triples'], array($t), $sub_r['triples']);
392 elseif((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) {
393 $t['o'] = $sub_r['id'];
394 $t['o_type'] = $sub_r['type'];
395 $r['triples'] = array_merge($r['triples'], array($t), $sub_r['triples']);
400 if ($state == 4) {/* expecting . or ; or , or ] */
401 if ($sub_r = $this->x('\.', $sub_v)) {
406 if ($sub_r = $this->x('\;', $sub_v)) {
411 if ($sub_r = $this->x('\,', $sub_v)) {
416 if ($sub_r = $this->x('\]', $sub_v)) {
424 return array($r, $sub_v);
433 function xCollection($v) {
434 if ($sub_r = $this->x('\(', $v)) {
436 $s = $this->createBnodeID();
437 $r = array('id' => $s, 'type' => 'bnode', 'triples' => array());
441 if ((list($sub_r, $sub_v) = $this->xVarOrTerm($sub_v)) && $sub_r) {
442 $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['value'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => $this->v('lang', '', $sub_r), 'o_datatype' => $this->v('datatype', '', $sub_r));
445 elseif ((list($sub_r, $sub_v) = $this->xCollection($sub_v)) && $sub_r) {
446 $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => '');
447 $r['triples'] = array_merge($r['triples'], $sub_r['triples']);
450 elseif((list($sub_r, $sub_v) = $this->xBlankNodePropertyList($sub_v)) && $sub_r) {
451 $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'first', 'o' => $sub_r['id'], 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => $sub_r['type'], 'o_lang' => '', 'o_datatype' => '');
452 $r['triples'] = array_merge($r['triples'], $sub_r['triples']);
456 if ($sub_r = $this->x('\)', $sub_v)) {
458 $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'rest', 'o' => $this->rdf . 'nil', 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'uri', 'o_lang' => '', 'o_datatype' => '');
463 $next_s = $this->createBnodeID();
464 $r['triples'][] = array('type' => 'triple', 's' => $s, 'p' => $this->rdf . 'rest', 'o' => $next_s, 's_type' => 'bnode', 'p_type' => 'uri', 'o_type' => 'bnode', 'o_lang' => '', 'o_datatype' => '');
470 return array($r, $sub_v);
473 return array (0, $v);
478 function xVarOrTerm($v) {
479 if ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) {
480 return array($sub_r, $sub_v);
482 elseif ((list($sub_r, $sub_v) = $this->xGraphTerm($v)) && $sub_r) {
483 return array($sub_r, $sub_v);
491 if ($r = $this->x('(\?|\$)([^\s]+)', $v)) {
492 if ((list($sub_r, $sub_v) = $this->xVARNAME($r[2])) && $sub_r) {
493 if (!in_array($sub_r, $this->r['vars'])) {
494 $this->r['vars'][] = $sub_r;
496 return array(array('value' => $sub_r, 'type' => 'var'), $sub_v . $r[3]);
504 function xGraphTerm($v) {
507 'RDFLiteral' => 'literal',
508 'NumericLiteral' => 'literal',
509 'BooleanLiteral' => 'literal',
510 'BlankNode' => 'bnode',
512 'Placeholder' => 'placeholder'
513 ) as $term => $type) {
515 if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) {
516 if (!is_array($sub_r)) {
517 $sub_r = array('value' => $sub_r);
519 $sub_r['type'] = $this->v1('type', $type, $sub_r);
520 return array($sub_r, $sub_v);
528 function xRDFLiteral($v) {
529 if ((list($sub_r, $sub_v) = $this->xString($v)) && $sub_r) {
530 $sub_r['value'] = $this->unescapeNtripleUTF($sub_r['value']);
532 if ((list($sub_r, $sub_v) = $this->xLANGTAG($sub_v)) && $sub_r) {
535 elseif (!$this->x('\s', $sub_v) && ($sub_r = $this->x('\^\^', $sub_v)) && (list($sub_r, $sub_v) = $this->xIRIref($sub_r[1])) && $sub_r[1]) {
536 $r['datatype'] = $sub_r;
538 return array($r, $sub_v);
543 /* 61.., 62.., 63.., 64.. */
545 function xNumericLiteral($v) {
546 $sub_r = $this->x('(\-|\+)?', $v);
549 foreach (array('DOUBLE' => 'double', 'DECIMAL' => 'decimal', 'INTEGER' => 'integer') as $type => $xsd) {
551 if ((list($sub_r, $sub_v) = $this->$m($sub_v)) && ($sub_r !== false)) {
552 $r = array('value' => $prefix . $sub_r, 'type' => 'literal', 'datatype' => $this->xsd . $xsd);
553 return array($r, $sub_v);
561 function xBooleanLiteral($v) {
562 if ($r = $this->x('(true|false)', $v)) {
563 return array($r[1], $r[2]);
568 /* 66.., 87.., 88.., 89.., 90.., 91.. */
570 function xString($v) {/* largely simplified, may need some tweaks in following revisions */
572 if (!preg_match('/^\s*([\']{3}|\'|[\"]{3}|\")(.*)$/s', $sub_v, $m)) return array(0, $v);
575 $sub_types = array("'''" => 'literal_long1', '"""' => 'literal_long2', "'" => 'literal1', '"' => 'literal2');
576 $sub_type = $sub_types[$delim];
581 $delim_pos = strpos($rest, $delim, $pos);
582 if ($delim_pos === false) break;
583 $new_rest = substr($rest, $delim_pos + strlen($delim));
584 $r = substr($rest, 0, $delim_pos);
585 if (!preg_match('/([\x5c]+)$/s', $r, $m) || !(strlen($m[1]) % 2)) {
590 $pos = $delim_pos + 1;
595 return array(array('value' => $this->toUTF8($r) , 'type' => 'literal', 'sub_type' => $sub_type), $rest);
602 function xIRIref($v) {
603 if ((list($r, $v) = $this->xIRI_REF($v)) && $r) {
604 return array($this->calcURI($r, $this->base), $v);
606 elseif ((list($r, $v) = $this->xPrefixedName($v)) && $r) {
607 return array($r, $v);
614 function xPrefixedName($v) {
615 if ((list($r, $v) = $this->xPNAME_LN($v)) && $r) {
616 return array($r, $v);
618 elseif ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) {
619 return isset($this->prefixes[$r]) ? array($this->prefixes[$r], $sub_v) : array(0, $v);
624 /* 69.., 73.., 93, 94.. */
626 function xBlankNode($v) {
627 if (($r = $this->x('\_\:', $v)) && (list($r, $sub_v) = $this->xPN_LOCAL($r[1])) && $r) {
628 return array(array('type' => 'bnode', 'value' => '_:' . $r), $sub_v);
630 if ($r = $this->x('\[[\x20\x9\xd\xa]*\]', $v)) {
631 return array(array('type' => 'bnode', 'value' => $this->createBnodeID()), $r[1]);
636 /* 70.. @@sync with SPARQLParser */
638 function xIRI_REF($v) {
639 //if ($r = $this->x('\<([^\<\>\"\{\}\|\^\'[:space:]]*)\>', $v)) {
640 if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) {
641 return array($r[1], $r[2]);
643 elseif ($r = $this->x('\<\>', $v)) {
644 return array(true, $r[1]);
646 elseif ($r = $this->x('\<([^\s][^\<\>]*)\>', $v)) {
647 return array($r[1] ? $r[1] : true, $r[2]);
654 function xPNAME_NS($v) {
655 list($r, $sub_v) = $this->xPN_PREFIX($v);
656 $prefix = $r ? $r : '';
657 return ($r = $this->x("\:", $sub_v)) ? array($prefix . ':', $r[1]) : array(0, $v);
662 function xPNAME_LN($v) {
663 if ((list($r, $sub_v) = $this->xPNAME_NS($v)) && $r) {
664 if (!$this->x('\s', $sub_v) && (list($sub_r, $sub_v) = $this->xPN_LOCAL($sub_v)) && $sub_r) {
665 if (!isset($this->prefixes[$r])) {
668 return array($this->prefixes[$r] . $sub_r, $sub_v);
676 function xLANGTAG($v) {
677 if (!$this->x('\s', $v) && ($r = $this->x('\@([a-z]+(\-[a-z0-9]+)*)', $v))) {
678 return array($r[1], $r[3]);
685 function xINTEGER($v) {
686 if ($r = $this->x('([0-9]+)', $v)) {
687 return array($r[1], $r[2]);
689 return array(false, $v);
694 function xDECIMAL($v) {
695 if ($r = $this->x('([0-9]+\.[0-9]*)', $v)) {
696 return array($r[1], $r[2]);
698 if ($r = $this->x('(\.[0-9]+)', $v)) {
699 return array($r[1], $r[2]);
701 return array(false, $v);
706 function xDOUBLE($v) {
707 if ($r = $this->x('([0-9]+\.[0-9]*E[\+\-]?[0-9]+)', $v)) {
708 return array($r[1], $r[2]);
710 if ($r = $this->x('(\.[0-9]+E[\+\-]?[0-9]+)', $v)) {
711 return array($r[1], $r[2]);
713 if ($r = $this->x('([0-9]+E[\+\-]?[0-9]+)', $v)) {
714 return array($r[1], $r[2]);
716 return array(false, $v);
722 if ($r = $this->x('\([\x20\x9\xd\xa]*\)', $v)) {
723 return array(array('type' => 'uri', 'value' => $this->rdf . 'nil'), $r[1]);
730 function xPN_CHARS_BASE($v) {
731 if ($r = $this->x("([a-z]+|\\\u[0-9a-f]{1,4})", $v)) {
732 return array($r[1], $r[2]);
739 function xPN_CHARS_U($v) {
740 if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) {
741 return array($r, $sub_v);
743 elseif ($r = $this->x("(_)", $v)) {
744 return array($r[1], $r[2]);
751 function xVARNAME($v) {
755 if ($sub_r = $this->x('([0-9]+)', $v)) {
760 elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($v)) && $sub_r) {
765 elseif ($r && ($sub_r = $this->x('([\xb7\x300-\x36f]+)', $v))) {
771 return array($r, $v);
776 function xPN_CHARS($v) {
777 if ((list($r, $sub_v) = $this->xPN_CHARS_U($v)) && $r) {
778 return array($r, $sub_v);
780 elseif ($r = $this->x('([\-0-9\xb7\x300-\x36f])', $v)) {
781 return array($r[1], $r[2]);
783 return array(false, $v);
788 function xPN_PREFIX($v) {
789 if ($sub_r = $this->x("([^\s\:\(\)\{\}\;\,]+)", $v, 's')) {/* accelerator */
790 return array($sub_r[1], $sub_r[2]);/* @@testing */
792 if ((list($r, $sub_v) = $this->xPN_CHARS_BASE($v)) && $r) {
795 list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v);
796 if ($sub_r !== false) {
800 elseif ($sub_r = $this->x("\.", $sub_v)) {
806 list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v);
807 $r .= $sub_r ? $sub_r : '';
809 return array($r, $sub_v);
814 function xPN_LOCAL($v) {
815 if (($sub_r = $this->x("([^\s\(\)\{\}\[\]\;\,\.]+)", $v, 's')) && !preg_match('/^\./', $sub_r[2])) {/* accelerator */
816 return array($sub_r[1], $sub_r[2]);/* @@testing */
822 if ($this->x('\s', $sub_v)) {
823 return array($r, $sub_v);
825 if ($sub_r = $this->x('([0-9])', $sub_v)) {
830 elseif ((list($sub_r, $sub_v) = $this->xPN_CHARS_U($sub_v)) && $sub_r) {
835 if (($sub_r = $this->x('(\.)', $sub_v)) && !preg_match('/^[\s\}]/s', $sub_r[2])) {
839 if ((list($sub_r, $sub_v) = $this->xPN_CHARS($sub_v)) && $sub_r) {
845 return array($r, $sub_v);
850 function unescapeNtripleUTF($v) {
851 if (strpos($v, '\\') === false) return $v;
852 $mappings = array('t' => "\t", 'n' => "\n", 'r' => "\r", '\"' => '"', '\'' => "'");
853 foreach ($mappings as $in => $out) {
854 $v = preg_replace('/\x5c([' . $in . '])/', $out, $v);
856 if (strpos(strtolower($v), '\u') === false) return $v;
857 while (preg_match('/\\\(U)([0-9A-F]{8})/', $v, $m) || preg_match('/\\\(u)([0-9A-F]{4})/', $v, $m)) {
859 if ($no < 128) $char = chr($no);
860 else if ($no < 2048) $char = chr(($no >> 6) + 192) . chr(($no & 63) + 128);
861 else if ($no < 65536) $char = chr(($no >> 12) + 224) . chr((($no >> 6) & 63) + 128) . chr(($no & 63) + 128);
862 else if ($no < 2097152) $char = chr(($no >> 18) + 240) . chr((($no >> 12) & 63) + 128) . chr((($no >> 6) & 63) + 128) . chr(($no & 63) + 128);
864 $v = str_replace('\\' . $m[1] . $m[2], $char, $v);
871 function xPlaceholder($v) {
872 //if ($r = $this->x('(\?|\$)\{([^\}]+)\}', $v)) {
873 if ($r = $this->x('(\?|\$)', $v)) {
874 if (preg_match('/(\{(?:[^{}]+|(?R))*\})/', $r[2], $m) && strpos(trim($r[2]), $m[1]) === 0) {
875 $ph = substr($m[1], 1, -1);
876 $rest = substr(trim($r[2]), strlen($m[1]));
877 if (!isset($this->r['placeholders'])) $this->r['placeholders'] = array();
878 if (!in_array($ph, $this->r['placeholders'])) $this->r['placeholders'][] = $ph;
879 return array(array('value' => $ph, 'type' => 'placeholder'), $rest);