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


Python Lexer.expect_one_of方法代码示例

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


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

示例1: Parser

# 需要导入模块: from Lexer import Lexer [as 别名]
# 或者: from Lexer.Lexer import expect_one_of [as 别名]
class Parser(object):
    def __init__(self, file=None, stream=None, encoding="utf-8"):
        self.lexer = Lexer(file, stream, encoding)
        for NodeType in [
            AST.Rule,
            AST.Define,
            AST.Pattern,
            AST.Section,
            AST.SectionReference
        ]:
            setattr(self, NodeType.__name__, self.create_line_wrapper(NodeType))
        self.rules_file = [self.Section('::main::', None)]
        
    def create_line_wrapper(self, NodeType):
        """
        Uses reflection to create a version of each task with the line 
        number of the rules file lexer bound to the constructor
        @param NodeType: a Python class to wrap
        """
        parent_self = self
        class Wrapper(NodeType):
            def __init__(self, *args, **kw_args):
                NodeType.__init__(self, *args, line_number=parent_self.lexer.line, **kw_args)
        Wrapper.__name__ = NodeType.__name__ + 'Wrapper'
        return Wrapper
        
    def parse(self):
        """
        Parse the rules file into an AST tree
        @return: a Section object described by the rules file
        """
        self.lexer.skip('whitespace', 'comment', 'newline')
        while self.lexer.token != 'end of stream':
            self.parse_statement()
            self.lexer.skip('whitespace', 'comment', 'newline')
            
        if len(self.rules_file) > 1:
            raise RulesFileException("'Section' without 'End Section'")
        return self.rules_file.pop()
        
    def parse_statement(self):
        """
        Parse parse a single statement in the rules file, and attach it
        to the AST object
        @return: None
        """
        list_is_keyword = lambda x, y: len(x) == 1 and x[0].lower() == y.lower()    
        
        id, commands = self.parse_commands_and_id()
        self.lexer.skip('whitespace')
        if list_is_keyword(commands, 'let'):
            # Let ID = PATTERN
            self.lexer.expect('equals')
            self.lexer.skip('whitespace')
            self.parse_definition(id)
        elif len(commands) == 0 and id is not None and id.lower() == 'skip':
            # Skip: RULE
            self.lexer.expect('colon')
            self.parse_rule(None, 'skip')
        elif list_is_keyword(commands, 'section'):
            # Section ID
            self.lexer.skip('whitespace')
            attributes = self.parse_section_attributes()
            inherits = 'inherits' in attributes
            exits = 'exits' in attributes
            new_section = self.Section(id, self.rules_file[-1], inherits=inherits, exits=exits)
            self.rules_file[-1].add_scope('section', new_section)
            self.rules_file.append(new_section)
        elif list_is_keyword(commands, 'end') and id.lower() == 'section':
            # End Section
            if len(self.rules_file) < 2:
                self.lexer.throw('"End Section" statement when not in a section')
            self.rules_file.pop()
        else:
            # (COMMAND (,COMMAND)*)? ID: RULE
            if self.lexer.token in ('colon', 'literalsingle', 'literaldouble'):
                self.lexer.expect('colon')
            self.parse_rule(id, commands)
        self.lexer.skip('whitespace', 'comment')
        self.lexer.expect_one_of('newline', 'endofstream')
        
    def parse_commands_and_id(self):
        """
        Parse out a list of commands followed by an identifier
        @return: tuple with a string of the rule name and a list of commands. If ID is not provided, it is None.
        """
        id = self.lexer.expect('identifier')
        commands = []
        self.lexer.skip('whitespace')
        while self.lexer.token == 'comma':
            self.lexer.expect('comma')
            self.lexer.skip('whitespace')
            commands.append(self.lexer.expect('identifier'))
            self.lexer.skip('whitespace')
        if len(commands) > 0:
            commands.insert(0, id)
            id = None
        if id is None:
            id = self.lexer.expect('identifier')
        elif self.lexer.token == 'identifier':
#.........这里部分代码省略.........
开发者ID:bihai,项目名称:poodle-lex,代码行数:103,代码来源:Parser.py


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