Skip to content

Rename to cvldoc #20

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

Merged
merged 5 commits into from
Sep 22, 2022
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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cvldoc_parser"
name = "cvldoc_parser_core"
version = "0.4.0"
edition = "2021"

Expand Down
21 changes: 11 additions & 10 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{AssociatedElement, CvlDoc, Tag, DocData};
use crate::{AssociatedElement, CvlDoc, DocData, Tag};
use lsp_types::{Diagnostic, DiagnosticSeverity, Range};

impl AssociatedElement {
Expand Down Expand Up @@ -42,15 +42,16 @@ impl CvlDoc {
const WARNING: DiagnosticSeverity = DiagnosticSeverity::WARNING;
const ERROR: DiagnosticSeverity = DiagnosticSeverity::ERROR;

let mut add = |message: String, diag_range: Option<Range>, severity: DiagnosticSeverity| {
let diag = Diagnostic {
range: diag_range.unwrap_or(self.range),
severity: Some(severity),
message,
..Default::default()
let mut add =
|message: String, diag_range: Option<Range>, severity: DiagnosticSeverity| {
let diag = Diagnostic {
range: diag_range.unwrap_or(self.range),
severity: Some(severity),
message,
..Default::default()
};
warnings.push(diag);
};
warnings.push(diag);
};

if let Some(associated) = associated {
if tags.iter().all(|tag| tag.kind != Tag::Notice) {
Expand Down Expand Up @@ -80,7 +81,7 @@ impl CvlDoc {
}
}
} else {
let error_desc = "no associated element for NatSpec documentation block".into();
let error_desc = "no associated element for CVLDoc documentation block".into();
add(error_desc, None, ERROR);
}

Expand Down
13 changes: 7 additions & 6 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn commented_out_line<'src>() -> BoxedParser<'src, char, CvlDocBuilder, Simple<c
.boxed()
}

fn natspec_doc<'src>() -> BoxedParser<'src, char, CvlDocBuilder, Simple<char>> {
fn cvldoc_documentation<'src>() -> BoxedParser<'src, char, CvlDocBuilder, Simple<char>> {
let spanned_slashed_line = just("///")
.ignore_then(none_of('/').rewind())
.ignore_then(horizontal_ws())
Expand All @@ -109,10 +109,11 @@ fn natspec_doc<'src>() -> BoxedParser<'src, char, CvlDocBuilder, Simple<char>> {
.ignore_then(take_to_starred_terminator().map_with_span(builder::split_starred_doc_lines))
.boxed();

let doc = choice([slashed_documentation, starred_documentation])
let documentation = choice([slashed_documentation, starred_documentation])
.map_with_span(|spanned_body, span| (spanned_body, span));

doc.then(associated_element().or_not())
documentation
.then(associated_element().or_not())
.map(
|((spanned_body, span), element_under_doc)| CvlDocBuilder::Documentation {
span,
Expand All @@ -124,10 +125,10 @@ fn natspec_doc<'src>() -> BoxedParser<'src, char, CvlDocBuilder, Simple<char>> {
}

pub(super) fn parser() -> impl Parser<char, Vec<CvlDocBuilder>, Error = Simple<char>> {
let valid_natspec = choice([free_form_comment(), natspec_doc()]);
let natspec = choice((commented_out_block(), commented_out_line(), valid_natspec));
let valid_cvldoc = choice([free_form_comment(), cvldoc_documentation()]);
let cvldoc = choice((commented_out_block(), commented_out_line(), valid_cvldoc));

natspec
cvldoc
.recover_with(skip_until(['\n', ' '], |_| CvlDocBuilder::ParseError).consume_end())
.repeated()
.boxed()
Expand Down
2 changes: 1 addition & 1 deletion src/python_wrapper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.17.1", features = ["extension-module", "abi3-py37"] }
cvldoc_parser = { path = "../.." }
cvldoc_parser_core = { path = "../.." }
color-eyre = "0.6.2"
indoc = "1.0.7"
itertools = "0.10.4"
Expand Down
12 changes: 6 additions & 6 deletions src/python_wrapper/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# NatSpec Parser: Python module
Exposes a Python API for the NatSpec module. WIP.
# CVLDoc Parser: Python module
Exposes a Python API for the CVLDoc module. WIP.

## Usage
First, make sure **Rust 1.62** or newer is installed.
Clone [the entire repo](https://github.com/Certora/natspec_parser), then from the project base:
Clone [the entire repo](https://github.com/Certora/cvldoc_parser), then from the project base:
```bash
$ cd src/python_wrapper
$ python -m venv .env
Expand All @@ -20,9 +20,9 @@ in order to build the module, which can then be used in the `virtualenv` or inst
It is now possible to import the module:
```bash
$ python
>>> import natspec_parser
>>> import cvldoc_parser
```

## API
Currently exposes a single function, `natspec_parser.parse(paths)`. It takes a list of file paths as strings, and returns a list of parsed NatSpec objects for each path.
A parsed NatSpec object is either a `Documentation` or a `FreeForm`. They both support a `diagnostics()` method, that returns a list of warnings or errors associated with that object.
Currently exposes a single function, `cvldoc_parser.parse(paths)`. It takes a list of file paths as strings, and returns a list of parsed CVLDoc objects for each path.
A parsed CVLDoc object is either a `Documentation` or a `FreeForm`. They both support a `diagnostics()` method, that returns a list of warnings or errors associated with that object.
14 changes: 7 additions & 7 deletions src/python_wrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod wrapper_structs;

use ::cvldoc_parser::CvlDoc;
use color_eyre::eyre::WrapErr;
use color_eyre::Result;
use cvldoc_parser_core::CvlDoc;
use itertools::Itertools;
use pyo3::{prelude::*, types::PyList};
use ropey::Rope;
Expand All @@ -20,13 +20,13 @@ fn cvldocs_from_path(path: &str) -> Result<Vec<CvlDoc>> {
Rope::from_str(&data)
};

let natspecs = CvlDoc::from_rope(rope);
let cvldocs = CvlDoc::from_rope(rope);

Ok(natspecs)
Ok(cvldocs)
}

/// takes a list of file paths as strings, returns a list of parsed cvldocs for each path,
/// if any natspecs were parsed for the path, otherwise returns an empty list for that path.
/// if any cvldocs were parsed for the path, otherwise returns an empty list for that path.
/// currently panics if a file fails to open, or fails to read.
#[pyfunction]
fn parse(paths: Vec<&str>) -> Vec<Py<PyAny>> {
Expand All @@ -39,10 +39,10 @@ fn parse(paths: Vec<&str>) -> Vec<Py<PyAny>> {
Python::with_gil(|py| {
cvldocs_per_file
.into_iter()
.map(|file_natspecs| {
let elements = file_natspecs
.map(|file_cvldocs| {
let elements = file_cvldocs
.into_iter()
.map(|natspec| cvldoc_to_py_object(natspec, py));
.map(|cvldoc| cvldoc_to_py_object(cvldoc, py));

PyList::new(py, elements).into_py(py)
})
Expand Down
20 changes: 13 additions & 7 deletions src/python_wrapper/src/wrapper_structs/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use super::{
AssociatedElement, Diagnostic, Documentation, DocumentationTag, FreeForm, Position, Range,
Severity,
};
use cvldoc_parser_core::{
AssociatedElement as AssociatedElementR, CvlDoc as CvlDocR, DocData as DocDataR,
DocumentationTag as DocumentationTagR,
};
use lsp_types::{
Diagnostic as DiagnosticR, DiagnosticSeverity as DiagnosticSeverityR, Position as PositionR,
Range as RangeR,
};
use cvldoc_parser::{
AssociatedElement as AssociatedElementR, CvlDoc as CvlDocR, DocData as DocDataR,
DocumentationTag as DocumentationTagR,
};
use pyo3::{IntoPy, Py, PyAny, Python};

impl From<DocumentationTagR> for DocumentationTag {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl From<FreeForm> for CvlDocR {
CvlDocR {
raw,
range: range.into(),
data: DocDataR::FreeForm(text)
data: DocDataR::FreeForm(text),
}
}
}
Expand All @@ -95,10 +95,16 @@ pub fn cvldoc_to_py_object(doc: CvlDocR, py: Python<'_>) -> Py<PyAny> {
match doc.data {
DocDataR::FreeForm(text) => {
let range = doc.range.into();
FreeForm { raw: doc.raw, range, text }.into_py(py)
FreeForm {
raw: doc.raw,
range,
text,
}
.into_py(py)
}
DocDataR::Documentation { tags, associated } => {
Documentation::from_rust_repr_components(doc.raw, tags, associated, doc.range).into_py(py)
Documentation::from_rust_repr_components(doc.raw, tags, associated, doc.range)
.into_py(py)
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/python_wrapper/src/wrapper_structs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
pub mod conversions;

use cvldoc_parser_core::{Param, Ty};
use derivative::Derivative;
use cvldoc_parser::{Param, Ty};
use pyo3::prelude::*;

#[derive(Derivative, Clone)]
#[derivative(Debug)]
#[pyclass(module = "cvldoc_parser")]
pub struct Documentation {
#[pyo3(get)]
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
pub raw: String,
#[pyo3(get)]
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
pub range: Range,
#[pyo3(get)]
pub tags: Vec<DocumentationTag>,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Position {
#[derivative(Debug)]
#[pyclass(module = "cvldoc_parser")]
pub struct Diagnostic {
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
#[pyo3(get)]
pub range: Range,
#[pyo3(get)]
Expand All @@ -83,10 +83,10 @@ pub enum Severity {
#[derivative(Debug)]
#[pyclass(module = "cvldoc_parser")]
pub struct FreeForm {
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
#[pyo3(get)]
pub raw: String,
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
#[pyo3(get)]
pub range: Range,
#[pyo3(get)]
Expand All @@ -100,7 +100,7 @@ impl Documentation {
}

fn diagnostics(&self) -> Vec<Diagnostic> {
let c: cvldoc_parser::CvlDoc = self.clone().into();
let c: cvldoc_parser_core::CvlDoc = self.clone().into();
c.enumerate_diagnostics()
.into_iter()
.map(Into::into)
Expand All @@ -115,7 +115,7 @@ impl FreeForm {
}

fn diagnostics(&self) -> Vec<Diagnostic> {
let c: cvldoc_parser::CvlDoc = self.clone().into();
let c: cvldoc_parser_core::CvlDoc = self.clone().into();
c.enumerate_diagnostics()
.into_iter()
.map(Into::into)
Expand All @@ -125,7 +125,7 @@ impl FreeForm {

#[derive(Debug, Clone)]
#[pyclass(module = "cvldoc_parser")]
pub struct AssociatedElement(cvldoc_parser::AssociatedElement);
pub struct AssociatedElement(cvldoc_parser_core::AssociatedElement);

#[pymethods]
impl AssociatedElement {
Expand Down Expand Up @@ -192,7 +192,7 @@ pub struct DocumentationTag {
pub kind: String,
#[pyo3(get)]
pub description: String,
#[derivative(Debug="ignore")]
#[derivative(Debug = "ignore")]
#[pyo3(get)]
pub range: Option<Range>,
}
Expand Down