-
Notifications
You must be signed in to change notification settings - Fork 47
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
Parser changes for documentation generator #780
Draft
fnussbaum
wants to merge
14
commits into
viperproject:master
Choose a base branch
from
fnussbaum:viperdoc/parser-changes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ce22bac
Allow annotations to be placed before specification keywords
fnussbaum 2df97a7
Parse ///-docstring as annotation WIP
fnussbaum 4509a30
Enable ///-parsing, fix ambiguity with division
fnussbaum 725ef22
Directly construct docAnnotations
fnussbaum 2469a2e
Add annotations to specification nodes in the Parse AST
fnussbaum 4149349
Move specification annotations to their expressions in Viper AST
fnussbaum bc9e747
Fix with NoCut
fnussbaum 320c5b1
Remove duplicated whitespace definition
fnussbaum 30cf8bb
Add positions to docAnnotations
fnussbaum 0c839f5
Add basic test
fnussbaum 9c7b16f
Only parse exactly three slashes as beginning of documentation
fnussbaum ee33a36
Implement feedback wip
fnussbaum b2d0d9f
Add DocPlugin to extract sparser documentation tree from CST
fnussbaum 125aa7c
Add command line entry point similar to silicon
fnussbaum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) 2011-2024 ETH Zurich. | ||
|
||
import org.scalatest.funsuite.AnyFunSuite | ||
import viper.silver.ast.Program | ||
import viper.silver.frontend._ | ||
import viper.silver.logger.ViperStdOutLogger | ||
import viper.silver.reporter.StdIOReporter | ||
import viper.silver.parser.{PProgram, PAnnotatedExp, PWhile} | ||
|
||
class DocAnnotationTests extends AnyFunSuite { | ||
object AstProvider extends ViperAstProvider(StdIOReporter(), ViperStdOutLogger("DocAnnotationTestsLogger").get) { | ||
def setCode(code: String): Unit = { | ||
_input = Some(code) | ||
} | ||
|
||
override def reset(input: java.nio.file.Path): Unit = { | ||
if (state < DefaultStates.Initialized) sys.error("The translator has not been initialized.") | ||
_state = DefaultStates.InputSet | ||
_inputFile = Some(input) | ||
/** must be set by [[setCode]] */ | ||
// _input = None | ||
_errors = Seq() | ||
_parsingResult = None | ||
_semanticAnalysisResult = None | ||
_verificationResult = None | ||
_program = None | ||
resetMessages() | ||
} | ||
} | ||
|
||
def generatePAstAndAst(code: String): Option[(PProgram, Program)] = { | ||
val code_id = code.hashCode.asInstanceOf[Short].toString | ||
AstProvider.setCode(code) | ||
AstProvider.execute(Seq("--ignoreFile", code_id)) | ||
|
||
if (AstProvider.errors.isEmpty) { | ||
Some(AstProvider.parsingResult, AstProvider.translationResult) | ||
} else { | ||
AstProvider.logger.error(s"An error occurred while translating ${AstProvider.errors}") | ||
None | ||
} | ||
} | ||
|
||
test("Basic parsing of documentation") { | ||
import viper.silver.ast._ | ||
|
||
val code = | ||
"""/// a field | ||
|field f: Int | ||
|/// P is a predicate | ||
|predicate P(x: Int) | ||
| | ||
|/// a function | ||
|function fun(x: Int): Int { | ||
| (x / 1 == x) ? 42 : 0 | ||
|} | ||
|/// a second function | ||
|function fun2(x: Int): Int | ||
| /// precondition | ||
| requires x > 0 | ||
| /// post- | ||
| /// condition | ||
| ensures result > 0 | ||
| | ||
|/// very important domain | ||
|domain ImportantType { | ||
| | ||
| /// this function | ||
| /// is crucial | ||
| function phi(ImportantType): Int | ||
| | ||
| /// the only axiom | ||
| axiom { | ||
| /// documenting an expression | ||
| true | ||
| } | ||
|} | ||
| | ||
|/// a macro | ||
|define plus(a, b) (a+b) | ||
| | ||
|/// this is a method | ||
|/// it does something | ||
|method m(x: Ref, y: Ref) | ||
| /// this documents the first precondition | ||
| requires acc(x.f) | ||
| /// documentation of the second precondition | ||
| requires acc(y.f) | ||
|{ | ||
| var local: Bool | ||
| | ||
| while (true)///the invariant | ||
| /// is always true | ||
| invariant true | ||
| /// termination | ||
| decreases x.f | ||
| {} | ||
| | ||
|} | ||
|""".stripMargin | ||
|
||
val pAst: PProgram = generatePAstAndAst(code).get._1 | ||
|
||
val fieldAnn = pAst.fields.head.annotations.head.values.inner.first.get.str | ||
assert(fieldAnn == " a field") | ||
|
||
val predicateAnnotation = pAst.predicates.head.annotations.head.values.inner.first.get.str | ||
assert(predicateAnnotation == " P is a predicate") | ||
|
||
val functionAnnotation = pAst.functions.head.annotations.head.values.inner.first.get.str | ||
assert(functionAnnotation == " a function") | ||
|
||
val fun2Annotation = pAst.functions(1).annotations.head.values.inner.first.get.str | ||
val fun2PreAnnotations = pAst.functions(1).pres.head.annotations.map(_.values.inner.first.get.str) | ||
val fun2PostAnnotations = pAst.functions(1).posts.head.annotations.map(_.values.inner.first.get.str) | ||
assert(fun2Annotation == " a second function") | ||
assert(fun2PreAnnotations == Seq(" precondition")) | ||
assert(fun2PostAnnotations == Seq(" post-", " condition")) | ||
|
||
val domainAnnotation = pAst.domains.head.annotations.head.values.inner.first.get.str | ||
assert(domainAnnotation == " very important domain") | ||
|
||
val domainFunctionAnnotations = pAst.domains.head.members.inner.funcs.head.annotations.map(_.values.inner.first.get.str) | ||
assert(domainFunctionAnnotations == Seq(" this function", " is crucial")) | ||
|
||
val axiomAnnotations = pAst.domains.head.members.inner.axioms.head.annotations.map(_.values.inner.first.get.str) | ||
assert(axiomAnnotations == Seq(" the only axiom")) | ||
val axiomExpAnnotations = pAst.domains.head.members.inner.axioms.head.exp.e.inner.asInstanceOf[PAnnotatedExp].annotation.values.inner.first.get.str | ||
assert(axiomExpAnnotations == " documenting an expression") | ||
|
||
val macroAnnotation = pAst.macros.head.annotations.head.values.inner.first.get.str | ||
assert(macroAnnotation == " a macro") | ||
|
||
val methodAnnotations = pAst.methods.head.annotations.map(_.values.inner.first.get.str) | ||
assert(methodAnnotations == Seq(" this is a method", " it does something")) | ||
|
||
val methodPreAnnotations = pAst.methods.head.pres.toSeq.map(_.annotations.head.values.inner.first.get.str) | ||
assert(methodPreAnnotations == Seq(" this documents the first precondition", " documentation of the second precondition")) | ||
|
||
val loopInvariantAnnotations = pAst.methods.head.body.get.ss.inner.inner.collect { | ||
case (_, w: PWhile) => w.invs.toSeq.flatMap(_.annotations.map(_.values.inner.first.get.str)) | ||
}.flatten | ||
assert(loopInvariantAnnotations == Seq("the invariant", " is always true", " termination")) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
///
should be parsed with e.g.P(PSym.TripleSlash)
, the position of theCharsWhile(_ != '\n', 0).!
should actually be precise when saving it to aPRawString
- desugaring during parsing is the wrong place to do it. I would create a sealedPAnnotation
trait with all the methods that the currentPAnnotation
has and have two classes implementing it.