import re import sys from base_classes import Statement # Auxiliary tools is_name = re.compile(r'\w+\Z').match def split_comma(line, item): newitem = item.copy(line, True) apply_map = newitem.apply_map items = [] for s in newitem.get_line().split(','): s = apply_map(s).strip() if not s: continue items.append(s) return items def specs_split_comma(line, item): specs0 = split_comma(line, item) specs = [] for spec in specs0: i = spec.find('=') if i!=-1: kw = spec[:i].strip().upper() v = spec[i+1:].strip() specs.append('%s = %s' % (kw, v)) else: specs.append(spec) return specs class StatementWithNamelist(Statement): """ [ :: ] """ def process_item(self): assert not self.item.has_map() clsname = self.__class__.__name__.lower() line = self.item.get_line()[len(clsname):].lstrip() if line.startswith('::'): line = line[2:].lstrip() self.items = [s.strip() for s in line.split(',')] return def __str__(self): clsname = self.__class__.__name__.upper() s = ', '.join(self.items) if s: s = ' ' + s return self.get_indent_tab() + clsname + s # Execution statements class GeneralAssignment(Statement): """ = => """ match = re.compile(r'\w[^=]*\s*=\>?').match item_re = re.compile(r'(?P\w[^=]*)\s*(?P=\>?)\s*(?P.*)\Z',re.I).match def process_item(self): m = self.item_re(self.item.get_line()) if not m: self.isvalid = False return self.sign = sign = m.group('sign') if isinstance(self, Assignment) and sign != '=': self.isvalid = False return elif isinstance(self, PointerAssignment) and sign != '=>': self.isvalid = False return else: if sign=='=>': self.__class__ = PointerAssignment else: self.__class__ = Assignment apply_map = self.item.apply_map self.variable = apply_map(m.group('variable').replace(' ','')) self.expr = apply_map(m.group('expr')) return def __str__(self): return self.get_indent_tab() + '%s %s %s' \ % (self.variable, self.sign, self.expr) class Assignment(GeneralAssignment): pass class PointerAssignment(GeneralAssignment): pass class Assign(Statement): """ ASSIGN