本文整理汇总了Python中stream.Stream.unread方法的典型用法代码示例。如果您正苦于以下问题:Python Stream.unread方法的具体用法?Python Stream.unread怎么用?Python Stream.unread使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Stream
的用法示例。
在下文中一共展示了Stream.unread方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Lexer
# 需要导入模块: from stream import Stream [as 别名]
# 或者: from stream.Stream import unread [as 别名]
class Lexer():
def __init__(self, stream):
self.stream = Stream(stream)
self.buffer = []
self.tokens = []
self.line = 1
self.col = 1
def read(self):
r = self.stream.read()
if r == "\n":
self.col = 1
return r
def next(self):
r = self.read()
if r != "\x00":
self.buffer.append(r)
return r
def ignore(self):
self.buffer = []
def skip(self):
self.next()
self.ignore()
def fast_forward(self, n):
for i in range(n):
self.next()
def peek(self):
r = self.stream.read()
self.stream.unread()
return r
def emit_value(self, token_type, value):
t = Token((self.line, self.col), token_type, value)
self.tokens.append(t)
print(self.tokens[-1])
self.ignore()
def emit(self, token_type):
self.emit_value(token_type, "".join(self.buffer))
def follow(self, n):
to_unread = 0
for expected in n:
r = self.stream.read()
to_unread += 1
if expected != r:
return False
for i in range(to_unread):
self.stream.unread()
return True
def lex_main(self):
while True:
n = self.peek()
if n == "#":
return self.comment
elif n in ("\n", "\r"):
self.skip()
elif n in (" ", "\t"):
self.skip()
elif n == '"':
return self.string
if n == "\x00":
break
time.sleep(0.25)
return None
def comment(self):
while True:
n = self.peek()
if n == '\n' or n == "\x00":
break
if n == "\r" and self.follow("\r\n"):
break
self.next()
self.ignore()
return self.lex_main
#.........这里部分代码省略.........