本文整理汇总了Python中utils.sequences.consume函数的典型用法代码示例。如果您正苦于以下问题:Python consume函数的具体用法?Python consume怎么用?Python consume使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了consume函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: char_literal
def char_literal(char_stream, location):
char = consume(char_stream) and consume(char_stream) # consume initial single quote, consume char
if char == TOKENS.SINGLE_QUOTE: # empty char ...
return CHAR('', location)
if char == '\\': # if char is being escaped
char = escape_characters.get(peek(char_stream), consume(char_stream))
return error_if_not_value(char_stream, TOKENS.SINGLE_QUOTE) and CHAR(char, location)
示例2: composite_specifier
def composite_specifier(
tokens,
symbol_table,
obj_type=StructType,
member_parse_func=parse_struct_members,
terminal=object()
):
"""
: 'composite type' IDENTIFIER
| 'composite type' IDENTIFIER '{' members '}'
| 'composite type' '{' members '}'
"""
location = loc(consume(tokens))
if peek_or_terminal(tokens) == TOKENS.LEFT_BRACE: # anonymous composite ...
return obj_type(None, member_parse_func(tokens, symbol_table), location)
if isinstance(peek_or_terminal(tokens), IDENTIFIER):
obj = symbol_table.get(obj_type.get_name(peek(tokens)), obj_type(consume(tokens), None, location))
# some composites are bit tricky such as Struct/Union ...
# since any of its members may contain itself as a reference, so we'll add the type to
# the symbol table before adding the members ...
# TODO: make types immutable, right now they are being shared.
if symbol_table.get(obj.name, terminal) is terminal:
symbol_table[name(obj)] = obj
if peek_or_terminal(tokens) == TOKENS.LEFT_BRACE:
obj.members = member_parse_func(tokens, symbol_table)
return obj
raise ValueError('{l} Expected IDENTIFIER or LEFT_BRACE got {got}'.format(
l=loc(peek(tokens, EOFLocation)), got=peek(tokens, '')
))
示例3: arrow_operator
def arrow_operator(tokens, symbol_table, primary_exp):
l = loc(consume(tokens))
_ = error_if_not_type(c_type(primary_exp), PointerType), \
error_if_not_type(c_type(c_type(primary_exp)), (StructType, UnionType))
member_name = error_if_not_type(consume(tokens, EOFLocation), IDENTIFIER)
return ElementSelectionThroughPointerExpression(
primary_exp, member_name, c_type(member(c_type(c_type(primary_exp)), member_name))(l), l
)
示例4: unary_operator
def unary_operator(tokens, symbol_table):
operator = consume(tokens)
if operator == TOKENS.LOGICAL_AND:
return AddressOfLabelExpression(
error_if_not_type(consume(tokens, ''), IDENTIFIER), void_pointer_type(loc(operator)), loc(operator)
)
cast_exp = symbol_table['__ cast_expression __'](tokens, symbol_table)
return rules(unary_operator)[operator](cast_exp, operator)
示例5: _values
def _values(char_stream):
while peek_or_terminal(char_stream) is not terminal:
current_value = consume(char_stream)
yield current_value
# we have consumed a star check if its adjacent value is a forward slash if it is consume and break
if current_value == TOKENS.STAR and peek_or_terminal(char_stream) == TOKENS.FORWARD_SLASH:
yield consume(char_stream)
break
示例6: pre_processor
def pre_processor(char_stream, location): # returns pre_processing symbol or #identifier ...
values = consume(char_stream)
if peek_or_terminal(char_stream) == TOKENS.NUMBER_SIGN: # token concatenation symbol ...
values += consume(char_stream)
else:
_ = exhaust(takewhile({' ', '\t', '\a'}.__contains__, char_stream))
values += ''.join(takewhile(letters.__contains__, char_stream))
return rules(pre_processor).get(values, IDENTIFIER)(values, location)
示例7: number
def number(char_stream, hexadecimal_chars={'x', 'X'}):
initial_char, _digits = '', digits
if peek_or_terminal(char_stream) == digit(0):
initial_char = consume(char_stream)
if peek_or_terminal(char_stream) in hexadecimal_chars:
initial_char += consume(char_stream)
_digits = hexadecimal_digits
return initial_char + ''.join(takewhile(_digits.__contains__, char_stream))
示例8: DEFINE
def DEFINE(token_seq, macros):
line = get_line(token_seq)
define_token = consume(line)
name = consume(line)
value = consume(line, default=IGNORE())
if value == TOKENS.LEFT_PARENTHESIS and column_number(name) + len(name) == column_number(value):
macro = _func_macro_definition(name, line)
else: # object macro
macro = ObjectMacro(name, tuple(filter_out_empty_tokens(chain((value,), line))))
_ = name in macros and macros.pop(name) and logger.warning('{0} Redefining macro {1}'.format(loc(name), name))
macros[name] = macro
yield IGNORE(location=loc(define_token))
示例9: remove_allocation
def remove_allocation(instrs):
"""
optimize 1 or more sequence of allocations ...
take their sum and if zero replace with the next instruction in case this one is referenced.
other wise do one allocation and remove rest
replace allocate 1 with POP, which only requires a single address translation vs 2 (instr, oprn) for allocate.
"""
alloc_instrs = tuple(takewhile(lambda i: isinstance(i, Allocate) and isinstance(opern(i), (int, long)), instrs))
if not alloc_instrs: # Operand must be non-primitive type (Address) ... must wait for its value.
yield consume(instrs)
else:
total = sum(imap(opern, alloc_instrs))
if total: # non-zero allocates changes the state of the stack.
if total in pop_instrs:
new_instr = next(pop_instrs[total](loc(alloc_instrs[0])))
elif len(alloc_instrs) != 1:
new_instr = alloc_instrs[0]
else:
new_instr = Allocate(loc(alloc_instrs[-1]), total)
yield replace_instrs(new_instr, alloc_instrs)
else: # stack remains unchanged, get next instruction for referencing, it one exists ...
if peek_or_terminal(instrs) is terminal:
yield replace_instr(Pass(loc(alloc_instrs[-1])), alloc_instrs)
else:
replace_instrs(peek(instrs), alloc_instrs)
示例10: _func_macro_arguments
def _func_macro_arguments(line):
symbol_table = SymbolTable()
while peek(line, TOKENS.RIGHT_PARENTHESIS) != TOKENS.RIGHT_PARENTHESIS:
if peek(line) == TOKENS.ELLIPSIS:
arg = FunctionMacroVariadicArgument(IDENTIFIER('__VA_ARGS__', loc(consume(line))))
else:
arg = FunctionMacroArgument(error_if_not_type(consume(line, EOFLocation), (IDENTIFIER, KEYWORD)))
if peek_or_terminal(line) == TOKENS.ELLIPSIS:
arg = FunctionMacroVariadicArgument(IDENTIFIER(arg, loc(consume(line))))
symbol_table[arg] = arg # check for duplicate argument name
yield arg # if ok add to the rest ...
if isinstance(arg, FunctionMacroVariadicArgument): # if variadic argument break ...
break
# consume expected comma if we don't see a right parenthesis ...
_ = peek(line, TOKENS.RIGHT_PARENTHESIS) != TOKENS.RIGHT_PARENTHESIS \
and error_if_not_value(line, TOKENS.COMMA, loc(arg))
示例11: string_literal
def string_literal(char_stream, location):
def _values(char_stream):
while peek(char_stream, TOKENS.DOUBLE_QUOTE) != TOKENS.DOUBLE_QUOTE:
value = consume(char_stream)
value = escape_characters.get(peek(char_stream), consume(char_stream)) if value == '\\' else value
yield value
_ = error_if_not_value(char_stream, TOKENS.DOUBLE_QUOTE)
return consume(char_stream) and STRING(''.join(_values(char_stream)), location)
示例12: function_call
def function_call(tokens, symbol_table, primary_exp):
l = loc(consume(tokens))
func_type = error_if_not_type(c_type(c_type(primary_exp)), FunctionType)
# get expression arguments.
expression_argument_list = ArgumentExpressionList(tuple(get_args(tokens, symbol_table, func_type)), l)
return error_if_not_value(tokens, TOKENS.RIGHT_PARENTHESIS) and FunctionCallExpression(
primary_exp, expression_argument_list, c_type(func_type)(l), l
)
示例13: get_repositioned_line
def get_repositioned_line(char_seq, location): # get next line ...
while not isinstance(peek(char_seq), NewLineStr):
char = consume(char_seq)
if char == '\\' and isinstance(peek(char_seq), NewLineStr):
_ = exhaust(takewhile(lambda token: isinstance(token, NewLineStr), char_seq))
for char in get_repositioned_line(char_seq, location):
yield char
else:
yield Str(char, location)
示例14: argument_expression_list
def argument_expression_list(tokens, symbol_table): # : assignment_expression (',' assignment_expression)*
assignment_expression = symbol_table['__ assignment_expression __']
return chain(
(assignment_expression(tokens, symbol_table),),
starmap(
assignment_expression,
takewhile(lambda i: peek(i[0]) == TOKENS.COMMA and consume(i[0]), repeat((tokens, symbol_table)))
)
)
示例15: parameter_type_list
def parameter_type_list(tokens, symbol_table): # : parameter_declaration (',' parameter_declaration)*
return chain(
(parameter_declaration(tokens, symbol_table),),
imap(
parameter_declaration,
takewhile(lambda tokens: peek(tokens) == TOKENS.COMMA and consume(tokens), repeat(tokens)),
repeat(symbol_table)
)
)