Ejemplo n.º 1
0
 def process_item(self):
     line = self.item.get_line()[8:].lstrip()
     if line.lower()=='none':
         self.items = []
         return
     items = []
     for item in split_comma(line, self.item):
         i = item.find('(')
         assert i!=-1 and item.endswith(')'),repr(item)
         specs = []
         for spec in split_comma(item[i+1:-1].strip(), self.item):
             if '-' in spec:
                 s,e = spec.lower().split('-')
                 s = s.strip()
                 e = e.strip()
                 assert s in self.letters and e in self.letters,repr((s,e))
             else:
                 e = s = spec.lower().strip()
                 assert s in self.letters,repr((s,e))
             specs.append((s,e))
         tspec = item[:i].rstrip()
         stmt = None
         for cls in declaration_type_spec:
             if cls.match(tspec):
                 stmt = cls(self, self.item.copy(tspec))
                 if stmt.isvalid:
                     break
         assert stmt is not None,repr((item,line))
         items.append((stmt,specs))
     self.items = items
     return
Ejemplo n.º 2
0
def test_split_comma_exceptions():
    ''' Test that we raise the expected exceptions if we don't supply
    the brackets in the right form '''
    with pytest.raises(ParseError) as excinfo:
        _ = split_comma("one, two", brackets="()")
    assert "brackets must be a tuple" in str(excinfo.value)
    with pytest.raises(ParseError) as excinfo:
        _ = split_comma("one, two", brackets=("()", ))
    assert "brackets tuple must contain just two items" in str(excinfo.value)
    with pytest.raises(ParseError) as excinfo:
        _ = split_comma("one, two", brackets=("(", "(", "("))
    assert "brackets tuple must contain just two items" in str(excinfo.value)
Ejemplo n.º 3
0
def test_split_comma():
    ''' Test the split_comma() function '''
    items = split_comma("hello, goodbye")
    print(items)
    assert items[0] == "hello"
    assert items[1] == "goodbye"
    # With trailing and leading white space
    items = split_comma("  hello, goodbye   ")
    print(items)
    assert items[0] == "hello"
    assert items[1] == "goodbye"
    items = split_comma("  ")
    assert not items
Ejemplo n.º 4
0
 def process_item(self):
     line = self.item.get_line()[4:].lstrip()
     if line.startswith('('):
         self.isvalid = False
         return
     specs = []
     i = line.find('::')
     if i != -1:
         for s in line[:i].split(','):
             s = s.strip()
             if s:
                 specs.append(s)
         line = line[i + 2:].lstrip()
     self.specs = specs
     i = line.find('(')
     if i != -1:
         self.name = line[:i].rstrip()
         assert line[-1] == ')', repr(line)
         self.params = split_comma(line[i + 1:-1].lstrip())
     else:
         self.name = line
         self.params = []
     if not is_name(self.name):
         self.isvalid = False
         return
     return BeginStatement.process_item(self)
Ejemplo n.º 5
0
def test_split_bracketed_list():
    ''' Test the splitting of a list bracketed with parentheses '''
    items = split_comma("(well(1), this(is), it)", brackets=("(", ")"))
    print(items)
    assert items[0] == "well(1)"
    assert items[1] == "this(is)"
    # With superfluous white space
    items = split_comma("  (  well(1), this(is), it  )  ", brackets=("(", ")"))
    print(items)
    assert items[0] == "well(1)"
    assert items[1] == "this(is)"
    assert items[2] == "it"
    # Square brackets
    items = split_comma("[well(1), this(is), it]", brackets=("[", "]"))
    print(items)
    assert items[0] == "well(1)"
    assert items[1] == "this(is)"
    assert items[2] == "it"
    # Mis-matched brackets
    items = split_comma("[well(1), this(is), it)", brackets=("[", "]"))
    assert not items
Ejemplo n.º 6
0
 def update(self, *attrs):
     attributes = self.attributes
     if len(attrs) == 1 and isinstance(attrs[0], (tuple, list)):
         attrs = attrs[0]
     for attr in attrs:
         lattr = attr.lower()
         uattr = attr.upper()
         if lattr.startswith('dimension'):
             assert self.dimension is None, repr((self.dimension, attr))
             line = attr[9:].lstrip()
             assert line[0] + line[-1] == '()', repr(line)
             self.set_dimension(
                 split_comma(line[1:-1].strip(), self.parent.item))
             continue
         if lattr.startswith('intent'):
             line = attr[6:].lstrip()
             assert line[0] + line[-1] == '()', repr(line)
             self.set_intent(
                 specs_split_comma(line[1:-1].strip(),
                                   self.parent.item,
                                   upper=True))
             continue
         if lattr.startswith('bind'):
             line = attr[4:].lstrip()
             assert line[0] + line[-1] == '()', repr(line)
             self.bind = specs_split_comma(line[1:-1].strip(),
                                           self.parent.item,
                                           upper=True)
             continue
         if lattr.startswith('check'):
             line = attr[5:].lstrip()
             assert line[0] + line[-1] == '()', repr(line)
             self.check.extend(
                 split_comma(line[1:-1].strip(), self.parent.item))
             continue
         if uattr not in attributes:
             if uattr not in self.known_attributes:
                 self.parent.warning('unknown attribute %r' % (attr))
             attributes.append(uattr)
     return
Ejemplo n.º 7
0
 def _parse_char_selector(self, selector):
     if not selector:
         return '',''
     if selector.startswith('*'):
         l = selector[1:].lstrip()
         if l.startswith('('):
             if l.endswith(','): l = l[:-1].rstrip()
             assert l.endswith(')'),repr(l)
             l = l[1:-1].strip()
             if l.lower().startswith('len'):
                 l = l[3:].lstrip()[1:].lstrip()
         kind=''
     else:
         assert selector[0]+selector[-1]=='()',repr(selector)
         l = split_comma(selector[1:-1].strip(), self.item)
         if len(l)==1:
             l = l[0]
             key, value = self._split_char_selector(l)
             if key=='len':
                 kind, l = '', value
             elif key=='kind':
                 kind, l = value, ''
             else:
                 kind = ''
         else:
             assert len(l)==2,repr(l)
             key0, value0 = self._split_char_selector(l[0])
             key1, value1 = self._split_char_selector(l[1])
             if key0=='len':
                 assert key1 in [None, 'kind'],repr(key1)
                 l,kind = value0, value1
             elif key0=='kind':
                 assert key1=='len',repr(key1)
                 l,kind = value1, value0
             else:
                 assert key0 is None,repr(key0)
                 assert key1 in [None,'kind'],repr(key1)
                 l, kind = value0, value1
     return l,kind
Ejemplo n.º 8
0
    def process_item(self):
        item = self.item
        apply_map = item.apply_map
        clsname = self.__class__.__name__.lower()
        line = item.get_line()
        from .block_statements import Function

        if not line.lower().startswith(clsname):
            i = 0
            j = 0
            for c in line:
                i += 1
                if c==' ': continue
                j += 1
                if j==len(clsname):
                    break
            line = line[:i].replace(' ','') + line[i:]

        assert line.lower().startswith(clsname),repr((line,clsname))
        line = line[len(clsname):].lstrip()

        if line.startswith('('):
            i = line.find(')')
            selector = apply_map(line[:i+1].strip())
            line = line[i+1:].lstrip()
        elif line.startswith('*'):
            selector = '*'
            line = line[1:].lstrip()
            if line.startswith('('):
                i = line.find(')')
                selector += apply_map(line[:i+1].rstrip())
                line = line[i+1:].lstrip()
            else:
                m = re.match(r'\d+(_\w+|)|[*]',line)
                if not m:
                    self.isvalid = False
                    return
                i = m.end()
                selector += line[:i].rstrip()
                line = line[i:].lstrip()
        else:
            selector = ''

        fm = Function.match(line)
        if fm:
            l2 = line[:fm.end()]
            m2 = re.match(r'.*?\b(?P<name>\w+)\Z',l2)
            if not m2:
                self.isvalid = False
                return
            fname = m2.group('name')
            fitem = item.copy(clsname+selector+' :: '+fname,
                              apply_map=True)
            self.parent.put_item(fitem)
            item.clone(line)
            self.isvalid = False
            return

        if line.startswith(','):
            line = line[1:].lstrip()

        self.raw_selector = selector
        if isinstance(self, Character):
            self.selector = self._parse_char_selector(selector)
        else:
            self.selector = self._parse_kind_selector(selector)

        i = line.find('::')
        if i==-1:
            self.attrspec = []
            self.entity_decls = split_comma(line, self.item)
        else:
            self.attrspec = split_comma(line[:i].rstrip(), self.item)
            self.entity_decls = split_comma(line[i+2:].lstrip(), self.item)
        for entity in self.entity_decls:
            if not is_entity_decl(entity):
                self.isvalid = False
                return

        if isinstance(self.parent, Function) \
               and self.parent.name in self.entity_decls:
            assert self.parent.typedecl is None,repr(self.parent.typedecl)
            self.parent.typedecl = self
            self.ignore = False
            if (len(self.entity_decls)>1):
                self.entity_decls.remove(self.parent.name)
            else:
                self.ignore = True
        if isinstance(self, Type):
            self.name = self.selector[1].lower()
            assert is_name(self.name),repr(self.name)
        else:
            self.name = clsname
        return