Пример #1
0
 def test_charClass(self):
     g = VisualRegexGrammar()
     self._parseStringTest(g.charClass(), '\\A', Schema.CharClass(cls='A'))
     self._parseStringTest(g.charClass(), '\\b', Schema.CharClass(cls='b'))
     self._parseStringTest(g.charClass(), '\\B', Schema.CharClass(cls='B'))
     self._parseStringTest(g.charClass(), '\\d', Schema.CharClass(cls='d'))
     self._parseStringTest(g.charClass(), '\\D', Schema.CharClass(cls='D'))
     self._parseStringTest(g.charClass(), '\\s', Schema.CharClass(cls='s'))
     self._parseStringTest(g.charClass(), '\\S', Schema.CharClass(cls='S'))
     self._parseStringTest(g.charClass(), '\\w', Schema.CharClass(cls='w'))
     self._parseStringTest(g.charClass(), '\\W', Schema.CharClass(cls='W'))
     self._parseStringTest(g.charClass(), '\\Z', Schema.CharClass(cls='Z'))
Пример #2
0
 def test_regex(self):
     g = VisualRegexGrammar()
     self._parseStringTest(g.regex(), 'a', Schema.LiteralChar(char='a'))
     self._parseStringTest(
         g.regex(), 'abc|def',
         Schema.Choice(subexps=[
             Schema.Sequence(subexps=[
                 Schema.LiteralChar(char='a'),
                 Schema.LiteralChar(char='b'),
                 Schema.LiteralChar(char='c')
             ]),
             Schema.Sequence(subexps=[
                 Schema.LiteralChar(char='d'),
                 Schema.LiteralChar(char='e'),
                 Schema.LiteralChar(char='f')
             ])
         ]))
     self._parseStringTest(
         g.regex(), r'[\w\-][\w\-\.]+@[\w\-][\w\-\.]+[a-zA-Z]{1,4}',
         Schema.Sequence(subexps=[
             Schema.CharSet(items=[
                 Schema.CharSetChar(char=Schema.CharClass(cls='w')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='-'))
             ]),
             Schema.OneOrMore(subexp=Schema.CharSet(items=[
                 Schema.CharSetChar(char=Schema.CharClass(cls='w')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='-')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='.'))
             ])),
             Schema.LiteralChar(char='@'),
             Schema.CharSet(items=[
                 Schema.CharSetChar(char=Schema.CharClass(cls='w')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='-'))
             ]),
             Schema.OneOrMore(subexp=Schema.CharSet(items=[
                 Schema.CharSetChar(char=Schema.CharClass(cls='w')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='-')),
                 Schema.CharSetChar(char=Schema.EscapedChar(char='.'))
             ])),
             Schema.RepeatRange(subexp=Schema.CharSet(items=[
                 Schema.CharSetRange(min=Schema.LiteralChar(char='a'),
                                     max=Schema.LiteralChar(char='z')),
                 Schema.CharSetRange(min=Schema.LiteralChar(char='A'),
                                     max=Schema.LiteralChar(char='Z'))
             ]),
                                min='1',
                                max='4',
                                greedy='1')
         ]))
Пример #3
0
 def test_sequence(self):
     g = VisualRegexGrammar()
     self._parseStringTest(
         g.sequence(), 'abc',
         Schema.Sequence(subexps=[
             Schema.LiteralChar(char='a'),
             Schema.LiteralChar(char='b'),
             Schema.LiteralChar(char='c')
         ]))
     self._parseStringTest(
         g.sequence(), 'a*bc',
         Schema.Sequence(subexps=[
             Schema.ZeroOrMore(subexp=Schema.LiteralChar(char='a')),
             Schema.LiteralChar(char='b'),
             Schema.LiteralChar(char='c')
         ]))
     self._parseStringTest(
         g.sequence(), 'ab*c',
         Schema.Sequence(subexps=[
             Schema.LiteralChar(char='a'),
             Schema.ZeroOrMore(subexp=Schema.LiteralChar(char='b')),
             Schema.LiteralChar(char='c')
         ]))
     self._parseStringTest(
         g.sequence(), 'abc*',
         Schema.Sequence(subexps=[
             Schema.LiteralChar(char='a'),
             Schema.LiteralChar(char='b'),
             Schema.ZeroOrMore(subexp=Schema.LiteralChar(char='c'))
         ]))
     self._parseStringTest(
         g.sequence(), '[a-z]bc',
         Schema.Sequence(subexps=[
             Schema.CharSet(items=[
                 Schema.CharSetRange(min=Schema.LiteralChar(char='a'),
                                     max=Schema.LiteralChar(char='z'))
             ]),
             Schema.LiteralChar(char='b'),
             Schema.LiteralChar(char='c')
         ]))
     self._parseStringTest(
         g.sequence(), '\\wbc*',
         Schema.Sequence(subexps=[
             Schema.CharClass(cls='w'),
             Schema.LiteralChar(char='b'),
             Schema.ZeroOrMore(subexp=Schema.LiteralChar(char='c'))
         ]))
Пример #4
0
 def charClass(self):
     return (Literal('\\') + RegEx('[AbBdDsSwWZ]')).action(
         lambda input, begin, end, x, bindings: Schema.CharClass(cls=x[1]))