本文整理汇总了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]
#.........这里部分代码省略.........
示例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"
示例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