Wiki

Ticket #34: var-isnames-assign-reorder.patch

File var-isnames-assign-reorder.patch, 9.3 KB (added by hopscc, 15 years ago)
  • Source/Cobra.Lang/CobraCore.cobra

     
    253253            ## Will check control flags 
    254254 
    255255            # CC: make a public section/block here 
    256             var _willCheckInvariant = true 
    257                 is public 
    258             var _willCheckRequire = true 
    259                 is public 
    260             var _willCheckEnsure = true 
    261                 is public 
    262             var _willCheckAssert = true 
    263                 is public 
    264             var _willCheckNil = true 
    265                 is public 
    266  
     256            var _willCheckInvariant is public = true 
     257            var _willCheckRequire is public = true 
     258            var _willCheckEnsure is public = true 
     259            var _willCheckAssert is public = true 
     260            var _willCheckNil    is public = true 
     261             
    267262            pro willCheckInvariant from var 
    268263            pro willCheckRequire from var 
    269264            pro willCheckEnsure from var 
  • Source/CobraParser.cobra

     
    630630            .throwError('Unexpected end of source.') 
    631631        else if .peek.which == 'EOL' 
    632632            .endOfLine 
    633         else if .peek.isKeyword 
     633        else if .peek.isKeyword or .peek.which == 'ASSIGN' 
    634634            pass 
    635635        else 
    636636            .throwError('Expecting end-of-line or next clause instead of "[.peek.which]".') 
     
    12271227        token = .expect(if(isVar, 'VAR', 'CONST')) 
    12281228        idToken = .expect('ID') 
    12291229        name = idToken.text 
     1230        _validateBoxFieldId(name, isVar) 
     1231        type = if(.optional('AS'), .typeId, nil) to ITypeProxy? 
     1232        isNames = List<of String>(_isNamesStack) 
     1233        attribs = initExpr = nil 
     1234        docString = '' to ? 
     1235        if .peek.which in ['IS', 'ASSIGN']  # one liner variant 
     1236            isNames = .isDeclNames 
     1237            attribs = .hasAttribs 
     1238            if .optional('ASSIGN'), initExpr = .expression to ? 
     1239            .optional('EOL') 
     1240            assert .last.which=='EOL' 
     1241            .undo  # need the EOL 
     1242            if .optionalIndent 
     1243                # special for only old form with both isnames and initValue that compiled 
     1244                # "var id <=init> <EOL> <indent> is <isnames> 
     1245                if .peek.which == 'IS', isNames = .isDeclNames # TODO: transition out 
     1246                docString = .docString 
     1247                .dedent 
     1248        else if .peek.which == 'EOL'     
     1249            if .optionalIndent 
     1250                isNames = .isDeclNames 
     1251                attribs = .hasAttribs 
     1252                if .optional('ASSIGN'), initExpr = .expression to ? 
     1253                .optional('EOL') 
     1254                docString = .docString 
     1255                .dedent 
     1256        if not initExpr and type is nil # no initial value and no 'as' clause 
     1257            type = .typeProvider.defaultType 
     1258        if isVar 
     1259            field = BoxVar(token, idToken, .curBox, name, type, isNames, initExpr, attribs, docString) to BoxField 
     1260        else 
     1261            field = BoxConst(token, idToken, .curBox, name, type, isNames, initExpr, attribs, docString) 
     1262        return field 
     1263 
     1264    def _validateBoxFieldId(name as String, isVar as bool) 
     1265        """Run various checks on name as a boxField Id and throw errors if checks fail."""   
    12301266        other = .curBox.declForName(name) 
    12311267        if other 
    12321268            .throwError('The name "[name]" was already declared earlier.')  # TODO: show the location of the previous symbol 
     
    12411277            sugg = String(c'_', numUnderscores) + s[0].toString.toLower + s[1:] 
    12421278            sugg = ' Try "[sugg]".' 
    12431279            .throwError('Class variables must start with lowercase letters (after the underscore(s)) to distinguish them from other types of identifiers.[sugg]') 
    1244         type = if(.optional('AS'), .typeId, nil) to ITypeProxy? 
    1245         if .optional('ASSIGN') 
    1246             initExpr = .expression to ? 
    1247         else 
    1248             initExpr = nil 
    1249             if type is nil 
    1250                 type = .typeProvider.defaultType 
    1251         docString = '' to ? 
    1252         isNames = List<of String>(_isNamesStack) 
    1253         if .peek.which=='IS' 
    1254             isNames = .isDeclNames 
    1255             attribs = .hasAttribs 
    1256             assert .last.which=='EOL' 
    1257             .undo  # need the EOL 
    1258             if .optionalIndent 
    1259                 docString = .docString 
    1260                 .dedent 
    1261         else if .optionalIndent 
    1262             isNames = .isDeclNames 
    1263             attribs = .hasAttribs 
    1264             docString = .docString 
    1265             .dedent 
    1266         if isVar 
    1267             field = BoxVar(token, idToken, .curBox, name, type, isNames, initExpr, attribs, docString) to BoxField 
    1268         else 
    1269             field = BoxConst(token, idToken, .curBox, name, type, isNames, initExpr, attribs, docString) 
    1270         return field 
    1271                  
     1280     
     1281         
    12721282    def declareInvariant 
    12731283        .expect('INVARIANT') 
    12741284        if .optional('COLON') 
  • Tests/120-classes/228-fields-multiple-syntax-variants.cobra

     
     1# Test many/most/all of the non block syntax variants for declaring fields  
     2# (new clause order, modified syntax Oct-2009) 
     3# Triggered by ticket34: includes commented src showing old bug in using  
     4# isnames and initialAssign on field decl (var) 
     5 
     6# Modified field dcl clause order is: 
     7# var <variableName> [as <Type>] [is <AccessModifiers> [has <Attributes>] ] [= <initialValue>] 
     8#    [<DocString>] 
     9#   and 
     10# var <variableName> [as <Type>]   
     11#    [is <AccessModifiers>] 
     12#    [has <Attributes>] 
     13#    [= <initialValue>]     
     14#    [<DocString>] 
     15 
     16# The executable code is just for completeness, any failure will be in the field decls (var ...) 
     17 
     18class FieldIsNameAssign 
     19    # simplest, most common 
     20    var myVal0 is shared 
     21    var myVal1  = 'works' 
     22    var myVal2 as int 
     23     
     24    var myVal3 = 110 
     25        """DocStr myVal3""" 
     26    var myVal4 as int is shared 
     27        """DocStr myVal4""" 
     28    var myVal5 as int = 111 
     29        """DocStr myVal5""" 
     30         
     31# This shows original ticket:34 error 
     32#   var myValBroke = "now not broke" is shared  
     33#   gives:  
     34#   error: Expecting an expression. "shared" is a reserved keyword that is not expected here. 
     35# Could not have both isnames and assignment in (simple) single line form 
     36# The error was due to parse collision on 'is' binaryOp in expr (from assignment) and 
     37# 'is' keyword for isnames (the binaryOP 'is' takes precedence from the assignment 
     38# expression parsing and the <AccessModifier(s)> are not handleable as an expression 
     39# (instead correctly recognised as keyword(s)) consequently the 'is' <accessModifiers> clause   
     40# is not seen by the var/fields parse code. 
     41 
     42# This worked (below) - initial Expr and is accessModifier - (oldstyle) but is hardly obvious 
     43# This continues to compile but should be deprecated 
     44    var myValOldStyle = "not broke" 
     45        is shared 
     46        """docstr3""" 
     47    # the new form equivalent is  
     48    var myValNewStyle is shared = "not broke" 
     49    # or 
     50    var myValNewStyle1 is shared = "not broke" 
     51        """docstr3 newStyle""" 
     52         
     53# The modified syntax (listed above) puts the 'is <AccessModifier>' 
     54#   (and 'has <Attributes>',  they're kept together for some reason) clauses 
     55#   before the initial Expr ( = <initialExpr>) clause which removes the  
     56#  parsing collision/ambiguity. 
     57 
     58    # With modified syntax - 'single' line variant - is before assign 
     59    var myStr as String is shared = "OK"  
     60    var myValMod0 is shared,public = "new Mod 1, no docstr"  
     61    var myValMod1 is shared = "new Mod 0"  
     62        """docstrMod0""" 
     63    var myValMod2 is shared has System.Obsolete('NoUse') = "new Mod 2" 
     64        """docstrMod2 - full clause set, 1 liner + docstr""" 
     65    var avar as int is shared = 99 
     66    var avar1 as int = 100 
     67 
     68    # Multiliner - isnames before assign 
     69    # This looks a bit funny with all the clauses used (initialExpr buried in lines) but 
     70    # it has the advantage of being ordered consistently with the single line form 
     71    var myValMod3  
     72        is shared  
     73        has System.Obsolete('NoUse')  
     74        = "new Mod 3"  
     75        """docstrMod3""" 
     76 
     77    # multiliner, simpler probably more common 
     78    var myValMod4 #[as String] 
     79        is shared 
     80        = "new Mod 4"        
     81        """docstrMod4""" 
     82     
     83    # multiliner, probably most common use of multiline variants 
     84    var myValMod5 #[as String] 
     85        is shared 
     86        = "new Mod 5"        
     87 
     88     
     89    def main is shared 
     90        assert not .myVal0  
     91        assert .myStr == 'OK'  
     92        assert .avar == 99 
     93        assert .myVal4 == 0 
     94 
     95        v = FieldIsNameAssign() 
     96        assert v.myVal1 == 'works'       
     97        assert v.myVal2 == 0 
     98        assert v.myVal3 == 110 
     99        a = v 
     100        b = FieldIsNameAssign() 
     101        assert a is v 
     102        assert a is not b 
     103        assert b is not v 
     104         
     105        assert .myValOldStyle == 'not broke' 
     106        assert .myValMod3 == 'new Mod 3' 
     107        assert .myValMod4 == 'new Mod 4' 
  • Tests/110-basics-two/160-lines-and-comments/220-multiline-comment-indented.cobra

     
    1212        _foo as int = 9 is shared 
    1313        _bar as String is shared  
    1414        #/  
    15         var foo1 as int = 9  
    16             is shared, private 
     15        var foo1 as int is shared,private = 9  
    1716 
    1817            /# 3 
    1918            %error die 
  • Tests/110-basics-two/160-lines-and-comments/200-multiline-comment.cobra

     
    1212        _foo as int = 9 is shared 
    1313        _bar as String is shared  
    1414#/ 
    15         var foo1 as int = 9  
    16             is shared, private 
     15        var foo1 as int is shared,private = 9  
    1716 
    1817 
    1918/#{ 3