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

core-elements.mddocs/

Core Parser Elements

Fundamental building blocks for creating parsing expressions. These classes form the foundation of all pyparsing grammars, providing terminal elements that match specific text patterns and expression combinators that define how elements relate to each other.

Required imports for type annotations:

from typing import Union, Optional, Generator
from pyparsing import ParserElement, ParseResults, Token, ParseExpression

Capabilities

Base Classes

Abstract base classes that define the core parsing interface and provide common functionality for all parser elements.

class ParserElement:
    """Abstract base class for all parsing elements."""
    
    def parse_string(self, instring: str, parse_all: bool = False) -> ParseResults:
        """Parse a string and return results."""
    
    def parse_file(self, file_or_filename, parse_all: bool = False) -> ParseResults:
        """Parse contents of a file."""
    
    def search_string(self, instring: str, maxMatches: int = None) -> list:
        """Search for matches within a string."""
    
    def scan_string(self, instring: str, maxMatches: int = None) -> Generator:
        """Generator that yields matches and locations."""
    
    def transform_string(self, instring: str) -> str:
        """Transform string by replacing matches."""
    
    def set_results_name(self, name: str) -> ParserElement:
        """Assign a name to parsed results."""
    
    def set_parse_action(self, *fns) -> ParserElement:
        """Assign parse actions to be called when successfully parsed."""
    
    def add_parse_action(self, *fns) -> ParserElement:
        """Add parse actions to existing ones."""
    
    def set_fail_action(self, fn) -> ParserElement:
        """Set action to be called if parsing fails."""
    
    def set_debug(self, flag: bool = True) -> ParserElement:
        """Enable/disable debug output for this element."""
    
    def copy(self) -> ParserElement:
        """Create a copy of this parser element."""
    
    def __add__(self, other) -> ParserElement:
        """Implement + operator for And combinations."""
    
    def __or__(self, other) -> ParserElement:
        """Implement | operator for MatchFirst combinations."""
    
    def __xor__(self, other) -> ParserElement:
        """Implement ^ operator for Or combinations."""
    
    def __and__(self, other) -> ParserElement:
        """Implement & operator for Each combinations."""
    
    def __mul__(self, other) -> ParserElement:
        """Implement * operator for repetition."""
    
    def __pos__(self) -> ParserElement:
        """Implement unary + operator for OneOrMore."""
    
    def __invert__(self) -> ParserElement:
        """Implement ~ operator for NotAny."""
class ParseExpression(ParserElement):
    """Base class for parser expressions that contain other parser elements."""
    
    def __init__(self, exprs: list, savelist: bool = False): ...
    
    def append(self, other: ParserElement) -> ParserElement:
        """Add another element to this expression."""
    
    def ignore(self, other: ParserElement) -> ParserElement:
        """Ignore the specified expression while parsing."""
class Token(ParserElement):
    """Base class for terminal parsing elements."""
    
    def __init__(self): ...

Terminal Elements

Parser elements that match specific text patterns and consume input directly.

class Literal(Token):
    """Match an exact literal string."""
    
    def __init__(self, matchString: str): ...

class CaselessLiteral(Literal):
    """Match a literal string, ignoring case."""
    
    def __init__(self, matchString: str): ...
class Word(Token):
    """Match words composed of specified character sets."""
    
    def __init__(self, 
                 init_chars: str = "", 
                 body_chars: str = None, 
                 min: int = 1, 
                 max: int = 0,
                 exact: int = 0,
                 as_keyword: bool = False,
                 exclude_chars: str = None,
                 *,
                 # Backward compatibility parameters
                 initChars: str = "",
                 bodyChars: str = None,
                 asKeyword: bool = False,
                 excludeChars: str = None): ...

Usage example:

# Match Python identifiers
identifier = Word(alphas + "_", alphanums + "_")

# Match integers
integer = Word(nums)

# Match exactly 3 digits
three_digits = Word(nums, exact=3)

# Using keyword parameters
float_num = Word(nums, body_chars=nums + ".", min=1)
class Char(Token):
    """Match a single character from specified set."""
    
    def __init__(self, charset: str, asKeyword: bool = False, excludeChars: str = None): ...
class Keyword(Token):
    """Match a specific keyword with word boundaries."""
    
    def __init__(self, matchString: str, ident_chars: str = None, caseless: bool = False): ...

class CaselessKeyword(Keyword):
    """Match a keyword ignoring case."""
    
    def __init__(self, matchString: str, ident_chars: str = None): ...
class Regex(Token):
    """Match using regular expressions."""
    
    def __init__(self, pattern: str, flags: int = 0, asGroupList: bool = False, asMatch: bool = False): ...
class QuotedString(Token):
    """Match quoted strings with escape character handling."""
    
    def __init__(self, 
                 quoteChar: str, 
                 escChar: str = None, 
                 escQuote: str = None,
                 multiline: bool = False, 
                 unquoteResults: bool = True,
                 endQuoteChar: str = None,
                 convertWhitespaceEscapes: bool = True): ...
class CharsNotIn(Token):
    """Match characters not in the specified set."""
    
    def __init__(self, notChars: str, min: int = 1, max: int = None, exact: int = None): ...
class White(Token):
    """Match whitespace characters."""
    
    def __init__(self, ws: str = " \t\r\n", min: int = 1, max: int = None, exact: int = None): ...
class Empty(Token):
    """Always matches without consuming input."""
    
    def __init__(self): ...

Expression Combinators

Classes that combine multiple parser elements to create complex parsing expressions.

class And(ParseExpression):
    """Match all expressions in sequence."""
    
    def __init__(self, exprs: list, savelist: bool = True): ...

class Or(ParseExpression):
    """Match any expression, longest match wins."""
    
    def __init__(self, exprs: list, savelist: bool = False): ...

class MatchFirst(ParseExpression):
    """Match first successful expression."""
    
    def __init__(self, exprs: list, savelist: bool = False): ...

class Each(ParseExpression):
    """Match all expressions in any order."""
    
    def __init__(self, exprs: list, savelist: bool = True): ...

Usage example:

# Sequential matching with And (or + operator)
greet = Word(alphas) + "," + Word(alphas) + "!"

# Alternative matching with MatchFirst (or | operator)  
number_word = "one" | "two" | "three" | Word(nums)

# Longest match with Or (or ^ operator)
floating_point = Regex(r'\d+\.\d+') ^ Word(nums)

# All expressions in any order with Each (or & operator)
attrs = "width" + "=" + Word(nums) & "height" + "=" + Word(nums)

Position-Based Elements

Elements that match based on position within the input rather than consuming text.

class PositionToken(Token):
    """Base class for position-based matching."""
    
    def __init__(self): ...

class LineStart(PositionToken):
    """Match at start of line."""
    
    def __init__(self): ...

class LineEnd(PositionToken):
    """Match at end of line."""
    
    def __init__(self): ...

class StringStart(PositionToken):
    """Match at start of string."""
    
    def __init__(self): ...

class StringEnd(PositionToken):
    """Match at end of string."""
    
    def __init__(self): ...

class WordStart(PositionToken):
    """Match at start of word boundary."""
    
    def __init__(self): ...

class WordEnd(PositionToken):
    """Match at end of word boundary."""
    
    def __init__(self): ...

class AtLineStart(PositionToken):
    """Assertion that position is at line start."""
    
    def __init__(self): ...

class AtStringStart(PositionToken):
    """Assertion that position is at string start."""
    
    def __init__(self): ...

class GoToColumn(PositionToken):
    """Match at specific column position."""
    
    def __init__(self, colno: int): ...

Special Elements

Specialized elements for advanced parsing scenarios.

class Forward(ParserElement):
    """Forward declaration for recursive grammars."""
    
    def __init__(self, other: ParserElement = None): ...
    
    def __lshift__(self, other: ParserElement) -> ParserElement:
        """Set the forward reference using << operator."""

class SkipTo(ParseElementEnhance):
    """Skip to specified expression."""
    
    def __init__(self, 
                 other: ParserElement, 
                 include: bool = False, 
                 ignore: ParserElement = None,
                 failOn: ParserElement = None): ...

class NoMatch(Token):
    """Never matches - useful for debugging."""
    
    def __init__(self): ...

Usage example:

# Forward reference for recursive grammar
expr = Forward()
term = Word(nums) | "(" + expr + ")"
expr <<= term + ZeroOrMore(("+" | "-") + term)

# Skip to closing tag
content = SkipTo("</body>")

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