Пример #1
0
    def expect_proto_name(self, indent='', step=' '):
        line = self.get_line()
        if indent != '' and not line.startswith(indent):
            raise ParseException("missing indent on protocol line '%s'" % line)

        # from here we ignore any indentation
        words = line.split()
        self.expect_token_count(words, 'protocol', 2)
        if words[0] == 'protocol':
            validate_dotted_name(words[1])
            return words[1]
            print("DEBUG: protocol is '%s'" % str(self._protocol))
        else:
            raise ParserError("expected protocol line, found '%s'" % line)
Пример #2
0
    def expect_field(self, msg_spec, line, indent, step):
        if not line.startswith(indent):
            raise ParserError(
                "wrong indent for field declaration: '%s'" %
                line)

        # DEBUG
        print("expectField: line = '%s'" % line)
        # END

        line = line[len(indent):]

        # from here we are very sloppy about the indent

        # accept NAME FTYPE(Q)? (@N)? (=DEFAULT)?

        words = line.split()
        word_count = len(words)

        # DEBUG
        print("             found %d tokens: %s" % (word_count, words))
        # END

        if word_count < 2:
            raise ParserError("too few tokens in field def '%s'" % line)
        if word_count > 5:
            raise ParserError("too many tokens in field def '%s'" % line)

        # -- field name -------------------------
        f_name = words[0]
        validate_simple_name(f_name)

        # -- quantifier -------------------------
        qqq = words[1][-1]
        if qqq == '?' or qqq == '*' or qqq == '+':
            words[1] = words[1][:-1]
            if qqq == '?':
                quantifier = Q_OPTIONAL
            elif qqq == '*':
                quantifier = Q_STAR
            else:
                quantifier = Q_PLUS
        else:
            quantifier = Q_REQUIRED

        # -- field type --------------------------
        type_name = words[1]
        validate_dotted_name(type_name)
        field_type = None

        # DEBUG ###
        print("             field '%s' type '%s' quant %d" % (
            f_name, type_name, quantifier))
        # END #####

        # first check against list of names of field types
        f_types = FieldStr()
        try:
            field_type = f_types.ndx(type_name)
            # DEBUG
            print(
                "LIST type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END
        except KeyError:
            # DEBUG
            print("NOT IN LIST type_name '%s'" % type_name)
            # END
            field_type = None

        if field_type is None:
            # check at the message level
            field_type = msg_spec.reg.name2reg_id(type_name)
            # DEBUG
            print(
                "MSG type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END

        if field_type is None:
            # ask the parent to resolve
            field_type = msg_spec.parent.reg.name2reg_id(type_name)
            # DEBUG
            print(
                "PARENT type_name is '%s', index is '%s'" %
                (type_name, field_type))
            # END

        if field_type is None:
            err_msg = "type_name = '%s'; can't determine field type in line: '%s'" % (
                type_name, line)
            raise ParserError(err_msg)

        # -- field number -----------------------
        field_nbr = -1
        if word_count > 2:
            if words[2].startswith('@'):
                field_nbr = int(words[2][1:])    # could use some validation
#               if fieldNbr < nextFieldNbr:
#                   raise ValueError('field number <= last field number')

        # -- default ----------------------------
        # XXX STUB - NOT IMPLEMENTED YET

        msg_spec.add_field(
            FieldSpec(msg_spec.reg, f_name, field_type, quantifier, field_nbr))