CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pyparsing

A Python parsing module providing an alternative approach to creating and executing simple grammars

Pending
Overview
Eval results
Files

enhancement.mddocs/

Expression Enhancement

Modifiers and enhancers that control repetition, optionality, lookahead/lookbehind, and token transformation. These classes wrap other parser elements to create sophisticated parsing behavior while maintaining the compositional design of pyparsing.

Required imports for type annotations:

from typing import Union, Optional
from pyparsing import ParserElement, ParseElementEnhance, TokenConverter

Capabilities

Repetition and Optionality

Elements that control how many times an expression should match.

class Optional(ParseElementEnhance):
    """Make an expression optional."""
    
    def __init__(self, expr: ParserElement, default: str = ""): ...

# Alias for Optional
Opt = Optional
class ZeroOrMore(ParseElementEnhance):
    """Match zero or more repetitions of an expression."""
    
    def __init__(self, 
                 expr: Union[str, ParserElement], 
                 stop_on: Optional[Union[ParserElement, str]] = None,
                 *,
                 stopOn: Optional[Union[ParserElement, str]] = None): ...

class OneOrMore(ParseElementEnhance):
    """Match one or more repetitions of an expression."""
    
    def __init__(self, 
                 expr: Union[str, ParserElement], 
                 stop_on: Optional[Union[ParserElement, str]] = None,
                 *,
                 stopOn: Optional[Union[ParserElement, str]] = None): ...

Usage examples:

# Optional element with default
name = Optional(Word(alphas), "Anonymous")

# Repetition patterns
numbers = OneOrMore(Word(nums))
optional_numbers = ZeroOrMore(Word(nums))

# Stop repetition on specific pattern
items = ZeroOrMore(Word(alphas), stopOn=Literal(";"))

Lookahead and Lookbehind

Assertion elements that check for patterns without consuming input.

class FollowedBy(ParseElementEnhance):
    """Positive lookahead assertion."""
    
    def __init__(self, expr: ParserElement): ...

class PrecededBy(ParseElementEnhance):
    """Positive lookbehind assertion."""
    
    def __init__(self, expr: ParserElement, retreat: int = None): ...

class NotAny(ParseElementEnhance):
    """Negative lookahead assertion."""
    
    def __init__(self, expr: ParserElement): ...

Usage examples:

# Match word only if followed by punctuation
word_before_punct = Word(alphas) + FollowedBy(oneOf(". , ; !"))

# Match word only if preceded by whitespace
word_after_space = PrecededBy(White()) + Word(alphas)

# Match word only if not followed by equals sign
var_not_assignment = Word(alphas) + NotAny("=")

Token Conversion

Classes that modify or transform parsed tokens.

class TokenConverter(ParseElementEnhance):
    """Base class for token conversion classes."""
    
    def __init__(self, expr: ParserElement, savelist: bool = False): ...

class Combine(TokenConverter):
    """Combine parsed tokens into a single string."""
    
    def __init__(self, expr: ParserElement, joinString: str = "", adjacent: bool = True): ...

class Group(TokenConverter):
    """Group results into a list."""
    
    def __init__(self, expr: ParserElement): ...

class Dict(TokenConverter):
    """Convert results to dictionary."""
    
    def __init__(self, expr: ParserElement): ...

class Suppress(TokenConverter):
    """Suppress tokens from results."""
    
    def __init__(self, expr: ParserElement): ...

Usage examples:

# Combine tokens into single string
ip_addr = Combine(Word(nums) + "." + Word(nums) + "." + Word(nums) + "." + Word(nums))

# Group related tokens
point = Group("(" + Word(nums) + "," + Word(nums) + ")")

# Create dictionary from key-value pairs
attr = Dict(Word(alphas) + "=" + QuotedString('"'))

# Suppress punctuation from results
expr = Word(alphas) + Suppress(",") + Word(alphas)

Location and Context

Elements that add position information or context to results.

class Located(TokenConverter):
    """Add location information to results."""
    
    def __init__(self, expr: ParserElement): ...

class Tag(TokenConverter):
    """Tag results with metadata."""
    
    def __init__(self, expr: ParserElement, tag_name: str): ...

class IndentedBlock(ParseElementEnhance):
    """Parse indented blocks of text."""
    
    def __init__(self, blockStatementExpr: ParserElement, indentStack: list = None, indent: bool = True): ...

Usage examples:

# Add line/column info to results
located_word = Located(Word(alphas))

# Tag results for identification
tagged_expr = Tag(Word(nums), "number")

# Parse Python-style indented blocks
stmt = Word(alphas) + ":"
block = IndentedBlock(OneOrMore(Word(alphas)))
python_block = stmt + block

Pattern Matching

Advanced pattern matching capabilities.

class CloseMatch(Token):
    """Match strings within specified edit distance."""
    
    def __init__(self, match_string: str, maxMismatches: int = 1): ...

Usage example:

# Match strings with up to 2 character differences
fuzzy_match = CloseMatch("hello", maxMismatches=2)
# Will match "hello", "helo", "helloo", "hallo", etc.

Parse Actions and Conditions

Elements that enable conditional parsing and action execution.

class OnlyOnce(object):
    """Wrapper to ensure parse action runs only once."""
    
    def __init__(self, methodCall: callable): ...
    
    def __call__(self, s: str, loc: int, tokens: ParseResults): ...

Usage example:

# Ensure initialization happens only once
init_action = OnlyOnce(lambda: print("Initializing parser"))
parser = Word(alphas).set_parse_action(init_action)

Specialized Enhancement

Specialized enhancement classes for specific parsing scenarios.

class ParseElementEnhance(ParserElement):
    """Base class for elements that enhance other parsing elements."""
    
    def __init__(self, expr: ParserElement, savelist: bool = False): ...
    
    def ignore(self, other: ParserElement) -> ParserElement:
        """Ignore specified expression while parsing."""
    
    def suppress_first(self) -> ParserElement:
        """Suppress first matching element from results."""
    
    def suppress_last(self) -> ParserElement:
        """Suppress last matching element from results."""

Utility Functions for Enhancement

Functions that create commonly used enhancement patterns.

def ungroup(expr: ParserElement) -> ParserElement:
    """Remove grouping from expression results."""

def original_text_for(expr: ParserElement, asString: bool = True) -> ParserElement:
    """Return original text instead of parsed tokens."""

Usage examples:

# Remove grouping from complex expression
grouped_items = Group(delimited_list(Word(alphas)))
ungrouped_items = ungroup(grouped_items)

# Get original text instead of parsed tokens
number_text = original_text_for(Word(nums) + Optional("." + Word(nums)))

Install with Tessl CLI

npx tessl i tessl/pypi-pyparsing

docs

common-expressions.md

core-elements.md

enhancement.md

exceptions.md

helpers.md

index.md

testing-debugging.md

tile.json