Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make {: one token instead of two for annotations #360

Merged
merged 3 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Dafny/Cloner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public Attributes CloneAttributes(Attributes attrs) {
return CloneAttributes(attrs.Prev);
} else if (attrs is UserSuppliedAttributes) {
var usa = (UserSuppliedAttributes)attrs;
return new UserSuppliedAttributes(Tok(usa.tok), Tok(usa.OpenBrace), Tok(usa.Colon), Tok(usa.CloseBrace), attrs.Args.ConvertAll(CloneExpr), CloneAttributes(attrs.Prev));
return new UserSuppliedAttributes(Tok(usa.tok), Tok(usa.OpenBrace), Tok(usa.CloseBrace), attrs.Args.ConvertAll(CloneExpr), CloneAttributes(attrs.Prev));
} else {
return new Attributes(attrs.Name, attrs.Args.ConvertAll(CloneExpr), CloneAttributes(attrs.Prev));
}
Expand Down
56 changes: 24 additions & 32 deletions Source/Dafny/Dafny.atg
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ public Parser(Scanner/*!*/ scanner, Errors/*!*/ errors, Include include, ModuleD
theVerifyThisFile = verifyThisFile;
}

bool IsAttribute() {
IToken x = scanner.Peek();
return la.kind == _lbrace && x.kind == _colon;
}

bool IsLabel(bool allowLabel) {
if (!allowLabel) {
return false;
Expand Down Expand Up @@ -220,18 +215,15 @@ bool IsGets() {

// an existential guard starts with an identifier and is then followed by
// * a colon (if the first identifier is given an explicit type),
// * a comma (if there's a list a bound variables and the first one is not given an explicit type),
// * a comma (if there's a list of bound variables and the first one is not given an explicit type),
// * a start-attribute (if there's one bound variable and it is not given an explicit type and there are attributes), or
// * a bored smiley (if there's one bound variable and it is not given an explicit type).
bool IsExistentialGuard() {
scanner.ResetPeek();
if (la.kind == _ident) {
Token x = scanner.Peek();
if (x.kind == _colon || x.kind == _comma || x.kind == _boredSmiley) {
if (x.kind == _colon || x.kind == _comma || x.kind == _boredSmiley || x.kind == _lbracecolon) {
return true;
} else if (x.kind == _lbrace) {
x = scanner.Peek();
return x.kind == _colon;
}
}
return false;
Expand Down Expand Up @@ -773,6 +765,7 @@ TOKENS
ensures = "ensures".
ghost = "ghost".
witness = "witness".
lbracecolon = "{:".
lbrace = '{'.
rbrace = '}'.
lbracket = '['.
Expand Down Expand Up @@ -1593,7 +1586,7 @@ MethodSpec<.List<MaybeFreeExpression> req, List<FrameExpression> mod, List<Maybe
IToken lbl = null;
.)
SYNC
( "modifies" { IF(IsAttribute()) Attribute<ref modAttrs> }
( "modifies" { Attribute<ref modAttrs> }
FrameExpression<out fe, false, false> (. Util.AddFrameExpression(mod, fe, performThisDeprecatedCheck, errors); .)
{ "," FrameExpression<out fe, false, false> (. Util.AddFrameExpression(mod, fe, performThisDeprecatedCheck, errors); .)
}
Expand All @@ -1603,16 +1596,16 @@ MethodSpec<.List<MaybeFreeExpression> req, List<FrameExpression> mod, List<Maybe
.)
]
( "requires"
{ IF(IsAttribute()) Attribute<ref reqAttrs> }
{ Attribute<ref reqAttrs> }
[ IF(IsLabel(true))
LabelIdent<out lbl> ":"
]
Expression<out e, false, false> OldSemi (. req.Add(new MaybeFreeExpression(e, isFree, lbl == null ? null : new AssertLabel(lbl, lbl.val), reqAttrs)); .)
| "ensures"
{ IF(IsAttribute()) Attribute<ref ensAttrs> }
{ Attribute<ref ensAttrs> }
Expression<out e, false, false> OldSemi (. ens.Add(new MaybeFreeExpression(e, isFree, ensAttrs)); .)
)
| "decreases" { IF(IsAttribute()) Attribute<ref decAttrs> } DecreasesList<decreases, true, false> OldSemi
| "decreases" { Attribute<ref decAttrs> } DecreasesList<decreases, true, false> OldSemi
)
.
IteratorSpec<.List<FrameExpression/*!*/>/*!*/ reads, List<FrameExpression/*!*/>/*!*/ mod, List<Expression/*!*/> decreases,
Expand All @@ -1623,12 +1616,12 @@ IteratorSpec<.List<FrameExpression/*!*/>/*!*/ reads, List<FrameExpression/*!*/>/
IToken lbl = null;
.)
SYNC
( "reads" { IF(IsAttribute()) Attribute<ref readsAttrs> }
( "reads" { Attribute<ref readsAttrs> }
FrameExpression<out fe, false, false> (. reads.Add(fe); .)
{ "," FrameExpression<out fe, false, false> (. reads.Add(fe); .)
}
OldSemi
| "modifies" { IF(IsAttribute()) Attribute<ref modAttrs> }
| "modifies" { Attribute<ref modAttrs> }
FrameExpression<out fe, false, false> (. mod.Add(fe); .)
{ "," FrameExpression<out fe, false, false> (. mod.Add(fe); .)
}
Expand All @@ -1650,15 +1643,15 @@ IteratorSpec<.List<FrameExpression/*!*/>/*!*/ reads, List<FrameExpression/*!*/>/
req.Add(new MaybeFreeExpression(e, isFree, al, null));
}
.)
| "ensures" { IF(IsAttribute()) Attribute<ref ensAttrs> }
| "ensures" { 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));
}
.)
)
| "decreases" { IF(IsAttribute()) Attribute<ref decrAttrs> } DecreasesList<decreases, false, false> OldSemi
| "decreases" { Attribute<ref decrAttrs> } DecreasesList<decreases, false, false> OldSemi
)
.
Formals<.bool incoming, bool allowGhostKeyword, bool allowNewKeyword, List<Formal> formals.>
Expand Down Expand Up @@ -2009,15 +2002,15 @@ FunctionSpec<.List<MaybeFreeExpression/*!*/>/*!*/ reqs, List<FrameExpression/*!*
Attributes ensAttrs = null; Attributes reqAttrs = null; .)
SYNC
( "requires"
{ IF(IsAttribute()) Attribute<ref reqAttrs> }
{ Attribute<ref reqAttrs> }
Expression<out e, false, false> OldSemi (. reqs.Add(new MaybeFreeExpression(e, false, reqAttrs)); .)
| "reads"
PossiblyWildFrameExpression<out fe, false> (. reads.Add(fe); .)
{ "," PossiblyWildFrameExpression<out fe, false> (. reads.Add(fe); .)
}
OldSemi
| "ensures"
{ IF(IsAttribute()) Attribute<ref ensAttrs> }
{ Attribute<ref ensAttrs> }
Expression<out e, false, false> OldSemi (. ens.Add(new MaybeFreeExpression(e, false, ensAttrs)); .)
| "decreases" (. if (decreases == null) {
SemErr(t, "'decreases' clauses are meaningless for copredicates, so they are not allowed");
Expand Down Expand Up @@ -2503,15 +2496,15 @@ LoopSpec<.List<MaybeFreeExpression> invariants, List<Expression> decreases, ref
[ "free" (. isFree = true; errors.Deprecated(t, "the 'free' keyword is soon to be deprecated"); .)
]
"invariant"
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
Expression<out e, false, true> (. invariants.Add(new MaybeFreeExpression(e, isFree, attrs)); .)
OldSemi
| SYNC "decreases"
{ IF(IsAttribute()) Attribute<ref decAttrs> }
{ Attribute<ref decAttrs> }
DecreasesList<decreases, true, true>
OldSemi
| SYNC "modifies" (. mod = mod ?? new List<FrameExpression>(); .)
{ IF(IsAttribute()) Attribute<ref modAttrs> }
{ Attribute<ref modAttrs> }
FrameExpression<out fe, false, true> (. mod.Add(fe); .)
{ "," FrameExpression<out fe, false, true> (. mod.Add(fe); .)
}
Expand Down Expand Up @@ -2618,7 +2611,7 @@ AssertStmt<out Statement/*!*/ s, bool inExprContext>
IToken lbl = null;
.)
"assert" (. x = t; .)
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
( [ IF(IsLabel(!inExprContext))
LabelIdent<out lbl> ":"
]
Expand All @@ -2643,7 +2636,7 @@ AssumeStmt<out Statement/*!*/ s>
IToken dotdotdot = null;
.)
"assume" (. x = t; .)
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
( Expression<out e, false, true>
| "..." (. dotdotdot = t; .)
)
Expand Down Expand Up @@ -2737,7 +2730,7 @@ ModifyStmt<out Statement s>
IToken ellipsisToken = null;
.)
"modify" (. tok = t; .)
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
/* Note, there is an ambiguity here, because a curly brace may look like a FrameExpression and
* may also look like a BlockStmt. We're happy to parse the former, because if the user intended
* the latter, then an explicit FrameExpression of {} could be given.
Expand Down Expand Up @@ -2771,7 +2764,7 @@ CalcStmt<out Statement s>
IToken danglingOperator = null;
.)
"calc" (. x = t; .)
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
[ CalcOp<out opTok, out userSuppliedOp> (. if (userSuppliedOp.ResultOp(userSuppliedOp) == null) { // guard against non-transitive calcOp (like !=)
SemErr(opTok, "the main operator of a calculation must be transitive");
} else {
Expand Down Expand Up @@ -3836,7 +3829,7 @@ QuantifierDomain<.out List<BoundVar> bvars, out Attributes attrs, out Expression
{ ","
IdentTypeOptional<out bv> (. bvars.Add(bv); .)
}
{ IF(IsAttribute()) Attribute<ref attrs> }
{ Attribute<ref attrs> }
[ IF(la.kind == _verticalbar) /* Coco complains about this ambiguity, thinking that a "|" can follow a body-less forall statement; I don't see how that's possible, but this IF is good and suppresses the reported ambiguity */
"|"
Expression<out range, true, true>
Expand Down Expand Up @@ -3877,17 +3870,16 @@ Expressions<.List<Expression> args.>
.
/*------------------------------------------------------------------------*/
Attribute<ref Attributes attrs>
= (. IToken openBrace, colon, closeBrace;
= (. IToken openBrace, closeBrace;
IToken x = null;
var args = new List<Expression>();
.)
"{" (. openBrace = t; .)
":" (. colon = t; .)
"{:" (. openBrace = t; .)
(. ConvertKeywordTokenToIdent(); .)
NoUSIdent<out x>
[ Expressions<args> ]
"}" (. closeBrace = t; .)
(. attrs = new UserSuppliedAttributes(x, openBrace, colon, closeBrace, args, attrs); .)
(. attrs = new UserSuppliedAttributes(x, openBrace, closeBrace, args, attrs); .)
.
/*------------------------------------------------------------------------*/
Ident<out IToken/*!*/ x>
Expand Down
11 changes: 3 additions & 8 deletions Source/Dafny/DafnyAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -516,19 +516,16 @@ public class UserSuppliedAttributes : Attributes
{
public readonly IToken tok; // may be null, if the attribute was constructed internally
public readonly IToken OpenBrace;
public readonly IToken Colon;
public readonly IToken CloseBrace;
public bool Recognized; // set to true to indicate an attribute that is processed by some part of Dafny; this allows it to be colored in the IDE
public UserSuppliedAttributes(IToken tok, IToken openBrace, IToken colon, IToken closeBrace, List<Expression> args, Attributes prev)
public UserSuppliedAttributes(IToken tok, IToken openBrace, IToken closeBrace, List<Expression> args, Attributes prev)
: base(tok.val, args, prev) {
Contract.Requires(tok != null);
Contract.Requires(openBrace != null);
Contract.Requires(colon != null);
Contract.Requires(closeBrace != null);
Contract.Requires(args != null);
this.tok = tok;
OpenBrace = openBrace;
Colon = colon;
CloseBrace = closeBrace;
}
}
Expand Down Expand Up @@ -6210,9 +6207,8 @@ public override IEnumerable<Statement> SubStatements {
public void AddCustomizedErrorMessage(IToken tok, string s) {
var args = new List<Expression>() { new StringLiteralExpr(tok, s, true) };
IToken openBrace = tok;
IToken colon = new Token(tok.line, tok.col + 1);
IToken closeBrace = new Token(tok.line, tok.col + 7 + s.Length + 1); // where 7 = length(":error ")
this.Attributes = new UserSuppliedAttributes(tok, openBrace, colon, closeBrace, args, this.Attributes);
this.Attributes = new UserSuppliedAttributes(tok, openBrace, closeBrace, args, this.Attributes);
}
}

Expand Down Expand Up @@ -10719,9 +10715,8 @@ public MaybeFreeExpression(Expression e, bool isFree, AssertLabel/*?*/ label, At
public void AddCustomizedErrorMessage(IToken tok, string s) {
var args = new List<Expression>() { new StringLiteralExpr(tok, s, true) };
IToken openBrace = tok;
IToken colon = new Token(tok.line, tok.col + 1);
IToken closeBrace = new Token(tok.line, tok.col + 7 + s.Length + 1); // where 7 = length(":error ")
this.Attributes = new UserSuppliedAttributes(tok, openBrace, colon, closeBrace, args, this.Attributes);
this.Attributes = new UserSuppliedAttributes(tok, openBrace, closeBrace, args, this.Attributes);
}
}

Expand Down
Loading