Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
flags.py
Go to the documentation of this file.
1 ''' @file examples/exp/flags.py
2 @brief Demonstrates using the pattern matching flags.
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 from apg_py.lib import utilities as utils
12 
13 title = '''This example will demonstrate the pattern matching flags
14 and how they work, and specifly setting "last_index", the starting character.
15 - with the global flag, "g", set, repeated calls to exec() will find all matches
16 - with the sticky flag, "y", set, repeated calls to exec() will find all consecutive matches
17 - with the character code flag, "c", set, input and results are in tuples of integers rather than strings
18 - with the trace flag, "t", set, a parsing trace facilitates debugging patterns in input strings
19 '''
20 print()
21 print(title)
22 
23 pattern = 'start = "abc"\n'
24 input_global = '---abc===ABC---'
25 input_sticky = '---abcABC---'
26 header = 'RESULT'
27 testno = 0
28 
29 # global
30 exp = ApgExp(pattern, 'g')
31 input = input_global
32 result = exp.exec(input)
33 testno += 1
34 print('\n' + str(testno) + ') the global flag finds all matches')
35 print('input string: ' + input)
36 while(result):
37  print(header)
38  print(result)
39  result = exp.exec(input)
40 
41 # sticky
42 exp = ApgExp(pattern, 'y')
43 exp.last_index = 3
44 input = input_sticky
45 result = exp.exec(input)
46 testno += 1
47 print('\n' + str(testno) + ') the sticky flag finds all consecutive matches')
48 print('input string: ' + input)
49 while(result):
50  print(header)
51  print(result)
52  result = exp.exec(input)
53 
54 exp.last_index = 1
55 input = input_sticky
56 result = exp.exec(input)
57 testno += 1
58 print(
59  '\n' +
60  str(testno) +
61  ') with the sticky flag the match must be at exactly last_index')
62 print('input string: ' + input)
63 print('last_index: ' + str(exp.last_index))
64 print(header)
65 print(result)
66 
67 # character codes
68 exp = ApgExp(pattern, 'c')
69 input = input_global
70 result = exp.exec(utils.string_to_tuple(input))
71 testno += 1
72 print(
73  '\n' +
74  str(testno) +
75  ') with the character codes flag "c" set input and results are tuples of integers')
76 print('input string: ' + str(utils.string_to_tuple(input)))
77 print(header)
78 print(result)
79 
80 # trace
81 exp = ApgExp(pattern, 't')
82 input = input_global
83 testno += 1
84 print(
85  '\n' +
86  str(testno) +
87  ') with the trace flag "t" a trace of the parser is displayed')
88 result = exp.exec(input)
89 print('input string: ' + input)
90 print(header)
91 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.