Crates.io | mago-docblock |
lib.rs | mago-docblock |
version | |
source | src |
created_at | 2024-12-07 12:18:17.254258 |
updated_at | 2024-12-10 23:51:34.251822 |
description | Analyzes PHP docblocks to extract annotations, tags, and documentation comments, aiding tools that rely on inline documentation. |
homepage | https://github.com/carthage-software/mago |
repository | https://github.com/carthage-software/mago |
max_upload_size | |
id | 1475558 |
Cargo.toml error: | TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This crate provides a parser for PHPDoc comments, offering a standardized approach to interpreting PHPDoc blocks. Given the lack of a strict standard for PHPDoc formatting, we've established our own conventions to ensure consistent parsing and to facilitate tooling such as linters or documentation generators.
Due to the absence of an official standard for PHPDoc formatting, we've established conventions to ensure consistent parsing. This standardization helps in maintaining uniform documentation and facilitates tooling.
PHPDoc comments must start with /**
and end with */
. Comments can be single-line or multi-line.
For single-line comments:
/**
.*/
./** This is a single-line PHPDoc comment */
For multi-line comments:
*
.*
./**
* This is a multi-line PHPDoc comment.
* Each line starts with an asterisk.
*/
/**
must have the same level of indentation.The parser recognizes various elements within PHPDoc comments:
Plain text within the comment is parsed and can include inline code or inline tags.
`code`
, ``code``
)./**
* This is an example of inline code: `echo "Hello, World!";`.
*/
{@
and }
.{@
unless it's at the start of a line./**
* For more information, see {@see \Some\Class}.
*/
The parser supports two types of code blocks: fenced code blocks and indented code blocks.
```
.```php,no-run
)./**
* This is a fenced code block:
*
* ```php
* echo "Hello, World!";
* ```
*/
/**
* This is an indented code block:
*
* echo "Hello, World!";
*/
Tags provide metadata and additional information about the code. They start with @ and are followed by a tag name.
@
followed by the tag name./**
* @param string $name The name of the user.
*/
Note: This approach simplifies parsing and delegates the responsibility of interpreting tag descriptions to other tools if necessary.
Annotations are similar to tags but have distinct syntax and usage.
@
followed by a name that must start with an uppercase letter, underscore _
, or backslash \
.(
and )
./**
* @Route("/home", name="homepage")
*/
The parser provides detailed error messages and suggestions to help users correct formatting issues. Errors include:
}
.`
.```
.)
.\*
.*
in a line./**
in a single-line comment.*/
in a single-line comment.Each error includes:
To use the parser, include the crate in your project and utilize the public API provided.
use mago_interner::ThreadedInterner;
use mago_span::Span;
use mago_docblock::parse_phpdoc_with_span;
const PHPDOC: &str = r#"/**
* This is a simple description.
*
* @param string $name The name of the user.
* @return void
*/
"#;
pub fn main() {
let interner = ThreadedInterner::new();
let span = Span::new(Position::dummy(0), Position::dummy(phpdoc.len()));
let result = parse_phpdoc_with_span(&interner, phpdoc, span);
match result {
Ok(document) => {
println!("Document: {:#?}", document);
}
Err(err) => {
eprintln!("Error: {:#?}", err);
}
}
}
This parser aims to provide a consistent and reliable way to parse PHPDoc comments, adhering to a standardized format. By following the conventions outlined above, you can ensure that your PHPDoc comments are well-structured and easily parsed by tooling, enhancing code readability and maintainability.
[!IMPORTANT]
This parser focuses on the structural aspects of PHPDoc comments and does not interpret the semantics of tag descriptions or annotation arguments. The descriptions are treated as plain text, allowing other tools to process them as needed.