Changeset 239

Show
Ignore:
Timestamp:
08/13/06 08:07:36 (2 years ago)
Author:
pragma
Message:

Fixed bugs:

  • type conversion bug in types.d
  • maintained order when generating rule parameter lists

Library update:

  • D Tokenizer compiles (not tested)
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/enki/BaseParser.d

    r235 r239  
    5151    protected ErrorData[] errors; 
    5252     
    53     public this(){      
     53    public this(){ 
    5454    } 
    5555     
  • trunk/enki/Directive.d

    r235 r239  
    269269        return "Alias Directive"; 
    270270    }    
     271     
     272    public void render(CodeGenerator generator,Statement passterm,Statement failterm){ 
     273        generator.emit("alias parse_" ~ rule ~ " parse_" ~ ruleAlias ~ ";"); 
     274    } 
    271275} 
    272276 
  • trunk/enki/EnkiBackend.d

    r235 r239  
    6767    String header; 
    6868    String utf; 
    69     UserProduction[] userProductions; 
     69    UserProduction[String] userProductions; 
    7070 
    7171    public this(){ 
     
    121121         
    122122    public void defineUserProduction(String returnType,String name,String description){ 
    123         userProductions ~= UserProduction(returnType,name,description); 
     123        if(name in rules || name in userProductions){ 
     124            throw new Exception("Rule '" ~ name ~ "' is already defined."); 
     125        } 
     126        debug writefln("defined user production: %s",name); 
     127        userProductions[name] = UserProduction(returnType,name,description); 
    124128    } 
    125129     
     
    145149            rules[aliasName] = rules[ruleName]; 
    146150        } 
     151        else if(ruleName in userProductions){ 
     152            debug writefln("%s aliased to user production %s",aliasName,ruleName); 
     153            userProductions[aliasName] = userProductions[ruleName]; 
     154        } 
    147155        else{ 
    148156            throw new Exception("Cannot alias '" ~ aliasName ~ "'. Rule '" ~ ruleName ~ "' is not defined."); 
     
    161169    public String getTypeForRule(String name){ 
    162170        String type; 
    163         foreach(uProd; userProductions){ 
    164             if(uProd.name == name){ 
    165                 type = uProd.returnType; 
    166                 break; 
    167             } 
     171        if(name in userProductions){ 
     172            type = userProductions[name].returnType; 
    168173        } 
    169174        if(!type){ 
     
    177182     
    178183    public bool isTerminal(String name){ 
    179         foreach(uProd; userProductions){ 
    180             if(uProd.name == name){ 
    181                 return uProd.isTerminal; 
    182             } 
    183         } 
     184        if(name in userProductions){ 
     185            return userProductions[name].isTerminal; 
     186        }        
    184187        return true; 
    185188    } 
     
    187190    public String getTerminalName(String terminalName){ 
    188191        String name; 
    189         foreach(uProd; userProductions){ 
    190             if(uProd.name == terminalName){ 
    191                 name = uProd.description; 
    192             } 
     192        if(name in userProductions){ 
     193            return userProductions[name].description; 
    193194        } 
    194195        if(!name) name = terminalName; 
  • trunk/enki/Expression.d

    r237 r239  
    574574         
    575575        with(generator){ 
    576             auto startLabel = getUniqueLabel("start");               
     576            auto startLabel = getUniqueLabel("start"); 
     577            auto termPos = getUniqueLabel("termPos");                
    577578            emit("{//ZeroOrMoreExpr");  
    578579            indent(); 
    579580                if(term) emit("uint " ~ startLabel ~ " = position;"); 
    580                 emit("uint termPos;"); 
     581                emit("uint " ~ termPos ~ ";"); 
    581582                render(loopStart); 
    582583     
    583                 emit("termPos = position;"); 
     584                emit(termPos ~ " = position;"); 
    584585                if(term){ 
    585586                    renderPass(term,gotoLoopEnd); 
     
    594595                 
    595596                render(loopEnd); 
    596                 if(binding) emit(binding.assignExpr(generator.getParseType(),"sliceData(" ~ startLabel ~ ",termPos)")~";"); 
     597                if(binding) emit(binding.assignExpr(generator.getParseType(),"sliceData(" ~ startLabel ~ "," ~ termPos ~ ")")~";"); 
    597598                render(pass); 
    598599            unindent(); 
     
    653654    public String toBNF(){ 
    654655        if(binding){ 
    655             return "\"" ~ text ~ "\"" ~ binding.toBNF(); 
    656         } 
    657         else{ 
    658             return "\"" ~ text ~ "\""; 
     656            return " \"" ~ text ~ "\"" ~ binding.toBNF(); 
     657        } 
     658        else{ 
     659            return " \"" ~ text ~ "\""; 
    659660        } 
    660661    }    
  • trunk/enki/Rule.d

    r237 r239  
    4141    bool semanticDone; 
    4242    Param[String] params; 
     43     
     44    protected this(){ 
     45    } 
    4346         
    4447    public this(String name,RulePredicate pred,Expression expr,RuleDecl decl=null){ 
     
    129132                    emit("debug writefln(\"parse_" ~ name ~ "()\");"); 
    130133                    emit("uint " ~ startLabel ~ " = position;"); 
    131                     pred.renderDeclarations(generator,decl.params); 
     134                    pred.renderDeclarations(generator,decl.paramXRef); 
    132135                    emit(""); 
    133136                    foreach(param; params){ 
     
    162165 
    163166class RuleDecl{ 
    164     Param[String] params; 
     167    Param[String] paramXRef; 
     168    Param[] params; 
    165169     
    166170    public this(Param[] params...){ 
    167171        foreach(param; params){ 
    168             this.params[param.name] = param; 
     172            this.params ~= param; 
     173            this.paramXRef[param.name] = param; 
    169174        } 
    170175    } 
     
    199204     
    200205    public String resolveBindingType(String name){ 
    201         foreach(param; params){ 
    202             if(param.name == name) return param.realType; 
     206        if(name in paramXRef){ 
     207            return paramXRef[name].realType; 
    203208        } 
    204209        return null; 
  • trunk/enki/library/d/CharEntity.bnf

    r235 r239  
    2626NamedCharacterEntity  
    2727    = String tok 
    28     ::= 
    29         "quot"          @literal!("""):tok | 
     28    ::= "quot"          @literal!("""):tok | 
    3029        "amp"           @literal!("&"):tok | 
    3130        "lt"            @literal!("<"):tok | 
  • trunk/enki/library/d/CharEntity.d

    r238 r239  
    2222    OTHER DEALINGS IN THE SOFTWARE. 
    2323+/ 
    24 module enki.library.d.Entity; 
     24module enki.library.d.CharEntity; 
    2525 
    2626private import enki.types; 
     
    8383    charEntity["deg"]       = "°"; 
    8484    charEntity["plusmn"]    = "±"; 
    85     charEntity["sup"]2        = "²"; 
    86     charEntity["sup"]3        = "³"; 
     85    charEntity["sup2"]        = "²"; 
     86    charEntity["sup3"]        = "³"; 
    8787    charEntity["acute"]     = "´"; 
    8888    charEntity["micro"]     = "µ"; 
     
    9090    charEntity["middot"]    = "·"; 
    9191    charEntity["cedil"]     = "¸"; 
    92     charEntity["sup"]1        = "¹"; 
     92    charEntity["sup1"]        = "¹"; 
    9393    charEntity["ordm"]      = "º"; 
    9494    charEntity["raquo"]     = "»"; 
    95     charEntity["frac"]14  = "¼"; 
    96     charEntity["frac"]12  = "½"; 
    97     charEntity["frac"]34  = "¾"; 
     95    charEntity["frac14"]  = "¼"; 
     96    charEntity["frac12"]  = "½"; 
     97    charEntity["frac34"]  = "¾"; 
    9898    charEntity["iquest"]    = "¿"; 
    9999    charEntity["agrave"]    = "À"; 
  • trunk/enki/library/d/Keyword.bnf

    r235 r239  
    2828 
    2929Keyword 
    30     = Token Token.keyword(tok) 
     30    = Token Token.keyword(Tok tok) 
    3131    ::= "abstract"      @Tok.Abstract:tok       |     
    3232        "alias"         @Tok.Alia:tok           |         
  • trunk/enki/library/d/Keyword.d

    r235 r239  
    2222    OTHER DEALINGS IN THE SOFTWARE. 
    2323+/ 
     24module enki.library.d.Keyword; 
    2425 
    2526private import enki.types; 
    26 private import d.Token; 
     27private import enki.library.d.Token; 
    2728 
    28 uint keywordToken[String]; 
    29 String keywordXref[uint]; 
     29Tok keywordToken[String]; 
     30String keywordXref[Tok]; 
    3031 
    3132static this(){ 
  • trunk/enki/library/d/Operator.d

    r235 r239  
    2222    OTHER DEALINGS IN THE SOFTWARE. 
    2323+/ 
     24module enki.library.d.Operator; 
    2425 
    2526private import enki.types; 
    26 private import d.Token; 
     27private import enki.library.d.Token; 
    2728 
    28 uint operatorToken[String]; 
    29 String operatorXref[uint]; 
     29Tok operatorToken[String]; 
     30String operatorXref[Tok]; 
    3031 
    3132static this(){ 
  • trunk/enki/library/d/Token.d

    r235 r239  
    2222    OTHER DEALINGS IN THE SOFTWARE. 
    2323+/ 
    24 module TokenizerBase
     24module enki.library.d.Token
    2525/** 
    2626    D Token definitions 
     
    2828 
    2929private import enki.types; 
    30 private import d.Keyword; 
     30private import enki.library.d.Keyword; 
     31private import enki.library.d.Operator; 
    3132 
    3233alias ulong IntegerValue; //TODO: turn into a more advanced type 
    3334 
    34 struct FloatingPoint
     35struct FloatingPointValue
    3536    IntegerValue whole; 
    3637    IntegerValue fraction; 
     
    3940    bool imaginary; 
    4041     
    41     static FloatingPoint opCall(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign){ 
    42         FloatingPoint _this; 
     42    static FloatingPointValue opCall(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign){ 
     43        FloatingPointValue _this; 
    4344        _this.whole = whole; 
    4445        _this.fraction = fraction; 
     
    4950} 
    5051 
    51 struct LineDirective
     52struct LineDirectiveValue
    5253    IntegerValue line; 
    5354    String filespec; 
    5455     
    55     static LineDirective opCall(IntegerValue line,String filespec){ 
    56         LineDirective _this; 
     56    static LineDirectiveValue opCall(IntegerValue line,String filespec){ 
     57        LineDirectiveValue _this; 
    5758        _this.line = line; 
    5859        _this.filespec = filespec; 
    59         returh _this; 
     60        return _this; 
    6061    } 
    6162} 
     
    169170    Wchar, 
    170171    While, 
    171     With 
     172    With, 
    172173 
    173174    /* operator types */ 
     
    257258        String strValue; 
    258259        IntegerValue integerValue; 
    259         FloatingPoint floatingValue; 
    260         LineDirective lineDirective; 
     260        FloatingPointValue floatingValue; 
     261        LineDirectiveValue lineDirective; 
    261262    } 
    262263     
     
    279280        Token _this; 
    280281        _this.type = Keyword; 
    281         _this.value.ulongValue = id; 
     282        _this.value.token = id; 
    282283        return _this; 
    283284    } 
     
    286287        Token _this; 
    287288        _this.type = Operator; 
    288         _this.value.ulongValue = id; 
     289        _this.value.token = id; 
    289290        return _this; 
    290291    } 
     
    320321    } 
    321322         
    322     public static Token floatingLiteral(FloatingPoint value,TokenSubtype subtype,bool isImaginary){ 
    323         Token _this; 
    324         _this.type = TokenType.FloatingPoint; 
     323    public static Token floatingLiteral(FloatingPointValue value,TokenSubtype subtype,bool isImaginary){ 
     324        Token _this; 
     325        _this.type = FloatingPoint; 
    325326        _this.subtype = subtype; 
    326327        _this.value.floatingValue = value; 
     
    339340        Token _this; 
    340341        _this.type = LineDirective; 
    341         _this.value.lineDirective = LineDirective(line,filespec); 
     342        _this.value.lineDirective = LineDirectiveValue(line,filespec); 
    342343        return _this; 
    343344    } 
     
    347348            return value.strValue; 
    348349        } 
    349         else if(type == Token.Keyword) 
     350        else if(type == Token.Keyword){ 
    350351            return keywordXref[value.token]; 
    351352        } 
  • trunk/enki/library/d/Tokenizer.bnf

    r235 r239  
    2828 
    2929.parsetype("String"); 
    30  
    31 .module(d.Tokenizer); 
    32 .import(d.TokenizerBase); 
    33 .import(d.Token); 
     30.utf("32"); 
     31 
     32.module(enki.library.d.Tokenizer); 
     33 
     34.import(enki.library.d.TokenizerBase); 
     35.import(enki.library.d.Token); 
     36.import(enki.library.d.Keyword); 
     37.import(enki.library.d.Operator); 
     38.import(enki.library.d.CharEntity); 
     39 
     40 
    3441.baseclass(TokenizerBase); 
    3542.classname(Tokenizer); 
    3643 
    3744.define(bool,eoi,false,"End of Input"); 
    38 .define("String","Letter","true","letter"); 
    39 .define("String","Digit","true","digit"); 
     45.define("String","letter","true","letter"); 
     46    .alias("letter","Letter"); 
     47.define("String","digit","true","digit"); 
     48    .alias("digit","Digit"); 
    4049 
    4150.include("enki/library/d/CharEntity.bnf"); 
    4251.include("enki/library/d/Character.bnf"); 
    43 .include("enki/library/d/OtherToken.bnf"); 
     52.include("enki/library/d/Operator.bnf"); 
    4453.include("enki/library/d/Keyword.bnf"); 
    4554 
     55/* BUG: there's something wrong with DMD 0.164 that causes this to not evauluate correctly if it's first 
    4656Syntax 
    4757    = Token[] tokens 
     
    5767        SpecialTokenSequence:~tokens 
    5868    } RealEndOfFile; 
    59          
     69*/ 
    6070##### Space, Comments and EOF ##### 
    6171     
     
    7888Comment 
    7989    = Token Token.comment(comment) 
    80     ::= ("/*" {Character}:comment "*/") |  
    81         ("//" {Character}:comment EndOfLine) |  
    82         ("/+" SlashPlusContent:comment); 
     90    ::= ("\x2F\x2A" {Character}:comment "\x2A\x2F") |  
     91        ("\x2F\x2F" {Character}:comment EndOfLine) |  
     92        ("\x2F\x2B" SlashPlusContent:comment); 
    8393 
    8494SlashPlusContent 
    8595    = String comment 
    86     ::= {Character}:comment ("+/" | ("/+" SlashPlusContent:~comment)); 
    87  
     96    ::= {Character}:comment ("\x2B\x2F" | ("\x2F\x2B" SlashPlusContent:~comment)); 
     97 
     98##HACK: Place this here for now to avoid problems with templates 
     99Syntax 
     100    = Token[] tokens 
     101    ::= { 
     102        Space:~tokens |  
     103        Token:~tokens |  
     104        StringLiteral:~tokens |  
     105        CharacterLiteral:~tokens |  
     106        IntegerLiteral:~tokens |  
     107        FloatLiteral:~tokens |  
     108        Keyword:~tokens |  
     109        SpecialToken:~tokens |  
     110        SpecialTokenSequence:~tokens 
     111    } RealEndOfFile;     
    88112     
    89113##### Tokens ##### 
     
    112136             
    113137UniversalAlpha  
    114     ::= ("\u" HexDigit HexDigit HexDigit HexDigit) | 
    115     ("\U" HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit); 
     138    ::= ("\x5Cu" HexDigit HexDigit HexDigit HexDigit) | 
     139    ("\x5CU" HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit HexDigit); 
    116140     
    117141     
     
    146170EscapeSequence 
    147171    = String value 
    148     ::= "\\" ( 
     172    ::= "\x5C" ( 
    149173        "'":value | 
    150174        "\"":value | 
     
    167191     
    168192HexChar2 
    169     = String hexToChar(digits) 
     193    = Char hexToChar(digits) 
    170194    ::= (HexDigit HexDigit):digits; 
    171195     
     
    187211 
    188212HexString 
    189     = Token Token.stringLiteral(str,width) 
     213    = Token Token.stringLiteral(String str,width) 
    190214    ::= "x\"" {HexStringChar:~str} "\"" [Postfix:width]; 
    191215 
    192216HexStringChar 
    193     = Char hexToChar(digits) 
     217    = Char hexToChar(String digits) 
    194218    ::= WhiteSpace HexDigit:~digits WhiteSpace HexDigit:~digits; 
    195219 
     
    220244 
    221245Integer 
    222     = Integer value 
     246    = IntegerValue value 
    223247    ::= Decimal:value | Binary:value | Octal:value | Hexadecimal:value; 
    224248 
     
    234258     
    235259Decimal 
    236     = Integer convertToDecimal(String value) 
     260    = IntegerValue convertToDecimal(String value) 
    237261    ::= ("0":~value | (NonZeroDigit:~value {"_"} {DecimalDigit:~value {"_"}})); 
    238262     
    239263DecimalDigits 
    240     = Integer convertToDecimal(value) 
     264    = IntegerValue convertToDecimal(String value) 
    241265    ::= {"_"} {DecimalDigit:~value {"_"}}; 
    242266     
    243267Binary 
    244     = Integer convertToBinary(value) 
     268    = IntegerValue convertToBinary(String value) 
    245269    ::= ("0b"|"0B") {"_"} {BinaryDigit:~value {"_"}}; 
    246270 
    247271Octal 
    248     = Integer convertToOctal(value) 
     272    = IntegerValue convertToOctal(String value) 
    249273    ::= "0" {"_"} {OctalDigit:~value {"_"}}; 
    250274 
     
    253277 
    254278Hexadecimal 
    255     = Integer convertToHex(value) 
     279    = IntegerValue convertToHex(String value) 
    256280    ::= HexPrefix {"_"} {HexDigit:~value {"_"}}; 
    257281 
    258282HexDigits 
    259     = Integer convertToHex(value) 
     283    = IntegerValue convertToHex(String value) 
    260284    ::= {"_"} {HexDigit:~value {"_"}}; 
    261285     
     
    285309 
    286310Float 
    287     = FloatingPoint value 
     311    = FloatingPointValue value 
    288312    ::= DecimalFloat:value | HexFloat:value; 
    289313 
    290314DecimalFloat 
    291     = FloatingPoint FloatingPoint(whole,fraction,exponent,exponentSign) 
     315    = FloatingPointValue FloatingPointValue(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign) 
    292316    ::= Decimal:whole "." [DecimalDigits:fraction [DecimalExponent!(exponent,exponentSign)]] | 
    293         "." DecimalDigits:fraction [DecimalExponent:exponent] | 
     317        "." DecimalDigits:fraction [DecimalExponent!(exponent,exponentSign):exponent] | 
    294318        Decimal:whole DecimalExponent!(exponent,exponentSign); 
    295319 
    296 DecimalExponent(Integer exponent,bool exponentSign) 
     320DecimalExponent(IntegerValue exponent,bool exponentSign) 
    297321    ::= "e" DecimalDigits:exponent  @true:exponentSign | 
    298322        "E" DecimalDigits:exponent  @true:exponentSign | 
     
    303327 
    304328HexFloat 
    305     = FloatingPoint FloatingPoint(whole,fraction,exponent,exponentSign) 
     329    = FloatingPointValue FloatingPointValue(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign) 
    306330    ::= Hexadecimal:whole "." [HexDigits:fraction [HexExponent!(exponent,exponentSign)]] | 
    307331        HexPrefix "." HexDigits:fraction [HexExponent!(exponent,exponentSign)] | 
    308332        Hexadecimal:whole HexExponent!(exponent,exponentSign); 
    309333 
    310 HexExponent(Integer exponent,bool exponentSign) 
     334HexExponent(IntegerValue exponent,bool exponentSign) 
    311335    ::= "p" DecimalDigits:exponent  @true:exponentSign | 
    312336        "P" DecimalDigits:exponent  @true:exponentSign | 
  • trunk/enki/library/d/TokenizerBase.d

    r235 r239  
    2222    OTHER DEALINGS IN THE SOFTWARE. 
    2323+/ 
     24module enki.library.d.TokenizerBase; 
     25 
     26private import enki.types; 
     27private import enki.BaseParser; 
     28 
     29private import enki.library.d.Token; 
    2430 
    2531class TokenizerBase : BaseParser{ 
     32     
     33    public this(){ 
     34    } 
     35         
    2636    dchar hexToChar(String value){ 
    2737        dchar result; 
     
    6272    } 
    6373     
    64     IntegerValue convertToHex(value){ 
     74    IntegerValue convertToHex(String value){ 
    6575        ubyte hexValue(Char ch){ 
    6676            if(ch >= 'a' && ch <= 'f') return ch - 'a'+10; 
     
    7787    } 
    7888     
    79     IntegerValue convertToDecimal(value){ 
     89    IntegerValue convertToDecimal(String value){ 
    8090        IntegerValue result = 0; 
    8191         
     
    8696    } 
    8797     
    88     IntegerValue convertToOctal(value){ 
     98    IntegerValue convertToOctal(String value){ 
    8999        IntegerValue result = 0; 
    90100         
     
    95105    } 
    96106     
    97     IntegerValue convertToBinary(value){ 
     107    IntegerValue convertToBinary(String value){ 
    98108        IntegerValue result = 0; 
    99109         
     
    110120    } 
    111121} 
    112  
    113  
    114 /* 
    115 D.1.4 Universal Character Names (UCN) 
    116  
    117 6.4.3 Universal character names 
    118  
    119 UCN allows the use of any character in a C source, not just English characters. A UCN has the format: 
    120  
    121     * \u 4_hex_digits_value 
    122  
    123     * \U 8_hex_digits_value 
    124  
    125 A UCN must not specify a value less than 00A0 other than 0024 ($), 0040 (@), or 0060 (?), nor a value in the range D800 through DFFF inclusive. 
    126  
    127 UCN may be used in identifiers, character constants, and string literals to designate characters that are not in the C basic character set. 
    128  
    129 The UCN \unnnnnnnn designates the character whose eight-digit short identifier (as specified by ISO/IEC 10646) is nnnnnnnn. Similarly, the universal character name ¯nnnn designates the character whose four-digit short identifier is nnnn (and whose eight-digit short identifier is 0000nnnn).  
    130 */ 
  • trunk/enki/types.d

    r236 r239  
    3333    alias dchar Char; 
    3434    alias toUTF32 transcodeToString; 
     35    alias toUTF8 transcodeToUTF8; 
    3536} 
    3637else version(EnkiUTF16){ 
     
    3839    alias wchar Char; 
    3940    alias toUTF16 transcodeToString; 
     41    alias toUTF8 transcodeToUTF8; 
    4042} 
    4143else{ 
     
    4345    alias char Char; 
    4446    alias toUTF8 transcodeToString; 
     47    char[] transcodeToUTF8(char[] value){ return value; } 
    4548} 
    4649 
     
    6366            } 
    6467            else{ 
    65                 return transcodeToString(std.string.toString(value)); 
     68                static if(is(V == dchar) || is(V == wchar) || is(V == char)){ 
     69                    Char[] temp = new Char[1]; 
     70                    temp[0] = value; 
     71                    return transcodeToString(temp); 
     72                } 
     73                else{ 
     74                    return transcodeToString(std.string.toString(value)); 
     75                } 
    6676            } 
    6777        } 
     
    165175    }    
    166176} 
    167  
     177/* 
    168178template smartAssign(U : U[],V){ 
    169179    void smartAssign(inout U[] u,V v){ 
     
    176186        } 
    177187    }    
    178 } 
     188}*/ 
    179189 
    180190// template tuple type to help with parsing