Changeset 2564
- Timestamp:
- 12/06/07 13:17:11 (5 years ago)
- Files:
-
- 1 modified
-
lang/php/mumu/trunk/mumu.php (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/php/mumu/trunk/mumu.php
r2563 r2564 63 63 // block.super : �ƥƥ�졼�Ȥ�lock��������������ä��������� 64 64 65 // ����/ find_endtags�ϸ��Ĥ����parse�⤷�ơ�$sposư�����Ƥ⤤�������� 66 // FIXME�����褦�ˡ� 65 // ����/ FIXME�����褦�ˡ� 67 66 // �������嵡���Ȥ�������͡��ѡ����Ѥߤι�¤�ꥢ�饤�����롩 68 67 … … 149 148 class MuErrorNode extends MuNode { 150 149 private $errorCode; 150 private $filename; 151 private $linenumber; 151 152 // TODO: php5 don't support array as const 152 153 private static $errorMsg = array( … … 177 178 'unknown' => 'Unknown error. Maybe bugs in MuMu' 178 179 ); 179 function __construct($errorCode ) {180 function __construct($errorCode, $filename, $linenumber) { 180 181 if (array_key_exists($errorCode, self::$errorMsg)) { 181 182 $this->errorCode = $errorCode; … … 183 184 $this->errorCode = 'unknown'; 184 185 } 186 $this->filename = $filename; 187 $this->linenumber = $linenumber; 185 188 } 186 189 public function _render($context) { 187 return self::$errorMsg[$this->errorCode]; 190 return 'file: '. $this->filename .' line: '. $this->linenumber .' '. 191 self::$errorMsg[$this->errorCode]; 188 192 } 189 193 } … … 198 202 if ($parentPath) { 199 203 if (($this->parent_tfile = MuParser::parse_from_file($parentPath)) === FALSE) { 200 // TODO: ���顼���������ƥ�졼�������˶����Ƥ����� return new MuErrorNode('invalidfilename_extends');204 // TODO: ���顼���������ƥ�졼�������˶����Ƥ����� return $this->make_errornode('invalidfilename_extends'); 201 205 } 202 206 } … … 267 271 // FIXME: �������ƥ�����å���̵�¥롼�ץ���å� 268 272 if (($this->tplfile = MuParser::parse_from_file($includePath)) === FALSE) { 269 // TODO: ���顼���������ƥ�졼�������˶����Ƥ����� $this->tplfile = new MuErrorNode('invalidfilename_include');273 // TODO: ���顼���������ƥ�졼�������˶����Ƥ����� $this->tplfile = $this->make_errornode('invalidfilename_include'); 270 274 } 271 275 } … … 536 540 class MuParser { 537 541 private $template; // �ѡ�����Υƥ�졼����� private $template_len; // �ƥ�졼�����Ĺ�� 542 public $templatePath; // �ƥ�졼�ȤΥѥ�(����) 538 543 private $block_dict = array(); // block���� => block�ؤλ��� private $extends = false; // extends�ξ��Υե����� 539 544 private $spos = 0; // ���ߥѡ�����ΰ�� 540 545 # template syntax constants 541 546 const FILTER_SEPARATOR = '|'; … … 656 661 657 662 // ��λ����(#}�Ȥ�)����ơ����ΰ�֤�� 658 private function find_closetag($ spos, $closetag) {659 if (($fpos = strpos($this->template, $closetag, $ spos)) === FALSE) {663 private function find_closetag($closetag) { 664 if (($fpos = strpos($this->template, $closetag, $this->spos)) === FALSE) { 660 665 return FALSE; 661 666 } 662 667 return $fpos; 668 } 669 670 private function make_errornode($errorCode) { 671 // from $spos to linenumber 672 // TODO: count on parsing 673 $c = substr($this->template, 0, $this->spos); 674 $ln = 0; 675 for ($i = 0; $i < $this->spos; $i++) { 676 switch ($c[$i]) { 677 case "\r": 678 if ($c[$i + 1] == "\n") { 679 $i++; 680 } 681 $ln++; 682 break; 683 case "\n": 684 $ln++; 685 break; 686 } 687 } 688 return new MuErrorNode($errorCode, $this->templatePath, $ln); 663 689 } 664 690 665 691 // {% %}�����������ơ�MuNode����� 666 692 // extends����˽ʤ��Ȥ����ʤ� 667 private function parse_block( &$spos) {668 $ spos += 2;669 if (($lpos = $this->find_closetag( $spos,self::BLOCK_TAG_END)) === FALSE) {670 return new MuErrorNode('without_closetag_tag');671 } 672 $in = $this->smart_split(substr($this->template, $ spos, $lpos - $spos));693 private function parse_block() { 694 $this->spos += 2; 695 if (($lpos = $this->find_closetag(self::BLOCK_TAG_END)) === FALSE) { 696 return $this->make_errornode('without_closetag_tag'); 697 } 698 $in = $this->smart_split(substr($this->template, $this->spos, $lpos - $this->spos)); 673 699 switch ($in[0]) { 674 700 // TODO: ������å���� case 'extends': 675 701 if ($this->extends !== FALSE) { 676 return new MuErrorNode('multiple_extends_tag');702 return $this->make_errornode('multiple_extends_tag'); 677 703 } 678 704 if (count($in) != 2) { 679 return new MuErrorNode('numofparam_extends_tag');705 return $this->make_errornode('numofparam_extends_tag'); 680 706 } 681 707 $param = explode('"', $in[1]); 682 708 if (count($param) != 3) { 683 // Django��ѿ�K����ɤ� return new MuErrorNode('invalidparam_extends_tag');709 // Django��ѿ�K����ɤ� return $this->make_errornode('invalidparam_extends_tag'); 684 710 } 685 711 $this->extends = $param[1]; 686 $ spos = $lpos + 2;712 $this->spos = $lpos + 2; 687 713 break; 688 714 case 'include': 689 715 if (count($in) != 2) { 690 return new MuErrorNode('numofparam_include_tag');716 return $this->make_errornode('numofparam_include_tag'); 691 717 } 692 718 $param = explode('"', $in[1]); 693 719 if (count($param) != 3) { 694 // Django��ѿ�K����ɤ� return new MuErrorNode('invalidparam_include_tag');720 // Django��ѿ�K����ɤ� return $this->make_errornode('invalidparam_include_tag'); 695 721 } 696 722 $node = new MuIncludeNode($param[1]); 697 $ spos = $lpos + 2;723 $this->spos = $lpos + 2; 698 724 break; 699 725 case 'block': // endblock … … 702 728 $blockname = $in[1]; 703 729 if (array_key_exists($blockname, $this->block_dict)) { 704 return new MuErrorNode('multiple_block_tag');705 } 706 $ spos = $lpos + 2;707 list($nodelist) = $this->_parse( $spos,array('endblock'));730 return $this->make_errornode('multiple_block_tag'); 731 } 732 $this->spos = $lpos + 2; 733 list($nodelist) = $this->_parse(array('endblock')); 708 734 $node = new MuBlockNode($blockname, $nodelist); 709 735 $this->block_dict[$blockname] = $node; … … 712 738 // $in[1] = $loopvar, $in[2] = 'in', $in[3] = $sequence, $in[4] = 'reversed' 713 739 if ((count($in) != 4 && count($in) != 5) || $in[2] != 'in') { 714 return new MuErrorNode('numofparam_for_tag');740 return $this->make_errornode('numofparam_for_tag'); 715 741 } 716 742 if (count($in) == 5) { … … 718 744 $reversed = True; 719 745 } else { 720 return new MuErrorNode('invalidparam_for_tag');746 return $this->make_errornode('invalidparam_for_tag'); 721 747 } 722 748 } else { 723 749 $reversed = False; 724 750 } 725 $ spos = $lpos + 2;726 list($nodelist) = $this->_parse( $spos,array('endfor'));751 $this->spos = $lpos + 2; 752 list($nodelist) = $this->_parse(array('endfor')); 727 753 $node = new MuForNode($in[1], $in[3], $reversed, $nodelist); 728 754 break; … … 730 756 // TODO: implement namedCycleNodes 731 757 if (count($in) != 2) { 732 return new MuErrorNode('numofparam_cycle_tag');758 return $this->make_errornode('numofparam_cycle_tag'); 733 759 } 734 760 $cyclevars = explode(',', $in[1]); 735 761 if (count($cyclevars) == 0) { 736 return new MuErrorNode('invalidparam_cycle_tag');762 return $this->make_errornode('invalidparam_cycle_tag'); 737 763 } 738 764 $node = new MuCycleNode($cyclevars); 739 $ spos = $lpos + 2;765 $this->spos = $lpos + 2; 740 766 break; 741 767 case 'if': // else, endif 742 768 array_shift($in); 743 769 if (count($in) < 1) { 744 return new MuErrorNode('numofparam_if_tag');770 return $this->make_errornode('numofparam_if_tag'); 745 771 } 746 772 $bitstr = implode(' ', $in); … … 753 779 $link_type = MuIfNode::LINKTYPE_AND; 754 780 if (in_array(' or ', $bitstr)) { 755 return new MuErrorNode('andormixed_if_tag');781 return $this->make_errornode('andormixed_if_tag'); 756 782 } 757 783 } … … 762 788 list($not, $boolvar) = explode(' ', $boolpair); 763 789 if ($not != 'not') { 764 return new MuErrorNode('invalidparam_if_tag');790 return $this->make_errornode('invalidparam_if_tag'); 765 791 } 766 792 array_push($boolvars, array(True, $boolvar)); … … 769 795 } 770 796 } 771 $ spos = $lpos + 2;797 $this->spos = $lpos + 2; 772 798 list($nodelist_true, $nexttag) = 773 $this->_parse( $spos,array('else', 'endif'));799 $this->_parse(array('else', 'endif')); 774 800 if ($nexttag == 'else') { 775 list($nodelist_false) = $this->_parse( $spos,array('endif'));801 list($nodelist_false) = $this->_parse(array('endif')); 776 802 } else { 777 803 $nodelist_false = new MuNodeList(); … … 781 807 case 'debug': 782 808 $node = new MuDebugNode(); 783 $ spos = $lpos + 2;809 $this->spos = $lpos + 2; 784 810 break; 785 811 case 'now': 786 812 if (count($in) != 2) { 787 return new MuErrorNode('numofparam_now_tag');813 return $this->make_errornode('numofparam_now_tag'); 788 814 } 789 815 $param = explode('"', $in[1]); 790 816 if (count($param) != 3) { 791 return new MuErrorNode('invalidparam_now_tag');817 return $this->make_errornode('invalidparam_now_tag'); 792 818 } 793 819 $node = new MuNowNode($param[1]); 794 $ spos = $lpos + 2;820 $this->spos = $lpos + 2; 795 821 break; 796 822 case 'filter': // endfilter 797 823 if (count($in) != 2) { 798 return new MuErrorNode('numofparam_filter_tag');799 } 800 $ spos = $lpos + 2;824 return $this->make_errornode('numofparam_filter_tag'); 825 } 826 $this->spos = $lpos + 2; 801 827 if (($filter_expr = new MuFilterExpression('var|'. $in[1])) === FALSE) { 802 return new MuErrorNode('invalidparam_filter_tag');803 } 804 list($nodelist) = $this->_parse( $spos,array('endfilter'));828 return $this->make_errornode('invalidparam_filter_tag'); 829 } 830 list($nodelist) = $this->_parse(array('endfilter')); 805 831 $node = new MuFilterNode($filter_expr, $nodelist); 806 832 break; … … 811 837 case 'endfilter': 812 838 $node = $in[0]; // raw string 813 $ spos = $lpos + 2;839 $this->spos = $lpos + 2; 814 840 break; 815 841 default: 816 $node = new MuErrorNode('unknown_tag'); 817 echo $in[0]; 818 $spos = $lpos + 2; 842 $node = $this->make_errornode('unknown_tag'); 843 $this->spos = $lpos + 2; 819 844 break; 820 845 } … … 822 847 } 823 848 824 private function parse_variable(&$spos) { 825 $spos += 2; 826 if (($lpos = $this->find_closetag($spos, self::VARIABLE_TAG_END)) === FALSE) { 849 private function parse_variable() { 850 $this->spos += 2; 851 if (($lpos = $this->find_closetag(self::VARIABLE_TAG_END)) === FALSE) { 852 echo "uhyohyo\n"; 827 853 return FALSE; 828 854 } 829 855 // TODO: handle empty {{ }} 830 856 if (($fil = new MuFilterExpression( 831 substr($this->template, $ spos, $lpos - $spos))) === FALSE) {832 return new MuErrorNode('invalidparam_filter_variable');857 substr($this->template, $this->spos, $lpos - $this->spos))) === FALSE) { 858 return $this->make_errornode('invalidparam_filter_variable'); 833 859 } 834 860 $node = new MuVariableNode($fil); 835 $ spos = $lpos + 2;861 $this->spos = $lpos + 2; 836 862 return $node; 837 863 } 838 864 839 private function parse_comment( &$spos) {840 $ spos += 2;841 if (($lpos = $this->find_closetag( $spos,self::COMMENT_TAG_END)) === FALSE) {865 private function parse_comment() { 866 $this->spos += 2; 867 if (($lpos = $this->find_closetag(self::COMMENT_TAG_END)) === FALSE) { 842 868 return FALSE; 843 869 } 844 $ spos = $lpos + 2;870 $this->spos = $lpos + 2; 845 871 } 846 872 … … 850 876 } 851 877 $p = new MuParser($t); 852 $ spos = 0;853 list($nl) = $p->_parse( $spos,array());878 $p->templatePath = $templatePath; 879 list($nl) = $p->_parse(array()); 854 880 return new MuFile($nl, $p->block_dict, $p->extends); 855 881 } … … 857 883 static public function parse($templateStr) { 858 884 $p = new MuParser($templateStr); 859 $spos = 0; 860 list($nl) = $p->_parse($spos, array()); 885 list($nl) = $p->_parse(array()); 861 886 return new MuFile($nl, $p->block_dict); 862 887 } … … 869 894 } 870 895 871 private function _parse( &$spos,$parse_until) {896 private function _parse($parse_until) { 872 897 $nl = new MuNodeList(); 873 $tspos = $ spos;898 $tspos = $this->spos; 874 899 while (true) { 875 $spos = strpos($this->template, self::SINGLE_BRACE_START, $spos); 876 if ($spos === FALSE) { 900 $this->spos = strpos($this->template, self::SINGLE_BRACE_START, $this->spos); 901 if ($this->spos === FALSE) { 902 $this->spos = $this->template_len; 877 903 $this->add_textnode($nl, $tspos, $this->template_len); 878 904 return array($nl, null); 879 905 } 880 switch (substr($this->template, $ spos, 2)) {906 switch (substr($this->template, $this->spos, 2)) { 881 907 case self::BLOCK_TAG_START: 882 $this->add_textnode($nl, $tspos, $ spos);883 if (($node = $this->parse_block( $spos)) === FALSE) {908 $this->add_textnode($nl, $tspos, $this->spos); 909 if (($node = $this->parse_block()) === FALSE) { 884 910 return FALSE; 885 911 } 886 $tspos = $ spos;912 $tspos = $this->spos; 887 913 if (is_string($node)) { 888 914 // close tag … … 897 923 break; 898 924 case self::VARIABLE_TAG_START: 899 $this->add_textnode($nl, $tspos, $ spos);900 if (($node = $this->parse_variable( $spos)) === FALSE) {925 $this->add_textnode($nl, $tspos, $this->spos); 926 if (($node = $this->parse_variable()) === FALSE) { 901 927 return FALSE; 902 928 } 903 929 $nl->push($node); 904 $tspos = $ spos;930 $tspos = $this->spos; 905 931 break; 906 932 case self::COMMENT_TAG_START: 907 $this->add_textnode($nl, $tspos, $ spos);908 if (($node = $this->parse_comment( $spos)) === FALSE) {933 $this->add_textnode($nl, $tspos, $this->spos); 934 if (($node = $this->parse_comment()) === FALSE) { 909 935 return FALSE; 910 936 } 911 937 $nl->push($node); 912 $tspos = $ spos;938 $tspos = $this->spos; 913 939 break; 914 940 default: 915 941 // { only 916 $ spos += 1;942 $this->spos += 1; 917 943 } 918 944 }
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)