本文整理汇总了Python中lexer.Lexer.eatId方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.eatId方法的具体用法?Python Lexer.eatId怎么用?Python Lexer.eatId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lexer.Lexer
的用法示例。
在下文中一共展示了Lexer.eatId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import eatId [as 别名]
class Parser :
'''
Called by database.py
process create and insert SQL command
raise an exception on syntax error
'''
maxVarcharLen = 40
def __init__(self, query):
self.lex = Lexer(query)
def parse(self):
if self.lex.matchKeyword('create'):
return self.create()
elif self.lex.matchKeyword('insert'):
return self.insert()
else:
return self.select()
def select(self):
self.lex.eatKeyword('select')
projs = self.projectList()
self.lex.eatKeyword('from')
tables = self.tableList()
pred = {}
if self.lex.matchKeyword('where'):
self.lex.eatKeyword('where')
pred = self.predicate()
#print {'select':projs, 'from':tables, 'where':pred}
return {'select':projs, 'from':tables, 'where':pred}
def projectList(self):
fieldNames = []
aggFn = []
if self.lex.matchId() or self.lex.matchDelim('*'):
fieldNames.append(self.projectField())
else:
aggFn.append(self.aggregationFn())
while self.lex.matchDelim(','):
self.lex.eatDelim(',')
if self.lex.matchId():
fieldNames.append(self.projectField())
else:
aggFn.append(self.aggregationFn())
return {'fieldNames':fieldNames, 'aggFn':aggFn}
def projectField(self):
name = ''
if self.lex.matchDelim('*'):
name = self.lex.eatDelim('*')
else:
name = self.lex.eatId()
if self.lex.matchDelim('.'):
name += self.lex.eatDelim('.')
if self.lex.matchDelim('*'):
name += self.lex.eatDelim('*')
else:
name += self.lex.eatId()
return name
def aggregationFn(self):
fnType = ''
field = ''
if self.lex.matchKeyword('count'):
fnType = self.lex.eatKeyword('count')
else:
fnType = self.lex.eatKeyword('sum')
self.lex.eatDelim('(')
field = self.projectField()
self.lex.eatDelim(')')
return {'type':fnType, 'field':field}
def tableList(self):
tList = []
tList.append(self.tableName())
while self.lex.matchDelim(','):
self.lex.eatDelim(',')
tList.append(self.tableName())
return tList
def tableName(self):
alias = ''
name = self.lex.eatId()
if self.lex.matchKeyword('as'):
self.lex.eatKeyword('as')
alias = self.lex.eatId()
return {'tableName':name, 'alias':alias}
def predicate(self):
logic = ''
term2 = {}
term1 = self.term()
if self.lex.matchKeyword('and'):
logic = self.lex.eatKeyword('and')
term2 = self.term()
elif self.lex.matchKeyword('or'):
logic = self.lex.eatKeyword('or')
term2 = self.term()
return {'term1':term1, 'term2':term2, 'logic':logic}
#.........这里部分代码省略.........