Changeset 268
- Timestamp:
- 03/28/08 13:37:49 (7 months ago)
- Files:
-
- trunk/dparser/cut_gram2 (added)
- trunk/dparser/dparse.d (modified) (28 diffs)
- trunk/dparser/gram.sed (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/dparser/dparse.d
r267 r268 357 357 left side term with a Right side term 358 358 */ 359 class PObjectLeftFactorT(T , int i, char[] str) : PObjectVector!(i), PInterfaceLeftFactor!(T)359 class PObjectLeftFactorT(Tp, int i, char[] str) : PObjectVector!(i), PInterfaceLeftFactor!(Tp) 360 360 { 361 361 this(PObject[i] d){super(d);} 362 362 363 PObject InsertLeft(T that, PObject L)363 PObject InsertLeft(Tp that, PObject L) 364 364 { 365 365 PObject[i+1] args; … … 368 368 args[0] = L; 369 369 370 return that.Action!(str)(args);370 return SpecialAction!(Tp,str)(that,args); 371 371 } 372 372 } … … 588 588 str = str[i..$]; 589 589 i=0; 590 while(i < str.length && str[i] != ')') i++; 591 592 return str[0..i]; 593 } 590 int d = 1; 591 while(i < str.length && d > 0) 592 { 593 d += (str[i] == '('); 594 d -= (str[i] == ')'); 595 i++; 596 } 597 598 return str[0..i-1]; 599 } 600 601 static assert(ExtractAct("$(T,1,$(N,1,abc,def))") == "$(N,1,abc,def)", ExtractAct("$(T,1,$(N,1,abc,def))")); 594 602 595 603 /****************************************************************************** … … 598 606 599 607 /// Pack up stuff in a computed type of PObjectLeftFactorT Object 600 PObject L_Action(T , char[] str ) (T, PObject[ExtractCount(str[4..$])] i)608 PObject L_Action(Tp, char[] str ) (Tp, PObject[ExtractCount(str[4..$])] i) 601 609 { 602 610 static const int c = ExtractCount(str[4..$]); … … 604 612 static if(false) pragma(msg, str~" == $(L,"~c.stringof~","~n~")"); 605 613 606 return new PObjectLeftFactorT!(T , c, n)(i);614 return new PObjectLeftFactorT!(Tp, c, n)(i); 607 615 } 608 616 609 617 /// Left Process a Leftmost term and right side terms list. 610 PObject T_Action(T ,char[] str) (Tthat, PObject[1+ExtractCount(str[4..$])] i)618 PObject T_Action(Tp,char[] str) (Tp that, PObject[1+ExtractCount(str[4..$])] i) 611 619 { 612 620 static const int num = ExtractCount(str[4..$]); … … 615 623 616 624 PObject[num] args = i[0..num]; 617 auto ret = that.Action!(act)(args);625 auto ret = SpecialAction!(Tp,act)(that,args); 618 626 619 627 auto listO = cast(PObjectList!(PObject)) i[num]; … … 622 630 foreach(obj; list) 623 631 { 624 auto Tobj = cast(PInterfaceLeftFactor!(T ))obj;632 auto Tobj = cast(PInterfaceLeftFactor!(Tp))obj; 625 633 ret = Tobj.InsertLeft(that,ret); 626 634 } … … 628 636 } 629 637 630 template SpecialAction(T,char[] str) 631 { 638 639 template GetSize(Tp, char[] str) 640 { 641 alias Tp.Action!(str) Fn; 642 static if(is(typeof(Fn) Args == function)) 643 const int size = Args[0].length; 644 else 645 static assert(false); 646 } 647 648 template GetArgs(Tp, char[] str) 649 { 650 alias Tp.Action!(str) Fn; 651 static if(is(typeof(Fn) Args == function)) 652 {} 653 else 654 static assert(false); 655 } 656 657 /// Next actions 658 659 template N_parts(char[] str) 660 { 661 const int num = ExtractCount(str[4..$]); 662 const char[] firstAct = ExtractAct(str[4..$]); 663 const char[] s1 = str[FindChar!(',')(str[4..$]).length + 4..$]; 664 const char[] secondAct = s1[FindChar!(',')(s1).length..$-1]; 665 } 666 667 //alias N_parts!("$(N,3,dummy,dummy)") NP; 668 669 PObject N_Action(Tp,char[] str) (Tp that, GetArgs!(Tp,N_parts!(str).firstAct).Args i) 670 { 671 alias N_parts!(str) P; 672 static if(false) pragma(msg, str~" == $(N,"~P.num.stringof~","~P.act~")"); 673 674 // pragma(msg,">"~str); pragma(msg,">"~P.firstAct); pragma(msg,">"~P.secondAct); 675 676 PObject[] ret; 677 ret[0] = SpecialAction!(Tp,P.firstAct)(that,i); 678 ret[0] = SpecialAction!(Tp,P.secondAct)(that,ret); 679 680 return ret[0]; 681 } 682 683 684 685 686 template SpecialAction(Tp,char[] str) 687 { 688 // pragma(msg,str); 632 689 static if(str.length > 3 && str[0..2] == "$(") 633 690 { 691 static if(false) pragma(msg, str); 634 692 static if(str[2] == 'T') 635 alias T_Action!(T ,str) SpecialAction;693 alias T_Action!(Tp,str) SpecialAction; 636 694 else static if(str[2] == 'L') 637 alias L_Action!(T,str) SpecialAction; 695 alias L_Action!(Tp,str) SpecialAction; 696 else static if(str[2] == 'N') 697 { 698 alias N_Action!(Tp,str) SpecialAction; 699 } 638 700 else 639 701 static assert (false, "unknown special action: "~str); 640 702 } 641 703 else 642 static assert (false, "unknown special action: "~str); 704 { 705 706 PObject SpecialAction(Tp that, GetArgs!(Tp,str).Args i) 707 { 708 return that.Action!(str)(i); 709 } 710 //static assert (false, "unknown special action: "~str); 711 } 643 712 } 644 713 … … 663 732 } 664 733 } 665 struct UnittetParse_ID{ 666 // Tests 667 alias Parse_ID!(" \thello world") Parse_ID_test1; 668 static assert(Parse_ID_test1.without =="hello world","Parse_ID failed: "~Parse_ID_test1.without); 669 static assert(Parse_ID_test1.Remaining == " world", "Parse_ID failed: "~Parse_ID_test1.Remaining); 670 static assert(Parse_ID_test1.Text == "hello", "Parse_ID failed: "~Parse_ID_test1.Text); 671 static assert(Parse_ID_test1.Match, "Parse_ID failed: "); 672 static assert(Parse_ID_test1.Used == " \thello", "Parse_ID failed: \""~Parse_ID_test1.Used~\"); 673 674 alias Parse_ID!(" \t!hello world") Parse_ID_test2; 675 static assert(!Parse_ID_test2.Match,"Parse_ID failed: "); 676 } 677 678 679 /************************************************ 680 Parse an Sepcial action name name 734 struct UnittetParse_ID 735 { 736 // Tests 737 alias Parse_ID!(" \thello world") Parse_ID_test1; 738 static assert(Parse_ID_test1.without =="hello world","Parse_ID failed: "~Parse_ID_test1.without); 739 static assert(Parse_ID_test1.Remaining == " world", "Parse_ID failed: "~Parse_ID_test1.Remaining); 740 static assert(Parse_ID_test1.Text == "hello", "Parse_ID failed: "~Parse_ID_test1.Text); 741 static assert(Parse_ID_test1.Match, "Parse_ID failed: "); 742 static assert(Parse_ID_test1.Used == " \thello", "Parse_ID failed: \""~Parse_ID_test1.Used~\"); 743 744 alias Parse_ID!(" \t!hello world") Parse_ID_test2; 745 static assert(!Parse_ID_test2.Match,"Parse_ID failed: "); 746 } 747 748 749 char[] MatchPairs(char I, char O)(char[] str) 750 { 751 int i = 1; 752 foreach(int j, char c; str) 753 { 754 if (c == I) i++; 755 if (c == O) i--; 756 if (i == 0) return str[0..j+1]; 757 } 758 return ""; 759 } 760 761 762 /************************************************ 763 Parse a Sepcial action name name 681 764 '$([^)]*)' | 682 765 '$[A-Z][A-Za-z0-9_]*' | … … 688 771 //pragma(msg,">>"__FILE__":"~__LINE__.stringof[0..$-1]~": '"~without~\'); 689 772 690 static if(without.length == 0 || without[0] != '$') 691 { 692 pragma(msg,">>"__FILE__":"~__LINE__.stringof[0..$-1]~": unknown Special '"~without~\'); 773 //pragma(msg,__LINE__.stringof~":"~without); 774 775 static if(without.length < 4 || without[0..2] != "$(") 776 { 777 pragma(msg,">>"__FILE__":"~__LINE__.stringof[0..$-1]~": unknown Special '"~without~"' from '"~str~\'); 693 778 const bool Match = false; 694 779 } 695 780 else 696 781 { 697 static if( 698 without.length == 1 || 699 without[1] == ' ' || 700 without[1] == '\t' || 701 without[1] == '\n' || 702 without[1] == '\r' 703 ) 704 { 705 const bool Match = true; 706 const char[] Text = without[0..1]; 707 } 708 else static if(without[1] == '(') 709 { 710 const bool Match = true; 711 const char[] Text = FindChar!(')')(without); 712 } 713 else static if(without.length > 2 && 'A' <= without[1] && without[1] <= 'Z') 714 { 715 const bool Match = true; 716 const char[] Text = without[0..2] ~ GetID(without[2..$]); 717 } 718 else 719 { 720 pragma(msg,">>"__FILE__":"~__LINE__.stringof[0..$-1]~": unknown Special '"~without~\'); 721 const bool Match = false; 722 const char[] Text = ""; 723 } 724 725 const char[] Remaining = without[Text.length .. $]; 726 const char[] Used = str[0 .. $-Remaining.length]; 727 } 728 } 782 const char[] t = MatchPairs!('(',')')(without[2..$]); 783 784 //pragma(msg,__LINE__.stringof~":"~t); 785 const bool Match = (t != ""); 786 static if(Match) 787 { 788 const char[] Text = without[0..2+t.length]; 789 const char[] Remaining = without[Text.length .. $]; 790 const char[] Used = str[0 .. $-Remaining.length]; 791 } 792 } 793 //pragma(msg,__LINE__.stringof~":"~Match.stringof); 794 } 795 729 796 struct UnittetParse_Parse_SpecialAct{ 730 797 // Tests 731 static assert(Parse_SpecialAct!("$Ltree ").Match);732 798 static assert(Parse_SpecialAct!("$(Ltree) ").Match); 733 static assert(Parse_SpecialAct!("$ ").Match);734 799 735 800 //static assert(!Parse_SpecialAct!("$a ").Match); … … 885 950 alias act_1 act; 886 951 const bool Special = false; 952 //pragma(msg,__LINE__.stringof~":"~str); 887 953 } 888 954 else … … 890 956 alias Parse_SpecialAct!(str) act; 891 957 const bool Special = act.Match; 958 //pragma(msg,__LINE__.stringof~":"~Special.stringof~":"~str); 892 959 } 893 960 … … 905 972 } 906 973 else 974 { 975 //pragma(msg,__LINE__.stringof~":"~n1); 907 976 static const bool Match = false; 977 } 908 978 } 909 979 else 980 { 981 //pragma(msg,__LINE__.stringof~":"~str); 910 982 static const bool Match = false; 911 912 } 983 } 984 985 } 986 987 913 988 struct UnittestParse_Case 914 989 { … … 941 1016 static if(t_p[$-1] == '|') 942 1017 { 1018 //pragma(msg,__LINE__.stringof~":"~str); 943 1019 const char[] t = t_p[0..$-1]; 944 1020 } 945 1021 else static if(t_s[$-1] == ';') 946 1022 { 1023 //pragma(msg,__LINE__.stringof~":"~str); 947 1024 const char[] t = t_s[0..$-1]; 948 1025 } … … 982 1059 else 983 1060 { 1061 //pragma(msg,__LINE__.stringof~":"~str); 984 1062 static const bool Match = false; 985 1063 } 986 1064 } 1065 1066 987 1067 struct UnittestParse_Cases 988 1068 { … … 1009 1089 struct Parse_Rule (char[] str) 1010 1090 { 1091 // parse off the rule name 1011 1092 static private alias Parse_ID!(str) name; 1012 1093 static if(name.Match) 1013 1094 { 1095 // parse off to the ':' 1014 1096 static private const char[] n1 = DropWhiteF(name.Remaining); 1015 1097 static if(n1.length > 1 && n1[0] == ':') 1016 1098 { 1099 // pares the cases 1017 1100 static private alias Parse_Cases!(n1[1..$]) cases; 1018 1101 static if(cases.Match) … … 1029 1112 else 1030 1113 { 1114 //pragma(msg,__LINE__.stringof~":"~n2~\n\n); 1031 1115 static const bool Match = false; 1032 1116 } … … 1034 1118 else 1035 1119 { 1120 //pragma(msg,__LINE__.stringof~":"~str); 1036 1121 static const bool Match = false; 1037 1122 } … … 1039 1124 else 1040 1125 { 1126 //pragma(msg,__LINE__.stringof~":"~str); 1041 1127 static const bool Match = false; 1042 1128 } … … 1044 1130 else 1045 1131 { 1132 //pragma(msg,__LINE__.stringof~":"~str); 1046 1133 static const bool Match = false; 1047 1134 } … … 1060 1147 } 1061 1148 1062 1063 /************************************************1064 Parse a Grammar (Grammar ::= Rule Grammar | Rule)1065 */1066 1067 1149 char[] strOf(int v) 1068 1150 { … … 1102 1184 1103 1185 1186 /************************************************ 1187 Parse a Grammar (Grammar ::= Rule Grammar | Rule) 1188 */ 1104 1189 template Parse_Grammar(char[] str) 1105 1190 { … … 1190 1275 1191 1276 static const char[] nameIs = rule.Name; 1192 debug(dParse_light) pragma(msg, nameIs); 1277 //debug(dParse_light) 1278 pragma(msg, "<used>"~nameIs~"</used>"); 1193 1279 1194 1280 Stack!(Frame) backups; … … 1256 1342 // get one 1257 1343 debug(dparse_verbose) pragma(msg, "\t\t\t recurse in from \""~rule.Name~"\" on \""~cl.Text~\"\n); 1344 alias DotAction!(nameIs, cl.Text) _; 1258 1345 auto tmpStore1 = parserBase.Terminal!(cl.Text)(p); 1259 1346 assert(tmpStore1 !is null); … … 1312 1399 1313 1400 debug(dparse_verbose) pragma(msg, "\t\t\trecurse in from \""~rule.Name~"\" on \""~cl.Text~\"\n); 1401 alias DotAction!(nameIs, cl.Text) _; 1314 1402 auto tmpStore2 = parserBase.Terminal!(cl.Text)(p); 1315 1403 assert(tmpStore2 ! is null); … … 1338 1426 debug(dparse_verbose) pragma(msg,"\tdoing Action \""~casev.Action~\"); 1339 1427 debug(dParse_runtime) writef("doing Action \""~casev.Action~\"\n); 1340 static if(casev.Special) 1341 auto ret = SpecialAction!(ParserBase,casev.Action)(parserBase,temps); 1342 else 1343 auto ret = parserBase.Action!(casev.Action)(temps); 1428 auto ret = SpecialAction!(ParserBase,casev.Action)(parserBase,temps); 1344 1429 debug(dParse_runtime) writef("Action \""~casev.Action~"\" done\n"); 1345 1430 debug(dParse_runtime) writef("\treturn (%d)Act '%s' fail=%s\n", ind, casev.Action, ret.fail); … … 1350 1435 return new PObjectFail("Failed while looking for "~nameIs~\n);// debug(dparse_verbose) pragma(msg, ">>"__FILE__":"~itoa!(__LINE__)~": is this right?"); 1351 1436 debug(dparse_verbose) pragma(msg, "Done: "~rule.Name); 1437 } 1438 1439 template DotAction(char[] from, char[] to) 1440 { 1441 pragma(msg, from ~ " -> " ~ to); 1352 1442 } 1353 1443 … … 1496 1586 "; 1497 1587 1498 //const char[] gram2 = import("dmp.g");1588 const char[] gram2 = import("dmp.g"); 1499 1589 1500 1590 PObject Terminal(char[] str)(IParser i){return null;}
