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


Python Lexer.get_next方法代码示例

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


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

示例1: Parser

# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import get_next [as 别名]

#.........这里部分代码省略.........
        if len(commands) == 0 and id.lower() == 'skip':
            # Special case: command "skip" can be used without identifier
            return None, [id]
        else:
            return id, commands

    def parse_rule(self, name, actions):
        """
        Parse an expression and add the rule to the current section
        @return: None
        """
        self.lexer.skip('whitespace', 'comment')
        if self.lexer.token in ['newline', 'endofstream'] or self.lexer.is_keyword('enter', 'exit', 'switch'):
            expression = None
        else:
            expression = self.parse_expression()
        self.lexer.skip('whitespace')
        rule = self.Rule(name, expression, actions)
        self.rules_file[-1].add('rule', rule)
        if self.lexer.token == 'identifier':
            keyword = self.lexer.expect_keywords('enter', 'exit', 'switch').lower()
            self.lexer.skip('whitespace')
            if keyword == 'enter' or keyword == 'switch':
                self.lexer.skip('whitespace')
                text = self.lexer.expect('identifier')
                if text.lower() == 'section':
                    section = self.Section(name, self.rules_file[-1])
                    self.lexer.skip('whitespace')
                    attributes = self.parse_section_attributes()
                    section.inherits = 'inherits' in attributes
                    section.exits = 'exits' in attributes
                    rule.section_action = (keyword, section)
                    self.rules_file[-1].add_scope('section', section)
                    self.rules_file.append(section)
                else:
                    rule.section_action = (keyword, self.SectionReference(text))
            elif keyword == 'exit':
                rule.section_action = ('exit', None)
                self.lexer.expect_keywords('section')
                
    def parse_definition(self, name):
        """
        Parse an expression and add the define to the current section
        @param name: the ID of the definition being parsed
        @return: None
        """
        self.lexer.skip('whitespace')
        expression = self.parse_expression()
        define = self.Define(name, expression)
        self.rules_file[-1].add('define', define)
        
    def parse_expression(self):
        """
        Parse a concatenated list of regular expression strings and return
        @return: a Pattern object representing the expression
        """
        self.lexer.skip('whitespace')
        attributes = AST.PatternAttributes(False, False, False)
        history = {}
        if self.lexer.token == 'identifier':
            for letter in self.lexer.text.lower():
                if letter in history:
                    raise RulesFileException("Duplicate attribute '{id}'".format(id=letter))
                if letter == 'i':
                    attributes.is_case_insensitive = True
                elif letter == 'l':
                    attributes.is_literal = True
                elif letter == 'u':
                    attributes.is_unicode_default = True
                else:
                    raise RulesFileException("Unrecognized attribute '{id}'".format(id=letter))
                history[letter] = True
            self.lexer.get_next()
        string_value = self.lexer.expect_string()
        self.lexer.skip('whitespace')
        while self.lexer.token == 'plus':
            self.lexer.get_next()
            self.lexer.skip('whitespace', 'newline', 'comment')
            string_value += self.lexer.expect_string()
            self.lexer.skip('whitespace')
        return self.Pattern(string_value, attributes)
        
    def parse_section_attributes(self):
        """
        Parse a comma-delimited list of section attributes
        """
        attributes = set()
        while self.lexer.is_keyword('inherits', 'exits'):
            keyword = self.lexer.expect('identifier').lower()
            if keyword in attributes:
                raise Exception("duplicate attribute '{keyword}'".format(keyword=keyword))
            attributes.add(keyword)
            self.lexer.skip('whitespace')
            if self.lexer.token == 'comma':
                self.lexer.get_next()
                self.lexer.skip('whitespace')
            else:
                break
        return attributes
        
开发者ID:bihai,项目名称:poodle-lex,代码行数:103,代码来源:Parser.py


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