Changeset 305
- Timestamp:
- 05/27/08 20:12:57 (6 months ago)
- Files:
-
- trunk/enki2/enki/Backend.d (modified) (3 diffs)
- trunk/enki2/enki/EnkiLexer.d (modified) (28 diffs)
- trunk/enki2/enki/EnkiLexerBase.d (modified) (2 diffs)
- trunk/enki2/enki/EnkiParser.d (modified) (61 diffs)
- trunk/enki2/enki/EnkiParserBase.d (modified) (2 diffs)
- trunk/enki2/enki/EnkiToken.d (modified) (1 diff)
- trunk/enki2/enki/Expression.d (modified) (7 diffs)
- trunk/enki2/enki/Rule.d (modified) (1 diff)
- trunk/enki2/enki/RulePredicate.d (modified) (1 diff)
- trunk/enki2/enki/VariableRef.d (modified) (1 diff)
- trunk/enki2/enki/bootstrap/EnkiLexer.bnf (modified) (5 diffs)
- trunk/enki2/enki/bootstrap/EnkiParser.bnf (modified) (11 diffs)
- trunk/enki2/enki/bootstrap/Lexer.d (modified) (7 diffs)
- trunk/enki2/enki/bootstrap/Parser.d (modified) (20 diffs)
- trunk/enki2/enki/enki.d (modified) (3 diffs)
- trunk/enki2/enki/generator/BNFGenerator.d (modified) (2 diffs)
- trunk/enki2/enki/generator/BootstrapGenerator.d (modified) (2 diffs)
- trunk/enki2/enki/generator/DGenerator.d (modified) (24 diffs)
- trunk/enki2/enkilib/d/AST.d (modified) (1 diff)
- trunk/enki2/enkilib/d/CharParser.d (modified) (2 diffs)
- trunk/enki2/enkilib/d/Parser.d (modified) (6 diffs)
- trunk/enki2/enkilib/d/ParserException.d (modified) (1 diff)
- trunk/enki2/enkilib/d/PositionalCharParser.d (modified) (1 diff)
- trunk/enki2/enkilib/d/TokenParser.d (modified) (3 diffs)
- trunk/enki2/enkilib/d/WS.d (added)
- trunk/enki2/example (added)
- trunk/enki2/example/calc (added)
- trunk/enki2/example/calc/CalcParser.d (added)
- trunk/enki2/example/calc/CalcParserBase.d (added)
- trunk/enki2/example/calc/calc.bnf (added)
- trunk/enki2/example/calc/calcdemo.d (added)
- trunk/enki2/example/d (added)
- trunk/enki2/example/d/lexer.bnf (added)
- trunk/enki2/example/python (added)
- trunk/enki2/example/python/PythonParser.d (added)
- trunk/enki2/example/python/lex.bnf (added)
- trunk/enki2/example/python/parse.bnf (added)
- trunk/enki2/example/python/pydemo.d (added)
- trunk/enki2/example/python/python.bnf (added)
- trunk/enki2/example/wiki (added)
- trunk/enki2/example/wiki/wiki.bnf (added)
- trunk/enki2/example/xml (added)
- trunk/enki2/example/xml/XMLParser.d (added)
- trunk/enki2/example/xml/xml.bnf (added)
- trunk/enki2/example/xml/xmldemo.d (added)
- trunk/enki2/makeboot.sh (modified) (1 diff)
- trunk/enki2/makeenki.sh (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/enki2/enki/Backend.d
r303 r305 32 32 import tango.io.Stdout; 33 33 34 34 35 abstract class BackendT(CharT,BaseClass = Object) : BaseClass{ 35 36 mixin AllTypesMixin!(CharT); … … 41 42 public this(){ 42 43 ruleSet = new RuleSet(); 44 45 ruleSet.addRule(new RulePrototype("nop","bool")); 46 ruleSet.addRule(new RulePrototype("any","String")); 47 ruleSet.addRule(new RulePrototype("eoi","bool")); 43 48 } 44 49 … … 69 74 ruleSet.semanticPass(attributes); 70 75 } 76 77 public void runDirective(String name,String[] args){ 78 if(name == "include"){ 79 foreach(arg; args){ 80 includeFile(arg); 81 } 82 } 83 else{ 84 throw new Exception("Unknown directive '" ~ name ~ "'"); 85 } 86 } 71 87 72 88 public void toCode(Generator)(bool test=false){ trunk/enki2/enki/EnkiLexer.d
r303 r305 33 33 class EnkiLexerT(CharT):EnkiLexerBase!(CharT){ 34 34 35 /* 36 any = String; 37 */ 38 39 /* 40 eoi = bool; 41 */ 35 36 42 37 43 38 /* 44 39 Tokens 45 40 = EnkiToken[] tokens 46 ::= (Whitespace | SlashStarComment | SlashSlashComment | NestingComment | RegexLiteral:~tokens | StringLiteral:~tokens | Number:~tokens | Hex:~tokens | Identifier:~tokens | LiteralToken:~tokens | @err!("Unexpected char"))* eoi;41 ::= (Whitespace | SlashStarComment | SlashSlashComment | NestingComment | RegexLiteral:~tokens | StringLiteral:~tokens | Number:~tokens | Hex:~tokens | SpecialToken:~tokens | Identifier:~tokens | LiteralToken:~tokens | @err!("Unexpected char"))* eoi; 47 42 */ 48 43 bool parse_Tokens(){ … … 53 48 start2: 54 49 // (terminator) 55 terminator5:56 50 // Production 57 51 if(parse_eoi()){ … … 65 59 goto start2; 66 60 } 61 term5: 62 // Production 63 if(parse_SlashStarComment()){ 64 goto start2; 65 } 67 66 term6: 68 67 // Production 69 if(parse_SlashS tarComment()){68 if(parse_SlashSlashComment()){ 70 69 goto start2; 71 70 } 72 71 term7: 73 72 // Production 74 if(parse_ SlashSlashComment()){73 if(parse_NestingComment()){ 75 74 goto start2; 76 75 } 77 76 term8: 78 // Production79 if(parse_NestingComment()){80 goto start2;81 }82 term9:83 77 // Production 84 78 if(parse_RegexLiteral()){ … … 86 80 goto start2; 87 81 } 88 term 10:82 term9: 89 83 // Production 90 84 if(parse_StringLiteral()){ … … 92 86 goto start2; 93 87 } 94 term1 1:88 term10: 95 89 // Production 96 90 if(parse_Number()){ … … 98 92 goto start2; 99 93 } 100 term1 2:94 term11: 101 95 // Production 102 96 if(parse_Hex()){ … … 104 98 goto start2; 105 99 } 100 term12: 101 // Production 102 if(parse_SpecialToken()){ 103 smartAppend(var_tokens,getMatchValue!(EnkiToken)()); 104 goto start2; 105 } 106 106 term13: 107 107 // Production … … 118 118 term15: 119 119 // Literal 120 auto literal16 =err("Unexpected char");120 err("Unexpected char"); 121 121 goto start2; 122 122 end3: … … 139 139 debug Stdout("parse_Whitespace").newline; 140 140 // Iterator 141 size_t counter 6= 0;141 size_t counter5 = 0; 142 142 start2: 143 143 // (terminator) 144 terminator5:145 144 if(!hasMore()){ 146 145 goto end3; … … 148 147 // (expression) 149 148 expr4: 150 // OrGroup increment 7149 // OrGroup increment6 151 150 // Terminal 152 151 if(match(" ")){ 153 goto increment7; 152 goto increment6; 153 } 154 term7: 155 // Terminal 156 if(match("\t")){ 157 goto increment6; 154 158 } 155 159 term8: 156 160 // Terminal 157 if(match("\ t")){158 goto increment 7;161 if(match("\r")){ 162 goto increment6; 159 163 } 160 164 term9: 161 // Terminal162 if(match("\r")){163 goto increment7;164 }165 term10:166 165 // Terminal 167 166 if(!match("\n")){ 168 167 goto end3; 169 168 } 170 increment 7:169 increment6: 171 170 // (increment expr count) 172 counter 6++;171 counter5 ++; 173 172 goto start2; 174 173 end3: 175 174 // (range test) 176 if(!((counter 6>= 1))){175 if(!((counter5 >= 1))){ 177 176 goto fail1; 177 goto pass0; 178 178 } 179 179 // Rule … … 206 206 start7: 207 207 // (terminator) 208 terminator10:209 208 // Terminal 210 209 if(match("*/")){ … … 237 236 SlashSlashComment 238 237 $String err="Expected terminating newline" 239 ::= "//" ?!(err) any* "\n" ;238 ::= "//" ?!(err) any* "\n" | eoi; 240 239 */ 241 240 bool parse_SlashSlashComment(){ … … 254 253 start7: 255 254 // (terminator) 256 terminator10: 257 // Terminal 258 if(match("\n")){ 259 goto end8; 260 } 255 // OrGroup end8 256 // Terminal 257 if(match("\n")){ 258 goto end8; 259 } 260 term10: 261 // Production 262 if(parse_eoi()){ 263 goto end8; 264 } 261 265 // (expression) 262 266 expr9: … … 302 306 start7: 303 307 // (terminator) 304 terminator10:305 308 // Terminal 306 309 if(match("+/")){ … … 314 317 goto start7; 315 318 } 316 term1 1:319 term10: 317 320 // Production 318 321 if(!parse_any()){ … … 358 361 start7: 359 362 // (terminator) 360 terminator10:361 363 // Terminal 362 364 if(match("`")){ … … 416 418 start8: 417 419 // (terminator) 418 terminator11:419 420 // Terminal 420 421 if(match("\"\"\"")){ … … 437 438 term2: 438 439 // AndGroup 439 auto position1 4= getPos();440 auto position13 = getPos(); 440 441 // Terminal 441 442 if(!match("\"")){ 442 goto fail1 5;443 } 444 term1 6:443 goto fail14; 444 } 445 term15: 445 446 // ErrorPoint 446 447 // Iterator 447 start1 8:448 start17: 448 449 // (terminator) 449 terminator21:450 450 // Terminal 451 451 if(match("\"")){ 452 goto end1 9;452 goto end18; 453 453 } 454 454 // (expression) 455 expr 20:455 expr19: 456 456 // Production 457 457 if(!parse_StringChar()){ 458 goto fail1 7;458 goto fail16; 459 459 } 460 460 smartAppend(var_text,getMatchValue!(String)()); 461 goto start1 8;462 end1 9:461 goto start17; 462 end18: 463 463 goto pass0; 464 fail1 7:464 fail16: 465 465 error(var_err2); 466 fail1 5:467 setPos(position1 4);468 term1 2:466 fail14: 467 setPos(position13); 468 term11: 469 469 // AndGroup 470 auto position2 3= getPos();470 auto position21 = getPos(); 471 471 // Terminal 472 472 if(!match("\'")){ 473 goto fail2 4;474 } 475 term2 5:473 goto fail22; 474 } 475 term23: 476 476 // ErrorPoint 477 477 // Iterator 478 start2 7:478 start25: 479 479 // (terminator) 480 terminator30:481 480 // Terminal 482 481 if(match("\'")){ 483 goto end2 8;482 goto end26; 484 483 } 485 484 // (expression) 486 expr2 9:485 expr27: 487 486 // Production 488 487 if(!parse_StringChar()){ 489 goto fail2 6;488 goto fail24; 490 489 } 491 490 smartAppend(var_text,getMatchValue!(String)()); 492 goto start2 7;493 end2 8:491 goto start25; 492 end26: 494 493 goto pass0; 495 fail2 6:494 fail24: 496 495 error(var_err2); 497 fail2 4:498 setPos(position2 3);496 fail22: 497 setPos(position21); 499 498 goto fail1; 500 499 // Rule … … 612 611 term37: 613 612 // Literal 614 auto literal43 =err("Unexpected escape sequence");613 err("Unexpected escape sequence"); 615 614 goto pass0; 616 615 fail5: … … 636 635 Hex 637 636 = EnkiToken HexToken(String text) 638 $String err="Expected one,two, four or eight hex digits"639 ::= "#" ?!(err) ({#30-#39 | #41-#46 | #61-#66}< 1,2,4,8>):text;637 $String err="Expected two, four or eight hex digits" 638 ::= "#" ?!(err) ({#30-#39 | #41-#46 | #61-#66}<2,4,8>):text; 640 639 */ 641 640 bool parse_Hex(){ 642 641 debug Stdout("parse_Hex").newline; 643 String var_err = "Expected one,two, four or eight hex digits";642 String var_err = "Expected two, four or eight hex digits"; 644 643 String var_text; 645 644 … … 655 654 auto position7 = getPos(); 656 655 // Iterator 657 size_t counter1 3= 0;656 size_t counter12 = 0; 658 657 start9: 659 658 // (terminator) 660 terminator12: 661 if(counter13 == 8){ 659 if(counter12 == 8){ 662 660 goto end10; 663 661 } 664 goto start9;665 662 // (expression) 666 663 expr11: 667 // OrGroup increment1 4664 // OrGroup increment13 668 665 // CharRange 669 if(match('\x30','\x39')){ 670 goto increment14; 666 if(match(0x30,0x39)){ 667 goto increment13; 668 } 669 term14: 670 // CharRange 671 if(match(0x41,0x46)){ 672 goto increment13; 671 673 } 672 674 term15: 673 675 // CharRange 674 if(match('\x41','\x46')){ 675 goto increment14; 676 } 677 term16: 678 // CharRange 679 if(!match('\x61','\x66')){ 676 if(!match(0x61,0x66)){ 680 677 goto end10; 681 678 } 682 increment1 4:679 increment13: 683 680 // (increment expr count) 684 counter1 3++;681 counter12 ++; 685 682 goto start9; 686 683 end10: 687 684 // (range test) 688 if(!((counter1 3 >= 1) || (counter13 == 1) || (counter13 == 2) || (counter13 == 4) || (counter13== 8))){685 if(!((counter12 >= 1) || (counter12 == 2) || (counter12 == 4) || (counter12 == 8))){ 689 686 goto fail6; 687 goto pass8; 690 688 } 691 689 pass8: … … 718 716 719 717 // Iterator 720 size_t counter 6= 0;718 size_t counter5 = 0; 721 719 start2: 722 720 // (terminator) 723 terminator5:724 721 if(!hasMore()){ 725 722 goto end3; … … 728 725 expr4: 729 726 // CharRange 730 if(!match( '\x30','\x39')){727 if(!match(0x30,0x39)){ 731 728 goto end3; 732 729 } 733 730 smartAssign(var_text,getMatchValue!(String)()); 734 increment 7:731 increment6: 735 732 // (increment expr count) 736 counter 6++;733 counter5 ++; 737 734 goto start2; 738 735 end3: 739 736 // (range test) 740 if(!((counter 6>= 1))){737 if(!((counter5 >= 1))){ 741 738 goto fail1; 739 goto pass0; 742 740 } 743 741 // Rule … … 767 765 // OrGroup term7 768 766 // CharRange 769 if(match( '\x41','\x5A')){767 if(match(0x41,0x5A)){ 770 768 goto term7; 771 769 } 772 770 term8: 773 771 // CharRange 774 if(match( '\x61','\x7A')){772 if(match(0x61,0x7A)){ 775 773 goto term7; 776 774 } … … 785 783 start10: 786 784 // (terminator) 787 terminator13:788 785 if(!hasMore()){ 789 786 goto end11; … … 793 790 // OrGroup start10 794 791 // CharRange 795 if(match('\x30','\x39')){ 792 if(match(0x30,0x39)){ 793 goto start10; 794 } 795 term13: 796 // CharRange 797 if(match(0x41,0x5A)){ 796 798 goto start10; 797 799 } 798 800 term14: 799 801 // CharRange 800 if(match( '\x41','\x5A')){802 if(match(0x61,0x7A)){ 801 803 goto start10; 802 804 } 803 805 term15: 804 // CharRange805 if(match('\x61','\x7A')){806 goto start10;807 }808 term16:809 806 // Terminal 810 807 if(!match("_")){ … … 832 829 833 830 /* 831 SpecialToken 832 = EnkiToken CreateToken(String text,EnkiToken.ValueType tok) 833 ::= "new" @TOK_NEW:tok | "::=" @TOK_RULEASSIGN:tok | "?!" @TOK_ERRORPOINT:tok | ".." @TOK_RANGE:tok; 834 */ 835 bool parse_SpecialToken(){ 836 debug Stdout("parse_SpecialToken").newline; 837 EnkiToken.ValueType var_tok; 838 String var_text; 839 840 // OrGroup pass0 841 // AndGroup 842 auto position4 = getPos(); 843 // Terminal 844 if(!match("new")){ 845 goto fail5; 846 } 847 term6: 848 // Literal 849 auto literal7 = TOK_NEW; 850 smartAssign(var_tok,literal7); 851 goto pass0; 852 fail5: 853 setPos(position4); 854 term2: 855 // AndGroup 856 auto position10 = getPos(); 857 // Terminal 858 if(!match("::=")){ 859 goto fail11; 860 } 861 term12: 862 // Literal 863 auto literal13 = TOK_RULEASSIGN; 864 smartAssign(var_tok,literal13); 865 goto pass0; 866 fail11: 867 setPos(position10); 868 term8: 869 // AndGroup 870 auto position16 = getPos(); 871 // Terminal 872 if(!match("?!")){ 873 goto fail17; 874 } 875 term18: 876 // Literal 877 auto literal19 = TOK_ERRORPOINT; 878 smartAssign(var_tok,literal19); 879 goto pass0; 880 fail17: 881 setPos(position16); 882 term14: 883 // AndGroup 884 auto position21 = getPos(); 885 // Terminal 886 if(!match("..")){ 887 goto fail22; 888 } 889 term23: 890 // Literal 891 auto literal24 = TOK_RANGE; 892 smartAssign(var_tok,literal24); 893 goto pass0; 894 fail22: 895 setPos(position21); 896 goto fail1; 897 // Rule 898 pass0: 899 setMatchValue(CreateToken(var_text,var_tok)); 900 debug Stdout.format("\tparse_SpecialToken passed: {0}",getMatchValue!(EnkiToken)).newline; 901 return true; 902 fail1: 903 setMatchValue((EnkiToken).init); 904 debug Stdout.format("\tparse_SpecialToken failed").newline; 905 return false; 906 } 907 908 /* 834 909 LiteralToken 835 910 = EnkiToken LiteralToken(String text) 836 ::= ( "::=" | "!(" | "?!" | ".." | "," | ":" | "~" | ";" | "=" | "," | "{" | "}" | "(" | ")" | "[" | "]" | "<" | ">" | "@" | "#" | "$" | "%" | "^" | "&" | "*" | "!" | "?" | "/" | "-" | "+" | "|" | "."):text;911 ::= (#21-#2F | #3A-#40 | #5B-#60 | #7B-#7E):text; 837 912 */ 838 913 bool parse_LiteralToken(){ … … 843 918 auto position2 = getPos(); 844 919 // OrGroup pass3 845 // Terminal846 if(match( "::=")){920 // CharRange 921 if(match(0x21,0x2F)){ 847 922 goto pass3; 848 923 } 849 924 term4: 850 // Terminal851 if(match( "!(")){925 // CharRange 926 if(match(0x3A,0x40)){ 852 927 goto pass3; 853 928 } 854 929 term5: 855 // Terminal856 if(match( "?!")){930 // CharRange 931 if(match(0x5B,0x60)){ 857 932 goto pass3; 858 933 } 859 934 term6: 860 // Terminal 861 if(match("..")){ 862 goto pass3; 863 } 864 term7: 865 // Terminal 866 if(match(",")){ 867 goto pass3; 868 } 869 term8: 870 // Terminal 871 if(match(":")){ 872 goto pass3; 873 } 874 term9: 875 // Terminal 876 if(match("~")){ 877 goto pass3; 878 } 879 term10: 880 // Terminal 881 if(match(";")){ 882 goto pass3; 883 } 884 term11: 885 // Terminal 886 if(match("=")){ 887 goto pass3; 888 } 889 term12: 890 // Terminal 891 if(match(",")){ 892 goto pass3; 893 } 894 term13: 895 // Terminal 896 if(match("{")){ 897 goto pass3; 898 } 899 term14: 900 // Terminal 901 if(match("}")){ 902 goto pass3; 903 } 904 term15: 905 // Terminal 906 if(match("(")){ 907 goto pass3; 908 } 909 term16: 910 // Terminal 911 if(match(")")){ 912 goto pass3; 913 } 914 term17: 915 // Terminal 916 if(match("[")){ 917 goto pass3; 918 } 919 term18: 920 // Terminal 921 if(match("]")){ 922 goto pass3; 923 } 924 term19: 925 // Terminal 926 if(match("<")){ 927 goto pass3; 928 } 929 term20: 930 // Terminal 931 if(match(">")){ 932 goto pass3; 933 } 934 term21: 935 // Terminal 936 if(match("@")){ 937 goto pass3; 938 } 939 term22: 940 // Terminal 941 if(match("#")){ 942 goto pass3; 943 } 944 term23: 945 // Terminal 946 if(match("$")){ 947 goto pass3; 948 } 949 term24: 950 // Terminal 951 if(match("%")){ 952 goto pass3; 953 } 954 term25: 955 // Terminal 956 if(match("^")){ 957 goto pass3; 958 } 959 term26: 960 // Terminal 961 if(match("&")){ 962 goto pass3; 963 } 964
