Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
save.py
Go to the documentation of this file.
1 ''' @file examples/grammar_object/save.py
2 @brief Demonstrates how to save a grammar object for future use.
3 
4 For grammars that are used often,
5 saving the grammar object prevents the repeated and redundant
6 re-generation of the grammar object from the SABNF syntax over and over.
7 
8 Follow this with examples/grammar_object/use.py for a demonstration
9 of how to use this saved grammar object.
10 '''
11 import sys
12 import os
13 # add the current working directory to the path
14 # DO NOT MOVE THE FOLLOWING STATEMENT
15 # if using autopep8 formatter, for example, set argument '--ignore=E402'
16 sys.path.append(os.getcwd())
17 from apg_py.api.api import Api
18 
19 
21  title = '''Demonstrate how to save a grammar object to a file for later use.
22 The saved grammar object will be used in the companion example,
23 examples/grammar_object/main.py --use
24 '''
25  print()
26  print(title)
27 
28  syntax = 'abnf = tls / tbs/ trg\n'
29  syntax += 'tls = "A" "B"\n'
30  syntax += 'tbs = %d67.68\n'
31  syntax += 'trg = 1*%d128-255\n'
32  save_file = 'examples/grammar_object/abnf.py'
33  api = Api()
34  api.generate(syntax)
35  if(api.errors):
36  # report any errors
37  print('\n1) Grammar Errors')
38  print(api.display_errors())
39  else:
40  # save the grammar object for use with example
41  # examples/use_grammar_object.py
42  api.write_grammar(save_file)
43  print('grammar saved to file ' + save_file)
44  print('this file will be used by the example, examples/use_grammar_object.py')
The API class.
Definition: api.py:61
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.