Ticket #304: ticket-304.patch
File ticket-304.patch, 4.1 KB (added by hopscc, 12 years ago) |
---|
-
Source/CobraParser.cobra
114 114 115 115 var _spaceAgnosticIndentLevel as int 116 116 var _spaceAgnosticExprLevel as int 117 var _isContractOnSameLine as bool118 117 119 118 var _isTraceOn as bool 120 119 … … 1925 1924 <any statement 2> 1926 1925 <any statement N> 1927 1926 Example source 1927 require <cond 1> | 1928 1928 require 1929 1929 <cond 1> 1930 1930 <cond N> 1931 ensure <cond 1> | 1931 1932 ensure 1932 1933 <cond 1> 1933 1934 <cond N> 1934 test1935 [test 1935 1936 <any statement 1> 1936 1937 <any statement 2> 1937 <any statement N> 1938 <any statement N>] 1938 1939 [body 1939 1940 <any statement 1> 1940 1941 <any statement 2> … … 1956 1957 while .peek.which == 'EOL', .grab 1957 1958 if .peek.which.isOneOf('BODY.TEST.REQUIRE.ENSURE.OR.AND.') 1958 1959 # sectional 1959 # not flexible. sequence is signature, contract , test, implementation1960 _isContractOnSameLine = false1960 # not flexible. sequence is signature, contracts, tests, implementation body 1961 haveCode = false 1961 1962 if .peek.which.isOneOf('REQUIRE.OR.') 1963 haveCode = true 1962 1964 .requireSection(codePart) 1963 1965 if .peek.which.isOneOf('ENSURE.AND.') 1966 haveCode = true 1964 1967 .ensureSection(codePart) 1965 1968 while .peek.which == 'TEST' 1969 haveCode = true 1966 1970 .testSection(codePartContainingTest to !) 1967 1971 if .peek.which=='BODY' 1968 1972 .grab 1969 1973 .indent 1970 1974 _statementsFor(codePart) 1975 .dedent # back to level of body 1971 1976 else 1972 if _isContractOnSameLine1977 if haveCode # already have contract or test subclause so must also have body statements 1973 1978 _statementsFor(codePart) 1974 1979 else 1975 1980 pass 1976 1981 # There is no body for abstract or interface members. Error checking is done elsewhere. 1977 1982 # .throwError('Expecting `body` section.') 1978 if not _isContractOnSameLine1979 .dedent1980 1983 else 1981 1984 # non-sectional 1982 1985 _statementsFor(codePart) … … 2463 2466 # one expression, on the same line 2464 2467 exprs = [.expression] 2465 2468 .endOfLine 2466 _isContractOnSameLine = true2467 2469 return theClass(connectToken, mainToken, codeMember, exprs) to ContractPart 2468 2470 2469 2471 def throwStmt as Stmt -
Tests/340-contracts/090-parse-test.cobra
1 # test the main the variants of contract layout - looking for parsing errors 2 class ContractParsing 3 # this was the failure case 4 def dedentError(x as int) as char 5 require x < 3 6 test 7 assert true 8 body 9 return c'z' 10 # to here 11 12 def dedent1(x as int) as char 13 require 14 x < 3 15 test 16 assert true 17 body 18 return c'z' 19 20 21 def dedent2(x as int) as char 22 require x < 3 23 #test 24 # assert true 25 body 26 return c'z' 27 28 def dedent2a(x as int) as char 29 require 30 x < 3 31 #test 32 # assert true 33 body 34 return c'z' 35 36 def dedent3(x as int) as char 37 require x < 3 38 #test 39 # assert true 40 #body 41 return c'z' 42 43 def dedent3a(x as int) as char 44 require 45 x < 3 46 #test 47 # assert true 48 #body 49 return c'z' 50 51 def dedent4(x as int) as char 52 require x < 3 53 test 54 assert true 55 #body 56 return c'z' 57 58 def dedent4a(x as int) as char 59 require 60 x < 3 61 test 62 assert true 63 #body 64 return c'z' 65 66 def dedent5(x as int) as char 67 #require x < 3 68 test 69 assert true 70 #body 71 return c'z' 72 73 def dedent5a(x as int) as char 74 #require x < 3 75 test 76 assert true 77 body 78 return c'z' 79 80 81 def main is shared 82 c = ContractParsing() 83 assert c.dedentError(1) == c'z' 84 assert c.dedent1(1) == c'z' 85 assert c.dedent2(1) == c'z' 86 assert c.dedent2a(2) == c'z' 87 assert c.dedent3(1) == c'z' 88 assert c.dedent3a(2) == c'z' 89 assert c.dedent4(1) == c'z' 90 assert c.dedent4a(2) == c'z' 91 assert c.dedent5(1) == c'z' 92 assert c.dedent5a(1) == c'z'