Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
rules.py
Go to the documentation of this file.
1 ''' @file examples/exp/rules.py
2 @brief Demonstrates including or excluding specific pattern rules in the result.
3 '''
4 import sys
5 import os
6 # add the current working directory to the path
7 # DO NOT MOVE THE FOLLOWING STATEMENT
8 # if using autopep8 formatter, for example, set argument '--ignore=E402'
9 sys.path.append(os.getcwd())
10 from apg_py.exp.exp import ApgExp
11 
12 title = '''This example will demonstrate how to include
13 or exclude specific rule names in the result.
14 '''
15 print()
16 print(title)
17 
18 pattern = '''phone-number = form1 / form2 / form3
19 form1 = open-paren area-code close-paren office-code hyphen subscriber
20 form2 = area-code hyphen office-code hyphen subscriber
21 form3 = area-code office-code subscriber
22 area-code = 3digit
23 office-code = 3digit
24 subscriber = 4digit
25 open-paren = %d40
26 close-paren = %d41
27 hyphen = %d45
28 digit = %d48-57
29 '''
30 print()
31 print('PATTERN')
32 print(pattern)
33 header = 'RESULT'
34 testno = 0
35 
36 # the full result
37 exp = ApgExp(pattern)
38 exp.include()
39 input = '(999)555-1234'
40 result = exp.exec(input)
41 testno += 1
42 print('\n' + str(testno) + ') Form 1 - the result with all rules.')
43 print(' Note the abundance of uninteresting information in the result.')
44 print('input string: ' + input)
45 print(header)
46 print(result)
47 
48 # just the important information
49 exp = ApgExp(pattern)
50 exp.include(['area-code', 'office-code', 'subscriber'])
51 input = '999-555-1234'
52 result = exp.exec(input)
53 testno += 1
54 print('\n' + str(testno) + ') Form 2, include only the telephone number components.')
55 print('input string: ' + input)
56 print(header)
57 print(result)
58 
59 # use exclude()
60 exp = ApgExp(pattern)
61 exp.exclude(['open-paren', 'close-paren', 'hyphen',
62  'digit', 'form1', 'form2', 'form3'])
63 input = '8005556666'
64 result = exp.exec(input)
65 testno += 1
66 print('\n' + str(testno) + ') Form 3 - using exclude().')
67 print('input string: ' + input)
68 print(header)
69 print(result)
The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
Definition: exp.py:79
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.