Skip to content

Commit

Permalink
Bump v0.4.1, allow using type as message field name, fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
hit9 committed Jul 31, 2021
1 parent 0f15e35 commit 5bf7f1d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions changes.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
.. currentmodule:: bitproto

.. _version-0.4.2:

Version 0.4.2
-------------

- Allow using ``type`` as message field name, fixes issue #39.

.. _version-0.4.0:

Version 0.4.0
Expand Down
2 changes: 1 addition & 1 deletion compiler/bitproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"""

__version__ = "0.4.1"
__version__ = "0.4.2"
__description__ = "bit level data interchange format."
9 changes: 8 additions & 1 deletion compiler/bitproto/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,14 @@
"""

r_message_field = """
message_field : type IDENTIFIER '=' INT_LITERAL optional_semicolon
message_field : type message_field_name '=' INT_LITERAL optional_semicolon
"""

# https://github.com/hit9/bitproto/issues/39
# Allow some keywords to be message names.
r_message_field_name = """
message_field_name : IDENTIFIER
| TYPE
"""

r_boolean_literal = """
Expand Down
4 changes: 4 additions & 0 deletions compiler/bitproto/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,10 @@ def p_message_field(self, p: P) -> None:
)
self.current_scope().push_member(message_field)

@override_docstring(r_message_field_name)
def p_message_field_name(self, p: P) -> None:
p[0] = p[1]

@override_docstring(r_boolean_literal)
def p_boolean_literal(self, p: P) -> None:
p[0] = p[1]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
proto message_field_name_keyword

enum PayloadDataTypeE: uint8 {
PAYLOAD_DATA_TYPE_UNKNOWN = 1
PAYLOAD_DATA_TYPE_DETECTOR = 2

}

message PayloadDataT {
PayloadDataTypeE type = 1
}
7 changes: 7 additions & 0 deletions tests/test_compiler/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,13 @@ def test_option_wrong_type() -> None:
parse(bitproto_filepath("option_wrong_type.bitproto"))


def test_parse_message_field_name_keyword() -> None:
proto = parse(bitproto_filepath("message_field_name_keyword.bitproto"))
message = cast_or_raise(Message, proto.get_member("PayloadDataT"))
field = cast_or_raise(MessageField, message.get_member("type"))
assert field.number == 1


def test_constants() -> None:
proto = parse(bitproto_filepath("constants.bitproto"))

Expand Down

0 comments on commit 5bf7f1d

Please sign in to comment.