Changeset 268

Show
Ignore:
Timestamp:
03/28/08 13:37:49 (7 months ago)
Author:
BCS
Message:

added a bunch of stuff for processing the D html docs into a usable grammar (it's not all there but it will be at some point, I hope)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dparser/dparse.d

    r267 r268  
    357357    left side term with a Right side term 
    358358*/ 
    359 class PObjectLeftFactorT(T, int i, char[] str) : PObjectVector!(i), PInterfaceLeftFactor!(T
     359class PObjectLeftFactorT(Tp, int i, char[] str) : PObjectVector!(i), PInterfaceLeftFactor!(Tp
    360360{ 
    361361    this(PObject[i] d){super(d);} 
    362362 
    363     PObject InsertLeft(T that, PObject L) 
     363    PObject InsertLeft(Tp that, PObject L) 
    364364    { 
    365365        PObject[i+1] args; 
     
    368368        args[0] = L; 
    369369 
    370         return that.Action!(str)(args); 
     370        return SpecialAction!(Tp,str)(that,args); 
    371371    } 
    372372} 
     
    588588    str = str[i..$]; 
    589589    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 
     601static assert(ExtractAct("$(T,1,$(N,1,abc,def))") == "$(N,1,abc,def)", ExtractAct("$(T,1,$(N,1,abc,def))")); 
    594602 
    595603/****************************************************************************** 
     
    598606 
    599607/// Pack up stuff in a computed type of PObjectLeftFactorT Object 
    600 PObject L_Action(T, char[] str ) (T, PObject[ExtractCount(str[4..$])] i) 
     608PObject L_Action(Tp, char[] str ) (Tp, PObject[ExtractCount(str[4..$])] i) 
    601609{ 
    602610    static const int c = ExtractCount(str[4..$]); 
     
    604612    static if(false) pragma(msg, str~" == $(L,"~c.stringof~","~n~")"); 
    605613 
    606     return new PObjectLeftFactorT!(T, c, n)(i); 
     614    return new PObjectLeftFactorT!(Tp, c, n)(i); 
    607615} 
    608616 
    609617/// Left Process a Leftmost term and right side terms list. 
    610 PObject T_Action(T,char[] str) (T that, PObject[1+ExtractCount(str[4..$])] i) 
     618PObject T_Action(Tp,char[] str) (Tp that, PObject[1+ExtractCount(str[4..$])] i) 
    611619{ 
    612620    static const int num = ExtractCount(str[4..$]); 
     
    615623 
    616624    PObject[num] args = i[0..num]; 
    617     auto ret = that.Action!(act)(args); 
     625    auto ret = SpecialAction!(Tp,act)(that,args); 
    618626 
    619627    auto listO = cast(PObjectList!(PObject)) i[num];  
     
    622630    foreach(obj; list) 
    623631    { 
    624         auto Tobj = cast(PInterfaceLeftFactor!(T))obj; 
     632        auto Tobj = cast(PInterfaceLeftFactor!(Tp))obj; 
    625633        ret = Tobj.InsertLeft(that,ret); 
    626634    } 
     
    628636} 
    629637 
    630 template SpecialAction(T,char[] str) 
    631 
     638 
     639template 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 
     648template 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 
     659template 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 
     669PObject 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 
     686template SpecialAction(Tp,char[] str) 
     687
     688//  pragma(msg,str); 
    632689    static if(str.length > 3 && str[0..2] == "$(") 
    633690    { 
     691        static if(false) pragma(msg, str); 
    634692        static if(str[2] == 'T') 
    635             alias T_Action!(T,str) SpecialAction; 
     693            alias T_Action!(Tp,str) SpecialAction; 
    636694        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        } 
    638700        else 
    639701            static assert (false, "unknown special action: "~str); 
    640702    } 
    641703    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    } 
    643712} 
    644713 
     
    663732    } 
    664733} 
    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 
     734struct 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 
     749char[] 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 
    681764    '$([^)]*)' | 
    682765    '$[A-Z][A-Za-z0-9_]*' | 
     
    688771    //pragma(msg,">>"__FILE__":"~__LINE__.stringof[0..$-1]~": '"~without~\'); 
    689772 
    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~\'); 
    693778        const bool Match = false; 
    694779    } 
    695780    else 
    696781    { 
    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 
    729796struct UnittetParse_Parse_SpecialAct{ 
    730797    // Tests 
    731     static assert(Parse_SpecialAct!("$Ltree ").Match); 
    732798    static assert(Parse_SpecialAct!("$(Ltree) ").Match); 
    733     static assert(Parse_SpecialAct!("$ ").Match); 
    734799 
    735800    //static assert(!Parse_SpecialAct!("$a ").Match); 
     
    885950        alias act_1 act; 
    886951        const bool Special = false; 
     952        //pragma(msg,__LINE__.stringof~":"~str); 
    887953    } 
    888954    else 
     
    890956        alias Parse_SpecialAct!(str) act; 
    891957        const bool Special = act.Match; 
     958        //pragma(msg,__LINE__.stringof~":"~Special.stringof~":"~str); 
    892959    } 
    893960 
     
    905972        } 
    906973        else 
     974        { 
     975            //pragma(msg,__LINE__.stringof~":"~n1); 
    907976            static const bool Match = false; 
     977        } 
    908978    } 
    909979    else 
     980    { 
     981        //pragma(msg,__LINE__.stringof~":"~str); 
    910982        static const bool Match = false; 
    911  
    912 
     983    } 
     984 
     985
     986 
     987 
    913988struct UnittestParse_Case 
    914989{ 
     
    9411016    static if(t_p[$-1] == '|') 
    9421017    { 
     1018        //pragma(msg,__LINE__.stringof~":"~str); 
    9431019        const char[] t = t_p[0..$-1]; 
    9441020    } 
    9451021    else static if(t_s[$-1] == ';') 
    9461022    { 
     1023        //pragma(msg,__LINE__.stringof~":"~str); 
    9471024        const char[] t = t_s[0..$-1]; 
    9481025    } 
     
    9821059    else 
    9831060    { 
     1061        //pragma(msg,__LINE__.stringof~":"~str); 
    9841062        static const bool Match = false; 
    9851063    } 
    9861064} 
     1065 
     1066 
    9871067struct UnittestParse_Cases 
    9881068{ 
     
    10091089struct Parse_Rule (char[] str) 
    10101090{ 
     1091        // parse off the rule name 
    10111092    static private alias Parse_ID!(str) name; 
    10121093    static if(name.Match) 
    10131094    { 
     1095            // parse off to the ':' 
    10141096        static private const char[] n1 = DropWhiteF(name.Remaining); 
    10151097        static if(n1.length > 1 && n1[0] == ':') 
    10161098        { 
     1099                // pares the cases 
    10171100            static private alias Parse_Cases!(n1[1..$]) cases; 
    10181101            static if(cases.Match) 
     
    10291112                else 
    10301113                { 
     1114                    //pragma(msg,__LINE__.stringof~":"~n2~\n\n); 
    10311115                    static const bool Match = false; 
    10321116                } 
     
    10341118            else 
    10351119            { 
     1120                //pragma(msg,__LINE__.stringof~":"~str); 
    10361121                static const bool Match = false; 
    10371122            } 
     
    10391124        else 
    10401125        { 
     1126            //pragma(msg,__LINE__.stringof~":"~str); 
    10411127            static const bool Match = false; 
    10421128        } 
     
    10441130    else 
    10451131    { 
     1132        //pragma(msg,__LINE__.stringof~":"~str); 
    10461133        static const bool Match = false; 
    10471134    } 
     
    10601147} 
    10611148 
    1062  
    1063 /************************************************ 
    1064     Parse a Grammar (Grammar ::= Rule Grammar | Rule) 
    1065 */ 
    1066  
    10671149char[] strOf(int v) 
    10681150{ 
     
    11021184 
    11031185 
     1186/************************************************ 
     1187    Parse a Grammar (Grammar ::= Rule Grammar | Rule) 
     1188*/ 
    11041189template Parse_Grammar(char[] str) 
    11051190{ 
     
    11901275 
    11911276    static const char[] nameIs = rule.Name; 
    1192     debug(dParse_light) pragma(msg, nameIs); 
     1277    //debug(dParse_light)  
     1278    pragma(msg, "<used>"~nameIs~"</used>"); 
    11931279 
    11941280    Stack!(Frame) backups; 
     
    12561342                        // get one 
    12571343                    debug(dparse_verbose) pragma(msg, "\t\t\t recurse in from \""~rule.Name~"\" on \""~cl.Text~\"\n); 
     1344                        alias DotAction!(nameIs, cl.Text) _; 
    12581345                    auto tmpStore1 = parserBase.Terminal!(cl.Text)(p); 
    12591346                    assert(tmpStore1 !is null); 
     
    13121399 
    13131400                    debug(dparse_verbose) pragma(msg, "\t\t\trecurse in from \""~rule.Name~"\" on \""~cl.Text~\"\n); 
     1401                        alias DotAction!(nameIs, cl.Text) _; 
    13141402                    auto tmpStore2 = parserBase.Terminal!(cl.Text)(p); 
    13151403                    assert(tmpStore2 ! is null); 
     
    13381426        debug(dparse_verbose) pragma(msg,"\tdoing Action \""~casev.Action~\"); 
    13391427        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); 
    13441429        debug(dParse_runtime) writef("Action \""~casev.Action~"\" done\n"); 
    13451430        debug(dParse_runtime) writef("\treturn (%d)Act '%s' fail=%s\n", ind, casev.Action, ret.fail); 
     
    13501435    return new PObjectFail("Failed while looking for "~nameIs~\n);//    debug(dparse_verbose) pragma(msg, ">>"__FILE__":"~itoa!(__LINE__)~": is this right?"); 
    13511436    debug(dparse_verbose) pragma(msg, "Done: "~rule.Name); 
     1437} 
     1438 
     1439template DotAction(char[] from, char[] to) 
     1440{ 
     1441    pragma(msg, from ~ " -> " ~ to); 
    13521442} 
    13531443 
     
    14961586                "; 
    14971587 
    1498 //        const char[] gram2 = import("dmp.g"); 
     1588      const char[] gram2 = import("dmp.g"); 
    14991589 
    15001590        PObject Terminal(char[] str)(IParser i){return null;}