Skip to content

Commit 36d9eed

Browse files
authored
Merge pull request #63 from hit9/option-value-const
Feature: Allow using constants as option values
2 parents 4833ddf + af35ddf commit 36d9eed

File tree

18 files changed

+550
-164
lines changed

18 files changed

+550
-164
lines changed

changes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
.. currentmodule:: bitproto
22

3+
Version 1.1.2
4+
-------------
5+
6+
.. _version-1.1.2
7+
8+
- Feature: Allow using constants as option values. ISSUE #61, PR #63
9+
310
Version 1.1.1
411
-------------
512

compiler/bitproto/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
99
"""
1010

11-
__version__ = "1.1.1"
11+
__version__ = "1.1.2"
1212
__description__ = "bit level data interchange format."

compiler/bitproto/grammars.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
option_value : boolean_literal
7272
| integer_literal
7373
| string_literal
74+
| constant_reference
7475
"""
7576

7677
r_alias = """

compiler/bitproto/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def p_option(self, p: P) -> None:
339339

340340
@override_docstring(r_option_value)
341341
def p_option_value(self, p: P) -> None:
342-
p[0] = p[1]
342+
p[0] = p[1].unwrap() if isinstance(p[1], Constant) else p[1]
343343

344344
@override_docstring(r_alias)
345345
def p_alias(self, p: P) -> None:

docs/language.rst

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ We can define an option in global scope and message scopes, like this:
571571

572572
option name = value
573573

574-
The value of an option can be an integer, string or boolean, according to the option itself.
574+
The value of an option can be an integer, string, boolean or ``constant``, according to the option itself.
575575

576576
For an example, there's an option named ``max_bytes`` to constraint message sizes, the
577577
bitproto compiler will report an error and refuse to compile if the declared message's
@@ -584,6 +584,19 @@ size is larger than the configured value:
584584
byte[4] field = 1 // Violated max_bytes constraint
585585
}
586586

587+
Another example that references constants as option values (supported since v1.1.2):
588+
589+
.. sourcecode:: bitproto
590+
591+
const MAX_MESSAGE_LENGTH = 128
592+
const HEADER_LENGTH = 5
593+
const MAX_PAYLOAD_LENGTH = MAX_MESSAGE_LENGTH - HEADER_LENGTH
594+
595+
message SomeMessage {
596+
option max_bytes = MAX_PAYLOAD_LENGTH
597+
}
598+
599+
587600
Full table of options supported:
588601

589602
``c.struct_packing_alignment``

docs/locales/zh/LC_MESSAGES/changelog.po

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,105 +8,130 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: bitproto 0.4.0\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2023-05-22 21:23+0800\n"
11+
"POT-Creation-Date: 2025-03-22 16:32+0800\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <LL@li.org>\n"
1515
"MIME-Version: 1.0\n"
1616
"Content-Type: text/plain; charset=utf-8\n"
1717
"Content-Transfer-Encoding: 8bit\n"
18-
"Generated-By: Babel 2.12.1\n"
18+
"Generated-By: Babel 2.17.0\n"
1919

20-
#: ../../changelog.rst:2 eb5e577b5cca451f97c84e6697bf5395
20+
#: ../../changelog.rst:2 bd0c49799ad1472b9ebc5a93eae789ae
2121
msgid "Changelog"
2222
msgstr "版本历史"
2323

24-
#: ../../../changes.rst:4 fc5a94d17a314c589b3622ad0c32e5a0
24+
#: ../../../changes.rst:4 4ca3aa397b074fb7a87296b215f52ecb
25+
msgid "Version 1.1.2"
26+
msgstr "Version 1.1.2"
27+
28+
#: ../../../changes.rst:8 427ec549afc64dd3ba6e4efb7ba67165
29+
msgid "Feature: Allow using constants as option values. ISSUE #61, PR #63"
30+
msgstr ""
31+
32+
#: ../../../changes.rst:11 4ca3aa397b074fb7a87296b215f52ecb
33+
msgid "Version 1.1.1"
34+
msgstr ""
35+
36+
#: ../../../changes.rst:15 61227587c4e443038089921f7bf8584f
37+
msgid ""
38+
"Fix bug: enum importing other bitproto's field name generation bug. #53 "
39+
"#52"
40+
msgstr ""
41+
42+
#: ../../../changes.rst:16 66206e28775a4993a98af4b796bd3097
43+
msgid ""
44+
"Fix bug: import statements of bitprotos should be placed ahead of other "
45+
"declarations. #53"
46+
msgstr ""
47+
48+
#: ../../../changes.rst:19 9c595ec0cf7f4e21b9cab4878149574c
2549
msgid "Version 1.1.0"
2650
msgstr ""
2751

28-
#: ../../../changes.rst:8 266428222faa4853ae12a6fe4a43b374
52+
#: ../../../changes.rst:23 668377b7aead4b6e9dfe5650f07f550d
2953
msgid ""
3054
"Performance improvements for C bitprotolib, 40~60us improvement per call "
3155
"on stm32. PR #48."
3256
msgstr ""
3357

34-
#: ../../../changes.rst:9 3242e95c31e649849edab6676af21f4d
58+
#: ../../../changes.rst:24 351457b1434647ff926e98fe7fb00c5a
3559
msgid ""
3660
"Fix Python nested message ``__post_init___`` function code generation. PR"
3761
" #48, commit 73f4b01."
3862
msgstr ""
3963

40-
#: ../../../changes.rst:12 fc5a94d17a314c589b3622ad0c32e5a0
64+
#: ../../../changes.rst:27 24745d925dd0496c8a1dd647e7b7225f
4165
msgid "Version 1.0.1"
4266
msgstr ""
4367

44-
#: ../../../changes.rst:16 c7bc4aba599d47b78b5b95e56f41257d
68+
#: ../../../changes.rst:31 c9cede67a4354f85a113fe9802d7c3f1
4569
msgid "Add support for Python 3.11"
4670
msgstr ""
4771

48-
#: ../../../changes.rst:19 fc5a94d17a314c589b3622ad0c32e5a0
72+
#: ../../../changes.rst:34 33d884cb4ef54a7e80d5c425391b372c
4973
msgid "Version 1.0.0"
5074
msgstr ""
5175

52-
#: ../../../changes.rst:23 c894eb85f9834f1b837622fd859c7e98
76+
#: ../../../changes.rst:38 3592669cb8e847c49cea8a365c2c3a5f
5377
msgid "First fully release version"
5478
msgstr "第一次发布 1.0 版本"
5579

56-
#: ../../../changes.rst:28 30bc2f453f9a4e4daec1f2b082a6ac91
80+
#: ../../../changes.rst:43 b3a828ec2f7a44a4b6401d53455fffc6
5781
msgid "Version 0.4.6"
5882
msgstr ""
5983

60-
#: ../../../changes.rst:30 9e9740e4c92e4ed595194bd543ebe147
84+
#: ../../../changes.rst:45 4f633212411841cd8396ddd38a9f24cc
6185
msgid "Support signed integers with arbitrary bits, e.g. int24 PR#45."
6286
msgstr "支持任意比特数目的有符号整数类型 例如 int24, 见 PR#45"
6387

64-
#: ../../../changes.rst:35 30bc2f453f9a4e4daec1f2b082a6ac91
88+
#: ../../../changes.rst:50 90f0ae1491a84c41a2b9c5dc0e66f845
6589
msgid "Version 0.4.5"
6690
msgstr ""
6791

68-
#: ../../../changes.rst:37 3ba33408fbab4e6e8fa4bd1348e776a9
92+
#: ../../../changes.rst:52 5390534c2c094a6b881c30f62d565d17
6993
msgid ""
7094
"Use Python IntEnum for enum generation (respecting backward "
7195
"compatibility) PR#41."
7296
msgstr ""
7397

74-
#: ../../../changes.rst:42 dc7770b7fd674da18fa53f58fea0f06f
98+
#: ../../../changes.rst:57 3bfcca062ff44f87aea90bf7a6f339b1
7599
msgid "Version 0.4.4"
76100
msgstr ""
77101

78-
#: ../../../changes.rst:44 ee1e046b6272442181a755b5b744d6ba
102+
#: ../../../changes.rst:59 c5a2117f96e4413da42ee13729394184
79103
msgid "Minor fix compiler setup.py path issue."
80104
msgstr ""
81105

82-
#: ../../../changes.rst:49 2cd432670b154c19a01259f94cb33bc3
106+
#: ../../../changes.rst:64 86561ffe65894180a12c77dd57d8172f
83107
msgid "Version 0.4.2"
84108
msgstr ""
85109

86-
#: ../../../changes.rst:51 a8406cdecd1d4dfc94ad9cebc4c927b5
110+
#: ../../../changes.rst:66 bc317a756c0f44999c0c22be675a7deb
87111
msgid "Allow using ``type`` as message field name, fixes issue #39."
88112
msgstr ""
89113

90-
#: ../../../changes.rst:56 fc5a94d17a314c589b3622ad0c32e5a0
114+
#: ../../../changes.rst:71 a00793c65d2c427f9b81c1ccab8ad424
91115
msgid "Version 0.4.0"
92116
msgstr ""
93117

94-
#: ../../../changes.rst:58 3c499990cd38434f932a3558376617c5
118+
#: ../../../changes.rst:73 b0917dbfc0524e74ba171e75741e97e2
95119
msgid ""
96120
"Add support for ``message`` and ``enum`` extensiblity for protocol "
97121
"forward compatibility."
98122
msgstr ""
99123

100-
#: ../../../changes.rst:59 ad10bcd6f05a4e5187b2d8ba2f3e5235
124+
#: ../../../changes.rst:74 95abb35c9fcd4d449a32519fd31ac1aa
101125
msgid "Cut down the code size of generated language-specific files."
102126
msgstr ""
103127

104-
#: ../../../changes.rst:60 a2e47a95cb5c4f44ac3a9d965d8d719c
128+
#: ../../../changes.rst:75 ccf790e19105464b9c222eb7d5916f8d
105129
msgid "Refactor the bitproto compiler."
106130
msgstr ""
107131

108-
#: ../../../changes.rst:61 480040dd74284c97acfb4dcc47354779
132+
#: ../../../changes.rst:76 6f93b6bbf73748d4b801a715f0cba57b
109133
msgid ""
110134
"Refactor the bitproto serialization mechanism, using language-specific "
111135
"libraries instead of pure compiler-generated files."
112136
msgstr ""
137+

0 commit comments

Comments
 (0)