Skip to content

Commit f9bee1b

Browse files
committed
Add cached proto3 grammar loader
Introduce a `proto3_grammar` function cached with `@lang.cached_function` to parse the protobuf ABNF grammar, update the module’s `_main` to use it, and integrate this loader into the A2A spec script for parsing and validating example specs.
1 parent 2a45f04 commit f9bee1b

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

omxtra/specs/proto/abnf.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import os.path
22

33
from omlish import check
4+
from omlish import lang
45
from omlish.text import abnf
56

67

7-
def _main() -> None:
8+
##
9+
10+
11+
@lang.cached_function(lock=True)
12+
def proto3_grammar() -> abnf.Grammar:
813
with open(os.path.join(os.path.dirname(__file__), 'proto3.abnf')) as f:
9-
gram_src = f.read()
14+
src = f.read()
15+
16+
return abnf.parse_grammar(src, root='proto-file')
1017

11-
gram = abnf.parse_grammar(gram_src, root='proto-file')
18+
19+
##
20+
21+
22+
def _main() -> None:
23+
gram = proto3_grammar()
1224

1325
with open(os.path.join(os.path.dirname(__file__), 'tests', 'examples', 'addressbook.proto')) as f:
1426
addressbook_src = f.read()

x/specs/a2a/spec.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def _main() -> None:
2626

2727
print(spec_src)
2828

29+
from omlish import check
30+
from omlish.text import abnf
31+
from omxtra.specs.proto.abnf import proto3_grammar
32+
33+
parsed = check.not_none(proto3_grammar().parse(spec_src))
34+
parsed = abnf.only_match_rules(parsed)
35+
36+
print(parsed)
37+
2938

3039
if __name__ == '__main__':
3140
_main()

0 commit comments

Comments
 (0)