当前位置: 首页>>代码示例>>Python>>正文


Python Scanner.require_token方法代码示例

本文整理汇总了Python中scanner.Scanner.require_token方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.require_token方法的具体用法?Python Scanner.require_token怎么用?Python Scanner.require_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scanner.Scanner的用法示例。


在下文中一共展示了Scanner.require_token方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: kwargs

# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import require_token [as 别名]

#.........这里部分代码省略.........
        try:
            result = self.__parse_elements()
        except UnexpectedTokenException:
            pass
        self.__advance_requiring(TokenType.rsquare)

        return result

    def __parse_members(self):
        """
        Parses members of potential object
        :return: dictionary with members
        """
        result = {}
        while True:
            try:
                name, value = self.__parse_pair()
                if self.simple_value is False:
                    result[name] = JSONValueModel(name, value, predict_references=self.predict_references)
                else:
                    result[name] = value
            except UnexpectedTokenException:
                raise UnexpectedTokenException
            try:
                self.__advance_requiring(TokenType.comma)
                if self.scanner.peek(1) == u'}':
                    raise MalformedObjectException("Malformed object: got comma and rbrace")
            except UnexpectedTokenException:
                return result

    def __parse_pair(self):
        """
        Parses pair of string: value
        :return: name and value
        """
        name = self.__parse_string()
        self.__advance_requiring(TokenType.colon)
        try:
            value = self.__parse_value()
        except TokenMismatchException as e:
            raise e

        return name, value

    def __parse_elements(self):
        """
        Parses elements of potential array
        :return: list filled with elements
        """
        array = []
        while True:
            try:
                array.append(self.__parse_value())
                self.__advance_requiring(TokenType.comma)
                if self.scanner.peek(1) == u']':
                    raise MalformedArrayException("Malformed array: got comma and rsquare instead of value")
            except UnexpectedTokenException:
                return array

    def __parse_string(self):
        self.__advance_requiring(TokenType.string)
        return self.scanner.token.value[1:-1]  # removes enclosing quotes

    def __parse_number(self):
        self.__advance_requiring(TokenType.number)
        try:
            return int(self.scanner.token.value)
        except ValueError:
            pass

        try:
            return float(self.scanner.token.value)
        except ValueError:
            raise TokenMismatchException

    def __parse_bool(self):
        try:
            self.__advance_requiring(TokenType.true)
            return True
        except UnexpectedTokenException:
            pass

        try:
            self.__advance_requiring(TokenType.false)
            return False
        except UnexpectedTokenException as e:
            raise e

    def __parse_null(self):
        try:
            self.__advance_requiring(TokenType.null)
            return None
        except UnexpectedTokenException as e:
            raise e

    def __advance_requiring(self, token_type):
        #print "Requiring: {0}".format(token_type)
        self.scanner.advance()
        self.scanner.require_token(token_type)
        self.parsed_string += self.scanner.token.value
开发者ID:theddnc,项目名称:json2code,代码行数:104,代码来源:parser.py


注:本文中的scanner.Scanner.require_token方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。