class Append(Token): def __init__(self, lexer, parser, parent): Token.__init__(self, 'Append', lexer, parser, parent) # First expect an expression. lexer.expect(self, 'keyword', 'append') lexer.expect(self, 'whitespace') self.expr = Term(lexer, parser, parent) # Expect "to" keyword. lexer.expect(self, 'whitespace') lexer.expect(self, 'keyword', 'to') # Expect a variable name. lexer.expect(self, 'whitespace') _, self.varname = lexer.token() lexer.expect(self, 'varname') self.parent.define(**{self.varname: []}) self.mark_end() def value(self, context): existing = self.parent.get(self.varname) args = {self.varname: existing + self.expr.value(context)} self.parent.define(**args) return 1 def dump(self, indent=0): print(' ' * indent) + self.name, "to", self.varname self.expr.dump(indent + 1)
class Append(Token): def __init__(self, lexer, parser, parent): Token.__init__(self, 'Append', lexer, parser, parent) # First expect an expression. lexer.expect(self, 'keyword', 'append') lexer.expect(self, 'whitespace') self.expr = Term(lexer, parser, parent) # Expect "to" keyword. lexer.expect(self, 'whitespace') lexer.expect(self, 'keyword', 'to') # Expect a variable name. lexer.expect(self, 'whitespace') _, self.varname = lexer.token() lexer.expect(self, 'varname') self.parent.define(**{self.varname: []}) self.mark_end() def value(self, context): existing = self.parent.get(self.varname) args = {self.varname: existing + self.expr.value(context)} self.parent.define(**args) return 1 def dump(self, indent = 0): print((' ' * indent) + self.name, "to", self.varname) self.expr.dump(indent + 1)
def __init__(self, lexer, parser, parent): Token.__init__(self, 'Extract', lexer, parser, parent) self.varnames = [] self.variables = {} self.append = False self.source = None if parser.no_prompt: msg = "'extract' keyword does not work with --no-prompt" lexer.syntax_error(msg, self) # First expect a regular expression. lexer.expect(self, 'keyword', 'extract') lexer.expect(self, 'whitespace') self.regex = Regex(lexer, parser, parent) # Expect "as" keyword. lexer.expect(self, 'whitespace') if lexer.next_if('keyword', 'as'): self.append = False elif lexer.next_if('keyword', 'into'): self.append = True else: _, token = lexer.token() msg = 'Expected "as" or "into" but got %s' % token lexer.syntax_error(msg, self) # Expect a list of variable names. while 1: # Variable name. lexer.expect(self, 'whitespace') _, token = lexer.token() lexer.expect(self, 'varname') if token in self.variables: lexer.syntax_error('Duplicate variable name %s', self) self.varnames.append(token) self.variables[token] = [] # Comma. if lexer.next_if('comma'): continue break self.parent.define(**self.variables) if len(self.varnames) != self.regex.n_groups: count = (len(self.varnames), self.regex.n_groups) error = '%s variables, but regex has %s groups' % count lexer.syntax_error(error, self) # Handle the "from" keyword. lexer.skip('whitespace') if lexer.next_if('keyword', 'from'): lexer.expect(self, 'whitespace') self.source = Term(lexer, parser, parent) self.mark_end()
def __init__(self, lexer, parser, parent): Token.__init__(self, 'Append', lexer, parser, parent) # First expect an expression. lexer.expect(self, 'keyword', 'append') lexer.expect(self, 'whitespace') self.expr = Term(lexer, parser, parent) # Expect "to" keyword. lexer.expect(self, 'whitespace') lexer.expect(self, 'keyword', 'to') # Expect a variable name. lexer.expect(self, 'whitespace') _, self.varname = lexer.token() lexer.expect(self, 'varname') self.parent.define(**{self.varname: []}) self.mark_end()
def __init__(self, lexer, parser, parent): Token.__init__(self, "Extract", lexer, parser, parent) self.varnames = [] self.variables = {} self.append = False self.source = None if parser.no_prompt: msg = "'extract' keyword does not work with --no-prompt" lexer.syntax_error(msg, self) # First expect a regular expression. lexer.expect(self, "keyword", "extract") lexer.expect(self, "whitespace") self.regex = Regex(lexer, parser, parent) # Expect "as" keyword. lexer.expect(self, "whitespace") if lexer.next_if("keyword", "as"): self.append = False elif lexer.next_if("keyword", "into"): self.append = True else: _, token = lexer.token() msg = 'Expected "as" or "into" but got %s' % token lexer.syntax_error(msg, self) # Expect a list of variable names. while 1: # Variable name. lexer.expect(self, "whitespace") _, token = lexer.token() lexer.expect(self, "varname") if token in self.variables: lexer.syntax_error("Duplicate variable name %s", self) self.varnames.append(token) self.variables[token] = [] # Comma. if lexer.next_if("comma"): continue break self.parent.define(**self.variables) if len(self.varnames) != self.regex.n_groups: count = (len(self.varnames), self.regex.n_groups) error = "%s variables, but regex has %s groups" % count lexer.syntax_error(error, self) # Handle the "from" keyword. lexer.skip("whitespace") if lexer.next_if("keyword", "from"): lexer.expect(self, "whitespace") self.source = Term(lexer, parser, parent) self.mark_end()
def __init__(self, lexer, parser, parent): Token.__init__(self, 'Loop', lexer, parser, parent) self.during = None self.until = None self.thefrom = None self.theto = None self.list_variables = [] self.iter_varnames = [] # Expect one ore more lists. lexer.expect(self, 'keyword', 'loop') lexer.expect(self, 'whitespace') if not lexer.current_is('keyword', 'while') and \ not lexer.current_is('keyword', 'until') and \ not lexer.current_is('keyword', 'from'): self.list_variables = [Term(lexer, parser, parent)] lexer.next_if('whitespace') while lexer.next_if('comma'): lexer.skip(['whitespace', 'newline']) self.list_variables.append(Term(lexer, parser, parent)) lexer.skip(['whitespace', 'newline']) # Expect the "as" keyword. lexer.expect(self, 'keyword', 'as') # The iterator variable. lexer.next_if('whitespace') _, iter_varname = lexer.token() lexer.expect(self, 'varname') parent.define(**{iter_varname: []}) self.iter_varnames = [iter_varname] lexer.next_if('whitespace') while lexer.next_if('comma'): lexer.skip(['whitespace', 'newline']) _, iter_varname = lexer.token() lexer.expect(self, 'varname') parent.define(**{iter_varname: []}) self.iter_varnames.append(iter_varname) lexer.skip(['whitespace', 'newline']) if len(self.iter_varnames) != len(self.list_variables): error = '%s lists, but only %s iterators in loop' % (len(self.iter_varnames), len(self.list_variables)) lexer.syntax_error(error, self) # Check if this is a "from ... to ..." loop. if lexer.next_if('keyword', 'from'): lexer.expect(self, 'whitespace') self.thefrom = Expression(lexer, parser, parent) lexer.next_if('whitespace') lexer.expect(self, 'keyword', 'to') self.theto = Expression(lexer, parser, parent) lexer.next_if('whitespace') if lexer.next_if('keyword', 'as'): lexer.next_if('whitespace') _, iter_varname = lexer.token() lexer.expect(self, 'varname') lexer.next_if('whitespace') else: iter_varname = 'counter' parent.define(**{iter_varname: []}) self.iter_varnames = [iter_varname] # Check if this is a "while" loop. if lexer.next_if('keyword', 'while'): lexer.expect(self, 'whitespace') self.during = Expression(lexer, parser, parent) lexer.next_if('whitespace') # Check if this is an "until" loop. if lexer.next_if('keyword', 'until'): lexer.expect(self, 'whitespace') self.until = Expression(lexer, parser, parent) lexer.next_if('whitespace') # End of statement. self.mark_end() # Body of the loop block. lexer.skip(['whitespace', 'newline']) self.block = Code.Code(lexer, parser, parent)
class Extract(Token): def __init__(self, lexer, parser, parent): Token.__init__(self, 'Extract', lexer, parser, parent) self.varnames = [] self.variables = {} self.append = False self.source = None if parser.no_prompt: msg = "'extract' keyword does not work with --no-prompt" lexer.syntax_error(msg, self) # First expect a regular expression. lexer.expect(self, 'keyword', 'extract') lexer.expect(self, 'whitespace') self.regex = Regex(lexer, parser, parent) # Expect "as" keyword. lexer.expect(self, 'whitespace') if lexer.next_if('keyword', 'as'): self.append = False elif lexer.next_if('keyword', 'into'): self.append = True else: _, token = lexer.token() msg = 'Expected "as" or "into" but got %s' % token lexer.syntax_error(msg, self) # Expect a list of variable names. while 1: # Variable name. lexer.expect(self, 'whitespace') _, token = lexer.token() lexer.expect(self, 'varname') if token in self.variables: lexer.syntax_error('Duplicate variable name %s', self) self.varnames.append(token) self.variables[token] = [] # Comma. if lexer.next_if('comma'): continue break self.parent.define(**self.variables) if len(self.varnames) != self.regex.n_groups: count = (len(self.varnames), self.regex.n_groups) error = '%s variables, but regex has %s groups' % count lexer.syntax_error(error, self) # Handle the "from" keyword. lexer.skip('whitespace') if lexer.next_if('keyword', 'from'): lexer.expect(self, 'whitespace') self.source = Term(lexer, parser, parent) self.mark_end() def extract(self, context): # Re-initialize the variable content, because this method # might be called multiple times. for varname in self.varnames: self.variables[varname] = [] if self.source is None: buffer = self.parent.get('__response__') else: buffer = self.source.value(context) #print "Buffer contains", buffer # Walk through all lines, matching each one against the regular # expression. for line in buffer: match = self.regex.value(context).search(line) if match is None: continue # If there was a match, store the extracted substrings in our # list variables. i = 0 for varname in self.varnames: i += 1 try: value = match.group(i) except IndexError: # This happens if the user provided a regex with less # groups in it than the number of variables. msg = 'Extract: %s variables, but regular expression' % i msg += '\ncontains only %s groups.' % (i - 1) self.lexer.runtime_error(msg, self) self.variables[varname].append(value) def value(self, context): self.extract(context) if not self.append: self.parent.define(**self.variables) else: for key in self.variables: existing = self.parent.get(key) self.parent.define(**{key: existing + self.variables[key]}) return 1 def dump(self, indent = 0): mode = self.append and 'into' or 'as' source = self.source is not None and self.source or 'buffer' print (' ' * indent) + self.name, self.regex.string, print mode, self.varnames, "from", source
class Extract(Token): def __init__(self, lexer, parser, parent): Token.__init__(self, "Extract", lexer, parser, parent) self.varnames = [] self.variables = {} self.append = False self.source = None if parser.no_prompt: msg = "'extract' keyword does not work with --no-prompt" lexer.syntax_error(msg, self) # First expect a regular expression. lexer.expect(self, "keyword", "extract") lexer.expect(self, "whitespace") self.regex = Regex(lexer, parser, parent) # Expect "as" keyword. lexer.expect(self, "whitespace") if lexer.next_if("keyword", "as"): self.append = False elif lexer.next_if("keyword", "into"): self.append = True else: _, token = lexer.token() msg = 'Expected "as" or "into" but got %s' % token lexer.syntax_error(msg, self) # Expect a list of variable names. while 1: # Variable name. lexer.expect(self, "whitespace") _, token = lexer.token() lexer.expect(self, "varname") if token in self.variables: lexer.syntax_error("Duplicate variable name %s", self) self.varnames.append(token) self.variables[token] = [] # Comma. if lexer.next_if("comma"): continue break self.parent.define(**self.variables) if len(self.varnames) != self.regex.n_groups: count = (len(self.varnames), self.regex.n_groups) error = "%s variables, but regex has %s groups" % count lexer.syntax_error(error, self) # Handle the "from" keyword. lexer.skip("whitespace") if lexer.next_if("keyword", "from"): lexer.expect(self, "whitespace") self.source = Term(lexer, parser, parent) self.mark_end() def extract(self, context): # Re-initialize the variable content, because this method # might be called multiple times. for varname in self.varnames: self.variables[varname] = [] if self.source is None: buffer = self.parent.get("__response__") else: buffer = self.source.value(context) # print "Buffer contains", buffer # Walk through all lines, matching each one against the regular # expression. for line in buffer: match = self.regex.value(context).search(line) if match is None: continue # If there was a match, store the extracted substrings in our # list variables. i = 0 for varname in self.varnames: i += 1 try: value = match.group(i) except IndexError: # This happens if the user provided a regex with less # groups in it than the number of variables. msg = "Extract: %s variables, but regular expression" % i msg += "\ncontains only %s groups." % (i - 1) self.lexer.runtime_error(msg, self) self.variables[varname].append(value) def value(self, context): self.extract(context) if not self.append: self.parent.define(**self.variables) else: for key in self.variables: existing = self.parent.get(key) self.parent.define(**{key: existing + self.variables[key]}) return 1 def dump(self, indent=0): mode = self.append and "into" or "as" source = self.source is not None and self.source or "buffer" print (" " * indent) + self.name, self.regex.string, print mode, self.varnames, "from", source