1 #!/usr/bin/env python2
2 """
3 func_eggex.py
4 """
5 from __future__ import print_function
6
7 from _devbuild.gen.runtime_asdl import value, value_t
8 from core import error, state, vm
9 from frontend import typed_args
10 from ysh import val_ops
11
12 from typing import List, Dict
13
14
15 class Match(vm._Callable):
16 """_match(0) or _match(): get the whole match _match(1) ..
17
18 _match(N): submatch
19 """
20
21 def __init__(self, mem):
22 # type: (state.Mem) -> None
23 vm._Callable.__init__(self)
24 self.mem = mem
25
26 def Call(self, args):
27 # type: (typed_args.Reader) -> value_t
28 arg = 0
29 if args.NumPos():
30 arg = args.PosInt()
31 args.Done()
32
33 # TODO: Support strings
34 s = self.mem.GetMatch(arg)
35 # Oil code doesn't deal well with exceptions!
36 #if s is None:
37 # raise IndexError('No such group')
38 if s is not None:
39 return value.Str(s)
40
41 return value.Null
42
43
44 class Start(vm._Callable):
45 """Same signature as _match(), but for start positions."""
46
47 def __init__(self, mem):
48 # type: (state.Mem) -> None
49 vm._Callable.__init__(self)
50 self.mem = mem
51
52 def Call(self, args):
53 # type: (typed_args.Reader) -> value_t
54 raise NotImplementedError('_start')
55
56
57 class End(vm._Callable):
58 """Same signature as _match(), but for end positions."""
59
60 def __init__(self, mem):
61 # type: (state.Mem) -> None
62 vm._Callable.__init__(self)
63 self.mem = mem
64
65 def Call(self, args):
66 # type: (typed_args.Reader) -> value_t
67 raise NotImplementedError('_end')
68
69
70 # vim: sw=4