-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc comments and annotations in Souffle datalog (#2472)
* doc comments and annotations in Souffle datalog Annotations are free meta-data that can be attached to most items of a Souffle datalog program. A documentation comment is a syntactic sugar for a `doc` annotation. The design is close to Rust's *attributes*. All program's and component's items can have *outer* annotations of the form `@[annotation]` or `/// doc comment`. Attributes and ADT branches also accept outer annotations. Some items can have *inner* annotations of the for `@![annotation]` or `//! doc comment`: - after `{` of a component, before the first component's item. - after `{` of an ADT branch, before the first attribute of the branch. - after `:-` of a clause, before the first constraint. Annotations should not appear before `.include` or before any `}` or before the end of a file. An annotation's payload may be of these forms: - an identifier: `@[IDENT]` - possibly followed by delimited token stream: `@[IDENT DELIM]` - or `=` and a delimited token stream: `@[IDENT = DELIM]` or `@[IDENT = TOKEN TT*]` Where: - `IDENT` is an identifier or a keyword of Souffle. - `TOKEN` is any token except delimiters. - `TS ::= TT+` is a token-stream. - `TT ::= TOKEN | DELIM` is a token-tree. - `DELIM ::= '(' TS? ')' | '[' TS? ']' | '{' TS? '}'` is a delimited token-stream. Use cases: - documentation comments attached to AST nodes ease the writing of documentation generation tools. - annotations could replace "dirty" parts of the current Souffle grammar (stateful, choice-domain, .plan ...). - annotations brings extensibility without modifications of the core grammar.
- Loading branch information
Showing
95 changed files
with
2,307 additions
and
206 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Souffle - A Datalog Compiler | ||
* Copyright (c) 2022, The Souffle Developers. All rights reserved | ||
* Licensed under the Universal Permissive License v 1.0 as shown at: | ||
* - https://opensource.org/licenses/UPL | ||
* - <souffle root>/licenses/SOUFFLE-UPL.txt | ||
*/ | ||
#include "Annotation.h" | ||
|
||
#include <cassert> | ||
|
||
namespace souffle::ast { | ||
|
||
Annotation::Annotation() : kind(), style(), label(), tokens(), location() {} | ||
|
||
Annotation::Annotation(Kind k, Style sty, QualifiedName lbl, TokenStream ts, SrcLocation loc) | ||
: kind(k), style(sty), label(lbl), tokens(ts), location(loc) {} | ||
|
||
bool Annotation::operator==(const Annotation& other) const { | ||
return (kind == other.kind) && (style == other.style) && (label == other.label) && | ||
(tokens == other.tokens); | ||
} | ||
|
||
void Annotation::print(std::ostream& os) const { | ||
os << "@"; | ||
if (style == Style::Inner) { | ||
os << "!"; | ||
} | ||
os << "[" << label; | ||
if (!tokens.empty()) { | ||
os << " "; | ||
} | ||
printTokenStream(os, tokens); | ||
os << "]"; | ||
} | ||
|
||
void Annotation::printAsOuter(std::ostream& os) const { | ||
os << "@[" << label; | ||
if (!tokens.empty()) { | ||
os << " "; | ||
} | ||
printTokenStream(os, tokens); | ||
os << "]"; | ||
} | ||
|
||
Annotation::Kind Annotation::getKind() const { | ||
return kind; | ||
} | ||
|
||
Annotation::Style Annotation::getStyle() const { | ||
return style; | ||
} | ||
|
||
const SrcLocation& Annotation::getSrcLoc() const { | ||
return location; | ||
} | ||
|
||
const QualifiedName& Annotation::getLabel() const { | ||
return label; | ||
} | ||
|
||
const TokenStream& Annotation::getTokens() const { | ||
return tokens; | ||
} | ||
} // namespace souffle::ast |
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,61 @@ | ||
/* | ||
* Souffle - A Datalog Compiler | ||
* Copyright (c) 2022, The Souffle Developers. All rights reserved | ||
* Licensed under the Universal Permissive License v 1.0 as shown at: | ||
* - https://opensource.org/licenses/UPL | ||
* - <souffle root>/licenses/SOUFFLE-UPL.txt | ||
*/ | ||
#pragma once | ||
|
||
#include "QualifiedName.h" | ||
#include "TokenTree.h" | ||
#include "parser/SrcLocation.h" | ||
|
||
#include <list> | ||
|
||
namespace souffle::ast { | ||
|
||
class Annotation { | ||
public: | ||
enum class Kind { Normal, DocComment }; | ||
|
||
enum class Style { Outer, Inner }; | ||
|
||
Annotation(); | ||
Annotation(Kind kind, Style style, QualifiedName lbl, TokenStream ts, SrcLocation loc = {}); | ||
Annotation(Annotation&&) = default; | ||
Annotation(const Annotation&) = default; | ||
Annotation& operator=(Annotation&&) = default; | ||
Annotation& operator=(const Annotation&) = default; | ||
|
||
bool operator==(const Annotation& other) const; | ||
|
||
Kind getKind() const; | ||
|
||
Style getStyle() const; | ||
|
||
const SrcLocation& getSrcLoc() const; | ||
|
||
const QualifiedName& getLabel() const; | ||
|
||
const TokenStream& getTokens() const; | ||
|
||
void print(std::ostream&) const; | ||
|
||
void printAsOuter(std::ostream&) const; | ||
|
||
private: | ||
Kind kind; | ||
|
||
Style style; | ||
|
||
QualifiedName label; | ||
|
||
TokenStream tokens; | ||
|
||
SrcLocation location; | ||
}; | ||
|
||
using AnnotationList = std::list<Annotation>; | ||
|
||
} // namespace souffle::ast |
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
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
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
Oops, something went wrong.