using System.Collections.Generic; using System.Numerics; using Microsoft.Boogie; using System.IO; using System.Text; using System; using System.Diagnostics.Contracts; namespace Microsoft.Dafny { public class Parser { public const int _EOF = 0; public const int _ident = 1; public const int _digits = 2; public const int _hexdigits = 3; public const int _decimaldigits = 4; public const int _arrayToken = 5; public const int _bool = 6; public const int _char = 7; public const int _int = 8; public const int _nat = 9; public const int _real = 10; public const int _object = 11; public const int _string = 12; public const int _set = 13; public const int _multiset = 14; public const int _seq = 15; public const int _map = 16; public const int _imap = 17; public const int _charToken = 18; public const int _stringToken = 19; public const int _colon = 20; public const int _comma = 21; public const int _verticalbar = 22; public const int _doublecolon = 23; public const int _bullet = 24; public const int _dot = 25; public const int _semi = 26; public const int _darrow = 27; public const int _arrow = 28; public const int _assume = 29; public const int _calc = 30; public const int _case = 31; public const int _then = 32; public const int _else = 33; public const int _decreases = 34; public const int _invariant = 35; public const int _function = 36; public const int _predicate = 37; public const int _inductive = 38; public const int _lemma = 39; public const int _copredicate = 40; public const int _modifies = 41; public const int _reads = 42; public const int _requires = 43; public const int _lbrace = 44; public const int _rbrace = 45; public const int _lbracket = 46; public const int _rbracket = 47; public const int _openparen = 48; public const int _closeparen = 49; public const int _openAngleBracket = 50; public const int _closeAngleBracket = 51; public const int _eq = 52; public const int _neq = 53; public const int _neqAlt = 54; public const int _star = 55; public const int _notIn = 56; public const int _ellipsis = 57; public const int maxT = 136; const bool _T = true; const bool _x = false; const int minErrDist = 2; public Scanner/*!*/ scanner; public Errors/*!*/ errors; public Token/*!*/ t; // last recognized token public Token/*!*/ la; // lookahead token int errDist = minErrDist; readonly Expression/*!*/ dummyExpr; readonly AssignmentRhs/*!*/ dummyRhs; readonly FrameExpression/*!*/ dummyFrameExpr; readonly Statement/*!*/ dummyStmt; readonly ModuleDecl theModule; readonly BuiltIns theBuiltIns; readonly bool theVerifyThisFile; int anonymousIds = 0; struct MemberModifiers { public bool IsGhost; public bool IsStatic; public bool IsProtected; } /// /// Parses top-level things (modules, classes, datatypes, class members) from "filename" /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner. /// public static int Parse (string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, Errors/*!*/ errors, bool verifyThisFile=true) /* throws System.IO.IOException */ { Contract.Requires(filename != null); Contract.Requires(module != null); string s; if (filename == "stdin.dfy") { s = Microsoft.Boogie.ParserHelper.Fill(System.Console.In, new List()); return Parse(s, filename, module, builtIns, errors, verifyThisFile); } else { using (System.IO.StreamReader reader = new System.IO.StreamReader(filename)) { s = Microsoft.Boogie.ParserHelper.Fill(reader, new List()); return Parse(s, DafnyOptions.Clo.UseBaseNameForFileName ? Path.GetFileName(filename) : filename, module, builtIns, errors, verifyThisFile); } } } /// /// Parses top-level things (modules, classes, datatypes, class members) /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner. /// public static int Parse (string/*!*/ s, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, bool verifyThisFile=true) { Contract.Requires(s != null); Contract.Requires(filename != null); Contract.Requires(module != null); Errors errors = new Errors(); return Parse(s, filename, module, builtIns, errors, verifyThisFile); } /// /// Parses top-level things (modules, classes, datatypes, class members) /// and appends them in appropriate form to "module". /// Returns the number of parsing errors encountered. /// Note: first initialize the Scanner with the given Errors sink. /// public static int Parse (string/*!*/ s, string/*!*/ filename, ModuleDecl module, BuiltIns builtIns, Errors/*!*/ errors, bool verifyThisFile=true) { Contract.Requires(s != null); Contract.Requires(filename != null); Contract.Requires(module != null); Contract.Requires(errors != null); byte[]/*!*/ buffer = cce.NonNull( UTF8Encoding.Default.GetBytes(s)); MemoryStream ms = new MemoryStream(buffer,false); Scanner scanner = new Scanner(ms, errors, filename); Parser parser = new Parser(scanner, errors, module, builtIns, verifyThisFile); parser.Parse(); return parser.errors.count; } public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors, ModuleDecl module, BuiltIns builtIns, bool verifyThisFile=true) : this(scanner, errors) // the real work { // initialize readonly fields dummyExpr = new LiteralExpr(Token.NoToken); dummyRhs = new ExprRhs(dummyExpr, null); dummyFrameExpr = new FrameExpression(dummyExpr.tok, dummyExpr, null); dummyStmt = new ReturnStmt(Token.NoToken, Token.NoToken, null); theModule = module; theBuiltIns = builtIns; theVerifyThisFile = verifyThisFile; } bool IsAttribute() { Token x = scanner.Peek(); return la.kind == _lbrace && x.kind == _colon; } bool IsAlternative() { Token x = scanner.Peek(); return la.kind == _lbrace && x.kind == _case; } bool IsLoopSpec() { return la.kind == _invariant | la.kind == _decreases | la.kind == _modifies; } bool IsFunctionDecl() { switch (la.kind) { case _function: case _predicate: case _copredicate: return true; case _inductive: return scanner.Peek().kind != _lemma; default: return false; } } bool IsParenStar() { scanner.ResetPeek(); Token x = scanner.Peek(); return la.kind == _openparen && x.kind == _star; } bool IsEquivOp() { return la.val == "<==>" || la.val == "\u21d4"; } bool IsImpliesOp() { return la.val == "==>" || la.val == "\u21d2"; } bool IsExpliesOp() { return la.val == "<==" || la.val == "\u21d0"; } bool IsAndOp() { return la.val == "&&" || la.val == "\u2227"; } bool IsOrOp() { return la.val == "||" || la.val == "\u2228"; } bool IsRelOp() { return la.val == "==" || la.val == "<" || la.val == ">" || la.val == "<=" || la.val == ">=" || la.val == "!=" || la.val == "in" || la.kind == _notIn || la.val =="!" || la.val == "\u2260" || la.val == "\u2264" || la.val == "\u2265"; } bool IsAddOp() { return la.val == "+" || la.val == "-"; } bool IsMulOp() { return la.kind == _star || la.val == "/" || la.val == "%"; } bool IsQSep() { return la.kind == _doublecolon || la.kind == _bullet; } bool IsNonFinalColon() { return la.kind == _colon && scanner.Peek().kind != _rbracket; } bool IsMapDisplay() { return la.kind == _map && scanner.Peek().kind == _lbracket; } bool IsIMapDisplay() { return la.kind == _imap && scanner.Peek().kind == _lbracket; } bool IsSuffix() { return la.kind == _dot || la.kind == _lbracket || la.kind == _openparen; } string UnwildIdent(string x, bool allowWildcardId) { if (x.StartsWith("_")) { if (allowWildcardId && x.Length == 1) { return "_v" + anonymousIds++; } else { SemErr("cannot declare identifier beginning with underscore"); } } return x; } bool IsLambda(bool allowLambda) { if (!allowLambda) { return false; } scanner.ResetPeek(); Token x; // peek at what might be a signature of a lambda expression if (la.kind == _ident) { // cool, that's the entire candidate signature } else if (la.kind != _openparen) { return false; // this is not a lambda expression } else { int identCount = 0; x = scanner.Peek(); while (x.kind != _closeparen) { if (identCount != 0) { if (x.kind != _comma) { return false; // not the signature of a lambda } x = scanner.Peek(); } if (x.kind != _ident) { return false; // not a lambda expression } identCount++; x = scanner.Peek(); if (x.kind == _colon) { // a colon belongs only in a lamdba signature, so this must be a lambda (or something ill-formed) return true; } } } // What we have seen so far could have been a lambda signature or could have been some // other expression (in particular, an identifier, a parenthesized identifier, or a // tuple all of whose subexpressions are identifiers). // It is a lambda expression if what follows is something that must be a lambda. x = scanner.Peek(); return x.kind == _darrow || x.kind == _arrow || x.kind == _reads || x.kind == _requires; } bool IsIdentParen() { Token x = scanner.Peek(); return la.kind == _ident && x.kind == _openparen; } bool IsIdentColonOrBar() { Token x = scanner.Peek(); return la.kind == _ident && (x.kind == _colon || x.kind == _verticalbar); } bool SemiFollowsCall(bool allowSemi, Expression e) { return allowSemi && la.kind == _semi && e is ApplySuffix; } bool CloseOptionalBrace(bool usesOptionalBrace) { return usesOptionalBrace && la.kind == _rbrace; } bool IsNotEndOfCase() { return la.kind != _EOF && la.kind != _rbrace && la.kind != _case; } /* The following is the largest lookahead there is. It needs to check if what follows * can be nothing but "<" Type { "," Type } ">". */ bool IsGenericInstantiation() { scanner.ResetPeek(); IToken pt = la; if (!IsTypeList(ref pt)) { return false; } /* There are ambiguities in the parsing. For example: * F( a < b , c > (d) ) * can either be a unary function F whose argument is a function "a" with type arguments "" and * parameter "d", or can be a binary function F with the two boolean arguments "a < b" and "c > (d)". * To make the situation a little better, we (somewhat heuristically) look at the character that * follows the ">". Note that if we, contrary to a user's intentions, pick "a" out as a function * with a type instantiation, the user can disambiguate it by making sure the ">" sits inside some * parentheses, like: * F( a < b , (c > (d)) ) */ switch (pt.kind) { case _dot: // here, we're sure it must have been a type instantiation we saw, because an expression cannot begin with dot case _openparen: // it was probably a type instantiation of a function/method case _lbracket: // it is possible that it was a type instantiation case _lbrace: // it was probably a type instantiation of a function/method // In the following cases, we're sure we must have read a type instantiation that just ended an expression case _closeparen: case _rbracket: case _rbrace: case _semi: case _then: case _else: case _case: case _eq: case _neq: case _neqAlt: case _openAngleBracket: case _closeAngleBracket: return true; default: return false; } } bool IsTypeList(ref IToken pt) { if (pt.kind != _openAngleBracket) { return false; } pt = scanner.Peek(); return IsTypeSequence(ref pt, _closeAngleBracket); } bool IsTypeSequence(ref IToken pt, int endBracketKind) { while (true) { if (!IsType(ref pt)) { return false; } if (pt.kind == endBracketKind) { // end of type list pt = scanner.Peek(); return true; } else if (pt.kind == _comma) { // type list continues pt = scanner.Peek(); } else { // not a type list return false; } } } bool IsType(ref IToken pt) { switch (pt.kind) { case _bool: case _char: case _nat: case _int: case _real: case _object: case _string: pt = scanner.Peek(); return true; case _arrayToken: case _set: case _multiset: case _seq: case _map: case _imap: pt = scanner.Peek(); return IsTypeList(ref pt); case _ident: while (true) { // invariant: next token is an ident pt = scanner.Peek(); if (pt.kind == _openAngleBracket && !IsTypeList(ref pt)) { return false; } if (pt.kind != _dot) { // end of the type return true; } pt = scanner.Peek(); // get the _dot if (pt.kind != _ident) { return false; } } case _openparen: pt = scanner.Peek(); return IsTypeSequence(ref pt, _closeparen); default: return false; } } /*--------------------------------------------------------------------------*/ public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors) { this.scanner = scanner; this.errors = errors; Token/*!*/ tok = new Token(); tok.val = ""; this.la = tok; this.t = new Token(); // just to satisfy its non-null constraint } void SynErr (int n) { if (errDist >= minErrDist) errors.SynErr(la.filename, la.line, la.col, n); errDist = 0; } public void SemErr (string/*!*/ msg) { Contract.Requires(msg != null); if (errDist >= minErrDist) errors.SemErr(t, msg); errDist = 0; } public void SemErr(IToken/*!*/ tok, string/*!*/ msg) { Contract.Requires(tok != null); Contract.Requires(msg != null); errors.SemErr(tok, msg); } void Get () { for (;;) { t = la; la = scanner.Scan(); if (la.kind <= maxT) { ++errDist; break; } la = t; } } void Expect (int n) { if (la.kind==n) Get(); else { SynErr(n); } } bool StartOf (int s) { return set[s, la.kind]; } void ExpectWeak (int n, int follow) { if (la.kind == n) Get(); else { SynErr(n); while (!StartOf(follow)) Get(); } } bool WeakSeparator(int n, int syFol, int repFol) { int kind = la.kind; if (kind == n) {Get(); return true;} else if (StartOf(repFol)) {return false;} else { SynErr(n); while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) { Get(); kind = la.kind; } return StartOf(syFol); } } void Dafny() { ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; TopLevelDecl td; IteratorDecl iter; List membersDefaultClass = new List(); ModuleDecl submodule; // to support multiple files, create a default module only if theModule is null DefaultModuleDecl defaultModule = (DefaultModuleDecl)((LiteralModuleDecl)theModule).ModuleDef; // theModule should be a DefaultModuleDecl (actually, the singular DefaultModuleDecl) TraitDecl/*!*/ trait; Contract.Assert(defaultModule != null); while (la.kind == 58) { Get(); Expect(19); { string parsedFile = t.filename; bool isVerbatimString; string includedFile = Util.RemoveParsedStringQuotes(t.val, out isVerbatimString); includedFile = Util.RemoveEscaping(includedFile, isVerbatimString); string fullPath = includedFile; if (!Path.IsPathRooted(includedFile)) { string basePath = Path.GetDirectoryName(parsedFile); includedFile = Path.Combine(basePath, includedFile); fullPath = Path.GetFullPath(includedFile); } defaultModule.Includes.Add(new Include(t, includedFile, fullPath)); } } while (StartOf(1)) { switch (la.kind) { case 59: case 60: case 62: { SubModuleDecl(defaultModule, out submodule); defaultModule.TopLevelDecls.Add(submodule); break; } case 67: { ClassDecl(defaultModule, out c); defaultModule.TopLevelDecls.Add(c); break; } case 73: case 74: { DatatypeDecl(defaultModule, out dt); defaultModule.TopLevelDecls.Add(dt); break; } case 76: { NewtypeDecl(defaultModule, out td); defaultModule.TopLevelDecls.Add(td); break; } case 77: { OtherTypeDecl(defaultModule, out td); defaultModule.TopLevelDecls.Add(td); break; } case 78: { IteratorDecl(defaultModule, out iter); defaultModule.TopLevelDecls.Add(iter); break; } case 69: { TraitDecl(defaultModule, out trait); defaultModule.TopLevelDecls.Add(trait); break; } case 36: case 37: case 38: case 39: case 40: case 70: case 71: case 72: case 75: case 81: case 82: case 83: case 84: { ClassMemberDecl(membersDefaultClass, false, !DafnyOptions.O.AllowGlobals); break; } } } DefaultClassDecl defaultClass = null; foreach (TopLevelDecl topleveldecl in defaultModule.TopLevelDecls) { defaultClass = topleveldecl as DefaultClassDecl; if (defaultClass != null) { defaultClass.Members.AddRange(membersDefaultClass); break; } } if (defaultClass == null) { // create the default class here, because it wasn't found defaultClass = new DefaultClassDecl(defaultModule, membersDefaultClass); defaultModule.TopLevelDecls.Add(defaultClass); } Expect(0); } void SubModuleDecl(ModuleDefinition parent, out ModuleDecl submodule) { ClassDecl/*!*/ c; DatatypeDecl/*!*/ dt; TopLevelDecl td; IteratorDecl iter; Attributes attrs = null; IToken/*!*/ id; TraitDecl/*!*/ trait; List namedModuleDefaultClassMembers = new List();; List idRefined = null, idPath = null, idAssignment = null; ModuleDefinition module; ModuleDecl sm; submodule = null; // appease compiler bool isAbstract = false; bool opened = false; if (la.kind == 59 || la.kind == 60) { if (la.kind == 59) { Get(); isAbstract = true; } Expect(60); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 61) { Get(); QualifiedModuleName(out idRefined); } module = new ModuleDefinition(id, id.val, isAbstract, false, idRefined == null ? null : idRefined, parent, attrs, false); Expect(44); module.BodyStartTok = t; while (StartOf(1)) { switch (la.kind) { case 59: case 60: case 62: { SubModuleDecl(module, out sm); module.TopLevelDecls.Add(sm); break; } case 67: { ClassDecl(module, out c); module.TopLevelDecls.Add(c); break; } case 69: { TraitDecl(module, out trait); module.TopLevelDecls.Add(trait); break; } case 73: case 74: { DatatypeDecl(module, out dt); module.TopLevelDecls.Add(dt); break; } case 76: { NewtypeDecl(module, out td); module.TopLevelDecls.Add(td); break; } case 77: { OtherTypeDecl(module, out td); module.TopLevelDecls.Add(td); break; } case 78: { IteratorDecl(module, out iter); module.TopLevelDecls.Add(iter); break; } case 36: case 37: case 38: case 39: case 40: case 70: case 71: case 72: case 75: case 81: case 82: case 83: case 84: { ClassMemberDecl(namedModuleDefaultClassMembers, false, !DafnyOptions.O.AllowGlobals); break; } } } Expect(45); module.BodyEndTok = t; module.TopLevelDecls.Add(new DefaultClassDecl(module, namedModuleDefaultClassMembers)); submodule = new LiteralModuleDecl(module, parent); } else if (la.kind == 62) { Get(); if (la.kind == 63) { Get(); opened = true; } NoUSIdent(out id); if (la.kind == 64 || la.kind == 65) { if (la.kind == 64) { Get(); QualifiedModuleName(out idPath); submodule = new AliasModuleDecl(idPath, id, parent, opened); } else { Get(); QualifiedModuleName(out idPath); if (la.kind == 66) { Get(); QualifiedModuleName(out idAssignment); } submodule = new ModuleFacadeDecl(idPath, id, parent, idAssignment, opened); } } if (la.kind == 26) { while (!(la.kind == 0 || la.kind == 26)) {SynErr(137); Get();} Get(); errors.Warning(t, "the semi-colon that used to terminate a sub-module declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } if (submodule == null) { idPath = new List(); idPath.Add(id); submodule = new AliasModuleDecl(idPath, id, parent, opened); } } else SynErr(138); } void ClassDecl(ModuleDefinition/*!*/ module, out ClassDecl/*!*/ c) { Contract.Requires(module != null); Contract.Ensures(Contract.ValueAtReturn(out c) != null); IToken/*!*/ id; Type trait = null; List/*!*/ traits = new List(); Attributes attrs = null; List typeArgs = new List(); List members = new List(); IToken bodyStart; while (!(la.kind == 0 || la.kind == 67)) {SynErr(139); Get();} Expect(67); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 50) { GenericParameters(typeArgs); } if (la.kind == 68) { Get(); Type(out trait); traits.Add(trait); while (la.kind == 21) { Get(); Type(out trait); traits.Add(trait); } } Expect(44); bodyStart = t; while (StartOf(2)) { ClassMemberDecl(members, true, false); } Expect(45); c = new ClassDecl(id, id.val, module, typeArgs, members, attrs, traits); c.BodyStartTok = bodyStart; c.BodyEndTok = t; } void DatatypeDecl(ModuleDefinition/*!*/ module, out DatatypeDecl/*!*/ dt) { Contract.Requires(module != null); Contract.Ensures(Contract.ValueAtReturn(out dt)!=null); IToken/*!*/ id; Attributes attrs = null; List typeArgs = new List(); List ctors = new List(); IToken bodyStart = Token.NoToken; // dummy assignment bool co = false; while (!(la.kind == 0 || la.kind == 73 || la.kind == 74)) {SynErr(140); Get();} if (la.kind == 73) { Get(); } else if (la.kind == 74) { Get(); co = true; } else SynErr(141); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 50) { GenericParameters(typeArgs); } Expect(64); bodyStart = t; DatatypeMemberDecl(ctors); while (la.kind == 22) { Get(); DatatypeMemberDecl(ctors); } if (la.kind == 26) { while (!(la.kind == 0 || la.kind == 26)) {SynErr(142); Get();} Get(); errors.Warning(t, "the semi-colon that used to terminate a (co)datatype declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } if (co) { dt = new CoDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs); } else { dt = new IndDatatypeDecl(id, id.val, module, typeArgs, ctors, attrs); } dt.BodyStartTok = bodyStart; dt.BodyEndTok = t; } void NewtypeDecl(ModuleDefinition module, out TopLevelDecl td) { IToken id, bvId; Attributes attrs = null; td = null; Type baseType = null; Expression wh; Expect(76); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); Expect(64); if (IsIdentColonOrBar()) { NoUSIdent(out bvId); if (la.kind == 20) { Get(); Type(out baseType); } if (baseType == null) { baseType = new OperationTypeProxy(true, true, false, false, false); } Expect(22); Expression(out wh, false, true); td = new NewtypeDecl(theVerifyThisFile ? id : new IncludeToken(id), id.val, module, new BoundVar(bvId, bvId.val, baseType), wh, attrs); } else if (StartOf(3)) { Type(out baseType); td = new NewtypeDecl(theVerifyThisFile ? id : new IncludeToken(id), id.val, module, baseType, attrs); } else SynErr(143); } void OtherTypeDecl(ModuleDefinition module, out TopLevelDecl td) { IToken id; Attributes attrs = null; var eqSupport = TypeParameter.EqualitySupportValue.Unspecified; var typeArgs = new List(); td = null; Type ty; Expect(77); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48) { Get(); Expect(52); Expect(49); eqSupport = TypeParameter.EqualitySupportValue.Required; if (la.kind == 50) { GenericParameters(typeArgs); } } else if (StartOf(4)) { if (la.kind == 50) { GenericParameters(typeArgs); } if (la.kind == 64) { Get(); Type(out ty); td = new TypeSynonymDecl(id, id.val, typeArgs, module, ty, attrs); } } else SynErr(144); if (td == null) { td = new OpaqueTypeDecl(id, id.val, module, eqSupport, typeArgs, attrs); } if (la.kind == 26) { while (!(la.kind == 0 || la.kind == 26)) {SynErr(145); Get();} Get(); errors.Warning(t, "the semi-colon that used to terminate an opaque-type declaration has been deprecated; in the new syntax, just leave off the semi-colon"); } } void IteratorDecl(ModuleDefinition module, out IteratorDecl/*!*/ iter) { Contract.Ensures(Contract.ValueAtReturn(out iter) != null); IToken/*!*/ id; Attributes attrs = null; List/*!*/ typeArgs = new List(); List ins = new List(); List outs = new List(); List reads = new List(); List mod = new List(); List decreases = new List(); List req = new List(); List ens = new List(); List yieldReq = new List(); List yieldEns = new List(); List dec = new List(); Attributes readsAttrs = null; Attributes modAttrs = null; Attributes decrAttrs = null; BlockStmt body = null; IToken signatureEllipsis = null; IToken bodyStart = Token.NoToken; IToken bodyEnd = Token.NoToken; while (!(la.kind == 0 || la.kind == 78)) {SynErr(146); Get();} Expect(78); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48 || la.kind == 50) { if (la.kind == 50) { GenericParameters(typeArgs); } Formals(true, true, ins); if (la.kind == 79 || la.kind == 80) { if (la.kind == 79) { Get(); } else { Get(); SemErr(t, "iterators don't have a 'returns' clause; did you mean 'yields'?"); } Formals(false, true, outs); } } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(147); while (StartOf(5)) { IteratorSpec(reads, mod, decreases, req, ens, yieldReq, yieldEns, ref readsAttrs, ref modAttrs, ref decrAttrs); } if (la.kind == 44) { BlockStmt(out body, out bodyStart, out bodyEnd); } iter = new IteratorDecl(id, id.val, module, typeArgs, ins, outs, new Specification(reads, readsAttrs), new Specification(mod, modAttrs), new Specification(decreases, decrAttrs), req, ens, yieldReq, yieldEns, body, attrs, signatureEllipsis); iter.BodyStartTok = bodyStart; iter.BodyEndTok = bodyEnd; } void TraitDecl(ModuleDefinition/*!*/ module, out TraitDecl/*!*/ trait) { Contract.Requires(module != null); Contract.Ensures(Contract.ValueAtReturn(out trait) != null); IToken/*!*/ id; Attributes attrs = null; List typeArgs = new List(); //traits should not support type parameters at the moment List members = new List(); IToken bodyStart; while (!(la.kind == 0 || la.kind == 69)) {SynErr(148); Get();} Expect(69); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 50) { GenericParameters(typeArgs); } Expect(44); bodyStart = t; while (StartOf(2)) { ClassMemberDecl(members, true, false); } Expect(45); trait = new TraitDecl(id, id.val, module, typeArgs, members, attrs); trait.BodyStartTok = bodyStart; trait.BodyEndTok = t; } void ClassMemberDecl(List mm, bool allowConstructors, bool moduleLevelDecl) { Contract.Requires(cce.NonNullElements(mm)); Method/*!*/ m; Function/*!*/ f; MemberModifiers mmod = new MemberModifiers(); IToken staticToken = null, protectedToken = null; while (la.kind == 70 || la.kind == 71 || la.kind == 72) { if (la.kind == 70) { Get(); mmod.IsGhost = true; } else if (la.kind == 71) { Get(); mmod.IsStatic = true; staticToken = t; } else { Get(); mmod.IsProtected = true; protectedToken = t; } } if (la.kind == 75) { if (moduleLevelDecl) { SemErr(la, "fields are not allowed to be declared at the module level; instead, wrap the field in a 'class' declaration"); mmod.IsStatic = false; mmod.IsProtected = false; } FieldDecl(mmod, mm); } else if (IsFunctionDecl()) { if (moduleLevelDecl && staticToken != null) { errors.Warning(staticToken, "module-level functions are always non-instance, so the 'static' keyword is not allowed here"); mmod.IsStatic = false; } FunctionDecl(mmod, out f); mm.Add(f); } else if (StartOf(6)) { if (moduleLevelDecl && staticToken != null) { errors.Warning(staticToken, "module-level methods are always non-instance, so the 'static' keyword is not allowed here"); mmod.IsStatic = false; } if (protectedToken != null) { SemErr(protectedToken, "only functions, not methods, can be declared 'protected'"); mmod.IsProtected = false; } MethodDecl(mmod, allowConstructors, out m); mm.Add(m); } else SynErr(149); } void Attribute(ref Attributes attrs) { string name; var args = new List(); Expect(44); Expect(20); Expect(1); name = t.val; if (StartOf(7)) { Expressions(args); } Expect(45); attrs = new Attributes(name, args, attrs); } void NoUSIdent(out IToken/*!*/ x) { Contract.Ensures(Contract.ValueAtReturn(out x) != null); Expect(1); x = t; if (x.val.StartsWith("_")) { SemErr("cannot declare identifier beginning with underscore"); } } void QualifiedModuleName(out List ids) { IToken id; ids = new List(); Ident(out id); ids.Add(id); while (la.kind == 25) { Get(); Ident(out id); ids.Add(id); } } void Ident(out IToken/*!*/ x) { Contract.Ensures(Contract.ValueAtReturn(out x) != null); Expect(1); x = t; } void GenericParameters(List/*!*/ typeArgs) { Contract.Requires(cce.NonNullElements(typeArgs)); IToken/*!*/ id; TypeParameter.EqualitySupportValue eqSupport; Expect(50); NoUSIdent(out id); eqSupport = TypeParameter.EqualitySupportValue.Unspecified; if (la.kind == 48) { Get(); Expect(52); Expect(49); eqSupport = TypeParameter.EqualitySupportValue.Required; } typeArgs.Add(new TypeParameter(id, id.val, eqSupport)); while (la.kind == 21) { Get(); NoUSIdent(out id); eqSupport = TypeParameter.EqualitySupportValue.Unspecified; if (la.kind == 48) { Get(); Expect(52); Expect(49); eqSupport = TypeParameter.EqualitySupportValue.Required; } typeArgs.Add(new TypeParameter(id, id.val, eqSupport)); } Expect(51); } void Type(out Type ty) { Contract.Ensures(Contract.ValueAtReturn(out ty) != null); IToken/*!*/ tok; TypeAndToken(out tok, out ty); } void FieldDecl(MemberModifiers mmod, List/*!*/ mm) { Contract.Requires(cce.NonNullElements(mm)); Attributes attrs = null; IToken/*!*/ id; Type/*!*/ ty; while (!(la.kind == 0 || la.kind == 75)) {SynErr(150); Get();} Expect(75); if (mmod.IsStatic) { SemErr(t, "fields cannot be declared 'static'"); } while (la.kind == 44) { Attribute(ref attrs); } FIdentType(out id, out ty); mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs)); while (la.kind == 21) { Get(); FIdentType(out id, out ty); mm.Add(new Field(id, id.val, mmod.IsGhost, ty, attrs)); } OldSemi(); } void FunctionDecl(MemberModifiers mmod, out Function/*!*/ f) { Contract.Ensures(Contract.ValueAtReturn(out f)!=null); Attributes attrs = null; IToken/*!*/ id = Token.NoToken; // to please compiler List typeArgs = new List(); List formals = new List(); Type/*!*/ returnType = new BoolType(); List reqs = new List(); List ens = new List(); List reads = new List(); List decreases; Expression body = null; bool isPredicate = false; bool isIndPredicate = false; bool isCoPredicate = false; bool isFunctionMethod = false; IToken bodyStart = Token.NoToken; IToken bodyEnd = Token.NoToken; IToken signatureEllipsis = null; bool missingOpenParen; if (la.kind == 36) { Get(); if (la.kind == 81) { Get(); isFunctionMethod = true; } if (mmod.IsGhost) { SemErr(t, "functions cannot be declared 'ghost' (they are ghost by default)"); } while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48 || la.kind == 50) { if (la.kind == 50) { GenericParameters(typeArgs); } Formals(true, isFunctionMethod, formals); Expect(20); Type(out returnType); } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(151); } else if (la.kind == 37) { Get(); isPredicate = true; if (la.kind == 81) { Get(); isFunctionMethod = true; } if (mmod.IsGhost) { SemErr(t, "predicates cannot be declared 'ghost' (they are ghost by default)"); } while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (StartOf(8)) { if (la.kind == 50) { GenericParameters(typeArgs); } missingOpenParen = true; if (la.kind == 48) { Formals(true, isFunctionMethod, formals); missingOpenParen = false; } if (missingOpenParen) { errors.Warning(t, "with the new support of higher-order functions in Dafny, parentheses-less predicates are no longer supported; in the new syntax, parentheses are required for the declaration and uses of predicates, even if the predicate takes no additional arguments"); } if (la.kind == 20) { Get(); SemErr(t, "predicates do not have an explicitly declared return type; it is always bool"); } } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(152); } else if (la.kind == 38) { Get(); Expect(37); isIndPredicate = true; if (mmod.IsGhost) { SemErr(t, "inductive predicates cannot be declared 'ghost' (they are ghost by default)"); } while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48 || la.kind == 50) { if (la.kind == 50) { GenericParameters(typeArgs); } Formals(true, isFunctionMethod, formals); if (la.kind == 20) { Get(); SemErr(t, "inductive predicates do not have an explicitly declared return type; it is always bool"); } } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(153); } else if (la.kind == 40) { Get(); isCoPredicate = true; if (mmod.IsGhost) { SemErr(t, "copredicates cannot be declared 'ghost' (they are ghost by default)"); } while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48 || la.kind == 50) { if (la.kind == 50) { GenericParameters(typeArgs); } Formals(true, isFunctionMethod, formals); if (la.kind == 20) { Get(); SemErr(t, "copredicates do not have an explicitly declared return type; it is always bool"); } } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(154); } else SynErr(155); decreases = isIndPredicate || isCoPredicate ? null : new List(); while (StartOf(9)) { FunctionSpec(reqs, reads, ens, decreases); } if (la.kind == 44) { FunctionBody(out body, out bodyStart, out bodyEnd); } if (DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 && !Attributes.Contains(attrs, "axiom") && !Attributes.Contains(attrs, "imported")) { SemErr(t, "a function with an ensures clause must have a body, unless given the :axiom attribute"); } IToken tok = theVerifyThisFile ? id : new IncludeToken(id); if (isPredicate) { f = new Predicate(tok, id.val, mmod.IsStatic, mmod.IsProtected, !isFunctionMethod, typeArgs, formals, reqs, reads, ens, new Specification(decreases, null), body, Predicate.BodyOriginKind.OriginalOrInherited, attrs, signatureEllipsis); } else if (isIndPredicate) { f = new InductivePredicate(tok, id.val, mmod.IsStatic, mmod.IsProtected, typeArgs, formals, reqs, reads, ens, body, attrs, signatureEllipsis); } else if (isCoPredicate) { f = new CoPredicate(tok, id.val, mmod.IsStatic, mmod.IsProtected, typeArgs, formals, reqs, reads, ens, body, attrs, signatureEllipsis); } else { f = new Function(tok, id.val, mmod.IsStatic, mmod.IsProtected, !isFunctionMethod, typeArgs, formals, returnType, reqs, reads, ens, new Specification(decreases, null), body, attrs, signatureEllipsis); } f.BodyStartTok = bodyStart; f.BodyEndTok = bodyEnd; theBuiltIns.CreateArrowTypeDecl(formals.Count); if (isIndPredicate || isCoPredicate) { // also create an arrow type for the corresponding prefix predicate theBuiltIns.CreateArrowTypeDecl(formals.Count + 1); } } void MethodDecl(MemberModifiers mmod, bool allowConstructor, out Method/*!*/ m) { Contract.Ensures(Contract.ValueAtReturn(out m) !=null); IToken/*!*/ id = Token.NoToken; bool hasName = false; IToken keywordToken; Attributes attrs = null; List/*!*/ typeArgs = new List(); List ins = new List(); List outs = new List(); List req = new List(); List mod = new List(); List ens = new List(); List dec = new List(); Attributes decAttrs = null; Attributes modAttrs = null; BlockStmt body = null; bool isLemma = false; bool isConstructor = false; bool isIndLemma = false; bool isCoLemma = false; IToken signatureEllipsis = null; IToken bodyStart = Token.NoToken; IToken bodyEnd = Token.NoToken; while (!(StartOf(10))) {SynErr(156); Get();} switch (la.kind) { case 81: { Get(); break; } case 39: { Get(); isLemma = true; break; } case 82: { Get(); isCoLemma = true; break; } case 83: { Get(); isCoLemma = true; errors.Warning(t, "the 'comethod' keyword has been deprecated; it has been renamed to 'colemma'"); break; } case 38: { Get(); Expect(39); isIndLemma = true; break; } case 84: { Get(); if (allowConstructor) { isConstructor = true; } else { SemErr(t, "constructors are allowed only in classes"); } break; } default: SynErr(157); break; } keywordToken = t; if (isLemma) { if (mmod.IsGhost) { SemErr(t, "lemmas cannot be declared 'ghost' (they are automatically 'ghost')"); } } else if (isConstructor) { if (mmod.IsGhost) { SemErr(t, "constructors cannot be declared 'ghost'"); } if (mmod.IsStatic) { SemErr(t, "constructors cannot be declared 'static'"); } } else if (isIndLemma) { if (mmod.IsGhost) { SemErr(t, "inductive lemmas cannot be declared 'ghost' (they are automatically 'ghost')"); } } else if (isCoLemma) { if (mmod.IsGhost) { SemErr(t, "colemmas cannot be declared 'ghost' (they are automatically 'ghost')"); } } while (la.kind == 44) { Attribute(ref attrs); } if (la.kind == 1) { NoUSIdent(out id); hasName = true; } if (!hasName) { id = keywordToken; if (!isConstructor) { SemErr(la, "a method must be given a name (expecting identifier)"); } } if (la.kind == 48 || la.kind == 50) { if (la.kind == 50) { GenericParameters(typeArgs); } Formals(true, !mmod.IsGhost, ins); if (la.kind == 80) { Get(); if (isConstructor) { SemErr(t, "constructors cannot have out-parameters"); } Formals(false, !mmod.IsGhost, outs); } } else if (la.kind == 57) { Get(); signatureEllipsis = t; } else SynErr(158); while (StartOf(11)) { MethodSpec(req, mod, ens, dec, ref decAttrs, ref modAttrs); } if (la.kind == 44) { BlockStmt(out body, out bodyStart, out bodyEnd); } if (DafnyOptions.O.DisallowSoundnessCheating && body == null && ens.Count > 0 && !Attributes.Contains(attrs, "axiom") && !Attributes.Contains(attrs, "imported") && !Attributes.Contains(attrs, "decl") && theVerifyThisFile) { SemErr(t, "a method with an ensures clause must have a body, unless given the :axiom attribute"); } IToken tok = theVerifyThisFile ? id : new IncludeToken(id); if (isConstructor) { m = new Constructor(tok, hasName ? id.val : "_ctor", typeArgs, ins, req, new Specification(mod, modAttrs), ens, new Specification(dec, decAttrs), body, attrs, signatureEllipsis); } else if (isIndLemma) { m = new InductiveLemma(tok, id.val, mmod.IsStatic, typeArgs, ins, outs, req, new Specification(mod, modAttrs), ens, new Specification(dec, decAttrs), body, attrs, signatureEllipsis); } else if (isCoLemma) { m = new CoLemma(tok, id.val, mmod.IsStatic, typeArgs, ins, outs, req, new Specification(mod, modAttrs), ens, new Specification(dec, decAttrs), body, attrs, signatureEllipsis); } else if (isLemma) { m = new Lemma(tok, id.val, mmod.IsStatic, typeArgs, ins, outs, req, new Specification(mod, modAttrs), ens, new Specification(dec, decAttrs), body, attrs, signatureEllipsis); } else { m = new Method(tok, id.val, mmod.IsStatic, mmod.IsGhost, typeArgs, ins, outs, req, new Specification(mod, modAttrs), ens, new Specification(dec, decAttrs), body, attrs, signatureEllipsis); } m.BodyStartTok = bodyStart; m.BodyEndTok = bodyEnd; } void DatatypeMemberDecl(List/*!*/ ctors) { Contract.Requires(cce.NonNullElements(ctors)); Attributes attrs = null; IToken/*!*/ id; List formals = new List(); while (la.kind == 44) { Attribute(ref attrs); } NoUSIdent(out id); if (la.kind == 48) { FormalsOptionalIds(formals); } ctors.Add(new DatatypeCtor(id, id.val, formals, attrs)); } void FormalsOptionalIds(List/*!*/ formals) { Contract.Requires(cce.NonNullElements(formals)); IToken/*!*/ id; Type/*!*/ ty; string/*!*/ name; bool isGhost; Expect(48); if (StartOf(12)) { TypeIdentOptional(out id, out name, out ty, out isGhost); formals.Add(new Formal(id, name, ty, true, isGhost)); while (la.kind == 21) { Get(); TypeIdentOptional(out id, out name, out ty, out isGhost); formals.Add(new Formal(id, name, ty, true, isGhost)); } } Expect(49); } void FIdentType(out IToken/*!*/ id, out Type/*!*/ ty) { Contract.Ensures(Contract.ValueAtReturn(out id) != null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null); id = Token.NoToken; if (la.kind == 1) { WildIdent(out id, false); } else if (la.kind == 2) { Get(); id = t; } else SynErr(159); Expect(20); Type(out ty); } void OldSemi() { if (la.kind == 26) { while (!(la.kind == 0 || la.kind == 26)) {SynErr(160); Get();} Get(); } } void Expression(out Expression e, bool allowSemi, bool allowLambda) { Expression e0; IToken endTok; EquivExpression(out e, allowSemi, allowLambda); if (SemiFollowsCall(allowSemi, e)) { Expect(26); endTok = t; Expression(out e0, allowSemi, allowLambda); e = new StmtExpr(e.tok, new UpdateStmt(e.tok, endTok, new List(), new List() { new ExprRhs(e, null) }), e0); } } void GIdentType(bool allowGhostKeyword, out IToken/*!*/ id, out Type/*!*/ ty, out bool isGhost) { Contract.Ensures(Contract.ValueAtReturn(out id)!=null); Contract.Ensures(Contract.ValueAtReturn(out ty)!=null); isGhost = false; if (la.kind == 70) { Get(); if (allowGhostKeyword) { isGhost = true; } else { SemErr(t, "formal cannot be declared 'ghost' in this context"); } } IdentType(out id, out ty, true); } void IdentType(out IToken/*!*/ id, out Type/*!*/ ty, bool allowWildcardId) { Contract.Ensures(Contract.ValueAtReturn(out id) != null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null); WildIdent(out id, allowWildcardId); Expect(20); Type(out ty); } void WildIdent(out IToken x, bool allowWildcardId) { Contract.Ensures(Contract.ValueAtReturn(out x) != null); Expect(1); x = t; t.val = UnwildIdent(t.val, allowWildcardId); } void LocalIdentTypeOptional(out LocalVariable var, bool isGhost) { IToken id; Type ty; Type optType = null; WildIdent(out id, true); if (la.kind == 20) { Get(); Type(out ty); optType = ty; } var = new LocalVariable(id, id, id.val, optType == null ? new InferredTypeProxy() : optType, isGhost); } void IdentTypeOptional(out BoundVar var) { Contract.Ensures(Contract.ValueAtReturn(out var) != null); IToken id; Type ty; Type optType = null; WildIdent(out id, true); if (la.kind == 20) { Get(); Type(out ty); optType = ty; } var = new BoundVar(id, id.val, optType == null ? new InferredTypeProxy() : optType); } void TypeIdentOptional(out IToken/*!*/ id, out string/*!*/ identName, out Type/*!*/ ty, out bool isGhost) { Contract.Ensures(Contract.ValueAtReturn(out id)!=null); Contract.Ensures(Contract.ValueAtReturn(out ty)!=null); Contract.Ensures(Contract.ValueAtReturn(out identName)!=null); string name = null; id = Token.NoToken; ty = new BoolType()/*dummy*/; isGhost = false; if (la.kind == 70) { Get(); isGhost = true; } if (StartOf(3)) { TypeAndToken(out id, out ty); if (la.kind == 20) { Get(); UserDefinedType udt = ty as UserDefinedType; if (udt != null && udt.TypeArgs.Count == 0) { name = udt.Name; } else { SemErr(id, "invalid formal-parameter name in datatype constructor"); } Type(out ty); } } else if (la.kind == 2) { Get(); id = t; name = id.val; Expect(20); Type(out ty); } else SynErr(161); if (name != null) { identName = name; } else { identName = "#" + anonymousIds++; } } void TypeAndToken(out IToken tok, out Type ty) { Contract.Ensures(Contract.ValueAtReturn(out tok)!=null); Contract.Ensures(Contract.ValueAtReturn(out ty) != null); tok = Token.NoToken; ty = new BoolType(); /*keep compiler happy*/ List gt; List tupleArgTypes = null; switch (la.kind) { case 6: { Get(); tok = t; break; } case 7: { Get(); tok = t; ty = new CharType(); break; } case 9: { Get(); tok = t; ty = new NatType(); break; } case 8: { Get(); tok = t; ty = new IntType(); break; } case 10: { Get(); tok = t; ty = new RealType(); break; } case 11: { Get(); tok = t; ty = new ObjectType(); break; } case 13: { Get(); tok = t; gt = new List(); if (la.kind == 50) { GenericInstantiation(gt); } if (gt.Count > 1) { SemErr("set type expects only one type argument"); } ty = new SetType(gt.Count == 1 ? gt[0] : null); break; } case 14: { Get(); tok = t; gt = new List(); if (la.kind == 50) { GenericInstantiation(gt); } if (gt.Count > 1) { SemErr("multiset type expects only one type argument"); } ty = new MultiSetType(gt.Count == 1 ? gt[0] : null); break; } case 15: { Get(); tok = t; gt = new List(); if (la.kind == 50) { GenericInstantiation(gt); } if (gt.Count > 1) { SemErr("seq type expects only one type argument"); } ty = new SeqType(gt.Count == 1 ? gt[0] : null); break; } case 12: { Get(); tok = t; ty = new UserDefinedType(tok, tok.val, null); break; } case 16: { Get(); tok = t; gt = new List(); if (la.kind == 50) { GenericInstantiation(gt); } if (gt.Count == 0) { ty = new MapType(true, null, null); } else if (gt.Count != 2) { SemErr("map type expects two type arguments"); ty = new MapType(true, gt[0], gt.Count == 1 ? new InferredTypeProxy() : gt[1]); } else { ty = new MapType(true, gt[0], gt[1]); } break; } case 17: { Get(); tok = t; gt = new List(); if (la.kind == 50) { GenericInstantiation(gt); } if (gt.Count == 0) { ty = new MapType(false, null, null); } else if (gt.Count != 2) { SemErr("imap type expects two type arguments"); ty = new MapType(false, gt[0], gt.Count == 1 ? new InferredTypeProxy() : gt[1]); } else { ty = new MapType(false, gt[0], gt[1]); } break; } case 5: { Get(); tok = t; gt = null; if (la.kind == 50) { gt = new List(); GenericInstantiation(gt); } int dims = tok.val.Length == 5 ? 1 : int.Parse(tok.val.Substring(5)); ty = theBuiltIns.ArrayType(tok, dims, gt, true); break; } case 48: { Get(); tok = t; tupleArgTypes = new List(); if (StartOf(3)) { Type(out ty); tupleArgTypes.Add(ty); while (la.kind == 21) { Get(); Type(out ty); tupleArgTypes.Add(ty); } } Expect(49); if (tupleArgTypes.Count == 1) { // just return the type 'ty' } else { var dims = tupleArgTypes.Count; var tmp = theBuiltIns.TupleType(tok, dims, true); // make sure the tuple type exists ty = new UserDefinedType(tok, BuiltIns.TupleTypeName(dims), dims == 0 ? null : tupleArgTypes); } break; } case 1: { Expression e; tok = t; NameSegmentForTypeName(out e); tok = t; while (la.kind == 25) { Get(); Expect(1); tok = t; List typeArgs = null; if (la.kind == 50) { typeArgs = new List(); GenericInstantiation(typeArgs); } e = new ExprDotName(tok, e, tok.val, typeArgs); } ty = new UserDefinedType(e.tok, e); break; } default: SynErr(162); break; } if (la.kind == 28) { Type t2; Get(); tok = t; Type(out t2); if (tupleArgTypes != null) { gt = tupleArgTypes; } else { gt = new List{ ty }; } ty = new ArrowType(tok, gt, t2); theBuiltIns.CreateArrowTypeDecl(gt.Count); } } void Formals(bool incoming, bool allowGhostKeyword, List formals) { Contract.Requires(cce.NonNullElements(formals)); IToken id; Type ty; bool isGhost; Expect(48); if (la.kind == 1 || la.kind == 70) { GIdentType(allowGhostKeyword, out id, out ty, out isGhost); formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); while (la.kind == 21) { Get(); GIdentType(allowGhostKeyword, out id, out ty, out isGhost); formals.Add(new Formal(id, id.val, ty, incoming, isGhost)); } } Expect(49); } void IteratorSpec(List/*!*/ reads, List/*!*/ mod, List decreases, List/*!*/ req, List/*!*/ ens, List/*!*/ yieldReq, List/*!*/ yieldEns, ref Attributes readsAttrs, ref Attributes modAttrs, ref Attributes decrAttrs) { Expression/*!*/ e; FrameExpression/*!*/ fe; bool isFree = false; bool isYield = false; Attributes ensAttrs = null; while (!(StartOf(13))) {SynErr(163); Get();} if (la.kind == 42) { Get(); while (IsAttribute()) { Attribute(ref readsAttrs); } FrameExpression(out fe, false, false); reads.Add(fe); while (la.kind == 21) { Get(); FrameExpression(out fe, false, false); reads.Add(fe); } OldSemi(); } else if (la.kind == 41) { Get(); while (IsAttribute()) { Attribute(ref modAttrs); } FrameExpression(out fe, false, false); mod.Add(fe); while (la.kind == 21) { Get(); FrameExpression(out fe, false, false); mod.Add(fe); } OldSemi(); } else if (StartOf(14)) { if (la.kind == 85) { Get(); isFree = true; errors.Warning(t, "the 'free' keyword is soon to be deprecated"); } if (la.kind == 87) { Get(); isYield = true; } if (la.kind == 43) { Get(); Expression(out e, false, false); OldSemi(); if (isYield) { yieldReq.Add(new MaybeFreeExpression(e, isFree)); } else { req.Add(new MaybeFreeExpression(e, isFree)); } } else if (la.kind == 86) { Get(); while (IsAttribute()) { Attribute(ref ensAttrs); } Expression(out e, false, false); OldSemi(); if (isYield) { yieldEns.Add(new MaybeFreeExpression(e, isFree, ensAttrs)); } else { ens.Add(new MaybeFreeExpression(e, isFree, ensAttrs)); } } else SynErr(164); } else if (la.kind == 34) { Get(); while (IsAttribute()) { Attribute(ref decrAttrs); } DecreasesList(decreases, false, false); OldSemi(); } else SynErr(165); } void BlockStmt(out BlockStmt/*!*/ block, out IToken bodyStart, out IToken bodyEnd) { Contract.Ensures(Contract.ValueAtReturn(out block) != null); List body = new List(); Expect(44); bodyStart = t; while (StartOf(15)) { Stmt(body); } Expect(45); bodyEnd = t; block = new BlockStmt(bodyStart, bodyEnd, body); } void MethodSpec(List/*!*/ req, List/*!*/ mod, List/*!*/ ens, List/*!*/ decreases, ref Attributes decAttrs, ref Attributes modAttrs) { Contract.Requires(cce.NonNullElements(req)); Contract.Requires(cce.NonNullElements(mod)); Contract.Requires(cce.NonNullElements(ens)); Contract.Requires(cce.NonNullElements(decreases)); Expression/*!*/ e; FrameExpression/*!*/ fe; bool isFree = false; Attributes ensAttrs = null; while (!(StartOf(16))) {SynErr(166); Get();} if (la.kind == 41) { Get(); while (IsAttribute()) { Attribute(ref modAttrs); } FrameExpression(out fe, false, false); mod.Add(fe); while (la.kind == 21) { Get(); FrameExpression(out fe, false, false); mod.Add(fe); } OldSemi(); } else if (la.kind == 43 || la.kind == 85 || la.kind == 86) { if (la.kind == 85) { Get(); isFree = true; errors.Warning(t, "the 'free' keyword is soon to be deprecated"); } if (la.kind == 43) { Get(); Expression(out e, false, false); OldSemi(); req.Add(new MaybeFreeExpression(e, isFree)); } else if (la.kind == 86) { Get(); while (IsAttribute()) { Attribute(ref ensAttrs); } Expression(out e, false, false); OldSemi(); ens.Add(new MaybeFreeExpression(e, isFree, ensAttrs)); } else SynErr(167); } else if (la.kind == 34) { Get(); while (IsAttribute()) { Attribute(ref decAttrs); } DecreasesList(decreases, true, false); OldSemi(); } else SynErr(168); } void FrameExpression(out FrameExpression fe, bool allowSemi, bool allowLambda) { Contract.Ensures(Contract.ValueAtReturn(out fe) != null); Expression/*!*/ e; IToken/*!*/ id; string fieldName = null; IToken feTok = null; fe = null; if (StartOf(7)) { Expression(out e, allowSemi, allowLambda); feTok = e.tok; if (la.kind == 88) { Get(); Ident(out id); fieldName = id.val; feTok = id; } fe = new FrameExpression(feTok, e, fieldName); } else if (la.kind == 88) { Get(); Ident(out id); fieldName = id.val; fe = new FrameExpression(id, new ImplicitThisExpr(id), fieldName); } else SynErr(169); } void DecreasesList(List decreases, bool allowWildcard, bool allowLambda) { Expression e; PossiblyWildExpression(out e, allowLambda); if (!allowWildcard && e is WildcardExpr) { SemErr(e.tok, "'decreases *' is allowed only on loops and tail-recursive methods"); } else { decreases.Add(e); } while (la.kind == 21) { Get(); PossiblyWildExpression(out e, allowLambda); if (!allowWildcard && e is WildcardExpr) { SemErr(e.tok, "'decreases *' is allowed only on loops and tail-recursive methods"); } else { decreases.Add(e); } } } void GenericInstantiation(List/*!*/ gt) { Contract.Requires(cce.NonNullElements(gt)); Type/*!*/ ty; Expect(50); Type(out ty); gt.Add(ty); while (la.kind == 21) { Get(); Type(out ty); gt.Add(ty); } Expect(51); } void NameSegmentForTypeName(out Expression e) { IToken id; List typeArgs = null; Ident(out id); if (la.kind == 50) { typeArgs = new List(); GenericInstantiation(typeArgs); } e = new NameSegment(id, id.val, typeArgs); } void FunctionSpec(List/*!*/ reqs, List/*!*/ reads, List/*!*/ ens, List decreases) { Contract.Requires(cce.NonNullElements(reqs)); Contract.Requires(cce.NonNullElements(reads)); Contract.Requires(decreases == null || cce.NonNullElements(decreases)); Expression/*!*/ e; FrameExpression/*!*/ fe; while (!(StartOf(17))) {SynErr(170); Get();} if (la.kind == 43) { Get(); Expression(out e, false, false); OldSemi(); reqs.Add(e); } else if (la.kind == 42) { Get(); PossiblyWildFrameExpression(out fe, false); reads.Add(fe); while (la.kind == 21) { Get(); PossiblyWildFrameExpression(out fe, false); reads.Add(fe); } OldSemi(); } else if (la.kind == 86) { Get(); Expression(out e, false, false); OldSemi(); ens.Add(e); } else if (la.kind == 34) { Get(); if (decreases == null) { SemErr(t, "'decreases' clauses are meaningless for copredicates, so they are not allowed"); decreases = new List(); } DecreasesList(decreases, false, false); OldSemi(); } else SynErr(171); } void FunctionBody(out Expression/*!*/ e, out IToken bodyStart, out IToken bodyEnd) { Contract.Ensures(Contract.ValueAtReturn(out e) != null); e = dummyExpr; Expect(44); bodyStart = t; Expression(out e, true, true); Expect(45); bodyEnd = t; } void PossiblyWildFrameExpression(out FrameExpression fe, bool allowSemi) { Contract.Ensures(Contract.ValueAtReturn(out fe) != null); fe = dummyFrameExpr; if (la.kind == 55) { Get(); fe = new FrameExpression(t, new WildcardExpr(t), null); } else if (StartOf(18)) { FrameExpression(out fe, allowSemi, false); } else SynErr(172); } void PossiblyWildExpression(out Expression e, bool allowLambda) { Contract.Ensures(Contract.ValueAtReturn(out e)!=null); e = dummyExpr; if (la.kind == 55) { Get(); e = new WildcardExpr(t); } else if (StartOf(7)) { Expression(out e, false, allowLambda); } else SynErr(173); } void Stmt(List/*!*/ ss) { Statement/*!*/ s; OneStmt(out s); ss.Add(s); } void OneStmt(out Statement/*!*/ s) { Contract.Ensures(Contract.ValueAtReturn(out s) != null); IToken/*!*/ x; IToken/*!*/ id; string label = null; s = dummyStmt; /* to please the compiler */ BlockStmt bs; IToken bodyStart, bodyEnd; int breakCount; while (!(StartOf(19))) {SynErr(174); Get();} switch (la.kind) { case 44: { BlockStmt(out bs, out bodyStart, out bodyEnd); s = bs; break; } case 99: { AssertStmt(out s); break; } case 29: { AssumeStmt(out s); break; } case 100: { PrintStmt(out s); break; } case 1: case 2: case 3: case 4: case 8: case 10: case 18: case 19: case 22: case 48: case 129: case 130: case 131: case 132: case 133: case 134: { UpdateStmt(out s); break; } case 70: case 75: { VarDeclStatement(out s); break; } case 96: { IfStmt(out s); break; } case 97: { WhileStmt(out s); break; } case 98: { MatchStmt(out s); break; } case 101: case 102: { ForallStmt(out s); break; } case 30: { CalcStmt(out s); break; } case 103: { ModifyStmt(out s); break; } case 89: { Get(); x = t; NoUSIdent(out id); Expect(20); OneStmt(out s); s.Labels = new LList