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


Python Lexer.scan方法代码示例

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


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

示例1: Parser

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import scan [as 别名]
class Parser(object):

    COMMENT = 'COMMENT'
    CODE = 'CODE'

    def __init__(self, fl, m2code=False, ghubf=False,
                 pyg=False, nofolds=False, foldmarker='{{{,}}}'):

        logger.debug(("Parser(fl:%s, m2code:%s, pyg:%s, "
                      "nofolds:%s, foldmarker:%s)")
                     % (fl, m2code, pyg, nofolds, foldmarker))
        self.fd = open(fl)

        self.ghubf = ghubf
        self.m2code = m2code or pyg
        self.pyg = pyg
        self.nofolds = nofolds
        self.foldmarker = foldmarker

        # GitHub code fences take precedence
        if self.ghubf:
            self.m2code = False
            self.pyg = False

        self.isspacey = re.compile('^\s*$')

        self.lexer = Lexer(vim_rules,
                           case_sensitive=True,
                           omit_whitespace=False,
                           )

        global _parser_instance
        _parser_instance = self
    #__init__()

    def block_to_markdown(self, state, block):
        logger.debug("\nblock_to_markdown %s:%s" % (state, block))
        if not (state and block):
            return ""

        res = []
        if state == Parser.CODE:

            # A super dirty and crapy way to remove
            # leading and trailing empty lines.
            while block and self.isspacey.match(block[0]):
                block.pop(0)
            while block and self.isspacey.match(block[-1:][0]):
                block.pop()

            # We ignore empty code blocks
            if not block:
                return ""

            if self.ghubf:
                cont = "".join(block)
                cb = "\n```vim\n%s```\n" % (cont)
            else:
                cont = "    ".join(block)
                cb = "\n    %s" % (cont)
                if self.m2code:
                    cb = "\n    :::vim%s" % (cb)
            logger.debug("code block : '%s'" % cb)
            res.append(cb)

        if state == Parser.COMMENT:
            res.extend(block)

        return "".join(res)
    #block_to_markdown()

    def parse(self):
        """
            A Very simple parser. Simply groups contiguous types of tokens
            together.

        TODO: Get rid of the inner for loop. Need to be able to feed the lexer
        the file object or an iterable wrapper around it so we can simplifiy
        the line reading into the scanner/lexer.
        """

        state = ""
        nstate = None
        block = []
        mkd_res = ""

        lineno = 1
        while 1:
            line = self.fd.readline()
            logger.debug("\nscanning lineno %s:'%s'" % (lineno, line,))

            if not line:
                break
            lineno += 1

            for token in self.lexer.scan(line):
                logger.debug(token)

                nstate = token[0]
                tok = token[1]
#.........这里部分代码省略.........
开发者ID:jeffbuttars,项目名称:Vimdown,代码行数:103,代码来源:parser.py

示例2: while

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import scan [as 别名]
from rdp_parser import Parser
tokens={
        "INT":"[0-9]+",
        "ADD":"\+",
        "SUB":"-",
        "MUL":"\*",
        "DIV":"/"
        }

language=[("Start","Expr"),
          ("Expr","Term","ExprP"),
          ("ExprP","ADD","Term","ExprP"),
          ("ExprP","SUB","Term","ExprP"),
          ("ExprP",""),
          ("Term","INT","TermP"),
          ("TermP","MUL","INT","TermP"),
          ("TermP","DIV","INT","TermP"),
          ("TermP","")
          ]

l=Lexer(tokens)
p=Parser(tokens,language)

while(True):
    string=raw_input("> ")
    tokens_stream=l.scan(string)
    if(p.accept(tokens_stream)):
        print "Accepted"
    else:
        print "Rejected"
开发者ID:andrefreitas,项目名称:feup-comp,代码行数:32,代码来源:hw3.py

示例3: tokenize

# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import scan [as 别名]
    def tokenize(self, content):
        lex = Lexer(self.rules, case_sensitive=False)
        self.tokens = [token for token in lex.scan(content) if token is not None and token.type != "COMMENT"]

        self.current = 0
开发者ID:franckv,项目名称:nscripter2renpy,代码行数:7,代码来源:parser.py


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