Skip to content
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
5 changes: 5 additions & 0 deletions docs/syntax/constant.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ custom grammar:
"Comments may be attached to values with a string following the definition"
VALUE_B : constant[16] = 234
"These are attached to the constant definitions"
VALUE_C: constant[2] = 3
"""
Multiline comments can be used for long descriptions.
Use triple quotes for these like with Python docstrings.
"""
}
```

Expand Down
66 changes: 66 additions & 0 deletions docs/syntax/docstrings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Descriptions can be added to packtype definitions as shown below.
The documentation is attached to the code in a way that will allow automated generation of documentation,
as with Python docstrings.

## Example

Descriptions can be added with normal [Python docstrings]((https://peps.python.org/pep-0257/)) or with the Packtype custom grammar:

=== "Python (.py)"

```python linenums="1"
import packtype
from packtype import Constant

@packtype.package()
class MyPackage:
"""
Package decription,
using normal Python docstrings
"""
...

@MyPackage.enum()
class Fruit:
"""
Class description,
using normal Python docstrings
"""
APPLE : Constant
ORANGE : Constant
PEAR : Constant
BANANA : Constant
```

=== "Packtype (.pt)"

```sv linenums="1"
package my_package {
"""
Package description
using multiline docstring syntax
"""
enum fruit_t {
"""
Class description
using multiline docstring syntax
"""
@prefix=FRUIT
APPLE : constant
"This is an apple"
ORANGE : constant
"""
Member descriptions can also be multiline.
Use triple quotes for these.
"""
PEAR : constant
"This is a pear"
BANANA : constant
"This is a banana"
}

}
```


```
2 changes: 1 addition & 1 deletion docs/syntax/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ custom grammar:

```sv linenums="1"
package my_package {
"Description of what purpose this package serves"
"""Description of what purpose this package serves"""
// ...
}
```
Expand Down
4 changes: 4 additions & 0 deletions docs/syntax/scalar.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ custom grammar:
TypeB : Scalar[TYPE_B_W]
"They can be queried from the digested result"
TypeC : Scalar[7]
"""
Multiline comments can be used for long descriptions.
Use triple quotes for these like with Python docstrings.
"""
}
```

Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ nav:
- Scalars: syntax/scalar.md
- Structs: syntax/struct.md
- Unions: syntax/union.md
- Docstrings: syntax/docstrings.md
- Utilities:
- Basic: utilities/basic.md
- Constants: utilities/constant.md
Expand Down
5 changes: 4 additions & 1 deletion packtype/grammar/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pathlib import Path

from lark import Lark
from lark.exceptions import UnexpectedToken
from lark.exceptions import UnexpectedToken, VisitError

from ..common.logging import get_log
from ..types.base import Base
Expand Down Expand Up @@ -93,6 +93,9 @@ def parse_string(
f"Failed to parse {source.name if source else 'input'} on line {exc.line}: "
f"\n\n{exc.get_context(definition)}\n{exc}"
) from exc
except VisitError as exc:
raise exc.orig_exc from exc

# Gather declarations
known_entities: dict[str, tuple[type[Base] | Constant, Position]] = {}

Expand Down
9 changes: 8 additions & 1 deletion packtype/grammar/packtype.lark
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

%import common.CNAME
%import common.ESCAPED_STRING
%import common._STRING_ESC_INNER
%import common.INT
%import common.SIGNED_INT
%import common.WS
%import common.NEWLINE
%ignore WS

// =============================================================================
Expand All @@ -42,7 +44,12 @@ dimension: "[" expr "]"
dimensions: dimension+

?name: CNAME
descr: ESCAPED_STRING

// Multiline docstrings as with Python - start and end with three double-quotes
_DOCSTRING_QUOTES: "\"\"\""
ESCAPED_MULTILINE_DOCSTRING: _DOCSTRING_QUOTES (_STRING_ESC_INNER|NEWLINE)* _DOCSTRING_QUOTES

descr: (ESCAPED_STRING | ESCAPED_MULTILINE_DOCSTRING)

modifier: "@" name "=" (name | ESCAPED_STRING | NUMERIC)

Expand Down
7 changes: 6 additions & 1 deletion packtype/grammar/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

import math
import textwrap

from lark import Transformer, v_args

Expand Down Expand Up @@ -85,7 +86,11 @@ def CNAME(self, body): # noqa: N802
return str(body)

def descr(self, body):
return Description(str(body[0]).strip('"'))
"""
Take description and trim surrounding quotes,
then remove common indentation and remove leading and trailing newlines.
"""
return Description(textwrap.dedent(str(body[0]).strip('"')).strip("\n"))

def modifier(self, body):
return Modifier(*body)
Expand Down
Loading