Changeset 1746

Show
Ignore:
Timestamp:
11/08/08 20:53:13 (2 months ago)
Author:
Chuck.Esterbrook
Message:

Added command line option -correct-case (or -cc) to change the case of types to the correct cases in situations that are not ambiguous.

Location:
cobra/trunk
Files:
1 added
5 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Developer/IntermediateReleaseNotes.text

    r1745 r1746  
    2929 
    3030Adding the extension (.exe or .dll) is neither required nor problematic. 
     31 
     32* Added command line option -correct-case (or -cc) to change the case of types to the correct cases in situations that are not ambiguous. 
    3133 
    3234* Modified commandline options so -r becomes synonym for -run rather   
  • cobra/trunk/Source/CommandLine.cobra

    r1738 r1746  
    149149                        'args': 'none|inline|methods', 
    150150                        'default': 'inline', 
     151                }, 
     152                { 
     153                        'name': 'correct-case', 
     154                        'synonyms': ['cc'], 
     155                        'description': 'Rewrite source files with case corrected where the correction is unambiguous. For example, "string" --> "String". If possible, you should set your editor to automatically reload files when using this option.', 
     156                        'type': 'bool', 
    151157                }, 
    152158                { 
  • cobra/trunk/Source/Compiler.cobra

    r1723 r1746  
    285285                if stopAfterBindInt, return  # in support of the -doc option 
    286286                .bindImp 
     287                .writeSourceCodeCorrections 
    287288                .writeSharp 
    288289                if writeTestInvocation 
     
    10991100 
    11001101        ## 
     1102        ## Source Code Corrections 
     1103        ## 
     1104 
     1105        var _sourceCorrections = Dictionary<of String, Dictionary<of int, List<of Replacement>>>() 
     1106                """ 
     1107                The first key of type String is filename. 
     1108                The second key of type int is line number. 
     1109                """ 
     1110 
     1111        def correctSource(token as IToken, replace as String) 
     1112                if not _sourceCorrections.containsKey(token.fileName) 
     1113                        _sourceCorrections[token.fileName] = Dictionary<of int, List<of Replacement>>() 
     1114                file = _sourceCorrections[token.fileName] 
     1115                if not file.containsKey(token.lineNum) 
     1116                        file[token.lineNum] = List<of Replacement>() 
     1117                file[token.lineNum].add(Replacement(token.colNum, token.text, replace)) 
     1118 
     1119        def writeSourceCodeCorrections 
     1120                # TODO: this doesn't properly handle when the replacements are a different length than the 
     1121                # original next, but then this isn't needed right now since the only type of correction 
     1122                # being done is case correction 
     1123                v = .verbosity 
     1124                for fileName, lineToReplacements in _sourceCorrections 
     1125                        if v, print 'Correcting source:', fileName 
     1126                        lineNums = List<of int>(lineToReplacements.keys) 
     1127                        lineNums.sort 
     1128                        lines = File.readAllLines(fileName) 
     1129                        for lineNum in lineNums 
     1130                                for rep in lineToReplacements[lineNum] 
     1131                                        assert rep.oldText.length == rep.newText.length  # see TODO above 
     1132                                        if v >= 2, print 'Correct: Line [lineNum], Column [rep.colNum], Old "[rep.oldText]", New "[rep.newText]"' 
     1133                                        i = lineNum - 1 
     1134                                        line = lines[i] 
     1135                                        # print line 
     1136                                        oldLen = line.length 
     1137                                        col = rep.colNum - 1 
     1138                                        lines[i] = line[:col] + rep.newText + line[col+rep.oldText.length:] 
     1139                                        # print lines[i] 
     1140                                        assert lines[i].length == oldLen 
     1141                        try 
     1142                                File.writeAllLines(fileName, lines) 
     1143                        catch exc as Exception 
     1144                                .warning(CobraWarning(fileName, nil, 'Cannot write source code corrections due to: [exc.message] ([exc.getType.name]).')) 
     1145         
     1146        ## 
    11011147        ## Important system library types 
    11021148        ## 
     
    15291575 
    15301576 
     1577class Replacement 
     1578 
     1579        def init(colNum as int, oldText as String, newText as String) 
     1580                _colNum = colNum 
     1581                _oldText = oldText 
     1582                _newText = newText 
     1583         
     1584        get colNum from var as int 
     1585         
     1586        get oldText from var as String 
     1587         
     1588        get newText from var as String 
     1589 
     1590 
    15311591class TestCompiler 
    15321592        inherits Compiler 
  • cobra/trunk/Source/TestifyRunner.cobra

    r1723 r1746  
    197197                                for baseName in paths 
    198198                                        baseName = Utils.normalizePath(baseName) 
     199                                        if baseName.startsWith('_'), continue 
    199200                                        if baseName.endsWith('.cobra') or baseName.endsWith('.COBRA') 
    200201                                                _testifyCount += .testifyFile(baseName) 
  • cobra/trunk/Source/TypeProxies.cobra

    r1718 r1746  
    485485        def _resolveType as IType is override 
    486486                # don't need to check "basic types" like int, bool, etc. here. the parser does those. 
    487                 if Utils.isCapped(_name) 
    488                         symbol = .compiler.symbolForName(.name, true, false)  # nested enum and class types are capped and can be members of the current class 
     487                name = .name 
     488                if Utils.isCapped(name) 
     489                        symbol = .compiler.symbolForName(name, true, false)  # nested enum and class types are capped and can be members of the current class 
    489490                else 
    490491                        symbol = nil 
    491492                if symbol is nil 
    492                         sugg = '' 
    493                         if not Utils.isCapped(_name) 
    494                                 sugg = _name.capped 
    495                                 other = .compiler.symbolForName(sugg, true, false) 
    496                                 sugg = if(other, ' Try "[sugg]".', '') 
    497                         .throwError('Cannot find type for "[_name]".[sugg]') 
     493                        correctCase = .compiler.options.boolValue('correct-case') 
     494                        if correctCase 
     495                                if not Utils.isCapped(name) 
     496                                        # not very sophisticated, but catches 'string', 'object', etc. 
     497                                        name = name.capped 
     498                                        symbol = .compiler.symbolForName(name, true, false) 
     499                                        if symbol is not nil 
     500                                                .compiler.correctSource(.token, name) 
     501                        if symbol is nil 
     502                                sugg = '' 
     503                                if not Utils.isCapped(_name) 
     504                                        sugg = name.capped 
     505                                        other = .compiler.symbolForName(sugg, true, false) 
     506                                        sugg = if(other, ' Try "[sugg]". Also, consider the -correct-case option (see cobra -h for details).', '') 
     507                                .throwError('Cannot find type for "[_name]".[sugg]') 
    498508                if symbol inherits ITypeProxy 
    499509                        return symbol.realType