Changeset 408

Show
Ignore:
Timestamp:
03/04/10 21:50:26 (2 years ago)
Author:
walter
Message:

add spell checking

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dmd-1.x/src/dsymbol.c

    r253 r408  
    11 
    22// Compiler implementation of the D programming language 
    3 // Copyright (c) 1999-2009 by Digital Mars 
     3// Copyright (c) 1999-2010 by Digital Mars 
    44// All Rights Reserved 
    55// written by Walter Bright 
     
    1414 
    1515#include "rmem.h" 
     16#include "speller.h" 
    1617 
    1718#include "mars.h" 
     
    331332    //printf("Dsymbol::search(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); 
    332333    return NULL; 
     334} 
     335 
     336/*************************************************** 
     337 * Search for symbol with correct spelling. 
     338 */ 
     339 
     340void *symbol_search_fp(void *arg, const char *seed) 
     341{ 
     342    Dsymbol *s = (Dsymbol *)arg; 
     343    Identifier id(seed, 0); 
     344    Module::clearCache(); 
     345    s = s->search(0, &id, 4|2); 
     346    return s; 
     347} 
     348 
     349Dsymbol *Dsymbol::search_correct(Identifier *ident) 
     350{ 
     351    if (global.gag) 
     352    return NULL;        // don't do it for speculative compiles; too time consuming 
     353 
     354    return (Dsymbol *)speller(ident->toChars(), &symbol_search_fp, this, idchars); 
    333355} 
    334356 
  • branches/dmd-1.x/src/dsymbol.h

    r342 r408  
    11 
    22// Compiler implementation of the D programming language 
    3 // Copyright (c) 1999-2009 by Digital Mars 
     3// Copyright (c) 1999-2010 by Digital Mars 
    44// All Rights Reserved 
    55// written by Walter Bright 
     
    148148    virtual void inlineScan(); 
    149149    virtual Dsymbol *search(Loc loc, Identifier *ident, int flags); 
     150    Dsymbol *search_correct(Identifier *id); 
    150151    Dsymbol *searchX(Loc loc, Scope *sc, Identifier *id); 
    151152    virtual int overloadInsert(Dsymbol *s); 
  • branches/dmd-1.x/src/expression.c

    r405 r408  
    20922092    return e->semantic(sc); 
    20932093    } 
    2094     error("undefined identifier %s", ident->toChars()); 
     2094#if DMDV2 
     2095    if (ident == Id::ctfe) 
     2096    {  // Create the magic __ctfe bool variable 
     2097       VarDeclaration *vd = new VarDeclaration(loc, Type::tbool, Id::ctfe, NULL); 
     2098       Expression *e = new VarExp(loc, vd); 
     2099       e = e->semantic(sc); 
     2100       return e; 
     2101    } 
     2102#endif 
     2103 
     2104    s = sc->search_correct(ident); 
     2105    if (s) 
     2106    error("undefined identifier %s, did you mean %s %s?", ident->toChars(), s->kind(), s->toChars()); 
     2107    else 
     2108    error("undefined identifier %s", ident->toChars()); 
    20952109    type = Type::terror; 
    20962110    return this; 
  • branches/dmd-1.x/src/module.c

    r342 r408  
    865865int Module::needModuleInfo() 
    866866{ 
     867    //printf("needModuleInfo() %s, %d, %d\n", toChars(), needmoduleinfo, global.params.cov); 
    867868    return needmoduleinfo || global.params.cov; 
    868869} 
     
    903904} 
    904905 
     906void Module::clearCache() 
     907{ 
     908    for (int i = 0; i < amodules.dim; i++) 
     909    {   Module *m = (Module *)amodules.data[i]; 
     910    m->searchCacheIdent = NULL; 
     911    } 
     912} 
    905913 
    906914/******************************************* 
  • branches/dmd-1.x/src/module.h

    r342 r408  
    142142    void addDeferredSemantic(Dsymbol *s); 
    143143    static void runDeferredSemantic(); 
     144    static void clearCache(); 
    144145    int imports(Module *m); 
    145146 
  • branches/dmd-1.x/src/mtype.c

    r382 r408  
    593593    { 
    594594    error(loc, ".size property should be replaced with .sizeof"); 
    595     e = new IntegerExp(loc, size(loc), Type::tsize_t); 
     595    e = new ErrorExp(); 
    596596    } 
    597597    else if (ident == Id::alignof) 
     
    631631    else 
    632632    { 
    633     error(loc, "no property '%s' for type '%s'", ident->toChars(), toChars()); 
    634     e = new IntegerExp(loc, 1, Type::tint32); 
     633    Dsymbol *s = NULL; 
     634    if (ty == Tstruct || ty == Tclass || ty == Tenum || ty == Ttypedef) 
     635        s = toDsymbol(NULL); 
     636    if (s) 
     637        s = s->search_correct(ident); 
     638    if (s) 
     639        error(loc, "no property '%s' for type '%s', did you mean '%s'?", ident->toChars(), toChars(), s->toChars()); 
     640    else 
     641        error(loc, "no property '%s' for type '%s'", ident->toChars(), toChars()); 
     642    e = new ErrorExp(); 
    635643    } 
    636644    return e; 
  • branches/dmd-1.x/src/scope.c

    r294 r408  
    11 
    2 // Copyright (c) 1999-2005 by Digital Mars 
     2// Copyright (c) 1999-2010 by Digital Mars 
    33// All Rights Reserved 
    44// written by Walter Bright 
     
    1212 
    1313#include "root.h" 
     14#include "speller.h" 
    1415 
    1516#include "mars.h" 
     
    360361    } 
    361362} 
     363 
     364 
     365/************************************************ 
     366 * Given the failed search attempt, try to find 
     367 * one with a close spelling. 
     368 */ 
     369 
     370void *scope_search_fp(void *arg, const char *seed) 
     371{ 
     372    //printf("scope_search_fp('%s')\n", seed); 
     373    Scope *sc = (Scope *)arg; 
     374    Identifier id(seed, 0); 
     375    Module::clearCache(); 
     376    Dsymbol *s = sc->search(0, &id, NULL); 
     377    return s; 
     378} 
     379 
     380Dsymbol *Scope::search_correct(Identifier *ident) 
     381{ 
     382    if (global.gag) 
     383    return NULL;        // don't do it for speculative compiles; too time consuming 
     384 
     385    return (Dsymbol *)speller(ident->toChars(), &scope_search_fp, this, idchars); 
     386} 
  • branches/dmd-1.x/src/scope.h

    r243 r408  
    111111 
    112112    Dsymbol *search(Loc loc, Identifier *ident, Dsymbol **pscopesym); 
     113    Dsymbol *search_correct(Identifier *ident); 
    113114    Dsymbol *insert(Dsymbol *s); 
    114115 
  • branches/dmd-1.x/src/template.c

    r351 r408  
    36603660    s = sc->search(loc, id, &scopesym); 
    36613661    if (!s) 
    3662     {   error("template '%s' is not defined", id->toChars()); 
     3662    { 
     3663        s = sc->search_correct(id); 
     3664        if (s) 
     3665        error("template '%s' is not defined, did you mean %s?", id->toChars(), s->toChars()); 
     3666        else 
     3667        error("template '%s' is not defined", id->toChars()); 
    36633668        return NULL; 
    36643669    } 
  • branches/dmd-1.x/src/win32.mak

    r262 r408  
    9797 
    9898ROOTOBJS= lstring.obj array.obj gnuc.obj man.obj rmem.obj port.obj root.obj \ 
    99     stringtable.obj dchar.obj response.obj async.obj 
     99    stringtable.obj dchar.obj response.obj async.obj speller.obj 
    100100 
    101101OBJS= $(OBJ1) $(OBJ8) $(ROOTOBJS) 
     
    150150    $(ROOT)\stringtable.h $(ROOT)\stringtable.c \ 
    151151    $(ROOT)\gnuc.h $(ROOT)\gnuc.c $(ROOT)\man.c $(ROOT)\port.c \ 
    152     $(ROOT)\response.c $(ROOT)\async.h $(ROOT)\async.c 
     152    $(ROOT)\response.c $(ROOT)\async.h $(ROOT)\async.c \ 
     153    $(ROOT)\speller.h $(ROOT)\speller.c 
    153154 
    154155MAKEFILES=win32.mak linux.mak osx.mak freebsd.mak solaris.mak 
     
    420421response.obj : $(ROOT)\response.c 
    421422    $(CC) -c $(CFLAGS) $(ROOT)\response.c 
     423 
     424speller.obj : $(ROOT)\speller.h $(ROOT)\speller.c 
     425    $(CC) -c $(CFLAGS) $(ROOT)\speller.c 
    422426 
    423427stringtable.obj : $(ROOT)\stringtable.c 
  • trunk/src/dsymbol.c

    r402 r408  
    11 
    22// Compiler implementation of the D programming language 
    3 // Copyright (c) 1999-2009 by Digital Mars 
     3// Copyright (c) 1999-2010 by Digital Mars 
    44// All Rights Reserved 
    55// written by Walter Bright 
     
    1414 
    1515#include "rmem.h" 
     16#include "speller.h" 
    1617 
    1718#include "mars.h" 
     
    331332    //printf("Dsymbol::search(this=%p,%s, ident='%s')\n", this, toChars(), ident->toChars()); 
    332333    return NULL; 
     334} 
     335 
     336/*************************************************** 
     337 * Search for symbol with correct spelling. 
     338 */ 
     339 
     340void *symbol_search_fp(void *arg, const char *seed) 
     341{ 
     342    Dsymbol *s = (Dsymbol *)arg; 
     343    Identifier id(seed, 0); 
     344    Module::clearCache(); 
     345    s = s->search(0, &id, 4|2); 
     346    return s; 
     347} 
     348 
     349Dsymbol *Dsymbol::search_correct(Identifier *ident) 
     350{ 
     351    if (global.gag) 
     352    return NULL;        // don't do it for speculative compiles; too time consuming 
     353 
     354    return (Dsymbol *)speller(ident->toChars(), &symbol_search_fp, this, idchars); 
    333355} 
    334356 
     
    12031225    assert(tab); 
    12041226#endif 
     1227    //printf("DsymbolTable::lookup(%s)\n", (char*)ident->string); 
    12051228    StringValue *sv = tab->lookup((char*)ident->string, ident->len); 
    12061229    return (Dsymbol *)(sv ? sv->ptrvalue : NULL); 
  • trunk/src/dsymbol.h

    r402 r408  
    150150    virtual void inlineScan(); 
    151151    virtual Dsymbol *search(Loc loc, Identifier *ident, int flags); 
     152    Dsymbol *search_correct(Identifier *id); 
    152153    Dsymbol *searchX(Loc loc, Scope *sc, Identifier *id); 
    153154    virtual int overloadInsert(Dsymbol *s); 
  • trunk/src/expression.c

    r406 r408  
    22582258       return e; 
    22592259    } 
    2260     error("undefined identifier %s", ident->toChars()); 
     2260 
     2261    s = sc->search_correct(ident); 
     2262    if (s) 
     2263    error("undefined identifier %s, did you mean %s %s?", ident->toChars(), s->kind(), s->toChars()); 
     2264    else 
     2265    error("undefined identifier %s", ident->toChars()); 
    22612266    type = Type::terror; 
    22622267    return this; 
     
    58615866     * the current module should have access to its own imports. 
    58625867     */ 
    5863     Dsymbol *s = ie->sds->search(loc, ident, //0); 
     5868    Dsymbol *s = ie->sds->search(loc, ident, 
    58645869        (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); 
    58655870    if (s) 
     
    1065210657    if (t1n->constOf() != t2n->constOf() && 
    1065310658        !((t1n->ty == Tchar || t1n->ty == Twchar || t1n->ty == Tdchar) && 
    10654           (t2n->ty == Tchar || t2n->ty == Twchar || t2n->ty == Tdchar)) 
     10659          (t2n->ty == Tchar || t2n->ty == Twchar || t2n->ty == Tdchar)) && 
     10660        !(t1n->ty == Tvoid || t2n->ty == Tvoid) 
    1065510661       ) 
    1065610662    {   /* Rewrite as: 
  • trunk/src/module.c

    r393 r408  
    913913} 
    914914 
     915void Module::clearCache() 
     916{ 
     917    for (int i = 0; i < amodules.dim; i++) 
     918    {   Module *m = (Module *)amodules.data[i]; 
     919    m->searchCacheIdent = NULL; 
     920    } 
     921} 
    915922 
    916923/******************************************* 
  • trunk/src/module.h

    r395 r408  
    145145    void addDeferredSemantic(Dsymbol *s); 
    146146    static void runDeferredSemantic(); 
     147    static void clearCache(); 
    147148    int imports(Module *m); 
    148149 
  • trunk/src/mtype.c

    r407 r408  
    16471647    else 
    16481648    { 
    1649     error(loc, "no property '%s' for type '%s'", ident->toChars(), toChars()); 
     1649    Dsymbol *s = NULL; 
     1650    if (ty == Tstruct || ty == Tclass || ty == Tenum || ty == Ttypedef) 
     1651        s = toDsymbol(NULL); 
     1652    if (s) 
     1653        s = s->search_correct(ident); 
     1654    if (s) 
     1655        error(loc, "no property '%s' for type '%s', did you mean '%s'?", ident->toChars(), toChars(), s->toChars()); 
     1656    else 
     1657        error(loc, "no property '%s' for type '%s'", ident->toChars(), toChars()); 
    16501658    e = new ErrorExp(); 
    16511659    } 
  • trunk/src/scope.c

    r294 r408  
    11 
    2 // Copyright (c) 1999-2005 by Digital Mars 
     2// Copyright (c) 1999-2010 by Digital Mars 
    33// All Rights Reserved 
    44// written by Walter Bright 
     
    1212 
    1313#include "root.h" 
     14#include "speller.h" 
    1415 
    1516#include "mars.h" 
     
    360361    } 
    361362} 
     363 
     364 
     365/************************************************ 
     366 * Given the failed search attempt, try to find 
     367 * one with a close spelling. 
     368 */ 
     369 
     370void *scope_search_fp(void *arg, const char *seed) 
     371{ 
     372    //printf("scope_search_fp('%s')\n", seed); 
     373    Scope *sc = (Scope *)arg; 
     374    Identifier id(seed, 0); 
     375    Module::clearCache(); 
     376    Dsymbol *s = sc->search(0, &id, NULL); 
     377    return s; 
     378} 
     379 
     380Dsymbol *Scope::search_correct(Identifier *ident) 
     381{ 
     382    if (global.gag) 
     383    return NULL;        // don't do it for speculative compiles; too time consuming 
     384 
     385    return (Dsymbol *)speller(ident->toChars(), &scope_search_fp, this, idchars); 
     386} 
  • trunk/src/scope.h

    r242 r408  
    111111 
    112112    Dsymbol *search(Loc loc, Identifier *ident, Dsymbol **pscopesym); 
     113    Dsymbol *search_correct(Identifier *ident); 
    113114    Dsymbol *insert(Dsymbol *s); 
    114115 
  • trunk/src/template.c

    r402 r408  
    40444044    s = sc->search(loc, id, &scopesym); 
    40454045    if (!s) 
    4046     {   error("template '%s' is not defined", id->toChars()); 
     4046    { 
     4047        s = sc->search_correct(id); 
     4048        if (s) 
     4049        error("template '%s' is not defined, did you mean %s?", id->toChars(), s->toChars()); 
     4050        else 
     4051        error("template '%s' is not defined", id->toChars()); 
    40474052        return NULL; 
    40484053    } 
  • trunk/src/win32.mak

    r258 r408  
    77D= 
    88DMDSVN=\svnproj\dmd\trunk\src 
     9#DMDSVN=\svnproj\dmd\branches\dmd-1.x\src 
    910SCROOT=$D\dm 
    1011INCLUDE=$(SCROOT)\include 
     
    9697 
    9798ROOTOBJS= lstring.obj array.obj gnuc.obj man.obj rmem.obj port.obj root.obj \ 
    98     stringtable.obj dchar.obj response.obj async.obj 
     99    stringtable.obj dchar.obj response.obj async.obj speller.obj 
    99100 
    100101OBJS= $(OBJ1) $(OBJ8) $(ROOTOBJS) 
     
    149150    $(ROOT)\stringtable.h $(ROOT)\stringtable.c \ 
    150151    $(ROOT)\gnuc.h $(ROOT)\gnuc.c $(ROOT)\man.c $(ROOT)\port.c \ 
    151     $(ROOT)\response.c $(ROOT)\async.h $(ROOT)\async.c 
     152    $(ROOT)\response.c $(ROOT)\async.h $(ROOT)\async.c \ 
     153    $(ROOT)\speller.h $(ROOT)\speller.c 
    152154 
    153155MAKEFILES=win32.mak linux.mak osx.mak freebsd.mak solaris.mak 
     
    419421response.obj : $(ROOT)\response.c 
    420422    $(CC) -c $(CFLAGS) $(ROOT)\response.c 
     423 
     424speller.obj : $(ROOT)\speller.h $(ROOT)\speller.c 
     425    $(CC) -c $(CFLAGS) $(ROOT)\speller.c 
    421426 
    422427stringtable.obj : $(ROOT)\stringtable.c