Changeset 115

Show
Ignore:
Timestamp:
09/18/07 02:41:33 (1 year ago)
Author:
Don Clugston
Message:

Support for comments. This makes the lexer complete (I think).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/blade/Blade.d

    r112 r115  
    5252 
    5353public import SyntaxTree : mixin_SymbolTable, AbstractSyntaxTree, syntaxtreeof, AST, Symbol; 
    54 private import BladeUtil
     54private import BladeUtil : wrapInQuotes
    5555private import BladeRank; 
    5656public import CodegenX86 : generateCodeForAsmX87, generateCodeForSSE2, isSSE2AsmPossible; 
  • trunk/blade/SyntaxTree.d

    r114 r115  
    3838*/ 
    3939module SyntaxTree; 
    40 import BladeUtil
     40import BladeUtil : wrapInQuotes
    4141 
    4242public: 
     
    4545 * Returns a string, which, when mixed in, lexes, parses, and semantically  
    4646 * analyses the given expression, then creates a struct literal of type 
    47  * AbstractSyntaxTree, containing a symbol table, and a syntax tree in a 
    48  * very simple placeholder format, which refers to the elements in the symbol table. 
     47 * AbstractSyntaxTree, containing a symbol table and a syntax tree. 
    4948 * 
    5049 * Syntax trees are represented as placeholder expressions, with associativity 
    51  * and precedence indicated by parentheses. 
     50 * and precedence indicated by parentheses. The placeholders are indices into 
     51 * the symbol table. 
    5252 * 
    5353  
     
    8181struct AbstractSyntaxTree { 
    8282    char [] expression; /// syntax tree in Placeholder format, eg A+=(B*C) 
    83     Symbol[] symbolTable; /// The types and values of A,B,C,... 
     83    Symbol[] symbolTable; /// Textual form of the types and values of A,B,C,... 
    8484} 
    8585 
     
    8888//  ==== LEXER ==== 
    8989 
    90 /// Return true if text is a D keyword 
     90/** Return true if text is a D keyword 
     91 */ 
    9192bool isKeyword(char [] text) { 
    9293    char [][] keywords = [ "abstract", "alias", "align", "asm", "assert",  
     
    136137    for(int i=0; i<expr.length; ++i) { 
    137138        char c = expr[i]; 
    138         if (quote!=0) {  // If inside a string literal, wait until it ends 
     139        // Deal with string literals 
     140        if (quote!=0) { 
    139141            symbol~=c; 
    140142            if (c==quote && (symbol.length<1 || symbol[$-1]!='\\')) quote=0; 
     
    177179               code ~= "."; 
    178180           } 
    179            code ~= c; 
     181           if (c!='/' || i >= expr.length-1) code ~= c; 
     182           else { 
     183                // Remove comments 
     184                if (expr[i..i+2]=="//") { 
     185                    for ( ; i<expr.length && expr[i]!='\n' ; ++i) {} 
     186                } else if (expr[i..i+2]=="/*") { 
     187                    i+=3; 
     188                    for ( ; i<expr.length && expr[i-1..i+1] != "*/"; ++i) {} 
     189                    if ( i >= expr.length-1 ) break; 
     190                } else if (expr[i..i+2]=="/+") { 
     191                    i++; 
     192                    for (int nest = 1; nest>0 && i < expr.length-1; ++i) { 
     193                        if (expr[i..i+2] == "+/") { --nest; ++i; } 
     194                        else if (expr[i..i+2] == "/+") { ++nest; ++i; } 
     195                    } 
     196                    --i; 
     197                    if (i >= expr.length-1) break; 
     198                } else code~=c; 
     199           } 
    180200        } 
    181201    } 
     
    183203    expr = code; 
    184204    return symbols; 
     205} 
     206 
     207unittest { 
     208    char [] expr = "xyzzy+ y/+ comment/++ /++//++/+/ +/*2"; 
     209    char [][] symbols = replaceSymbolsWithPlaceholders(expr); 
     210    assert(expr == "A+ B*C"); 
    185211} 
    186212