Wiki

Ticket #34: var-assign-is-has-augmented.patch

File var-assign-is-has-augmented.patch, 4.2 KB (added by hopscc, 11 years ago)
  • Source/CobraParser.cobra

     
    13131313            peek = .peek 
    13141314            if peek.isEOF, .throwError('Unexpected end of source.') 
    13151315             
    1316             branch peek.which 
     1316            branch peek.which # oneliner: order here must have HAS/IS before ASSIGN 
    13171317                on 'ASSIGN' 
    13181318                    .grab 
    1319                     initExpr = .expression to ? 
     1319                    try 
     1320                        initExpr = .expression to ? 
     1321                    catch pe as ParserException 
     1322                        if pe.message.contains('is a reserved keyword') and .last(1).which == 'IS' 
     1323                            .ungrab  
     1324                            .ungrab # backup to 'is' keyword so next loop cycle throws error below 
     1325                        else, throw pe   
    13201326                    didAssign = true 
    13211327                on 'HAS' 
    13221328                    lineNumHas = peek.lineNum 
     
    13321338                        # .hasAttribs and .isDeclNames consume the EOL but we need it for later 
    13331339                        .ungrab 
    13341340                    done = true 
    1335          
    13361341        if .optionalIndent 
    1337             while .peek.which in ['IS', 'HAS'] 
     1342            while .peek.which in ['IS', 'HAS', 'ASSIGN'] 
    13381343                if .peek.which == 'IS' 
    1339                     if lineNumIs > 0, .throwError(.grab, '"is" modifiers were already specified earlier on line [lineNumIs] for the field declaration: [name]') 
     1344                    if lineNumIs, .throwError(.grab, '"is" modifiers were already specified earlier on line [lineNumIs] for the field declaration: [name]') 
    13401345                    isNames = .isDeclNames 
    13411346                if .peek.which == 'HAS' 
    1342                     if lineNumHas > 0, .throwError(.grab, '"has" attributes were already specified earlier on line [lineNumHas] for the field declaration: [name]') 
     1347                    if lineNumHas, .throwError(.grab, '"has" attributes were already specified earlier on line [lineNumHas] for the field declaration: [name]') 
    13431348                    attribs = .hasAttribs 
     1349                if .peek.which == 'ASSIGN' 
     1350                    if didAssign, .throwError(.grab, 'initialiser expression was already specified earlier for the field declaration: [name]') 
     1351                    .grab 
     1352                    initExpr = .expression to ? 
     1353                    .expect('EOL') 
    13441354            docString = .docString 
    13451355            .dedent 
    13461356         
  • Tests/120-classes/228-fields-init-before-is-clause.cobra

     
     1# .error. "is" keyword and modifiers  
     2# Trap error from ticket34:  
     3# old bug in using isnames and initialAssign on field decl (var) in wrong order 
     4# isclause must precede any initialiser clause 
     5class FieldIsNameAssign 
     6     
     7    var myValBroke = "now not broke" is shared # fail here 
     8     
     9    # orig error: Expecting an expression. "shared" is a reserved keyword that is not expected here. 
  • Tests/120-classes/228-fields-multiple-syntax-variants.cobra

     
    6666    var avar1 as int = 100 
    6767 
    6868    # Multiliners 
    69     var myValMod3 = "new Mod 3" 
     69    # Multiliner - isnames before assign  
     70    # This looks a bit funny with all the clauses used (initialExpr buried in lines) but  
     71    # it has the advantage of being ordered consistently with the original single line form  
     72    var myValMod3   
     73        is shared   
     74        has System.Obsolete('NoUse')   
     75        = "new Mod 3"  
     76        """docstr newMod3""" 
     77         
     78    var myValMod3a = "new Mod 3" 
    7079        is shared 
    7180        """docstrMod3""" 
    7281 
     82    var myValMod4o #[as String]  
     83        is shared  
     84        = "new Mod 4" 
     85        """docstrMod4o""" 
     86         
    7387    var myValMod4 = "new Mod 4" 
    7488        is shared, readonly  
    7589        has Diagnostics.DebuggerDisplay('anAttribute')  
     
    96110    var myValMod5b has System.Obsolete = 5 
    97111        is shared 
    98112        """docString""" 
    99  
     113         
     114    var myValMod5c 
     115        is shared  
     116        = "new Mod 5"         
     117        """docString"""  
     118         
     119    # mixed form - has-clause but no is-clause - this one for use in IDE (code) 
     120    var myValMod5d = 6 
     121        has Diagnostics.DebuggerDisplayAttribute('LongForm') 
     122         
    100123    # long form one-liners 
    101124    var myValMod6 is shared has Diagnostics.DebuggerDisplayAttribute('LongForm') = 6 
    102125        """docString"""