Wiki

Ticket #211: implicit-line-cont.patch

File implicit-line-cont.patch, 5.3 KB (added by hopscc, 14 years ago)
  • Source/CobraTokenizer.cobra

     
    425425                firstTok = lastTok = newTok 
    426426        return firstTok 
    427427 
    428     var _didLineContinuation as bool  # only meaningful after an EOL 
     428    var _didLineContinuation as bool   
     429        """  
     430        True if had an explicit (or implicit) line continuation char just before an EOL 
     431        Only meaningful after an EOL. 
     432        """ 
     433    # List of binary ops that can act as implicit line continuation, ordered by approx freq of use 
     434    var implicitContinuationOps = [  
     435        'ASSIGN', 'AND', 'OR', 'STAR', 'SLASH', 'PLUS', 
     436        'MINUS','EQ', 'NE', 'LT', 'LE', 'GT', 'GE', 'IN', 'NOTIN', 
     437        'PLUS_EQUALS', 'MINUS_EQUALS', 'STAR_EQUALS', 'SLASH_EQUALS', 'PERCENT_EQUALS', 
     438        'SLASHSLASH', 'PERCENT', 'AMPERSAND', 'VERTICAL_BAR', 'CARET', 'DOUBLE_LT', 
     439        'DOUBLE_GT', 'AMPERSAND_EQUALS', 'VERTICAL_BAR_EQUALS', 'CARET_EQUALS',  
     440        'DOUBLE_LT_EQUALS', 'DOUBLE_GT_EQUALS', 'STARSTAR', 'STARSTAR_EQUALS'  
     441    ]  
    429442 
     443    var _seenSuppressor = false 
     444        """ 
     445        Record if have seen keywords on current line that may indicate a trailing implicit 
     446            line continuation token is not part of an expression 
     447        """ 
     448     
    430449    get justDidLineContinuation as bool 
    431450        return .lastToken and .lastToken.which == 'EOL' and _didLineContinuation 
    432451 
    433452    def onEOL(tok as IToken) as IToken? 
    434453        _didLineContinuation = .lastToken and .lastToken.text == '_' and .lastToken.which == 'ID' 
     454        if not _didLineContinuation and .lastToken  
     455            _didLineContinuation = .lastToken.which in .implicitContinuationOps 
     456            # These below need special handling to avoid false positives for trailing  
     457            # type declarations of streams (*) and generics (>) 
     458            if (.lastToken.which == 'STAR' or .lastToken.which == 'GT')  and _seenSuppressor 
     459                _didLineContinuation = false 
     460            #if _didLineContinuation 
     461            #   print .lastToken, .lastToken.fileName, .lastToken.lineNum, _didLineContinuation 
     462        _seenSuppressor = false 
    435463        return tok 
    436464 
    437465    def onSINGLE_LINE_COMMENT(tok as IToken) as IToken? 
     
    463491        require tok.text.trim.endsWith('<of') 
    464492        s = tok.text.trim 
    465493        tok.value = s[:-3] 
     494        if not _seenSuppressor, _seenSuppressor = true 
    466495        return tok 
    467  
     496         
     497    var _keywordsSuppressingILCont = ['DEF', 'CLASS', 'STRUCT', 'AS', 'IMPLEMENTS', 'INHERITS']  
     498        """ Keywords suppressing implicit line continuation from a trailing operator token""" 
     499     
    468500    def onID(tok as IToken) as IToken? 
    469501        tok = .keywordOrWhich(tok, 'ID') 
    470502        if tok.which<>'ID' 
    471503            tok.isKeyword = true 
     504            if not _seenSuppressor 
     505                _seenSuppressor = (tok.which in _keywordsSuppressingILCont) 
    472506        return tok 
    473507 
    474508    def onFLOAT_LIT_1(tok as IToken) as IToken? 
  • Source/CobraParser.cobra

     
    29042904            else 
    29052905                if .opStack.count and .opStack.peek=='DOT' and .peek.isKeyword 
    29062906                    return .identifierExpr 
    2907                 else 
     2907                else if .peek.which =='EOL' and .last.which in _tokenizer.implicitContinuationOps 
     2908                    # trailing op on EOL - implicit line continuation 
     2909                    .grab 
     2910                    return .expression2 
     2911                else         
    29082912                    try 
    29092913                        return .typeExpr 
    29102914                    catch pe as ParserException 
  • Tests/110-basics-two/150-line-continuation/120-implicit-line-cont-ops.cobra

     
     1# Test for implicit forms of line continuation: round brackets and trailing binary ops 
     2# ticket:211 (partially also addresses ticket 210) 
     3class Program 
     4 
     5    def main 
     6        assert 1 + 2 == 3       # ok 
     7             
     8        assert (1 +     # braces ok 
     9            2) == 3 
     10        assert 1 *      # implicit trailing '*', 0 indent 
     11        2  == 2 
     12         
     13        assert 1 +      # implicit trailing '+', 1 indent 
     14            2 == 3 
     15        assert (1 +     # Ticket 210: Cannot mix tabs and spaces in indentation. 
     16               2) 
     17        assert 1 +      # Both tickets. 
     18              2 == 3 
     19 
     20        assert (1 +     # braces, double indent 
     21                12) == 13 
     22        assert 1 +      # trailing '+', double indent after 
     23                2 == 3 
     24        assert 3 -      # trailing '-', double indent after 
     25                2 == 1 
     26                 
     27        assert 3 *      # ((3*1) + 4) -(8/2) 
     28            1 + 
     29            4 - 
     30            8 / 
     31            2 == 3 
     32             
     33        a = 1 
     34        b = 2 
     35        assert (a == 1 and   
     36            b == 2) 
     37        assert a == 1 and   #implicit on 'and' 
     38            b == 2 
     39        assert a == 1 or    # implicit on or 
     40            b == 2 
     41 
     42        assert a == 1 and   # implicit on and and tabs+spaces 
     43               b == 2 
     44 
     45             
  • Developer/IntermediateReleaseNotes.text

     
    505505* Fixed: In combination with the directive `@number float32`, negative numeric literals such as "-1.0" can cause a false compilation error about types. reported-by:Gekko 
    506506 
    507507* Fixed: A nil/null field in the a library caused the compiler to throw an exception. 
     508 
     509* Fixed: Add implicit line continuation for lines ending in an expression with a trailing operator. ticket:211