Exemplo n.º 1
0
 def match(lhs_cls, rhs_cls, string, upper_lhs = False, require_rhs=False, parens='()'):
     if not string.endswith(parens[1]): return
     line, repmap = string_replace_map(string)
     i = line.rfind(parens[0])
     if i==-1: return
     lhs = line[:i].rstrip()
     if not lhs: return
     rhs = line[i+1:-1].strip()
     lhs = repmap(lhs)
     if upper_lhs:
         lhs = lhs.upper()
     rhs = repmap(rhs)
     if isinstance(lhs_cls, str):
         if lhs_cls!=lhs: return
     else:
         lhs = lhs_cls(lhs)
     if rhs:
         if isinstance(rhs_cls, str):
             if rhs_cls!=rhs: return
         else:
             rhs = rhs_cls(rhs)
         return lhs, rhs
     elif require_rhs:
         return
     return lhs, None
Exemplo n.º 2
0
 def match(lhs_cls, op_pattern, rhs_cls, string, right=True):
     line, repmap = string_replace_map(string)
     if isinstance(op_pattern, str):
         if right:
             t = line.rsplit(op_pattern,1)
         else:
             t = line.split(op_pattern,1)
         if len(t)!=2: return
         lhs, rhs = t[0].rstrip(), t[1].lstrip()
         op = op_pattern
     else:
         if right:
             t = op_pattern.rsplit(line)
         else:
             t = op_pattern.lsplit(line)
         if t is None or len(t)!=3: return
         lhs, op, rhs = t
         lhs = lhs.rstrip()
         rhs = rhs.lstrip()
         op = op
     if not lhs: return
     if not rhs: return
     lhs_obj = lhs_cls(repmap(lhs))
     rhs_obj = rhs_cls(repmap(rhs))
     return lhs_obj, op, rhs_obj
Exemplo n.º 3
0
 def match(separator, subcls, string):
     line, repmap = string_replace_map(string)
     if isinstance(separator, str):
         splitted = line.split(separator)
     else:
         splitted = separator[1].split(line)
         separator = separator[0]
     if len(splitted)<=1: return
     lst = []
     for p in splitted:
         lst.append(subcls(repmap(p.strip())))
     return separator, tuple(lst)
Exemplo n.º 4
0
 def match(string):
     line, repmap = string_replace_map(string)
     t = line.split(':')
     if len(t)<=1 or len(t)>3: return
     lhs_obj,rhs_obj, stride_obj = None, None, None
     if len(t)==2:
         lhs,rhs = t[0].rstrip(),t[1].lstrip()
     else:
         lhs,rhs,stride = t[0].rstrip(),t[1].strip(),t[2].lstrip()
         if stride:
             stride_obj = Expr(repmap(stride))
     if lhs:
         lhs_obj = Expr(repmap(lhs))
     if rhs:
         rhs_obj = Expr(repmap(rhs))
     return lhs_obj, rhs_obj, stride_obj
Exemplo n.º 5
0
    def get_line(self, apply_map=False):
        if apply_map:
            return self.apply_map(self.get_line(apply_map=False))
        if self.strline is not None:
            return self.strline
        line = self.line
        if self.reader.isf77:
            # Handle Hollerith constants by replacing them
            # with char-literal-constants.
            # H constants may appear only in DATA statements and
            # in the argument list of CALL statement.
            # Hollerith constants were removed from the Fortran 77 standard.
            # The following handling is not perfect but works for simple
            # usage cases.
            # todo: Handle hollerith constants in DATA statement
            if _is_call_stmt(line):
                l2 = self.line[4:].lstrip()
                i = l2.find('(')
                if i != -1 and l2[-1] == ')':
                    substrings = ['call ' + l2[:i + 1]]
                    start_search = _hollerith_start_search
                    l2 = l2[i + 1:-1].strip()
                    m = start_search(l2)
                    while m:
                        substrings.append(l2[:m.start()])
                        substrings.append(m.group('pre'))
                        num = int(m.group('num'))
                        substrings.append("'" + l2[m.end():m.end() + num] +
                                          "'")
                        l2 = l2[m.end() + num:]
                        m = start_search(l2)
                    substrings.append(l2)
                    substrings.append(')')
                    line = ''.join(substrings)

        line, str_map = string_replace_map(line, lower=not self.reader.ispyf)
        self.strline = line
        self.strlinemap = str_map
        return line
Exemplo n.º 6
0
    def get_line(self, apply_map=False):
        if apply_map:
            return self.apply_map(self.get_line(apply_map=False))
        if self.strline is not None:
            return self.strline
        line = self.line
        if self.reader.isf77:
            # Handle Hollerith constants by replacing them
            # with char-literal-constants.
            # H constants may appear only in DATA statements and
            # in the argument list of CALL statement.
            # Holleriht constants were removed from the Fortran 77 standard.
            # The following handling is not perfect but works for simple
            # usage cases.
            # todo: Handle hollerith constants in DATA statement
            if _is_call_stmt(line):
                l2 = self.line[4:].lstrip()
                i = l2.find('(')
                if i != -1 and l2[-1]==')':
                    substrings = ['call '+l2[:i+1]]
                    start_search = _hollerith_start_search
                    l2 = l2[i+1:-1].strip()
                    m = start_search(l2)
                    while m:
                        substrings.append(l2[:m.start()])
                        substrings.append(m.group('pre'))
                        num = int(m.group('num'))
                        substrings.append("'"+l2[m.end():m.end()+num]+"'")
                        l2 = l2[m.end()+num:]
                        m = start_search(l2)
                    substrings.append(l2)
                    substrings.append(')')
                    line = ''.join(substrings)

        line, str_map = string_replace_map(line, lower=not self.reader.ispyf)
        self.strline = line
        self.strlinemap = str_map
        return line