The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
More...
|
def | __init__ (self, pattern, flags='') |
| The ApgExp constructor. More...
|
|
def | set_tree_depth (self, depth) |
| Limit the maximum tree depth that the parser may make. More...
|
|
def | set_node_hits (self, hits) |
| Limit the maximum number of parse tree nodes that the parser may visit. More...
|
|
def | define_udts (self, callbacks) |
| UDTs are user-written callback functions for specialized pattern matching. More...
|
|
def | include (self, names=[]) |
| Define the list of rule/UDT name phrases to be included in the matched results. More...
|
|
def | exclude (self, names=[]) |
| Define the list of rule/UDT name phrases to be excluded from the matched results. More...
|
|
def | exec (self, input) |
| Execute the pattern match. More...
|
|
def | test (self, input) |
| Same as exec() except for the return. More...
|
|
def | split (self, input, limit=0) |
| Split the input string on the matched delimiters. More...
|
|
def | replace (self, input, replacement) |
| Replace matched patterns. More...
|
|
The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp
Definition at line 79 of file exp.py.
◆ __init__()
def apg_py.exp.exp.ApgExp.__init__ |
( |
|
self, |
|
|
|
pattern, |
|
|
|
flags = '' |
|
) |
| |
The ApgExp constructor.
- Parameters
-
pattern | The SABNF pattern to match. |
flags | A string of characters specifying the operation characteristics. May be any of the following:
- c display results as lists of integer "character" codes
- g global matching mode - last_index follows the previous matched phrase
- t trace a trace of each match attempt is displayed
- y sticky similar to global except that the next match must be at exactly last_index
|
Note that:
- letters may be in any order
- multiple occurrances of letters allowed, the last occurrance wins
- the global, g, and sticky, y, flags are mutually exclusive
- any letter not in this list will raise and exception
Definition at line 83 of file exp.py.
◆ define_udts()
def apg_py.exp.exp.ApgExp.define_udts |
( |
|
self, |
|
|
|
callbacks |
|
) |
| |
UDTs are user-written callback functions for specialized pattern matching.
Callback functions must be defined for all UDTs in the SABNF grammar syntax.
- Parameters
-
callbacks | A dictionary defining one or more callbacks. Multiple calls may be made until all UDT callback are defined. callbacks = {'udt1': func[[, 'udt2': func2], etc.]} |
Definition at line 184 of file exp.py.
◆ exclude()
def apg_py.exp.exp.ApgExp.exclude |
( |
|
self, |
|
|
|
names = [] |
|
) |
| |
Define the list of rule/UDT name phrases to be excluded from the matched results.
- Parameters
-
names | A list of rule/UDT names. An empty list will include ALL rules and UDTs. Invalid names will raise an Exception. |
Definition at line 258 of file exp.py.
◆ exec()
def apg_py.exp.exp.ApgExp.exec |
( |
|
self, |
|
|
|
input |
|
) |
| |
Execute the pattern match.
Search for a match begins at last_index. (Note: last_index can be set prior to calling exec() with ApgExp.last_index = value.) If the g or y flag is set, last_index is set to the next character beyond the matched pattern or incremented by one if the matched pattern is empty. If the pattern is not matched, last_index is always set to 0.
- Parameters
-
input | The input as a string or tuple of character codes if the "c" flag is set. |
- Returns
- Returns the result object if pattern is matched. None otherwise.
Definition at line 290 of file exp.py.
◆ include()
def apg_py.exp.exp.ApgExp.include |
( |
|
self, |
|
|
|
names = [] |
|
) |
| |
Define the list of rule/UDT name phrases to be included in the matched results.
- Parameters
-
names | A list of rule/UDT names. An empty list will include ALL rules and UDTs. Invalid names will raise an Exception. |
Definition at line 207 of file exp.py.
◆ replace()
def apg_py.exp.exp.ApgExp.replace |
( |
|
self, |
|
|
|
input, |
|
|
|
replacement |
|
) |
| |
Replace matched patterns.
If a pattern match is found in "input" it will be replaced with "replacement". Works similar to the JavaScript String.replace() function. If the "g" or "y" flags are set, all matched patterns are replaced. Otherwise, only the first match is replaced.
- Parameters
-
input | The string to look for pattern matches in. If the "c" flag is set, input must be a tuple of integers. Otherwise, it is a string. |
replacement | This may be a simple replacement string, a complex replacement string with special characters or a function that returns the replacement string. If the "c" flag is not set, replacement must be a string, possibly with special characters or a function that returns a string. Special string characters are:
- $$ substitute $
- $` substitute the matched left context
- $& substitute the matched pattern itself
- $' substitute the matched right context
- ${name} substitue the matched rule/UDT name(case insensitive), note that if this rule has no match an empty string will be used.
The function must have the prototype
- fn(input, result) where input is the original input string and result is the pattern matching result object. The function must return a string
If the "c" flag is set, replacement must be a tuple of integers or a function that returns a tuple of integers. In this case there are no special characters comparable to the string special characters. However, since the function gets the result as an argument, it can be used for the same purpose. The function must have the prototype:
- fn(input, result) where input is the original input tuple and result is the pattern matching result object. The function must return a tuple of integers.
|
- Returns
- Returns the input string/tuple "input" with one or all matched patterns replaced with the replacement string, tuple or function.
Definition at line 482 of file exp.py.
◆ set_node_hits()
def apg_py.exp.exp.ApgExp.set_node_hits |
( |
|
self, |
|
|
|
hits |
|
) |
| |
Limit the maximum number of parse tree nodes that the parser may visit.
- Parameters
-
hits | The maximum allowed number of node hits the parser can make. If the parser exceeds this limit an exception is raised. |
Definition at line 177 of file exp.py.
◆ set_tree_depth()
def apg_py.exp.exp.ApgExp.set_tree_depth |
( |
|
self, |
|
|
|
depth |
|
) |
| |
Limit the maximum tree depth that the parser may make.
- Parameters
-
depth | The maximum allowed tree node depth. If the parser exceeds this limit an exception is raised. |
Definition at line 170 of file exp.py.
◆ split()
def apg_py.exp.exp.ApgExp.split |
( |
|
self, |
|
|
|
input, |
|
|
|
limit = 0 |
|
) |
| |
Split the input string on the matched delimiters.
The ApgExp pattern defines the delimiters. Works similar to the JavaScript String.split() function. All flags except the character code flag "c" are ignored. If the "c" flag is set, substitute "tuple of character codes" for string.
- if the input string is empty, the output list contains a single empty string
- if the pattern matches the entire string, the output list contains a single empty string.
- if no pattern matches are found in the input string, the output list contains a single string which is a copy of the input string.
- if the pattern finds multiple matches, the output list contains a each of the strings between the matches
- if the pattern matches the empty string, the output will be a list of the single characters.
- Parameters
-
input | The input string or tuple of character codes. |
limit | If limit > 0 only limit delimiters are matched. The trailing string suffix, if any, is ignored. |
- Returns
- Returns a list of strings or character code tuples.
Definition at line 402 of file exp.py.
◆ test()
def apg_py.exp.exp.ApgExp.test |
( |
|
self, |
|
|
|
input |
|
) |
| |
Same as exec() except for the return.
- Returns
- Returns True if a pattern match is found, False otherwise.
Definition at line 361 of file exp.py.
◆ flags
apg_py.exp.exp.ApgExp.flags |
◆ grammar
apg_py.exp.exp.ApgExp.grammar |
◆ last_index
apg_py.exp.exp.ApgExp.last_index |
◆ max_node_hits
apg_py.exp.exp.ApgExp.max_node_hits |
◆ max_tree_depth
apg_py.exp.exp.ApgExp.max_tree_depth |
◆ names
apg_py.exp.exp.ApgExp.names |
◆ parser
apg_py.exp.exp.ApgExp.parser |
◆ pattern
apg_py.exp.exp.ApgExp.pattern |
◆ rules
apg_py.exp.exp.ApgExp.rules |
The documentation for this class was generated from the following file:
Python APG, Version 1.0, is licensed under the
2-Clause BSD License,
an
Open Source Initiative Approved License.