# Path: luna/w_objects.py
# class W_Pri(W_Num):
#     def __init__(self, val):
#         assert val in (0, 1, 2)
#         self.n_val = val
# 
#     def to_str(self):
#         if self.n_val == 0:
#             return 'nil'
#         elif self.n_val == 1:
#             return 'false'
#         else:
#             return 'true'
# 
#     def is_true(self):
#         return self.n_val not in (0, 1) 
# 
#     def clone(self):
#         return W_Pri(self.n_val)
# 
# class W_Num(W_Object):
#     def __init__(self, val):
#         self.n_val = val
# 
#     def getval(self):
#         return self.n_val
# 
#     def eq(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val == w_other.getval()
# 
#     def neq(self, w_other):
#         return not self.eq(w_other)
# 
#     def gt(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val > w_other.n_val
# 
#     def lt(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val < w_other.n_val
# 
#     def le(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val <= w_other.n_val
# 
#     def ge(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val >= w_other.n_val
# 
#     def to_str(self):
#         i_val = int(self.n_val)
#         if self.n_val - i_val == 0:
#             return str(i_val)
#         else:
#             return str(self.n_val)
# 
#     def clone(self):
#         return W_Num(self.n_val)
# 
#     def hash(self):
#         return self.n_val
# 
# class W_Str(W_Object):
#     def __init__(self, val):
#         self.s_val = val
# 
#     def getval(self):
#         return self.s_val
# 
#     def eq(self, w_other):
#         if isinstance(w_other, W_Str):
#             return self.s_val == w_other.getval()
#         else:
#             assert isinstance(w_other, W_Pri)
#             if w_other.n_val == 0:
#                 return self.s_val is None
#             else:
#                 assert 0
# 
#     def neq(self, w_other):
#         return not self.eq(w_other)
# 
#     def gt(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val > w_other.s_val
# 
#     def lt(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val < w_other.s_val
# 
#     def le(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val <= w_other.s_val
# 
#     def ge(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val >= w_other.s_val
# 
#     def to_str(self):
#         return str(self.s_val)
# 
#     def clone(self):
#         return W_Str(self.s_val)
# 
#     def hash(self):
#         return compute_hash(self.s_val)
# 
# Path: luna/module.py
# class ModuleDef(object):
#     def __init__(self, name):
#         self.name = name
#         self.methods = W_Table()
# 
#     def function(self, name):
#         def adder(func):
#             self.methods.set(W_Str(name), LuaBuiltinFrame(func))
#         return adder
# 
#     def add_constant(self, name, w_const):
#         self.methods.set(W_Str(name), w_const)
# 
# Path: luna/modules/patterns.py
# def find2(expr, string, start):
#     assert isinstance(start, int)
#     if start < 0:
#         start = len(string) + start
#         # if negative offset is bigger than length of string
#         # start at the beginning
#         if start < 0:
#             start = 0
#         start = int(start)
# 
#     found = False
#     i = start
#     while i < len(string):
#         match = False
#         valid = True
#         j = i
#         state = expr
#         backtrack = []
#         while valid and not match and (j < len(string) or len(backtrack) > 0):
#             if j >= len(string) and len(backtrack) > 0:
#                 state, j = backtrack.pop()
#             if isinstance(state, StateCharRange):
#                 if not state.match(string[j]):
#                     if len(backtrack) == 0:
#                         valid = False
#                     else:
#                         state, j = backtrack.pop()
#                 else:
#                     state = state.out
#                     j += 1
#             elif isinstance(state, StateMatch):
#                 match = True
#             elif isinstance(state, StateSplit):
#                 backtrack.append((state.out2, j))
#                 state = state.out
#             else:
#                 valid = False
#             if j == len(string):
#                 if (isinstance(state, StateMatch) or
#                         (isinstance(state, StateSplit) and
#                             isinstance(state.out2, StateMatch))):
#                     match = True
#         if match:
#             found = True
#             yield (i+1, j)
#             if j > i:
#                 i = j
#             else:
#                 i += 1
#         else:
#             i += 1
#     if not found:
#         yield (-1, -1)
# 
# def compile_re(pattern, plain=False):
#     tokens = tokenize(pattern)
#     return tokens_to_expression(tokens)


import pytest
from luna.w_objects import W_Pri, W_Num, W_Str
from luna.module import ModuleDef
from luna.modules.patterns import find2, compile_re
"""
"""



StringModule = ModuleDef('string')


def handle_args(args):
    s = args[0].s_val
    start = 0
    plain = False
    expr = compile_re(args[1].s_val, plain)

    if len(args) > 2:
        start = args[2].n_val - 1
        if len(args) > 3:
            w_pri = args[3]

# Path: luna/w_objects.py
# class W_Pri(W_Num):
#     def __init__(self, val):
#         assert val in (0, 1, 2)
#         self.n_val = val
# 
#     def to_str(self):
#         if self.n_val == 0:
#             return 'nil'
#         elif self.n_val == 1:
#             return 'false'
#         else:
#             return 'true'
# 
#     def is_true(self):
#         return self.n_val not in (0, 1) 
# 
#     def clone(self):
#         return W_Pri(self.n_val)
# 
# class W_Num(W_Object):
#     def __init__(self, val):
#         self.n_val = val
# 
#     def getval(self):
#         return self.n_val
# 
#     def eq(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val == w_other.getval()
# 
#     def neq(self, w_other):
#         return not self.eq(w_other)
# 
#     def gt(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val > w_other.n_val
# 
#     def lt(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val < w_other.n_val
# 
#     def le(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val <= w_other.n_val
# 
#     def ge(self, w_other):
#         assert isinstance(w_other, W_Num)
#         return self.n_val >= w_other.n_val
# 
#     def to_str(self):
#         i_val = int(self.n_val)
#         if self.n_val - i_val == 0:
#             return str(i_val)
#         else:
#             return str(self.n_val)
# 
#     def clone(self):
#         return W_Num(self.n_val)
# 
#     def hash(self):
#         return self.n_val
# 
# class W_Str(W_Object):
#     def __init__(self, val):
#         self.s_val = val
# 
#     def getval(self):
#         return self.s_val
# 
#     def eq(self, w_other):
#         if isinstance(w_other, W_Str):
#             return self.s_val == w_other.getval()
#         else:
#             assert isinstance(w_other, W_Pri)
#             if w_other.n_val == 0:
#                 return self.s_val is None
#             else:
#                 assert 0
# 
#     def neq(self, w_other):
#         return not self.eq(w_other)
# 
#     def gt(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val > w_other.s_val
# 
#     def lt(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val < w_other.s_val
# 
#     def le(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val <= w_other.s_val
# 
#     def ge(self, w_other):
#         assert isinstance(w_other, W_Str)
#         return self.s_val >= w_other.s_val
# 
#     def to_str(self):
#         return str(self.s_val)
# 
#     def clone(self):
#         return W_Str(self.s_val)
# 
#     def hash(self):
#         return compute_hash(self.s_val)
# 
# Path: luna/module.py
# class ModuleDef(object):
#     def __init__(self, name):
#         self.name = name
#         self.methods = W_Table()
# 
#     def function(self, name):
#         def adder(func):
#             self.methods.set(W_Str(name), LuaBuiltinFrame(func))
#         return adder
# 
#     def add_constant(self, name, w_const):
#         self.methods.set(W_Str(name), w_const)
# 
# Path: luna/modules/patterns.py
# def find2(expr, string, start):
#     assert isinstance(start, int)
#     if start < 0:
#         start = len(string) + start
#         # if negative offset is bigger than length of string
#         # start at the beginning
#         if start < 0:
#             start = 0
#         start = int(start)
# 
#     found = False
#     i = start
#     while i < len(string):
#         match = False
#         valid = True
#         j = i
#         state = expr
#         backtrack = []
#         while valid and not match and (j < len(string) or len(backtrack) > 0):
#             if j >= len(string) and len(backtrack) > 0:
#                 state, j = backtrack.pop()
#             if isinstance(state, StateCharRange):
#                 if not state.match(string[j]):
#                     if len(backtrack) == 0:
#                         valid = False
#                     else:
#                         state, j = backtrack.pop()
#                 else:
#                     state = state.out
#                     j += 1
#             elif isinstance(state, StateMatch):
#                 match = True
#             elif isinstance(state, StateSplit):
#                 backtrack.append((state.out2, j))
#                 state = state.out
#             else:
#                 valid = False
#             if j == len(string):
#                 if (isinstance(state, StateMatch) or
#                         (isinstance(state, StateSplit) and
#                             isinstance(state.out2, StateMatch))):
#                     match = True
#         if match:
#             found = True
#             yield (i+1, j)
#             if j > i:
#                 i = j
#             else:
#                 i += 1
#         else:
#             i += 1
#     if not found:
#         yield (-1, -1)
# 
# def compile_re(pattern, plain=False):
#     tokens = tokenize(pattern)
#     return tokens_to_expression(tokens)


# Path: luna/modules/string.py
import pytest
from luna.w_objects import W_Pri, W_Num, W_Str
from luna.module import ModuleDef
from luna.modules.patterns import find2, compile_re

"""
"""



StringModule = ModuleDef('string')


def handle_args(args):
    s = args[0].s_val
    start = 0
    plain = False
    expr = compile_re(args[1].s_val, plain)

    if len(args) > 2:
        start = args[2].n_val - 1
        if len(args) > 3:
            w_pri = args[3]

            assert isinstance(w_pri, W_Pri)