aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-09-01 00:39:54 -0400
committerBobby <[email protected]>2022-09-01 00:39:54 -0400
commit0fa612e282a68b9d87d05b851d373852813ea7c5 (patch)
treedd9caca26540b6154ad15cb2ab5c880640b44fd6
parent9c6c9c4570a0c583d93e197d3414c35315afa5fb (diff)
downloadedify-0fa612e282a68b9d87d05b851d373852813ea7c5.tar.xz
edify-0fa612e282a68b9d87d05b851d373852813ea7c5.zip
Fix liniting errors
-rw-r--r--src/edify/builder/builder.py73
-rw-r--r--src/edify/builder/errors.py12
-rw-r--r--src/edify/builder/helpers/core.py8
-rw-r--r--src/edify/builder/helpers/regex_vars.py2
-rw-r--r--src/edify/builder/helpers/t.py3
5 files changed, 38 insertions, 60 deletions
diff --git a/src/edify/builder/builder.py b/src/edify/builder/builder.py
index d22ff24..a38877a 100644
--- a/src/edify/builder/builder.py
+++ b/src/edify/builder/builder.py
@@ -1,9 +1,24 @@
import re
-from sys import _current_frames
-from .helpers.core import create_stack_frame, assertion, escape_special
-from .helpers.t import t
+
+from .errors import can_not_end_while_building_root_exp
+from .errors import cannot_create_duplicate_named_group
+from .errors import cannot_define_start_after_end
+from .errors import end_input_already_defined
+from .errors import invalid_total_capture_groups_index
+from .errors import must_be_a_string
+from .errors import must_be_integer_greater_than_zero
+from .errors import must_be_one_character
+from .errors import must_be_positive_integer
+from .errors import name_not_valid
+from .errors import named_group_does_not_exist
+from .errors import start_input_already_defined
+from .errors import unable_to_quantify
+from .helpers.core import assertion
+from .helpers.core import create_stack_frame
+from .helpers.core import escape_special
from .helpers.regex_vars import named_group_regex
-from .errors import *
+from .helpers.t import t
+
class RegexBuilder:
"""Regular Expression Builder Class.
@@ -11,7 +26,6 @@ class RegexBuilder:
state = {}
-
def __init__(self):
self.state = {
'has_defined_start': False,
@@ -29,45 +43,35 @@ class RegexBuilder:
'total_capture_groups': 0,
}
-
def allow_multiple_matches(self):
self.state['stack']['flags']['g'] = True
-
def sticky(self):
self.state['stack']['flags']['y'] = True
-
def line_by_line(self):
self.state['stack']['flags']['m'] = True
-
def case_insensitive(self):
self.state['stack']['flags']['i'] = True
-
def unicode(self):
self.state['stack']['flags']['u'] = True
-
def single_line(self):
self.state['stack']['flags']['s'] = True
-
def get_current_frame(self):
return self.state['stack']
-
def get_current_element_array(self):
return self.get_current_frame()['elements']
-
def match_element(self, type_fn):
current_element_array = self.get_current_element_array()
current_element_array.append(self.apply_quantifier(type_fn))
return self
-
def apply_quantifier(self, element):
current_frame = self.get_current_frame()
if current_frame['quantifier'] is not None:
@@ -76,13 +80,11 @@ class RegexBuilder:
return wrapped
return element
-
def frame_creating_element(self, type_fn):
new_frame = create_stack_frame(type_fn)
self.state['stack'] = new_frame
return self
-
def tracked_named_group(self, name):
assertion(type(name) is str, must_be_a_string("Name", type(name)))
assertion(len(name) > 0, must_be_one_character("Name"))
@@ -90,14 +92,12 @@ class RegexBuilder:
assertion(re.compile(named_group_regex, re.I).match(name), name_not_valid(name))
self.state['named_groups'].append(name)
-
def capture(self):
new_frame = create_stack_frame(t['capture'])
self.state['stack'] = new_frame
self.state['total_capture_groups'] += 1
return self
-
def named_capture(self, name):
new_frame = create_stack_frame(t['named_capture'](name))
self.tracked_named_group(name)
@@ -105,7 +105,6 @@ class RegexBuilder:
self.state['total_capture_groups'] += 1
return self
-
def quantifier_element(self, type_fn):
current_frame = self.get_current_frame()
if current_frame['quantifier'] is not None:
@@ -113,64 +112,49 @@ class RegexBuilder:
current_frame['quantifier'] = t[type_fn]
return self
-
def any_char(self):
return self.match_element(t['any_char'])
-
def whitespace_char(self):
return self.match_element(t['whitespace_char'])
-
def non_whitespace_char(self):
return self.match_element(t['non_whitespace_char'])
-
def digit(self):
return self.match_element(t['digit'])
-
def non_digit(self):
return self.match_element(t['non_digit'])
-
def word(self):
return self.match_element(t['word'])
-
def non_word(self):
return self.match_element(t['non_word'])
-
def word_boundary(self):
return self.match_element(t['word_boundary'])
-
def non_word_boundary(self):
return self.match_element(t['non_word_boundary'])
-
def new_line(self):
return self.match_element(t['new_line'])
-
def carriage_return(self):
return self.match_element(t['carriage_return'])
-
def tab(self):
return self.match_element(t['tab'])
-
def null_byte(self):
return self.match_element(t['null_byte'])
-
def named_back_reference(self, name):
assertion(name in self.state['named_groups'], named_group_does_not_exist(name))
return self.match_element(t['named_back_reference'](name))
-
def back_reference(self, index: int):
assertion(type(index) is int, 'Index must be an integer.')
assertion(
@@ -179,51 +163,39 @@ class RegexBuilder:
)
return self.match_element(t['back_reference'](index))
-
def any_of(self):
return self.frame_creating_element(t['any_of'])
-
def group(self):
return self.frame_creating_element(t['group'])
-
def assert_ahead(self):
return self.frame_creating_element(t['assert_ahead'])
-
def assert_not_ahead(self):
return self.frame_creating_element(t['assert_not_ahead'])
-
def assert_behind(self):
return self.frame_creating_element(t['assert_behind'])
-
def assert_not_behind(self):
return self.frame_creating_element(t['assert_not_behind'])
-
def optional(self):
return self.quantifier_element('optional')
-
def zero_or_more(self):
return self.quantifier_element('zero_or_more')
-
def zero_or_more_lazy(self):
return self.quantifier_element('zero_or_more_lazy')
-
def one_or_more(self):
return self.quantifier_element('one_or_more')
-
def one_or_more_lazy(self):
return self.quantifier_element('one_or_more_lazy')
-
def exactly(self, count):
assertion(type(count) is int and count > 0, must_be_positive_integer('count'))
current_frame = self.get_current_frame()
@@ -232,7 +204,6 @@ class RegexBuilder:
current_frame['quantifier'] = t['exactly'](count)
return self
-
def at_least(self, count):
assertion(type(count) is int and count > 0, must_be_positive_integer('count'))
current_frame = self.get_current_frame()
@@ -241,7 +212,6 @@ class RegexBuilder:
current_frame['quantifier'] = t['at_least'](count)
return self
-
def between(self, x, y):
assertion(type(x) is int and x >= 0, must_be_integer_greater_than_zero('x'))
assertion(type(y) is int and y > 0, must_be_positive_integer('y'))
@@ -252,7 +222,6 @@ class RegexBuilder:
current_frame['quantifier'] = t['between'](x, y)
return self
-
def between_lazy(self, x, y):
assertion(type(x) is int and x >= 0, must_be_integer_greater_than_zero('x'))
assertion(type(y) is int and y > 0, must_be_positive_integer('y'))
@@ -271,7 +240,6 @@ class RegexBuilder:
current_element_array.append(t['start_of_input'])
return self
-
def end_of_input(self):
assertion(self.state['has_defined_end'] is False, end_input_already_defined())
self.state['has_defined_end'] = True
@@ -279,7 +247,6 @@ class RegexBuilder:
current_element_array.append(t['end_of_input'])
return self
-
def any_of_chars(self, chars):
element_value = t['any_of_chars'](escape_special(chars))
current_frame = self.get_current_frame()
@@ -292,5 +259,3 @@ class RegexBuilder:
current_frame = self.get_current_frame()
current_frame['elements'].append(self.apply_quantifier(old_frame['type']['value'](old_frame['elements'])))
return self
-
-
diff --git a/src/edify/builder/errors.py b/src/edify/builder/errors.py
index 27258cd..c9fe99a 100644
--- a/src/edify/builder/errors.py
+++ b/src/edify/builder/errors.py
@@ -1,38 +1,50 @@
def must_be_a_string(value, variable_name):
return '{} must be a string. (got {})'.format(value, type(variable_name))
+
def must_be_one_character(variable_name):
return '{} must be one character long.'.format(variable_name)
+
def cannot_create_duplicate_named_group(name):
return 'Can not create duplicate named group "{}".'.format(name)
+
def name_not_valid(name):
return 'Name {} is not valid. (only alphanumeric characters and underscores are allowed)'.format(name)
+
def named_group_does_not_exist(name):
return 'Named group "{}" does not exist (create one with .named_capture()).'.format(name)
+
def invalid_total_capture_groups_index(index, total_capture_groups):
return 'Invalid index #{}. There are only {} capture groups.'.format(index, total_capture_groups)
+
def must_be_positive_integer(variable_name):
return '{} must be a positive integer.'.format(variable_name)
+
def must_be_integer_greater_than_zero(variable_name):
return '{} must be an integer greater than zero.'.format(variable_name)
+
def unable_to_quantify(quantifier, type):
return 'Can not quantify regular expression with {}, because it has already been quantified with {}.'.format(quantifier, type)
+
def start_input_already_defined():
return 'Regex already has a start of input.'
+
def cannot_define_start_after_end():
return 'Can not define a start of input after defining an end of input.'
+
def end_input_already_defined():
return 'Regex already has an end of input.'
+
def can_not_end_while_building_root_exp():
return 'Can not end while building the root expression.'
diff --git a/src/edify/builder/helpers/core.py b/src/edify/builder/helpers/core.py
index fa775ff..15cf9ef 100644
--- a/src/edify/builder/helpers/core.py
+++ b/src/edify/builder/helpers/core.py
@@ -1,4 +1,6 @@
import re
+
+
def as_type(type, options={}):
def as_type_fn(value=None):
return {
@@ -9,7 +11,7 @@ def as_type(type, options={}):
return as_type_fn
-def deferred_type(type, options = {}):
+def deferred_type(type, options={}):
type_fn = as_type(type, options)
return type_fn(type_fn)
@@ -26,8 +28,6 @@ def assertion(condition, message):
if not condition:
raise Exception(message)
-# const escapeSpecial = s => specialChars.reduce((acc, char) => replaceAll(acc, char, `\\${char}`), s);
+
def escape_special(s):
return re.escape(s)
-
-
diff --git a/src/edify/builder/helpers/regex_vars.py b/src/edify/builder/helpers/regex_vars.py
index 4d7f17c..ebf8f28 100644
--- a/src/edify/builder/helpers/regex_vars.py
+++ b/src/edify/builder/helpers/regex_vars.py
@@ -1 +1 @@
-named_group_regex=r"^[a-z]+\w*$"
+named_group_regex = r"^[a-z]+\w*$"
diff --git a/src/edify/builder/helpers/t.py b/src/edify/builder/helpers/t.py
index 8ac3d7d..eb08763 100644
--- a/src/edify/builder/helpers/t.py
+++ b/src/edify/builder/helpers/t.py
@@ -1,4 +1,5 @@
-from .core import as_type, deferred_type
+from .core import as_type
+from .core import deferred_type
t = {
'root': as_type('root')(),