本文整理汇总了Python中scanner.Scanner.stringUpToCurrentToken方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.stringUpToCurrentToken方法的具体用法?Python Scanner.stringUpToCurrentToken怎么用?Python Scanner.stringUpToCurrentToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scanner.Scanner
的用法示例。
在下文中一共展示了Scanner.stringUpToCurrentToken方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Parser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import stringUpToCurrentToken [as 别名]
class Parser(object):
def parse(self, sourceStr):
self._completionMessage = "No errors"
self._parseSuccessful = True
self._scanner = Scanner(sourceStr)
self._expression()
self._accept(self._scanner.get(), Token.EOE,
"symbol after end of expression")
def parseStatus(self):
return self._completionMessage
def _accept(self, token, expected, errorMessage):
if token.getType() != expected:
self._fatalError(token, errorMessage)
def _fatalError(self, token, errorMessage):
self._parseSuccessful = False
self._completionMessage = "Parsing error -- " + \
errorMessage + \
"\nExpression so far = " + \
self._scanner.stringUpToCurrentToken()
raise Exception, self._completionMessage
def _expression(self):
"""Syntax rule:
expression = term { addingOperator term } """
self._term()
token = self._scanner.get()
while token.getType() in (Token.PLUS, Token.MINUS):
self._scanner.next()
self._term()
token = self._scanner.get()
def _term(self):
"""Syntax rule:
term = factor { multiplyingOperator factor } """
self._factor()
token = self._scanner.get()
while token.getType() in (Token.MUL, Token.DIV):
self._scanner.next()
self._factor()
token = self._scanner.get()
def _factor(self):
"""Syntax rule:
factor = number | "(" expression ") """"
token = self._scanner.get()
if token.getType() == Token.INT:
self._scanner.next()
elif token.getType() == Token.L_PAR:
self._scanner.next()
self._expression()
self._accept(self._scanner.get(),
Token.R_PAR,
"')' expected")
self._scanner.next()
else:
self._fatalError(token, "bad factor")
示例2: Parser
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import stringUpToCurrentToken [as 别名]
class Parser(object):
def parse(self, sourceStr):
self._completionMessage = "No errors"
self._parseSuccessful = True
self._scanner = Scanner(sourceStr)
tree = self._expression()
self._accept(self._scanner.get(), Token.EOE,
"symbol after end of expression")
return tree
def parseStatus(self):
return self._completionMessage
def _accept(self, token, expected, errorMessage):
if token.getType() != expected:
self._fatalError(token, errorMessage)
def _fatalError(self, token, errorMessage):
self._parseSuccessful = False
self._completionMessage = "Parsing error -- " + \
errorMessage + \
"\nExpression so far = " + \
self._scanner.stringUpToCurrentToken()
raise Exception(self._completionMessage)
def _expression(self):
tree = self._term()
token = self._scanner.get()
while token.getType() in (Token.PLUS, Token.MINUS):
op = str(token)
self._scanner.next()
tree = InteriorNode(op, tree, self._term())
token = self._scanner.get()
return tree
def _term(self):
tree = self._factor()
token = self._scanner.get()
while token.getType() in (Token.MUL, Token.DIV):
op = str(token)
self._scanner.next()
tree = InteriorNode(op, tree, self._factor())
token = self._scanner.get()
return tree
def _factor(self):
token = self._scanner.get()
if token.getType() == Token.INT:
tree = LeafNode(token.getValue())
self._scanner.next()
elif token.getType() == Token.L_PAR:
self._scanner.next()
tree = self._expression()
self._accept(self._scanner.get(),
Token.R_PAR,
"')' expected")
self._scanner.next()
else:
tree = None
self._fatalError(token, "bad factor")
return tree