Standard attributes as comments are birefly mentioned in The Gerber Layer Format Specification 2024.05 in section 4.1 Comment (G04), dedicated description is available in section 5.1.1 Comment attributes.
There are at least two ways to approach how this can be handled from perspective of AST node classes:
create separate collection of classes mrroring src/pygerber/gerberx3/ast/nodes/attribute but dedicated for comment attributes.
add is_comment member to each attribute base class (TA, TD, TF, TO)
First approach requires a lot of code duplication, will require separate set of on_<attr-name> callbacks on AstVisitor and in general will unnecessarily complicate implementation, in my humble opition, since comment attributes are bound to behave like the X3 standalone counterparts.
Therefore I would advise choosing second approach. This will require adding is_comment=False for all existing construction calls for attributes in Gerber grammar.
src/pygerber/gerberx3/ast/nodes/attribute/TA.py
class TA(Node):
"""Represents TA Gerber extended command."""
is_standalone: bool
@property
def attribute_name(self) -> str:
"""Get attribute name."""
raise NotImplementedError
Afterwards, separate grammar definition for all the attributes will be needed, as % signs must be a part of standalone attributes while comment attributes must not contain them.
def ta(self) -> pp.ParserElement:
"""Create a parser element capable of parsing TA attributes."""
return pp.MatchFirst(
[
self._ta_user_name,
self._ta_aper_function,
self._ta_drill_tolerance,
self._ta_flash_text,
]
)
But with _comment suffix. eg.
def ta_comment(self) -> pp.ParserElement:
"""Create a parser element capable of parsing TA comment attributes."""
return pp.MatchFirst(
[
self._ta_user_name_comment,
self._ta_aper_function_comment,
self._ta_drill_tolerance_comment,
self._ta_flash_text_comment,
]
)
Keep in mind that you cannot use self._extended_command as it includes % at the beginning and the end. I suppose example comment attribute definition could look like this:
Notice that G04 is a separate literal to allow undefined amount of whitespace characters between G04 and #@!. Make sure that I didn't mess up the special sequence, I am not sure that it is #@!
All of this will inevitebly require changes in test suite to account for new is_standalone member of attribute node classes.
It would also be good to prepare test assets similar to test/assets/gerberx3/tokens/attribute for comment attributes. Gerber assets created should automatically get included in test suite thanks to how test/gerberx3/test_parser/test_pyparsing/test_parser.py:test_pyparsing_parser_grammar() works.
Standard attributes as comments are birefly mentioned in The Gerber Layer Format Specification 2024.05 in section 4.1 Comment (G04), dedicated description is available in section 5.1.1 Comment attributes.
There are at least two ways to approach how this can be handled from perspective of AST node classes:
src/pygerber/gerberx3/ast/nodes/attribute
but dedicated for comment attributes.First approach requires a lot of code duplication, will require separate set of
on_<attr-name>
callbacks on AstVisitor and in general will unnecessarily complicate implementation, in my humble opition, since comment attributes are bound to behave like the X3 standalone counterparts.Therefore I would advise choosing second approach. This will require adding
is_comment=False
for all existing construction calls for attributes in Gerber grammar.src/pygerber/gerberx3/ast/nodes/attribute/TA.py
Afterwards, separate grammar definition for all the attributes will be needed, as
%
signs must be a part of standalone attributes while comment attributes must not contain them.src/pygerber/gerberx3/parser/pyparsing/grammar.py:582
G04 with
pp.MatchFirst
for comment attributes must be placed before general G04.src/pygerber/gerberx3/parser/pyparsing/grammar.py:1174
Comment attributes syntax should be defined similarily to standalone attributes:
src/pygerber/gerberx3/parser/pyparsing/grammar.py:558
src/pygerber/gerberx3/parser/pyparsing/grammar.py:570
But with
_comment
suffix. eg.Keep in mind that you cannot use
self._extended_command
as it includes%
at the beginning and the end. I suppose example comment attribute definition could look like this:Where
self._comment_attribute
has to be implemented and could look similarily toself._extended_command
, lets say:Notice that G04 is a separate literal to allow undefined amount of whitespace characters between
G04
and#@!
. Make sure that I didn't mess up the special sequence, I am not sure that it is#@!
All of this will inevitebly require changes in test suite to account for new
is_standalone
member of attribute node classes. It would also be good to prepare test assets similar totest/assets/gerberx3/tokens/attribute
for comment attributes. Gerber assets created should automatically get included in test suite thanks to howtest/gerberx3/test_parser/test_pyparsing/test_parser.py
:test_pyparsing_parser_grammar()
works.