hydra.regex module

A model for Hydra’s translingual regular-expression syntax: the abstract syntax tree (AST) into which hydra.parse.regex parses a pattern and from which hydra.print.regex (and the per-dialect hydra.print.<dialect>.regex renderers) emit target syntax. Covers the minimal core defined in docs/specification/regex.md (literals, character classes, ., quantifiers, alternation, anchors, grouping). See issue #567.

class hydra.regex.Atom

Bases: object

AtomLiteral | AtomAny | AtomAnchorStart | AtomAnchorEnd | AtomGroup | AtomClass

ANCHOR_END = Name(value='anchorEnd')
ANCHOR_START = Name(value='anchorStart')
ANY = Name(value='any')
CLASS = Name(value='class')
GROUP = Name(value='group')
LITERAL = Name(value='literal')
TYPE_ = Name(value='hydra.regex.Atom')
class hydra.regex.AtomAnchorEnd

Bases: object

The $ anchor; matches the empty string at the end of input.

class hydra.regex.AtomAnchorStart

Bases: object

The ^ anchor; matches the empty string at the start of input.

class hydra.regex.AtomAny

Bases: object

The . metacharacter; matches any single character, INCLUDING newline (unlike most host engines, whose native . excludes newline). To exclude newline, write [^n] explicitly.

class hydra.regex.AtomClass(value: T)

Bases: Node[CharacterClass]

A character class, [ … ]; matches any single character in (or, if negated, not in) the set.

class hydra.regex.AtomGroup(value: T)

Bases: Node[Alternation]

A parenthesized sub-expression, ( … ); groups an alternation for quantification.

class hydra.regex.AtomLiteral(value: T)

Bases: Node[int]

A literal character, matched exactly. The character is a Unicode scalar value in the range [U+0000, U+10FFFF] (surrogates excluded), held as an int32 code point (the range fits in signed 32-bit, and int32 matches Hydra’s string/parser code-point representation). In concrete syntax, a metacharacter is written escaped (e.g. .); escaping is purely a concrete-syntax concern, so the AST holds the bare code point.

class hydra.regex.CharacterClass(negated: Annotated[bool, 'True for a negated class ([^ ... ]), which matches any character NOT listed.'], items: Annotated[Sequence[ClassItem], 'The class members: individual characters and/or character ranges. Must be non-empty; the empty class [] and negated-empty class [^] are excluded from the core (they are not in the POSIX/PCRE/ECMA intersection). hydra.parse.regex rejects an empty class.'])

Bases: object

A bracketed character class, [ … ] or [^ … ].

class Builder(_negated: 'bool' = None, _items: 'Sequence[ClassItem]' = None)

Bases: object

build()
items(items)
negated(negated)
ITEMS = Name(value='items')
NEGATED = Name(value='negated')
TYPE_ = Name(value='hydra.regex.CharacterClass')
static builder()
items: Annotated[Sequence[ClassItem], 'The class members: individual characters and/or character ranges. Must be non-empty; the empty class [] and negated-empty class [^] are excluded from the core (they are not in the POSIX/PCRE/ECMA intersection). hydra.parse.regex rejects an empty class.']
negated: Annotated[bool, 'True for a negated class ([^ ... ]), which matches any character NOT listed.']
with_items(items)
with_negated(negated)
class hydra.regex.CharacterRange(from_: Annotated[int, 'The first character of the range (inclusive), as an int32 Unicode scalar value.'], to: Annotated[int, 'The last character of the range (inclusive), as an int32 Unicode scalar value.'])

Bases: object

An inclusive range of characters within a character class, e.g. a-z.

class Builder(_from_: 'int' = None, _to: 'int' = None)

Bases: object

build()
from_(from_)
to(to)
FROM = Name(value='from')
TO = Name(value='to')
TYPE_ = Name(value='hydra.regex.CharacterRange')
static builder()
from_: Annotated[int, 'The first character of the range (inclusive), as an int32 Unicode scalar value.']
to: Annotated[int, 'The last character of the range (inclusive), as an int32 Unicode scalar value.']
with_from_(from_)
with_to(to)
class hydra.regex.ClassItem

Bases: object

ClassItemCharacter | ClassItemRange

CHARACTER = Name(value='character')
RANGE = Name(value='range')
TYPE_ = Name(value='hydra.regex.ClassItem')
class hydra.regex.ClassItemCharacter(value: T)

Bases: Node[int]

A single character (int32 Unicode scalar value) in the class.

class hydra.regex.ClassItemRange(value: T)

Bases: Node[CharacterRange]

An inclusive range of characters, e.g. a-z.

class hydra.regex.Quantified(atom: Annotated[Atom, 'The atom being quantified.'], quantifier: Annotated[Quantifier, "The quantifier; use 'one' for an unquantified atom."])

Bases: object

An atom together with an optional quantifier applied to it.

ATOM = Name(value='atom')
class Builder(_atom: 'Atom' = None, _quantifier: 'Quantifier' = None)

Bases: object

atom(atom)
build()
quantifier(quantifier)
QUANTIFIER = Name(value='quantifier')
TYPE_ = Name(value='hydra.regex.Quantified')
atom: Annotated[Atom, 'The atom being quantified.']
static builder()
quantifier: Annotated[Quantifier, "The quantifier; use 'one' for an unquantified atom."]
with_atom(atom)
with_quantifier(quantifier)
class hydra.regex.Quantifier

Bases: object

QuantifierOne | QuantifierZeroOrOne | QuantifierZeroOrMore | QuantifierOneOrMore | QuantifierExactly | QuantifierAtLeast | QuantifierRange

AT_LEAST = Name(value='atLeast')
EXACTLY = Name(value='exactly')
ONE = Name(value='one')
ONE_OR_MORE = Name(value='oneOrMore')
RANGE = Name(value='range')
TYPE_ = Name(value='hydra.regex.Quantifier')
ZERO_OR_MORE = Name(value='zeroOrMore')
ZERO_OR_ONE = Name(value='zeroOrOne')
class hydra.regex.QuantifierAtLeast(value: T)

Bases: Node[int]

The {n,} quantifier; matches at least n occurrences.

class hydra.regex.QuantifierExactly(value: T)

Bases: Node[int]

The {n} quantifier; matches exactly n occurrences.

class hydra.regex.QuantifierOne

Bases: object

No quantifier; matches exactly one occurrence.

class hydra.regex.QuantifierOneOrMore

Bases: object

The + quantifier; matches one or more occurrences.

class hydra.regex.QuantifierRange(min: Annotated[int, 'The minimum number of occurrences (inclusive).'], max: Annotated[int, 'The maximum number of occurrences (inclusive).'])

Bases: object

The bounds of a {n,m} quantifier: between min and max (inclusive) occurrences.

class Builder(_min: 'int' = None, _max: 'int' = None)

Bases: object

build()
max(max)
min(min)
MAX = Name(value='max')
MIN = Name(value='min')
TYPE_ = Name(value='hydra.regex.QuantifierRange')
static builder()
max: Annotated[int, 'The maximum number of occurrences (inclusive).']
min: Annotated[int, 'The minimum number of occurrences (inclusive).']
with_max(max)
with_min(min)
class hydra.regex.QuantifierRange_(value: T)

Bases: Node[QuantifierRange]

The {n,m} quantifier; matches between n and m (inclusive) occurrences.

class hydra.regex.QuantifierZeroOrMore

Bases: object

The * quantifier; matches zero or more occurrences.

class hydra.regex.QuantifierZeroOrOne

Bases: object

The ? quantifier; matches zero or one occurrence.