You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromhumreimport*compile(
# optional negative or positive sign:optional(noncap_group(either(PLUS_SIGN, '-'))),
# whole number section:noncap_group(either(
# number with commas:noncap_group(between(1, 3, DIGIT), one_or_more(noncap_group(',', exactly(3, DIGIT)))),
# number without commas:one_or_more(DIGIT)
)),
# fractional number section (optional)optional(noncap_group(PERIOD, one_or_more(DIGIT)))
)
The main change I propose is to broaden the general type of a component from str to some abstract component type Part, and then to process these into strings in humre.compile.
This allows removing the noncap_group markers, and replacing PLUS_SIGN and PERIOD back to regular literals. That way the writer can forget how regular expressions are written!
Also, I think it is a lot more "human readable" to use variable names rather than nested comments.
What do you think?
In general, I suggest redoing the group functions to be just one function:
from __future__ importannotationsfromtypingimportTypeAliasclassPart:
def__init__(self, x: Part|str):
self.escaped=x.escapedifisinstance(x, Part) elseescape(x)
PartLike: TypeAlias=Part|str|tuple['PartLike', ...]
defgroup(*xs: PartLike,
minimum: None|int=None,
maximum: None|int=None,
capture: bool=False
) ->Part:
iflen(xs) ==0:
returnPart('')
iflen(xs) ==1andminimumismaximumisNoneandnotcapture:
x=xs[0]
ifisinstance(x, tuple):
returngroup(*x)
returnPart(x)
parts= (group(x) forxinxs)
inner=''.join(part.escapedforpartinparts)
# produce group from the part strings, taking into account flags.returnPart('('+inner+')')
etc.
The text was updated successfully, but these errors were encountered:
Cool project!
I think there are some simplifications that would lead to a nicer language.
Consider recasting the main example as:
rather than
The main change I propose is to broaden the general type of a component from
str
to some abstract component typePart
, and then to process these into strings inhumre.compile
.This allows removing the
noncap_group
markers, and replacing PLUS_SIGN and PERIOD back to regular literals. That way the writer can forget how regular expressions are written!Also, I think it is a lot more "human readable" to use variable names rather than nested comments.
What do you think?
In general, I suggest redoing the group functions to be just one function:
etc.
The text was updated successfully, but these errors were encountered: