Changeset 239
- Timestamp:
- 08/13/06 08:07:36 (2 years ago)
- Files:
-
- trunk/enki/BaseParser.d (modified) (1 diff)
- trunk/enki/Directive.d (modified) (1 diff)
- trunk/enki/EnkiBackend.d (modified) (6 diffs)
- trunk/enki/Expression.d (modified) (3 diffs)
- trunk/enki/Rule.d (modified) (4 diffs)
- trunk/enki/library/d/BaseParser.d (deleted)
- trunk/enki/library/d/CharEntity.bnf (modified) (1 diff)
- trunk/enki/library/d/CharEntity.d (moved) (moved from trunk/enki/library/d/Entity.d) (3 diffs)
- trunk/enki/library/d/Keyword.bnf (modified) (1 diff)
- trunk/enki/library/d/Keyword.d (modified) (1 diff)
- trunk/enki/library/d/Operator.bnf (moved) (moved from trunk/enki/library/d/OtherToken.bnf)
- trunk/enki/library/d/Operator.d (modified) (1 diff)
- trunk/enki/library/d/Token.d (modified) (11 diffs)
- trunk/enki/library/d/Tokenizer.bnf (modified) (12 diffs)
- trunk/enki/library/d/TokenizerBase.d (modified) (6 diffs)
- trunk/enki/types.d (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/enki/BaseParser.d
r235 r239 51 51 protected ErrorData[] errors; 52 52 53 public this(){ 53 public this(){ 54 54 } 55 55 trunk/enki/Directive.d
r235 r239 269 269 return "Alias Directive"; 270 270 } 271 272 public void render(CodeGenerator generator,Statement passterm,Statement failterm){ 273 generator.emit("alias parse_" ~ rule ~ " parse_" ~ ruleAlias ~ ";"); 274 } 271 275 } 272 276 trunk/enki/EnkiBackend.d
r235 r239 67 67 String header; 68 68 String utf; 69 UserProduction[ ] userProductions;69 UserProduction[String] userProductions; 70 70 71 71 public this(){ … … 121 121 122 122 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); 124 128 } 125 129 … … 145 149 rules[aliasName] = rules[ruleName]; 146 150 } 151 else if(ruleName in userProductions){ 152 debug writefln("%s aliased to user production %s",aliasName,ruleName); 153 userProductions[aliasName] = userProductions[ruleName]; 154 } 147 155 else{ 148 156 throw new Exception("Cannot alias '" ~ aliasName ~ "'. Rule '" ~ ruleName ~ "' is not defined."); … … 161 169 public String getTypeForRule(String name){ 162 170 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; 168 173 } 169 174 if(!type){ … … 177 182 178 183 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 } 184 187 return true; 185 188 } … … 187 190 public String getTerminalName(String terminalName){ 188 191 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; 193 194 } 194 195 if(!name) name = terminalName; trunk/enki/Expression.d
r237 r239 574 574 575 575 with(generator){ 576 auto startLabel = getUniqueLabel("start"); 576 auto startLabel = getUniqueLabel("start"); 577 auto termPos = getUniqueLabel("termPos"); 577 578 emit("{//ZeroOrMoreExpr"); 578 579 indent(); 579 580 if(term) emit("uint " ~ startLabel ~ " = position;"); 580 emit("uint termPos;");581 emit("uint " ~ termPos ~ ";"); 581 582 render(loopStart); 582 583 583 emit( "termPos= position;");584 emit(termPos ~ " = position;"); 584 585 if(term){ 585 586 renderPass(term,gotoLoopEnd); … … 594 595 595 596 render(loopEnd); 596 if(binding) emit(binding.assignExpr(generator.getParseType(),"sliceData(" ~ startLabel ~ ", termPos)")~";");597 if(binding) emit(binding.assignExpr(generator.getParseType(),"sliceData(" ~ startLabel ~ "," ~ termPos ~ ")")~";"); 597 598 render(pass); 598 599 unindent(); … … 653 654 public String toBNF(){ 654 655 if(binding){ 655 return " \"" ~ text ~ "\"" ~ binding.toBNF();656 } 657 else{ 658 return " \"" ~ text ~ "\"";656 return " \"" ~ text ~ "\"" ~ binding.toBNF(); 657 } 658 else{ 659 return " \"" ~ text ~ "\""; 659 660 } 660 661 } trunk/enki/Rule.d
r237 r239 41 41 bool semanticDone; 42 42 Param[String] params; 43 44 protected this(){ 45 } 43 46 44 47 public this(String name,RulePredicate pred,Expression expr,RuleDecl decl=null){ … … 129 132 emit("debug writefln(\"parse_" ~ name ~ "()\");"); 130 133 emit("uint " ~ startLabel ~ " = position;"); 131 pred.renderDeclarations(generator,decl.param s);134 pred.renderDeclarations(generator,decl.paramXRef); 132 135 emit(""); 133 136 foreach(param; params){ … … 162 165 163 166 class RuleDecl{ 164 Param[String] params; 167 Param[String] paramXRef; 168 Param[] params; 165 169 166 170 public this(Param[] params...){ 167 171 foreach(param; params){ 168 this.params[param.name] = param; 172 this.params ~= param; 173 this.paramXRef[param.name] = param; 169 174 } 170 175 } … … 199 204 200 205 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; 203 208 } 204 209 return null; trunk/enki/library/d/CharEntity.bnf
r235 r239 26 26 NamedCharacterEntity 27 27 = String tok 28 ::= 29 "quot" @literal!("""):tok | 28 ::= "quot" @literal!("""):tok | 30 29 "amp" @literal!("&"):tok | 31 30 "lt" @literal!("<"):tok | trunk/enki/library/d/CharEntity.d
r238 r239 22 22 OTHER DEALINGS IN THE SOFTWARE. 23 23 +/ 24 module enki.library.d. Entity;24 module enki.library.d.CharEntity; 25 25 26 26 private import enki.types; … … 83 83 charEntity["deg"] = "°"; 84 84 charEntity["plusmn"] = "±"; 85 charEntity["sup "]2= "²";86 charEntity["sup "]3= "³";85 charEntity["sup2"] = "²"; 86 charEntity["sup3"] = "³"; 87 87 charEntity["acute"] = "´"; 88 88 charEntity["micro"] = "µ"; … … 90 90 charEntity["middot"] = "·"; 91 91 charEntity["cedil"] = "¸"; 92 charEntity["sup "]1= "¹";92 charEntity["sup1"] = "¹"; 93 93 charEntity["ordm"] = "º"; 94 94 charEntity["raquo"] = "»"; 95 charEntity["frac "]14= "¼";96 charEntity["frac "]12= "½";97 charEntity["frac "]34= "¾";95 charEntity["frac14"] = "¼"; 96 charEntity["frac12"] = "½"; 97 charEntity["frac34"] = "¾"; 98 98 charEntity["iquest"] = "¿"; 99 99 charEntity["agrave"] = "À"; trunk/enki/library/d/Keyword.bnf
r235 r239 28 28 29 29 Keyword 30 = Token Token.keyword( tok)30 = Token Token.keyword(Tok tok) 31 31 ::= "abstract" @Tok.Abstract:tok | 32 32 "alias" @Tok.Alia:tok | trunk/enki/library/d/Keyword.d
r235 r239 22 22 OTHER DEALINGS IN THE SOFTWARE. 23 23 +/ 24 module enki.library.d.Keyword; 24 25 25 26 private import enki.types; 26 private import d.Token;27 private import enki.library.d.Token; 27 28 28 uintkeywordToken[String];29 String keywordXref[ uint];29 Tok keywordToken[String]; 30 String keywordXref[Tok]; 30 31 31 32 static this(){ trunk/enki/library/d/Operator.d
r235 r239 22 22 OTHER DEALINGS IN THE SOFTWARE. 23 23 +/ 24 module enki.library.d.Operator; 24 25 25 26 private import enki.types; 26 private import d.Token;27 private import enki.library.d.Token; 27 28 28 uintoperatorToken[String];29 String operatorXref[ uint];29 Tok operatorToken[String]; 30 String operatorXref[Tok]; 30 31 31 32 static this(){ trunk/enki/library/d/Token.d
r235 r239 22 22 OTHER DEALINGS IN THE SOFTWARE. 23 23 +/ 24 module TokenizerBase;24 module enki.library.d.Token; 25 25 /** 26 26 D Token definitions … … 28 28 29 29 private import enki.types; 30 private import d.Keyword; 30 private import enki.library.d.Keyword; 31 private import enki.library.d.Operator; 31 32 32 33 alias ulong IntegerValue; //TODO: turn into a more advanced type 33 34 34 struct FloatingPoint {35 struct FloatingPointValue{ 35 36 IntegerValue whole; 36 37 IntegerValue fraction; … … 39 40 bool imaginary; 40 41 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; 43 44 _this.whole = whole; 44 45 _this.fraction = fraction; … … 49 50 } 50 51 51 struct LineDirective {52 struct LineDirectiveValue{ 52 53 IntegerValue line; 53 54 String filespec; 54 55 55 static LineDirective opCall(IntegerValue line,String filespec){56 LineDirective _this;56 static LineDirectiveValue opCall(IntegerValue line,String filespec){ 57 LineDirectiveValue _this; 57 58 _this.line = line; 58 59 _this.filespec = filespec; 59 retur h_this;60 return _this; 60 61 } 61 62 } … … 169 170 Wchar, 170 171 While, 171 With 172 With, 172 173 173 174 /* operator types */ … … 257 258 String strValue; 258 259 IntegerValue integerValue; 259 FloatingPoint floatingValue;260 LineDirective lineDirective;260 FloatingPointValue floatingValue; 261 LineDirectiveValue lineDirective; 261 262 } 262 263 … … 279 280 Token _this; 280 281 _this.type = Keyword; 281 _this.value. ulongValue= id;282 _this.value.token = id; 282 283 return _this; 283 284 } … … 286 287 Token _this; 287 288 _this.type = Operator; 288 _this.value. ulongValue= id;289 _this.value.token = id; 289 290 return _this; 290 291 } … … 320 321 } 321 322 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; 325 326 _this.subtype = subtype; 326 327 _this.value.floatingValue = value; … … 339 340 Token _this; 340 341 _this.type = LineDirective; 341 _this.value.lineDirective = LineDirective (line,filespec);342 _this.value.lineDirective = LineDirectiveValue(line,filespec); 342 343 return _this; 343 344 } … … 347 348 return value.strValue; 348 349 } 349 else if(type == Token.Keyword) 350 else if(type == Token.Keyword){ 350 351 return keywordXref[value.token]; 351 352 } trunk/enki/library/d/Tokenizer.bnf
r235 r239 28 28 29 29 .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 34 41 .baseclass(TokenizerBase); 35 42 .classname(Tokenizer); 36 43 37 44 .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"); 40 49 41 50 .include("enki/library/d/CharEntity.bnf"); 42 51 .include("enki/library/d/Character.bnf"); 43 .include("enki/library/d/O therToken.bnf");52 .include("enki/library/d/Operator.bnf"); 44 53 .include("enki/library/d/Keyword.bnf"); 45 54 55 /* BUG: there's something wrong with DMD 0.164 that causes this to not evauluate correctly if it's first 46 56 Syntax 47 57 = Token[] tokens … … 57 67 SpecialTokenSequence:~tokens 58 68 } RealEndOfFile; 59 69 */ 60 70 ##### Space, Comments and EOF ##### 61 71 … … 78 88 Comment 79 89 = 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); 83 93 84 94 SlashPlusContent 85 95 = 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 99 Syntax 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; 88 112 89 113 ##### Tokens ##### … … 112 136 113 137 UniversalAlpha 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); 116 140 117 141 … … 146 170 EscapeSequence 147 171 = String value 148 ::= "\ \" (172 ::= "\x5C" ( 149 173 "'":value | 150 174 "\"":value | … … 167 191 168 192 HexChar2 169 = StringhexToChar(digits)193 = Char hexToChar(digits) 170 194 ::= (HexDigit HexDigit):digits; 171 195 … … 187 211 188 212 HexString 189 = Token Token.stringLiteral( str,width)213 = Token Token.stringLiteral(String str,width) 190 214 ::= "x\"" {HexStringChar:~str} "\"" [Postfix:width]; 191 215 192 216 HexStringChar 193 = Char hexToChar( digits)217 = Char hexToChar(String digits) 194 218 ::= WhiteSpace HexDigit:~digits WhiteSpace HexDigit:~digits; 195 219 … … 220 244 221 245 Integer 222 = Integer value246 = IntegerValue value 223 247 ::= Decimal:value | Binary:value | Octal:value | Hexadecimal:value; 224 248 … … 234 258 235 259 Decimal 236 = Integer convertToDecimal(String value)260 = IntegerValue convertToDecimal(String value) 237 261 ::= ("0":~value | (NonZeroDigit:~value {"_"} {DecimalDigit:~value {"_"}})); 238 262 239 263 DecimalDigits 240 = Integer convertToDecimal(value)264 = IntegerValue convertToDecimal(String value) 241 265 ::= {"_"} {DecimalDigit:~value {"_"}}; 242 266 243 267 Binary 244 = Integer convertToBinary(value)268 = IntegerValue convertToBinary(String value) 245 269 ::= ("0b"|"0B") {"_"} {BinaryDigit:~value {"_"}}; 246 270 247 271 Octal 248 = Integer convertToOctal(value)272 = IntegerValue convertToOctal(String value) 249 273 ::= "0" {"_"} {OctalDigit:~value {"_"}}; 250 274 … … 253 277 254 278 Hexadecimal 255 = Integer convertToHex(value)279 = IntegerValue convertToHex(String value) 256 280 ::= HexPrefix {"_"} {HexDigit:~value {"_"}}; 257 281 258 282 HexDigits 259 = Integer convertToHex(value)283 = IntegerValue convertToHex(String value) 260 284 ::= {"_"} {HexDigit:~value {"_"}}; 261 285 … … 285 309 286 310 Float 287 = FloatingPoint value311 = FloatingPointValue value 288 312 ::= DecimalFloat:value | HexFloat:value; 289 313 290 314 DecimalFloat 291 = FloatingPoint FloatingPoint(whole,fraction,exponent,exponentSign)315 = FloatingPointValue FloatingPointValue(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign) 292 316 ::= Decimal:whole "." [DecimalDigits:fraction [DecimalExponent!(exponent,exponentSign)]] | 293 "." DecimalDigits:fraction [DecimalExponent :exponent] |317 "." DecimalDigits:fraction [DecimalExponent!(exponent,exponentSign):exponent] | 294 318 Decimal:whole DecimalExponent!(exponent,exponentSign); 295 319 296 DecimalExponent(Integer exponent,bool exponentSign)320 DecimalExponent(IntegerValue exponent,bool exponentSign) 297 321 ::= "e" DecimalDigits:exponent @true:exponentSign | 298 322 "E" DecimalDigits:exponent @true:exponentSign | … … 303 327 304 328 HexFloat 305 = FloatingPoint FloatingPoint(whole,fraction,exponent,exponentSign)329 = FloatingPointValue FloatingPointValue(IntegerValue whole,IntegerValue fraction,IntegerValue exponent,bool exponentSign) 306 330 ::= Hexadecimal:whole "." [HexDigits:fraction [HexExponent!(exponent,exponentSign)]] | 307 331 HexPrefix "." HexDigits:fraction [HexExponent!(exponent,exponentSign)] | 308 332 Hexadecimal:whole HexExponent!(exponent,exponentSign); 309 333 310 HexExponent(Integer exponent,bool exponentSign)334 HexExponent(IntegerValue exponent,bool exponentSign) 311 335 ::= "p" DecimalDigits:exponent @true:exponentSign | 312 336 "P" DecimalDigits:exponent @true:exponentSign | trunk/enki/library/d/TokenizerBase.d
r235 r239 22 22 OTHER DEALINGS IN THE SOFTWARE. 23 23 +/ 24 module enki.library.d.TokenizerBase; 25 26 private import enki.types; 27 private import enki.BaseParser; 28 29 private import enki.library.d.Token; 24 30 25 31 class TokenizerBase : BaseParser{ 32 33 public this(){ 34 } 35 26 36 dchar hexToChar(String value){ 27 37 dchar result; … … 62 72 } 63 73 64 IntegerValue convertToHex( value){74 IntegerValue convertToHex(String value){ 65 75 ubyte hexValue(Char ch){ 66 76 if(ch >= 'a' && ch <= 'f') return ch - 'a'+10; … … 77 87 } 78 88 79 IntegerValue convertToDecimal( value){89 IntegerValue convertToDecimal(String value){ 80 90 IntegerValue result = 0; 81 91 … … 86 96 } 87 97 88 IntegerValue convertToOctal( value){98 IntegerValue convertToOctal(String value){ 89 99 IntegerValue result = 0; 90 100 … … 95 105 } 96 106 97 IntegerValue convertToBinary( value){107 IntegerValue convertToBinary(String value){ 98 108 IntegerValue result = 0; 99 109 … … 110 120 } 111 121 } 112 113 114 /*115 D.1.4 Universal Character Names (UCN)116 117 6.4.3 Universal character names118 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_value122 123 * \U 8_hex_digits_value124 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 33 33 alias dchar Char; 34 34 alias toUTF32 transcodeToString; 35 alias toUTF8 transcodeToUTF8; 35 36 } 36 37 else version(EnkiUTF16){ … … 38 39 alias wchar Char; 39 40 alias toUTF16 transcodeToString; 41 alias toUTF8 transcodeToUTF8; 40 42 } 41 43 else{ … … 43 45 alias char Char; 44 46 alias toUTF8 transcodeToString; 47 char[] transcodeToUTF8(char[] value){ return value; } 45 48 } 46 49 … … 63 66 } 64 67 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 } 66 76 } 67 77 } … … 165 175 } 166 176 } 167 177 /* 168 178 template smartAssign(U : U[],V){ 169 179 void smartAssign(inout U[] u,V v){ … … 176 186 } 177 187 } 178 } 188 }*/ 179 189 180 190 // template tuple type to help with parsing
