Skip to content

Commit

Permalink
implemented #10
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofelletti committed Mar 22, 2022
2 parents dc62d98 + c3d8c75 commit ac19e41
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 12 additions & 3 deletions pyregexp/pyrser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Union, Callable
from functools import lru_cache
import itertools
import math
from .lexer import Lexer
from .tokens import *
Expand Down Expand Up @@ -61,7 +62,7 @@ def next_tkn(without_consuming: bool = False) -> Union[Token, None]:
def parse_re() -> RE:
return RE(parse_re_seq())

def parse_re_seq(capturing: bool = True, group_name: str = 'default') -> Union[OrNode, GroupNode]:
def parse_re_seq(capturing: bool = True, group_name: str = None) -> Union[OrNode, GroupNode]:
match_start, match_end = False, False
if type(curr_tkn) is Start or type(curr_tkn) is Circumflex:
next_tkn()
Expand All @@ -86,7 +87,12 @@ def parse_re_seq(capturing: bool = True, group_name: str = 'default') -> Union[O

return node

def parse_group(capturing: bool = True, group_name: str = 'default') -> GroupNode:
def parse_group(capturing: bool = True, group_name: str = None) -> GroupNode:
nonlocal groups_counter

if group_name is None:
group_name = "Group " + str(next(groups_counter))

elements = deque() # holds the children of the GroupNode

while curr_tkn is not None and not isinstance(curr_tkn, OrToken) and \
Expand Down Expand Up @@ -229,7 +235,8 @@ def parse_inner_el() -> RangeElement:
return RangeElement(match_str="".join(sorted(set(match_str))), is_positive_logic=positive_logic)

def parse_el() -> Union[Element, OrNode, GroupNode]:
group_name = "default"
group_name: Union[str,None]
group_name = None
if isinstance(curr_tkn, ElementToken):
return Element(match_ch=curr_tkn.char)
elif isinstance(curr_tkn, Wildcard):
Expand Down Expand Up @@ -277,6 +284,8 @@ def parse_group_name() -> str:
raise Exception('Unexpected empty named group name.')
next_tkn() # consumes '>'
return group_name

groups_counter = itertools.count(start=1)

curr_tkn = None
next_tkn = next_tkn_initializer(re)
Expand Down
12 changes: 9 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from distutils.core import setup
from setuptools import setup

from pathlib import Path
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()

setup(
name='pyregexp',
packages=['pyregexp'],
version='0.1.6',
version='0.1.7',
license='MIT',
description='Simple regex library',
long_description=long_description,
long_description_content_type='text/markdown',
author='Lorenzo Felletti',
url='https://github.com/lorenzofelletti/pyregex',
download_url='https://github.com/lorenzofelletti/pyregex/archive/v0.1.6.tar.gz',
download_url='https://github.com/lorenzofelletti/pyregex/archive/v0.1.7.tar.gz',
keywords=['Regex', 'RegExp', 'Engine'],
install_requires=[],
classifiers=[
Expand Down

0 comments on commit ac19e41

Please sign in to comment.