Wiki

Ticket #34: 2014-02-24_var-assign-is-has.patch

File 2014-02-24_var-assign-is-has.patch, 4.7 KB (added by nerdzero, 4 years ago)
  • Source/CobraParser.cobra

     
    12981298        isNames = List<of String>(_modifierNamesStack)  # uuu 
    12991299        attribs = initExpr = nil 
    13001300        docString = '' to ? 
    1301         if .peek.which in ['IS', 'ASSIGN']  # one liner variant 
    1302             isNames = .isDeclNames 
    1303             attribs = .hasAttribs 
    1304             if .optional('ASSIGN'), initExpr = .expression to ? 
    1305             .optional('EOL') 
    1306             assert .last.which=='EOL' 
    1307             .ungrab  # need the EOL 
    1308             if .optionalIndent 
    1309                 # special for only old form with both isnames and initValue that compiled 
    1310                 # "var id <=init> <EOL> <indent> is <isnames> 
    1311                 if .peek.which == 'IS', isNames = .isDeclNames # TODO: transition out 
    1312                 docString = .docString 
    1313                 .dedent 
    1314         else if .peek.which == 'EOL'     
    1315             if .optionalIndent 
    1316                 isNames = .isDeclNames 
    1317                 attribs = .hasAttribs 
    1318                 if .optional('ASSIGN'), initExpr = .expression to ? 
    1319                 .optional('EOL') 
    1320                 docString = .docString 
    1321                 .dedent 
     1301        done = false 
     1302        didAssign = false 
     1303        lineNumIs, lineNumHas = 0, 0 # for reporting duplicate "is" and "has" specifications 
     1304         
     1305        while not done 
     1306            peek = .peek 
     1307            if peek.isEOF, .throwError('Unexpected end of source.') 
     1308             
     1309            branch peek.which 
     1310                on 'ASSIGN' 
     1311                    .grab 
     1312                    initExpr = .expression to ? 
     1313                    didAssign = true 
     1314                on 'HAS' 
     1315                    lineNumHas = peek.lineNum 
     1316                    if didAssign, .throwError(.grab, 'The "has" keyword and attributes should either precede the equals sign or be indented on the next line') 
     1317                    attribs = .hasAttribs 
     1318                on 'IS' 
     1319                    lineNumIs = peek.lineNum 
     1320                    if didAssign, .throwError(.grab, 'The "is" keyword and modifiers should either precede the equals sign or be indented on the next line') 
     1321                    isNames = .isDeclNames 
     1322                else 
     1323                    last = .last 
     1324                    if last is not nil and last.which == 'EOL' 
     1325                        # .hasAttribs and .isDeclNames consume the EOL but we need it for later 
     1326                        .ungrab 
     1327                    done = true 
     1328         
     1329        if .optionalIndent 
     1330            while .peek.which in ['IS', 'HAS'] 
     1331                if .peek.which == 'IS' 
     1332                    if lineNumIs > 0, .throwError(.grab, '"is" modifiers were already specified earlier on line [lineNumIs] for the field declaration: [name]') 
     1333                    isNames = .isDeclNames 
     1334                if .peek.which == 'HAS' 
     1335                    if lineNumHas > 0, .throwError(.grab, '"has" attributes were already specified earlier on line [lineNumHas] for the field declaration: [name]') 
     1336                    attribs = .hasAttribs 
     1337            docString = .docString 
     1338            .dedent 
     1339         
    13221340        if not initExpr and type is nil # no initial value and no 'as' clause 
    13231341            type = .typeProvider.defaultType 
    13241342        if isVar 
  • Tests/120-classes/228-fields-multiple-syntax-variants.cobra

     
    6565    var avar as int is shared = 99 
    6666    var avar1 as int = 100 
    6767 
    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"  
     68    # Multiliners 
     69    var myValMod3 = "new Mod 3" 
     70        is shared 
    7571        """docstrMod3""" 
    7672 
    77     # multiliner, simpler probably more common 
    78     var myValMod4 #[as String] 
    79         is shared 
    80         = "new Mod 4"        
     73    var myValMod4 = "new Mod 4" 
     74        is shared, readonly  
     75        has Diagnostics.DebuggerDisplay('anAttribute')  
    8176        """docstrMod4""" 
    82      
    83     # multiliner, probably most common use of multiline variants 
    84     var myValMod5 #[as String] 
     77 
     78    var myValMod4a as dynamic = "explicitly typed" 
     79        is shared, readonly  
     80        has Diagnostics.DebuggerDisplay('anAttribute')  
     81        """docstrMod4a""" 
     82 
     83    var myValMod4b = "Alternate is/has order" 
     84        has Diagnostics.DebuggerDisplay('anAttribute')  
     85        is shared, readonly  
     86        """docstrMod4a""" 
     87 
     88    var myValMod5 as dynamic is shared = 5 
     89        has Diagnostics.DebuggerDisplay('anAttribute')  
     90        """docString""" 
     91 
     92    var myValMod5a is shared = 5 
     93        has Diagnostics.DebuggerDisplay('anAttribute')  
     94        """docString""" 
     95 
     96    var myValMod5b has System.Obsolete = 5 
    8597        is shared 
    86         = "new Mod 5"        
     98        """docString""" 
    8799 
    88      
     100    # long form one-liners 
     101    var myValMod6 is shared has Diagnostics.DebuggerDisplayAttribute('LongForm') = 6 
     102        """docString""" 
     103 
     104    var myValMod6a as int is protected, shared, readonly has Diagnostics.DebuggerDisplayAttribute('LongForm') = 6 
     105        """docString""" 
     106 
     107    var myValMod6b as int has Diagnostics.DebuggerDisplayAttribute('LongForm') is protected, shared, readonly = 6 
     108        """docString""" 
     109 
    89110    def main is shared 
    90111        assert not .myVal0  
    91112        assert .myStr == 'OK'