class Command_Jump(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'jump\s*(.*)', s) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory(self.s_function,f_get_params=self.parsed_container.last_value).assign() def execute(self): expr_type = self.f.type try: if expr_type=='numeric_expression': vals = self.f.get_value() if vals: n = int(vals[0]) self.FI.skip_n(n) else: log.debug('Numeric expression could not be evaluated') elif expr_type == 'string': vals = self.f.get_value() self.FI.skip_until_string(pattern=vals[0]) elif expr_type=='regex': self.FI.skip_until_regex(pattern=self.f.pattern) except StopIteration: pass
def preprocess(self): s_GI = self.GI.s s_re = re.search(r'troublemaker\s+(.*):', s_GI) if s_re is None: raise SyntaxError self.ptn = ExpressionFactory(s_re.group(1)).assign() self.commands = self.get_enclosed_commands()
class Command_Jump(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'jump\s*(.*)', s) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory( self.s_function, f_get_params=self.parsed_container.last_value).assign() def execute(self): expr_type = self.f.type try: if expr_type == 'numeric_expression': vals = self.f.get_value() if vals: n = int(vals[0]) self.FI.skip_n(n) else: log.debug('Numeric expression could not be evaluated') elif expr_type == 'string': vals = self.f.get_value() self.FI.skip_until_string(pattern=vals[0]) elif expr_type == 'regex': self.FI.skip_until_regex(pattern=self.f.pattern) except StopIteration: pass
def preprocess(self, s): s_GI = self.GI.s s_re = re.search(r'if\s+(.*):', s_GI) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.ptn = ExpressionFactory(s_re.group(1)).assign() self.commands = self.get_enclosed_commands()
def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'jump\s*(.*)', s) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory( self.s_function, f_get_params=self.parsed_container.last_value).assign()
class Command_Assign(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s v = s.split('=', maxsplit=1) s_keys, s_function = strip_all(v) self.keys = strip_all(s_keys.split(',')) self.f = ExpressionFactory(s_function,f_get_params=self.parsed_container.last_value).assign() def execute(self): vals = self.f.get_value(self.FI.s) if vals is None: # expression was not found return if self.keys == ['row']: # are we parsing a table row? return vals if len(self.keys) != len(vals): raise SyntaxError for key, val in zip(self.keys,vals): assigned_element = ParsedElement(key, val) self.parsed_container.append(assigned_element)
class GlobalCommand_Troublemaker(Top_Grammar): def __init__(self, GI, FI, parsed_container): super().__init__(GI, FI, parsed_container) self.preprocess() def preprocess(self): s_GI = self.GI.s s_re = re.search(r'troublemaker\s+(.*):', s_GI) if s_re is None: raise SyntaxError self.ptn = ExpressionFactory(s_re.group(1)).assign() self.commands = self.get_enclosed_commands() def match(self, s_FI): return self.ptn.match(s_FI) def get_enclosed_commands(self): out = [] for s_grammar in self.GI: s = s_grammar.lstrip().split('#')[0] if s[:15]=='endtroublemaker': break cmd = LineCommandFactory(self.GI, self.FI, self.parsed_container, self.troublemakers).assign() out.append(cmd) return out def execute(self): for cmd in self.commands: cmd.execute()
def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s v = s.split('=', maxsplit=1) s_keys, s_function = strip_all(v) self.keys = strip_all(s_keys.split(',')) self.f = ExpressionFactory(s_function,f_get_params=self.parsed_container.last_value).assign()
def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'jump\s*(.*)', s) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory(self.s_function,f_get_params=self.parsed_container.last_value).assign()
def init_global_if(self): s_GI = self.GI.s s_re = re.search(r'if\s+(.*):', s_GI) # syntax "if /xxx/:" if s_re is None: raise SyntaxError s_re = s_re.group(1) ptn = ExpressionFactory(s_re).assign() self.pattern_type = ptn.type self.pattern = ptn.pattern self.start = ptn.start self.end = ptn.end self.commands = self.get_enclosed_commands() if self.start == 0: self.pattern_type += ' 0'
class Command_SafeJump(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'safejump\s*(.*)', s) if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory(self.s_function,f_get_params=self.parsed_container.last_value).assign() def execute(self): if not (self.f.type in ('numeric_expression','regex')): raise SyntaxError while not self.f.match(self.FI.s): next(self.FI) for troublemaker in self.troublemakers: if troublemaker.match(self.FI.s): troublemaker.execute()
class Command_If(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) self.pattern_type = None self.start = None self.end = None self.pattern = None self.commands = [] self.preprocess(self.GI.s) def preprocess(self, s): s_GI = self.GI.s s_re = re.search(r'if\s+(.*):', s_GI) # syntax "if /xxx/:" if s_re is None: raise SyntaxError self.ptn = ExpressionFactory(s_re.group(1)).assign() self.commands = self.get_enclosed_commands() def get_enclosed_commands(self): out = [] for s_grammar in self.GI: s = s_grammar.lstrip().split('#')[0] if s[:5]=='endif': break from Grammar.LineCommandFactory import LineCommandFactory # it has to be here to avoid import recursion cmd = LineCommandFactory(self.GI, self.FI, self.parsed_container, self.troublemakers).assign() out.append(cmd) return out def find(self, s_FI): return self.ptn.match(s_FI) def execute(self): if self.find(self.FI.s): out = None for cmd in self.commands: out = cmd.execute() return out
class Command_SafeJump(Top_Grammar): def __init__(self, GI, FI, parsed_container, troublemakers): super().__init__(GI, FI, parsed_container, troublemakers) s = self.GI.s s_re = re.search(r'safejump\s*(.*)', s) if s_re is None: raise SyntaxError self.s_function = s_re.group(1) self.f = ExpressionFactory( self.s_function, f_get_params=self.parsed_container.last_value).assign() def execute(self): if not (self.f.type in ('numeric_expression', 'regex')): raise SyntaxError while not self.f.match(self.FI.s): next(self.FI) for troublemaker in self.troublemakers: if troublemaker.match(self.FI.s): troublemaker.execute()