示例#1
0
    def group(self):
        if self.tokens[0][0] in ('TK_TYPE', 'TK_STRUCT'):
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            t = self.prev[1]

            if t == 'struct':
                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
                t += ' ' + self.prev[1]

            if not self.tokens[0][0] == 'TK_RPAR':
                raise ParseError('expected \')\' following type')
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]

            if self.tokens[0][0] == 'TK_LBRACE':
                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
                expr = self.compound_initializer()
                if not self.tokens[0][0] == 'TK_RBRACE':
                    raise ParseError(
                        'expected \'}\' following compound initializer')
                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
                return ('COMPOUND', t, expr)

            expr = self.expression()
            return ('CAST', t, expr)
        expr = ('GROUP', self.expression())
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        return expr
示例#2
0
    def parameter_list(self):
        params = tuple()
        if self.tokens[0][0] != 'TK_RPAR':
            while True:

                if len(params) > 255:
                    line, col = self.tokens[0][2], self.tokens[0][3]
                    raise ParseError('too many arguments in list', line, col)

                if not self.tokens[0][0] == 'TK_TYPE':
                    line, col = self.tokens[0][2], self.tokens[0][3]
                    raise ParseError('expected type', line, col)

                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
                type_info = self.prev[1]

                if not self.tokens[0][0] == 'TK_IDENTIFIER':
                    line, col = self.tokens[0][2], self.tokens[0][3]
                    raise ParseError('expected identifier name', line, col)

                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
                name = self.prev[1]

                params += (('PARAMETER', name, type_info), )

                if not self.tokens[0][0] == 'TK_COMMA':
                    break
                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        return params
示例#3
0
    def function_decl(self, type_info):
        params = None
        body = None
        name = self.prev[1]

        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        params = self.parameter_list()
        if not self.tokens[0][0] == 'TK_RPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \')\' following parameters', line, col)

        self.prev, self.tokens = self.tokens[0], self.tokens[1:]  # Consume )

        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        if self.prev[0] == 'TK_LBRACE':
            body = self.block_statement()
        elif self.prev[0] != 'TK_ENDLINE':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected function body or \';\'', line, col)

        if not body:
            return ('DECL_FUNC', name, type_info, params)

        return ('FUNCTION', name, type_info, params, body)
示例#4
0
    def parse(self, json: str) -> Command:
        """
        Parse JSON stringified representation of a command
        :param json: JSON stringified representation of the command
        :return: the corresponding command instance represented in the stringified JSON
        :raises ParseError: in case of parsing an invalid command
        """
        try:
            json = loads(json)
        except Exception:
            raise ParseError(['Invalid JSON'])
        if not isinstance(json, dict):
            raise ParseError(['Invalid JSON'])

        errors = [e.message for e in self.validator.iter_errors(json)]
        if len(errors) > 0:
            raise ParseError(errors)

        cmd_name = json['name']
        cmd: Command = self.classes.get(cmd_name)()

        if 'args' in json.keys():
            cmd.set_arguments(json['args'])

        return cmd
示例#5
0
    def parse_data(self, data):
        """
        解析微信服务器发送过来的数据并保存类中
        :param data: HTTP Request 的 Body 数据
        :raises ParseError: 解析微信服务器数据错误, 数据不合法
        """
        result = {}
        if type(data) == unicode:
            data = data.encode('utf-8')
        elif type(data) == str:
            pass
        else:
            raise ParseError()

        try:
            doc = minidom.parseString(data)
        except Exception:
            raise ParseError()

        params = [ele for ele in doc.childNodes[0].childNodes
                  if isinstance(ele, minidom.Element)]

        for param in params:
            if param.childNodes:
                text = param.childNodes[0]
                result[param.tagName] = text.data

        result['raw'] = data
        result['type'] = result.pop('MsgType').lower()

        message_type = MESSAGE_TYPES.get(result['type'], UnknownMessage)
        self.__message = message_type(result)
        self.__is_parse = True
示例#6
0
    def parse(self):
        """
        Split the tokens into the 5 main components of CAES:
        - PROPOSITION
        - ASSUMPTION
        - ARGUMENT
        - PARAMETER
        """
        found = set()  # set that contains the headers found
        headers = [
            'PROPOSITION', 'ARGUMENT', 'ASSUMPTION', 'PARAMETER', 'ISSUE',
            'PROOFSTANDARD'
        ]
        previous_idx = len(self.tokens)  # starting from the back
        for idx, tok in reversed(list(enumerate(self.tokens))):
            # find tok_type = `STMT`, and check if it is one of the headers
            if tok.tok_type == 'STMT' and tok.c in headers:
                if tok.c not in found:
                    # call generateStruct to create nodes for each headers
                    if tok.c == 'PROPOSITION':
                        toks = self.tokens[idx:previous_idx]
                        self.proposition = self.generateStruct(toks)

                    elif tok.c == 'ASSUMPTION':
                        toks = self.tokens[idx:previous_idx]
                        self.assumption = self.generateStruct(toks)

                    elif tok.c == 'ARGUMENT':
                        toks = self.tokens[idx:previous_idx]
                        self.argument = self.generateStruct(toks)

                    elif tok.c == 'PARAMETER':
                        toks = self.tokens[idx:previous_idx]
                        self.parameter = self.generateStruct(toks)

                    elif tok.c == 'ISSUE':
                        toks = self.tokens[idx:previous_idx]
                        self.issue = self.generateStruct(toks)

                    elif tok.c == 'PROOFSTANDARD':
                        toks = self.tokens[idx:previous_idx]
                        self.proofstandard = self.generateStruct(toks)

                    previous_idx = idx  # update the index so that the next header's indices will not include those of the previous header
                    found.add(tok.c)  # maintain a list of this that is found

                elif tok.c in found:
                    # prevent multiple usage of headers!
                    raise ParseError(
                        'More than one header of {} is found. Check that you only have one of each headers: {}'
                        .format(tok.c, headers))

        if len(found) != len(headers):
            raise ParseError(
                'Expected labels are: {}. However, only {} found.'.format(
                    headers, found))
示例#7
0
    def while_statement(self):
        if not self.tokens[0][0] == 'TK_LPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \'(\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        expr = self.expression()
        if not self.tokens[0][0] == 'TK_RPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \')\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        statement = self.statement()

        return ('WHILE', expr, statement)
示例#8
0
 def validate_arguments(self):
     errors = [e for e in self.validator.iter_errors(self.args)]
     if len(errors) > 0:
         raise ParseError(errors)
     if 'color' in self.args.keys():
         try:
             self.color = parse_color(self.args['color'])
         except ValueError:
             errors = ['color must be an hex or in rgb format']
             raise ParseError(errors)
     self.start = self.args['start'] if 'start' in self.args.keys(
     ) else None
     self.end = self.args['end'] if 'end' in self.args.keys() else None
     self.section_id = self.args['section_id']
示例#9
0
    def function_variable_decl(self):
        expr = None
        type_info = self.prev[1]

        if not self.tokens[0][0] == 'TK_IDENTIFIER':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected identifier name', line, col)

        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        name = self.prev[1]

        if type_info == 'struct':
            type_info += ' ' + name

        if self.tokens[0][0] == 'TK_IDENTIFIER':
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            name = self.prev[1]

        if self.tokens[0][0] == 'TK_LPAR':
            return self.function_decl(type_info)
        elif type_info.startswith('struct') and name not in type_info:
            return self.variable_decl((type_info))
        elif type_info.startswith('struct'):
            return self.struct_decl(type_info)
        else:
            return self.variable_decl(type_info)
示例#10
0
 def return_statement(self):
     stmt = self.expression()
     if not self.tokens[0][0] == 'TK_ENDLINE':
         line, col = self.tokens[0][2], self.tokens[0][3]
         raise ParseError('expected \';\' following expression', line, col)
     self.prev, self.tokens = self.tokens[0], self.tokens[1:]
     return ('RETURN', stmt)
示例#11
0
 def validate_arguments(self):
     errors = [e for e in self.validator.iter_errors(self.args)]
     if len(errors) > 0:
         raise ParseError(errors)
     try:
         test_overlapping([(s['start'], s['end']) for s in self.args['sections']])
     except Overlapping:
         raise ValidationError('section overlapping')
示例#12
0
 def from_string(cls, string):
     assert type(string) is str
     g = re.match(
         "^(inline):([\x21-\x3A,\x3C-\x7E]+?)(?:\|(.+?))?(?:\|(.+?))?$",
         string)
     if not g:
         raise ParseError("Error parsing CryptoKeyParams value: " +
                          repr(string))
     return cls(g.group(1), g.group(2), g.group(3), g.group(4))
示例#13
0
    def if_statement(self):
        if not self.tokens[0][0] == 'TK_LPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \'(\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        expr = self.expression()
        if not self.tokens[0][0] == 'TK_RPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \')\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        statement = self.statement()
        eblock = None

        while self.tokens[0][0] == 'TK_ELSE':
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            eblock = self.statement()

        return ('IF', expr, statement, eblock)
示例#14
0
 def from_string(cls, string):
     assert type(string) is str
     if string == cls.STANDARD:
         return cls.STANDARD
     elif string == cls.CUSTOM:
         return cls.CUSTOM
     elif string == "-":
         return None
     else:
         raise ParseError("SilenceSuppPref is " + repr(string))
示例#15
0
    def call_args(self):
        params = tuple()
        if self.tokens[0][0] != 'TK_RPAR':
            while True:

                if len(params) > 255:
                    line, col = self.tokens[0][2], self.tokens[0][3]
                    raise ParseError('too many arguments in list', line, col)

                params += (self.expression(), )

                if not self.tokens[0][0] == 'TK_COMMA':
                    break
                self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        if not self.tokens[0][0] == 'TK_RPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \')\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        return params
示例#16
0
 def from_string(cls, string):
     assert type(string) is str
     if string == cls.AES_CM_128_HMAC_SHA1_32:
         return cls.AES_CM_128_HMAC_SHA1_32
     elif string == cls.F8_128_HMAC_SHA1_32:
         return cls.F8_128_HMAC_SHA1_32
     elif string == cls.AES_CM_128_HMAC_SHA1_80:
         return cls.AES_CM_128_HMAC_SHA1_80
     else:
         raise ParseError("sdp::CryptoSuite is " + repr(string))
示例#17
0
 def from_string(cls, string):
     """
     a=rtpmap:97 /0
     """
     assert type(string) is str
     g = re.match("^(\d+) ([\w\d\-]*)/(\d+)(?:/(\d+))?$", string)
     if not g:
         raise ParseError("Error parsing rtpmap value: " + repr(string))
     return cls(int(g.group(1)), g.group(2), int(g.group(3)),
                int(g.group(4)) if g.group(4) else None)
示例#18
0
 def from_string(cls, string):
     assert type(string) is str
     g = re.match("^(on|off) (\-|\d+) (\-|standard|custom) (\-|No SID|Fixed Noise|Sampled Noise) (\-|\d+)$", string)
     if g:
         silence_supp_enable = True if g.group(1) == "on" else False
         silence_timer = int(g.group(2)) if g.group(2) != "-" else None
         supp_pref = SilenceSuppPref.from_string(g.group(3))
         sid_use = SilenceSuppSidUse.from_string(g.group(4))
         fxnslevel = int(g.group(5)) if g.group(5) != "-" else None
         return cls(silence_supp_enable, silence_timer, supp_pref, sid_use, fxnslevel)
     else:
         raise ParseError("Error parsing silenceSupp value: " + repr(string))
示例#19
0
 def from_string(cls, string):
     assert type(string) is str
     if string == cls.NO_SID:
         return cls.NO_SID
     elif string == cls.FIXED_NOISE:
         return cls.FIXED_NOISE
     elif string == cls.SAMPLED_NOISE:
         return cls.SAMPLED_NOISE
     elif string == "-":
         return None
     else:
         raise ParseError("SilenceSuppSidUse is " + repr(string))
示例#20
0
 def from_string(cls, string):
     assert type(string) is str
     g = re.match("^(\d+) send (.+)(?: recv (.+))?$", string)
     if g:
         return cls(int(g.group(1)), g.group(2), g.group(3))
     else:
         g = re.match("^(\d+) recv (.+)(?: send (.+))?$", string)
         if g:
             return cls(int(g.group(1)), g.group(3), g.group(2))
         else:
             raise ParseError("Error parsing imageattr value: " +
                              repr(string))
示例#21
0
 def from_string(cls, string):
     assert type(string) is str
     g = re.match("^(\d+) (\S+) (\S+)(?: (.+))?$", string)
     if not g:
         raise ParseError("Error parsing CryptoAttribute value: " +
                          repr(string))
     tag = int(g.group(1))
     crypto_suite = SrtpCryptoSuite.from_string(g.group(2))
     key_params = CryptoKeyParams.from_string(g.group(3))
     session_params = CryptoSessionParams.from_string(
         g.group(4)) if g.group(4) else None
     return cls(tag, crypto_suite, key_params, session_params)
示例#22
0
    def variable_decl(self, type_info):
        expr = None
        name = self.prev[1]

        if self.tokens[0][0] == 'TK_EQUAL':
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            expr = self.expression()

        if not self.tokens[0][0] == 'TK_ENDLINE':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \';\' following declaration', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        return ('DECLARE', name, type_info, expr)
示例#23
0
    def do_while_statement(self):
        statement = self.statement()

        if not self.tokens[0][0] == 'TK_WHILE':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected keyword \'while\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        if not self.tokens[0][0] == 'TK_LPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \'(\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        expr = self.expression()
        if not self.tokens[0][0] == 'TK_RPAR':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \')\'', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        if not self.tokens[0][0] == 'TK_ENDLINE':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \';\' following expression', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        return ('DO_WHILE', expr, statement)
示例#24
0
    def struct_decl_members(self):
        members = tuple()
        while self.tokens[0][0] != 'TK_RBRACE':

            if not self.tokens[0][0] == 'TK_TYPE':
                line, col = self.tokens[0][2], self.tokens[0][3]
                raise ParseError('expected type in member declaration', line,
                                 col)
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            type_info = self.prev[1]

            if not self.tokens[0][0] == 'TK_IDENTIFIER':
                line, col = self.tokens[0][2], self.tokens[0][3]
                raise ParseError('expected identifier name', line, col)
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            name = self.prev[1]

            if not self.tokens[0][0] == 'TK_ENDLINE':
                line, col = self.tokens[0][2], self.tokens[0][3]
                raise ParseError('expected \';\' after ', name, line, col)
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]

            members += ('MEMBER', name, type_info)
        return members
示例#25
0
    def struct_decl(self, name):
        members = tuple()

        if self.tokens[0][0] == 'TK_LBRACE':
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            members = self.struct_decl_members()

            self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        if not self.tokens[0][0] == 'TK_ENDLINE':
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('expected \';\' following declaration', line, col)
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]

        return ('STRUCT', name, members)
示例#26
0
    def _transcoding(self, data):
        """
        编码转换
        :param data: 需要转换的数据
        :return: 转换好的数据
        """
        if not data:
            return data

        result = None
        if type(data) == unicode:
            result = data.encode('utf-8')
        elif type(data) == str:
            #result = data.decode('utf-8')
            result = data
        else:
            raise ParseError()
        return result
示例#27
0
    def parse_precedence(self, precedence):
        self.prev, self.tokens = self.tokens[0], self.tokens[1:]
        rule = Parser.rule[self.prev[0]][0]
        if not rule:
            line, col = self.tokens[0][2], self.tokens[0][3]
            raise ParseError('bad rule entry, or token', line, col)

        # Since we have to get the function dynamically from
        # the lookup table, we will have to manually pass
        # the reference to the Parser instance
        # to the function as a parameter.
        lhs = rule(self)

        while precedence <= Parser.rule[self.tokens[0][0]][2]:
            self.prev, self.tokens = self.tokens[0], self.tokens[1:]
            rule = Parser.rule[self.prev[0]][1]
            lhs = rule(self, lhs)

        return lhs
示例#28
0
    def find_child(self, value):
        """
        Given the data of the child, iteratively search the Node's children. Once found, return that node.

        """
        # add the list of children (nodes) into the queue
        queue = deque(self.children)

        while len(queue) > 0:
            this_node = queue.pop()

            if this_node.data == value:
                return this_node

            for child_node in this_node.children:  # breath first search
                # if child_node not in visited:
                queue.appendleft(child_node)

        # throw error if not found
        raise ParseError('{} not found in {}'.format(value, self))
def run_server():
    """Run server for receive and send messages
    More information about ZeroMQ in http://zguide.zeromq.org/"""
    context = zmq.Context()
    socket = context.socket(zmq.REP)  # Socket to talk to client
    socket.bind('tcp://127.0.0.1:43000')
    while True:
        try:
            message = socket.recv_string()
            if message:
                print('Recieved message %s' % message)

                type, query = message.split()
                if type == 'GET' and query:  # Check message format, it should look: 'GET query'

                    if not query.isdigit():
                        raise TypeError('{0} is not a number'.format(query))

                    data = get_party(query)
                    socket.send_json(data['suggestions'][0]['data'],
                                     ensure_ascii=False)
                else:
                    raise ParseError(
                        'wrong format message(%s), it should look \'GET query\''
                        % message)
        except zmq.ZMQError as err:
            socket.send_string('ZMQError: {0}'.format(err.errno))
            raise
        except TypeError as err:
            socket.send_string('TypeError: {0}'.format(err))
            print(err)
        except ParseError as err:
            socket.send_string('ParseError: {0}'.format(err.message))
            print(err)
        except Exception as err:
            socket.send_string('Error: {0}'.format(err))
            print(err)
        else:
            print('Send JSON in client is successful')
示例#30
0
    def parse(self):
        ##LL1 parser for Flair
        self.parse_stack.pushProper(TokenType.EOF)
        self.parse_stack.pushProper(NonTerminal.PROGRAM)

        while self.parse_stack.size() > 0:
            #input()
            #print("================")
            #print("Parse", self.parse_stack)
            #print("------------------")
            #print("Semantic",self.semantic_stack)
            #print("==================")
            A = self.parse_stack.top()
            #print(self.semantic_stack.size())

            if isinstance(A, TokenType):
                t = self.scanner.next()
                if t.getTokenType() == A:

                    if t.getTokenType() in [TokenType.IDENTIFIER, \
                                            TokenType.NUMBER, TokenType.INTEGER , TokenType.BOOLEANLITERAL]:
                        self.semantic_stack.pushProper(t.getValue())
                    elif t.getTokenType() == TokenType.TRUE:
                        self.semantic_stack.pushProper('true')
                    elif t.getTokenType() == TokenType.FALSE:
                        self.semantic_stack.pushProper('false')
                    self.parse_stack.pop()
                else:
                    msg = 'token mismatch: {} and {}'
                    raise ParseError(msg.format(A, t))

            elif isinstance(A, NonTerminal):
                t = self.scanner.peek()

                tup = (A, t.getTokenType())
                #print("TUP", tup)
                try:
                    rule = self.parseTable[tup]  #error here (used to be)
                except:
                    msg = 'cannot expand {} on stack: {}'
                    raise ParseError(msg.format(A, t))
                #print("RULE", rule)
                self.parse_stack.pop()
                self.parse_stack.pushRule(rule)

            elif isinstance(A, AstAction):

                action = self.parse_stack.pop()
                #print("ACTION", action, "resulting stack ->")

                self.astTable[action](self.semantic_stack)
            else:
                msg = 'invalid item on stack: {}'
                raise ParseError(msg.format(A))

        if not t.isEOF():
            msg = 'unexpected token at end: {}'
            raise ParseError(msg.format(t))
        #print("SIZE:", self.semantic_stack.size())
        if (self.semantic_stack.size()) > 1:
            raise ValueError("Something went wrong")
        return (self.semantic_stack.pop())