Skip to content

Commit

Permalink
Impl min:max inpu prefix range
Browse files Browse the repository at this point in the history
  • Loading branch information
eldipa committed Jan 25, 2020
1 parent 74a636b commit bd28195
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion byexample/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ class Example(object):
'42'
>>> example.options
{'input': False, 'norm_ws': False, 'rm': [], 'tags': True}
{'input': False,
'input_prefix_range': (6, 12),
'norm_ws': False,
'rm': [],
'tags': True}
'''
def __init__(
Expand Down
4 changes: 2 additions & 2 deletions byexample/expected.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class _LinearExpected(Expected):
>>> from byexample.options import Options
>>> from byexample.finder import _build_fake_example as build_example
>>> opts = {'norm_ws': False, 'tags': True, 'rm': [], 'input': False}
>>> opts = {'norm_ws': False, 'tags': True, 'rm': [], 'input': False, 'input_prefix_range': (6,12)}
Consider the following example with a named capture in the expected:
Expand Down Expand Up @@ -142,7 +142,7 @@ class _LinearExpected(Expected):
(See byexample.parser docs)
>>> opts = {'norm_ws': True, 'tags': True, 'rm': [], 'input': False}
>>> opts = {'norm_ws': True, 'tags': True, 'rm': [], 'input': False, 'input_prefix_range': (6, 12)}
>>> ex = build_example('f()', '\n <a>A \n\nB <bc> C\n<c>', opts=opts)
>>> exp = ex.expected
Expand Down
3 changes: 2 additions & 1 deletion byexample/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class F:
'norm_ws': False,
'tags': True,
'rm': [],
'input': False
'input': False,
'input_prefix_range': (6, 12)
}
)
parser.extract_options = lambda x: opts
Expand Down
16 changes: 16 additions & 0 deletions byexample/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ def geometry(x):
return (lines, columns)


def _range(x):
min, max = [int(v.strip()) for v in str(x).split(':')]
if min < 0 or max < 0 or min > max:
raise ValueError("Invalid range %s" % x)

return (min, max)


def get_default_options_parser(cmdline_args):
options_parser = OptionParser()
options_parser.add_flag(
Expand Down Expand Up @@ -252,6 +260,14 @@ def get_default_options_parser(cmdline_args):
"input", default=False, help="enable the input tags [...]"
)

options_parser.add_argument(
"+input-prefix-range",
default=(6, 12),
type=_range,
help=
"amount of characters that must precede at minimum/maximum an input tag in the form min:max"
)

return options_parser


Expand Down
9 changes: 5 additions & 4 deletions byexample/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ def parse(self, example, concerns):
for x in options['rm']:
example.expected_str = example.expected_str.replace(x, '')

input_prefix_len_range = options['input_prefix_range']
expected_regexs, charnos, rcounts, tags_by_idx, input_list = self.expected_as_regexs(
example.expected_str, options['tags'], options['input'],
options['norm_ws']
options['norm_ws'], input_prefix_len_range
)

ExpectedClass = _LinearExpected
Expand Down Expand Up @@ -195,7 +196,8 @@ def parse(self, example, concerns):
return example

def expected_as_regexs(
self, expected, tags_enabled, input_enabled, normalize_whitespace
self, expected, tags_enabled, input_enabled, normalize_whitespace,
input_prefix_len_range
):
'''
From the expected string create a list of regular expressions that
Expand All @@ -219,7 +221,7 @@ def expected_as_regexs(
>>> import re
>>> parser = ExampleParser(0, 'utf8', None); parser.language = 'python'
>>> _as_regexs = partial(parser.expected_as_regexs, tags_enabled=True, input_enabled=True, normalize_whitespace=False)
>>> _as_regexs = partial(parser.expected_as_regexs, tags_enabled=True, input_enabled=True, normalize_whitespace=False, input_prefix_len_range=(6,12))
>>> expected = 'a<foo>b<bar>c'
>>> regexs, charnos, rcounts, tags_by_idx, input_list = _as_regexs(expected)
Expand Down Expand Up @@ -269,7 +271,6 @@ def expected_as_regexs(
>>> tags_by_idx
{2: None, 4: 'foo-bar'}
'''
input_prefix_len_range = (6, 12)
if normalize_whitespace:
sm = SM_NormWS(
self.capture_tag_regexs(), self.input_regexs(),
Expand Down
2 changes: 1 addition & 1 deletion docs/contrib/how-to-support-new-finders-and-languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ the scenes so you do not to be worry about the details):

```python
>>> from byexample.options import Options, OptionParser
>>> parser = ArnoldCParser(0, 'utf-8', Options(rm=[], norm_ws=False, tags=True, input=False, optparser=OptionParser(add_help=False)))
>>> parser = ArnoldCParser(0, 'utf-8', Options(rm=[], norm_ws=False, tags=True, input=False, input_prefix_range=(6,12), optparser=OptionParser(add_help=False)))

>>> from byexample.finder import Example
>>> runner = None # not yet
Expand Down

0 comments on commit bd28195

Please sign in to comment.