-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored replacement elements into base
- Loading branch information
1 parent
69f49e2
commit ee445b9
Showing
10 changed files
with
44 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
from types import EllipsisType | ||
from typing import Callable | ||
from ..EZRegex import EZRegex | ||
from .elements import base | ||
from string import Formatter | ||
|
||
|
||
def load_base(dialect, exclude=[]) -> dict: | ||
def load_base(dialect, rgroup_func: Callable[[int|str, EllipsisType], str], replace_entire_func=...) -> dict: | ||
rtn = {} | ||
for name, kwargs in base.items(): | ||
rtn[name] = EZRegex(**kwargs, dialect=dialect) | ||
|
||
# I'm creating this here, so we don't have to reimplement both of them every time | ||
def replace(string, rtn_str=True): | ||
class CustomFormatter(Formatter): | ||
def get_value(self, key, args, kwargs): | ||
return rgroup_func(key, '') | ||
|
||
string = CustomFormatter().format(string) | ||
|
||
return string if rtn_str else EZRegex(string, dialect, sanatize=False, replacement=True) | ||
|
||
|
||
rtn['replace'] = replace | ||
rtn['rgroup'] = EZRegex(rgroup_func, dialect, replacement=True) | ||
rtn['replace_entire'] = EZRegex( | ||
lambda cur=...: rgroup_func(0, cur=cur) if replace_entire_func is Ellipsis else replace_entire_func, | ||
dialect, | ||
replacement=True | ||
) | ||
|
||
return rtn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" Support for the JavaScript dialect of regular expressions""" | ||
__version__ = '1.1.1' | ||
__version__ = '0.0.1' | ||
|
||
from .elements import * | ||
from .psuedonymns import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,6 @@ | ||
# pyright: reportArgumentType = false | ||
import re | ||
from json import load | ||
|
||
# pyright: reportUndefinedVariable = false | ||
from ..base import load_base | ||
from ..EZRegex import EZRegex | ||
|
||
globals().update(load_base('javascript')) | ||
|
||
# TODO: Use https://docs.python.org/3/library/string.html#string.Formatter instead | ||
def replace(string, rtn_str=True): | ||
# Made with ezregex.org | ||
# Previous method | ||
# '{' + optional(anyCharExcept('{'), greedy=False) + group(+alphaNum) + '}' + optional(anyCharExcept('}'), greedy=False) | ||
# Current method | ||
# '{' + group(+alphaNum) + '}' | ||
r = r'\{((?:[A-Za-z0-9_])+)\}' | ||
# Convert them to something unique, then back, so we won't pick up things like {{this}} | ||
# {{this}} -> \0(this\0) -> {this} | ||
# instead of | ||
# {{this}} -> {this} -> \g<this> | ||
string = re.sub('{{', '\0\0(', string) | ||
string = re.sub('}}', '\0\0)', string) | ||
string = re.sub(r, r'\\g<\g<1>>', string) | ||
string = re.sub(r'\0\0\(', '{', string) | ||
string = re.sub(r'\0\0\)', '}', string) | ||
return string if rtn_str else EZRegex(string, 'javascript', sanatize=False, replacement=True) | ||
|
||
rgroup = EZRegex(lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>', 'javascript', replacement=True) | ||
replace_entire = EZRegex(lambda cur=...: cur + r'\g<0>', 'javascript', replacement=True) | ||
globals().update(load_base('javascript', lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
""" Support for the Perl dialect of regular expressions""" | ||
__version__ = '1.1.1' | ||
__version__ = '0.0.1' | ||
|
||
from .elements import * | ||
from .psuedonymns import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,6 @@ | ||
# pyright: reportArgumentType = false | ||
import re | ||
from json import load | ||
|
||
# pyright: reportUndefinedVariable = false | ||
from ..base import load_base | ||
from ..EZRegex import EZRegex | ||
|
||
globals().update(load_base('perl')) | ||
|
||
del UNICODE # type: ignore | ||
|
||
# TODO: Use https://docs.python.org/3/library/string.html#string.Formatter instead | ||
def replace(string, rtn_str=True): | ||
# Made with ezregex.org | ||
# Previous method | ||
# '{' + optional(anyCharExcept('{'), greedy=False) + group(+alphaNum) + '}' + optional(anyCharExcept('}'), greedy=False) | ||
# Current method | ||
# '{' + group(+alphaNum) + '}' | ||
r = r'\{((?:[A-Za-z0-9_])+)\}' | ||
# Convert them to something unique, then back, so we won't pick up things like {{this}} | ||
# {{this}} -> \0(this\0) -> {this} | ||
# instead of | ||
# {{this}} -> {this} -> \g<this> | ||
string = re.sub('{{', '\0\0(', string) | ||
string = re.sub('}}', '\0\0)', string) | ||
string = re.sub(r, r'\\g<\g<1>>', string) | ||
string = re.sub(r'\0\0\(', '{', string) | ||
string = re.sub(r'\0\0\)', '}', string) | ||
return string if rtn_str else EZRegex(string, 'perl', sanatize=False, replacement=True) | ||
|
||
rgroup = EZRegex(lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>', 'perl', replacement=True) | ||
replace_entire = EZRegex(lambda cur=...: cur + r'\g<0>', 'perl', replacement=True) | ||
globals().update(load_base('perl', lambda num_or_name, cur=...: fr'{cur}\g<{num_or_name}>')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
""" Support for the Python dialect of regular expressions""" | ||
__version__ = '0.1.0' | ||
__version__ = '1.1.0' | ||
|
||
from .elements import * | ||
from .psuedonymns import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// This goes ["regex", [things it should match], [things it shouldnt match]] | ||
[ | ||
[ | ||
|
||
], | ||
[ | ||
|
||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
# import jsonc | ||
from ezregex import python | ||
import jstyleson | ||
import ezregex as er | ||
|