Пример #1
0
    def match(string):
        '''
        Implements the matching for explicit coarray shape specification.

        :param str string: the string to match as deferred shape.

        :return: `None` if there is no match, otherwise a 2-tuple \
                 containing matched coshape-spec-list or `None` and \
                 matched lower-cobound or `None`.
        :rtype: `NoneType` or \
            (:py:class:`fparser.two.Fortran2008.Coshape_Spec_List` or `None`, \
             :py:class:`fparser.two:Fortran2008.Lower_Cobound` or `None`)

        '''
        if not string.endswith('*'):
            return None
        line = string[:-1].rstrip()
        if not line:
            return (None, None)
        if line.endswith(':'):
            line, repmap = string_replace_map(line[:-1].rstrip())
            sep_pos = line.rfind(',')
            if sep_pos == -1:
                return (None, Lower_Cobound(repmap(line)))
            return (Coshape_Spec_List(repmap(line[:sep_pos].rstrip())),
                    Lower_Cobound(repmap(line[sep_pos + 1:].lstrip())))
        if not line.endswith(','):
            return None
        line = line[:-1].rstrip()
        return (Coshape_Spec_List(line), None)
Пример #2
0
    def match(string):
        '''
        Implements the matching for a coarray shape.

        :param str string: the string to match as shape.

        :return: `None` if there is no match, otherwise a 2-tuple with \
                 lower bound if given or `None`, and upper bound.
        :rtype: `NoneType` or \
            (`None`, :py:class:`fparser.two.Fortran2008.Upper_Cobound`) or \
            (:py:class:`fparser.two.Fortran2008.Lower_Cobound`, \
             :py:class:`fparser.two.Fortran2008.Upper_Cobound`)

        '''
        line, repmap = string_replace_map(string)
        if ':' not in line:
            return (None, Upper_Cobound(string))
        lower, upper = line.split(':', 1)
        lower = lower.rstrip()
        upper = upper.lstrip()
        if not upper:
            return None
        if not lower:
            return None
        return (Lower_Cobound(repmap(lower)), Upper_Cobound(repmap(upper)))
Пример #3
0
 def match(lhs_cls, rhs_cls, string, upper_lhs=False, require_rhs=False):
     if not string.endswith(')'):
         return
     line, repmap = string_replace_map(string)
     i = line.rfind('(')
     if i == -1:
         return
     lhs = line[:i].rstrip()
     if not lhs:
         return
     j = line.rfind(')')
     rhs = line[i+1: j].strip()
     if line[j+1:].lstrip():
         return
     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
Пример #4
0
    def match(lhs_cls, op_pattern, rhs_cls, string, right=True,
              exclude_op_pattern=None, is_add=False):
        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, is_add=is_add)
            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.upper()
        if not lhs:
            return
        if not rhs:
            return
        if exclude_op_pattern is not None:
            if exclude_op_pattern.match(op):
                return

        lhs_obj = lhs_cls(repmap(lhs))
        rhs_obj = rhs_cls(repmap(rhs))
        return lhs_obj, op.replace(' ', ''), rhs_obj
Пример #5
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)
Пример #6
0
 def match(lhs_cls, rhs_cls, string, require_lhs=False, require_rhs=False):
     line, repmap = string_replace_map(string)
     if ':' not in line:
         return
     lhs, rhs = line.split(':', 1)
     lhs = lhs.rstrip()
     rhs = rhs.lstrip()
     lhs_obj, rhs_obj = None, None
     if lhs:
         if lhs_cls is None:
             return
         lhs_obj = lhs_cls(repmap(lhs))
     elif require_lhs:
         return
     if rhs:
         if rhs_cls is None:
             return
         rhs_obj = rhs_cls(repmap(rhs))
     elif require_rhs:
         return
     return lhs_obj, rhs_obj
Пример #7
0
 def match(decl_type_spec_cls, attr_spec_list_cls,
           entity_decl_list_cls, string):
     line, repmap = string_replace_map(string)
     i = line.find('::')
     if i != -1:
         j = line[:i].find(',')
         if j != -1:
             i = j
     else:
         if line[:6].upper() == 'DOUBLE':
             m = re.search(r'\s[a-z_]', line[6:].lstrip(), re.I)
             if m is None:
                 return
             i = m.start() + len(line)-len(line[6:].lstrip())
         else:
             m = re.search(r'\s[a-z_]', line, re.I)
             if m is None:
                 return
             i = m.start()
     type_spec = decl_type_spec_cls(repmap(line[:i].rstrip()))
     if type_spec is None:
         return
     line = line[i:].lstrip()
     if line.startswith(','):
         i = line.find('::')
         if i == -1:
             return
         attr_specs = attr_spec_list_cls(repmap(line[1:i].strip()))
         if attr_specs is None:
             return
         line = line[i:]
     else:
         attr_specs = None
     if line.startswith('::'):
         line = line[2:].lstrip()
     entity_decls = entity_decl_list_cls(repmap(line))
     if entity_decls is None:
         return
     return type_spec, attr_specs, entity_decls
Пример #8
0
def test_string_replace_map():
    '''Tests string_replace_map function.'''
    result, string_map = string_replace_map('a()')
    assert result == 'a()'
    assert string_map == {}