Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
Public Member Functions | Public Attributes | List of all members
apg_py.exp.exp.ApgExp Class Reference

The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp More...

Public Member Functions

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...
 

Public Attributes

 flags
 
 pattern
 
 grammar
 
 parser
 
 rules
 
 names
 
 last_index
 
 max_tree_depth
 
 max_node_hits
 

Detailed Description

The ApgExp class provides a pattern-matching engine similar to JavaScript's RegExp

Definition at line 79 of file exp.py.

Constructor & Destructor Documentation

◆ __init__()

def apg_py.exp.exp.ApgExp.__init__ (   self,
  pattern,
  flags = '' 
)

The ApgExp constructor.

Parameters
patternThe SABNF pattern to match.
flagsA 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.

Member Function Documentation

◆ 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
callbacksA 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
namesA 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
inputThe 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
namesA 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
inputThe 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.
replacementThis 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
hitsThe 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
depthThe 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
    inputThe input string or tuple of character codes.
    limitIf 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.

Member Data Documentation

◆ flags

apg_py.exp.exp.ApgExp.flags

Definition at line 118 of file exp.py.

◆ grammar

apg_py.exp.exp.ApgExp.grammar

Definition at line 133 of file exp.py.

◆ last_index

apg_py.exp.exp.ApgExp.last_index

Definition at line 141 of file exp.py.

◆ max_node_hits

apg_py.exp.exp.ApgExp.max_node_hits

Definition at line 143 of file exp.py.

◆ max_tree_depth

apg_py.exp.exp.ApgExp.max_tree_depth

Definition at line 142 of file exp.py.

◆ names

apg_py.exp.exp.ApgExp.names

Definition at line 140 of file exp.py.

◆ parser

apg_py.exp.exp.ApgExp.parser

Definition at line 134 of file exp.py.

◆ pattern

apg_py.exp.exp.ApgExp.pattern

Definition at line 132 of file exp.py.

◆ rules

apg_py.exp.exp.ApgExp.rules

Definition at line 139 of file exp.py.


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.