import cython
cython.declare(Nodes=object, ExprNodes=object, EncodedString=object)
+import logging
import os
import re
import sys
import Future
import Options
+
+_LOG = logging.getLogger('Cython.Compiler.Parsing')
+_LOG.setLevel(logging.CRITICAL)
+#_LOG.setLevel(logging.DEBUG)
+_LOG.addHandler(logging.StreamHandler())
+_LOG.handlers[-1].setLevel(logging.DEBUG)
+
+
class Ctx(object):
# Parsing context
level = 'other'
return ctx
def p_ident(s, message = "Expected an identifier"):
+ _LOG.debug('p_ident(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'IDENT':
name = s.systring
s.next()
s.error(message)
def p_ident_list(s):
+ _LOG.debug('p_ident_list(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
names = []
while s.sy == 'IDENT':
names.append(s.systring)
#------------------------------------------
def p_binop_operator(s):
+ _LOG.debug('p_binop_operator(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
op = s.sy
s.next()
return op, pos
def p_binop_expr(s, ops, p_sub_expr):
+ _LOG.debug('p_binop_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
n1 = p_sub_expr(s)
while s.sy in ops:
op, pos = p_binop_operator(s)
def p_lambdef(s, allow_conditional=True):
# s.sy == 'lambda'
+ _LOG.debug('p_lambdef(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if s.sy == ':':
#lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
def p_lambdef_nocond(s):
+ _LOG.debug('p_lambdef_nocond(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_lambdef(s, allow_conditional=False)
#test: or_test ['if' or_test 'else' test] | lambdef
def p_test(s):
+ _LOG.debug('p_test(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'lambda':
return p_lambdef(s)
pos = s.position()
#test_nocond: or_test | lambdef_nocond
def p_test_nocond(s):
+ _LOG.debug('p_test_nocond(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'lambda':
return p_lambdef_nocond(s)
else:
#or_test: and_test ('or' and_test)*
def p_or_test(s):
+ _LOG.debug('p_or_test(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_rassoc_binop_expr(s, ('or',), p_and_test)
def p_rassoc_binop_expr(s, ops, p_subexpr):
+ _LOG.debug('p_rassoc_binop_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
n1 = p_subexpr(s)
if s.sy in ops:
pos = s.position()
#and_test: not_test ('and' not_test)*
def p_and_test(s):
+ _LOG.debug('p_and_test(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
#return p_binop_expr(s, ('and',), p_not_test)
return p_rassoc_binop_expr(s, ('and',), p_not_test)
#not_test: 'not' not_test | comparison
def p_not_test(s):
+ _LOG.debug('p_or_test(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'not':
pos = s.position()
s.next()
#comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
def p_comparison(s):
+ _LOG.debug('p_comparison(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
n1 = p_starred_expr(s)
if s.sy in comparison_ops:
pos = s.position()
return n1
def p_test_or_starred_expr(s):
+ _LOG.debug('p_test_or_starred_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == '*':
return p_starred_expr(s)
else:
return p_test(s)
def p_starred_expr(s):
+ _LOG.debug('p_starred_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
if s.sy == '*':
starred = True
return expr
def p_cascaded_cmp(s):
+ _LOG.debug('p_cscaded_cmp(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
op = p_cmp_op(s)
n2 = p_starred_expr(s)
return result
def p_cmp_op(s):
+ _LOG.debug('p_cmp_op(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'not':
s.next()
s.expect('in')
#expr: xor_expr ('|' xor_expr)*
def p_bit_expr(s):
+ _LOG.debug('p_bit_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('|',), p_xor_expr)
#xor_expr: and_expr ('^' and_expr)*
def p_xor_expr(s):
+ _LOG.debug('p_xor_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('^',), p_and_expr)
#and_expr: shift_expr ('&' shift_expr)*
def p_and_expr(s):
+ _LOG.debug('p_and_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('&',), p_shift_expr)
#shift_expr: arith_expr (('<<'|'>>') arith_expr)*
def p_shift_expr(s):
+ _LOG.debug('p_shift_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('<<', '>>'), p_arith_expr)
#arith_expr: term (('+'|'-') term)*
def p_arith_expr(s):
+ _LOG.debug('p_arith_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('+', '-'), p_term)
#term: factor (('*'|'/'|'%') factor)*
def p_term(s):
+ _LOG.debug('p_term(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_binop_expr(s, ('*', '/', '%', '//'), p_factor)
#factor: ('+'|'-'|'~'|'&'|typecast|sizeof) factor | power
def p_factor(s):
+ _LOG.debug('p_factor(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
# little indirection for C-ification purposes
return _p_factor(s)
def _p_factor(s):
+ _LOG.debug('_p_factor(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
sy = s.sy
if sy in ('+', '-', '~'):
op = s.sy
def p_typecast(s):
# s.sy == "<"
+ _LOG.debug('p_typecast(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
base_type = p_c_base_type(s)
def p_sizeof(s):
# s.sy == ident "sizeof"
+ _LOG.debug('p_sizeof(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
s.expect('(')
def p_yield_expression(s):
# s.sy == "yield"
+ _LOG.debug('p_yield_expression(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if s.sy != ')' and s.sy not in statement_terminators:
def p_yield_statement(s):
# s.sy == "yield"
+ _LOG.debug('p_yield_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
yield_expr = p_yield_expression(s)
return Nodes.ExprStatNode(yield_expr.pos, expr=yield_expr)
#power: atom trailer* ('**' factor)*
def p_power(s):
+ _LOG.debug('p_power(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.systring == 'new' and s.peek()[0] == 'IDENT':
return p_new_expr(s)
n1 = p_atom(s)
def p_new_expr(s):
# s.systring == 'new'.
+ _LOG.debug('p_new_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
cppclass = p_c_base_type(s)
#trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
def p_trailer(s, node1):
+ _LOG.debug('p_trailer(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
if s.sy == '(':
return p_call(s, node1)
def p_call_parse_args(s, allow_genexp = True):
# s.sy == '('
+ _LOG.debug('p_call_parse_args(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
positional_args = []
return positional_args, keyword_args, star_arg, starstar_arg
def p_call_build_packed_args(pos, positional_args, keyword_args, star_arg):
+ _LOG.debug('p_call_build_packed_args()')
arg_tuple = None
keyword_dict = None
if positional_args or not star_arg:
def p_call(s, function):
# s.sy == '('
+ _LOG.debug('p_call(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
positional_args, keyword_args, star_arg, starstar_arg = \
def p_index(s, base):
# s.sy == '['
+ _LOG.debug('p_index(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
subscripts = p_subscript_list(s)
return result
def p_subscript_list(s):
+ _LOG.debug('p_subscript_list(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
items = [p_subscript(s)]
while s.sy == ',':
s.next()
# Parse a subscript and return a list of
# 1, 2 or 3 ExprNodes, depending on how
# many slice elements were encountered.
+ _LOG.debug('p_subscript(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
start = p_slice_element(s, (':',))
if s.sy != ':':
def p_slice_element(s, follow_set):
# Simple expression which may be missing iff
# it is followed by something in follow_set.
+ _LOG.debug('p_slice_element(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy not in follow_set:
return p_test(s)
else:
return None
def expect_ellipsis(s):
+ _LOG.debug('expect_ellipsis(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
s.expect('.')
s.expect('.')
s.expect('.')
# by p_subscript_list into a list of ExprNodes,
# creating SliceNodes for elements with 2 or
# more components.
+ _LOG.debug('make_slice_nodes(subscripts=%s)' % (subscripts,))
result = []
for subscript in subscripts:
if len(subscript) == 1:
return result
def make_slice_node(pos, start, stop = None, step = None):
+ _LOG.debug('make_slice_node()')
if not start:
start = ExprNodes.NoneNode(pos)
if not stop:
#atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']' | '{' [dict_or_set_maker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
def p_atom(s):
+ _LOG.debug('p_atom(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
sy = s.sy
if sy == '(':
s.error("Expected an identifier or literal")
def p_int_literal(s):
+ _LOG.debug('p_int_literal(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
value = s.systring
s.next()
longness = longness)
def p_name(s, name):
+ _LOG.debug('p_name(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
if not s.compile_time_expr and name in s.compile_time_env:
value = s.compile_time_env.lookup_here(name)
# A sequence of one or more adjacent string literals.
# Returns (kind, bytes_value, unicode_value)
# where kind in ('b', 'c', 'u', '')
+ _LOG.debug('p_cat_string_literal(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
kind, bytes_value, unicode_value = p_string_literal(s)
if kind == 'c' or s.sy != 'BEGIN_STRING':
return kind, bytes_value, unicode_value
return kind, bytes_value, unicode_value
def p_opt_string_literal(s, required_type='u'):
+ _LOG.debug('p_opt_string_literal(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'BEGIN_STRING':
kind, bytes_value, unicode_value = p_string_literal(s, required_type)
if required_type == 'u':
return None
def check_for_non_ascii_characters(string):
+ _LOG.debug('check_for_non_ascii_characters(string=%s)' % (string))
for c in string:
if c >= u'\x80':
return True
# representations.
# s.sy == 'BEGIN_STRING'
+ _LOG.debug('p_string_literal(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
is_raw = 0
is_python3_source = s.context.language_level >= 3
def p_list_maker(s):
# s.sy == '['
+ _LOG.debug('p_list_maker(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if s.sy == ']':
return ExprNodes.ListNode(pos, args = exprs)
def p_comp_iter(s, body):
+ _LOG.debug('p_comp_iter(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'for':
return p_comp_for(s, body)
elif s.sy == 'if':
def p_comp_for(s, body):
# s.sy == 'for'
+ _LOG.debug('p_comp_for(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
kw = p_for_bounds(s, allow_testlist=False)
def p_comp_if(s, body):
# s.sy == 'if'
+ _LOG.debug('p_comp_if(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
test = p_test_nocond(s)
def p_dict_or_set_maker(s):
# s.sy == '{'
+ _LOG.debug('p_dict_or_set_maker(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if s.sy == '}':
# NOTE: no longer in Py3 :)
def p_backquote_expr(s):
# s.sy == '`'
+ _LOG.debug('p_backquote_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
args = [p_test(s)]
return ExprNodes.BackquoteNode(pos, arg = arg)
def p_simple_expr_list(s, expr=None):
+ _LOG.debug('p_simple_expr_list(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
exprs = expr is not None and [expr] or []
while s.sy not in expr_terminators:
exprs.append( p_test(s) )
return exprs
def p_test_or_starred_expr_list(s, expr=None):
+ _LOG.debug('p_test_or_starred_expr_list(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
exprs = expr is not None and [expr] or []
while s.sy not in expr_terminators:
exprs.append( p_test_or_starred_expr(s) )
#testlist: test (',' test)* [',']
def p_testlist(s):
+ _LOG.debug('p_testlist(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
expr = p_test(s)
if s.sy == ',':
# testlist_star_expr: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
def p_testlist_star_expr(s):
+ _LOG.debug('p_testlist_star_expr(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
expr = p_test_or_starred_expr(s)
if s.sy == ',':
# testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
def p_testlist_comp(s):
+ _LOG.debug('p_testlist_comp(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
expr = p_test_or_starred_expr(s)
if s.sy == ',':
def p_genexp(s, expr):
# s.sy == 'for'
+ _LOG.debug('p_genexp(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
loop = p_comp_for(s, Nodes.ExprStatNode(
expr.pos, expr = ExprNodes.YieldExprNode(expr.pos, arg=expr)))
return ExprNodes.GeneratorExpressionNode(expr.pos, loop=loop)
def p_global_statement(s):
# assume s.sy == 'global'
+ _LOG.debug('p_global_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
names = p_ident_list(s)
return Nodes.GlobalNode(pos, names = names)
def p_expression_or_assignment(s):
+ _LOG.debug('p_expression_or_assignment(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
expr_list = [p_testlist_star_expr(s)]
while s.sy == '=':
s.next()
def p_print_statement(s):
# s.sy == 'print'
+ _LOG.debug('p_print_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
ends_with_comma = 0
s.next()
def p_exec_statement(s):
# s.sy == 'exec'
+ _LOG.debug('p_exec_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
args = [ p_bit_expr(s) ]
def p_del_statement(s):
# s.sy == 'del'
+ _LOG.debug('p_del_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
# FIXME: 'exprlist' in Python
return Nodes.DelStatNode(pos, args = args)
def p_pass_statement(s, with_newline = 0):
+ _LOG.debug('p_pass_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.expect('pass')
if with_newline:
def p_break_statement(s):
# s.sy == 'break'
+ _LOG.debug('p_break_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
return Nodes.BreakStatNode(pos)
def p_continue_statement(s):
# s.sy == 'continue'
+ _LOG.debug('p_continue_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
return Nodes.ContinueStatNode(pos)
def p_return_statement(s):
# s.sy == 'return'
+ _LOG.debug('p_return_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if s.sy not in statement_terminators:
def p_raise_statement(s):
# s.sy == 'raise'
+ _LOG.debug('p_raise_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
exc_type = None
def p_import_statement(s):
# s.sy in ('import', 'cimport')
+ _LOG.debug('p_import_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
kind = s.sy
s.next()
def p_from_import_statement(s, first_statement = 0):
# s.sy == 'from'
+ _LOG.debug('p_from_import_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
(dotted_name_pos, _, dotted_name, _) = \
imported_name_kinds = ('class', 'struct', 'union')
def p_imported_name(s, is_cimport):
+ _LOG.debug('p_imported_name(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
kind = None
if is_cimport and s.systring in imported_name_kinds:
return (pos, name, as_name, kind)
def p_dotted_name(s, as_allowed):
+ _LOG.debug('p_dotted_name(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
target_name = p_ident(s)
as_name = None
return (pos, target_name, u'.'.join(names), as_name)
def p_as_name(s):
+ _LOG.debug('p_as_name(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'IDENT' and s.systring == 'as':
s.next()
return p_ident(s)
def p_assert_statement(s):
# s.sy == 'assert'
+ _LOG.debug('p_assert_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
cond = p_test(s)
def p_if_statement(s):
# s.sy == 'if'
+ _LOG.debug('p_if_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
if_clauses = [p_if_clause(s)]
if_clauses = if_clauses, else_clause = else_clause)
def p_if_clause(s):
+ _LOG.debug('p_if_clause(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
test = p_test(s)
body = p_suite(s)
condition = test, body = body)
def p_else_clause(s):
+ _LOG.debug('p_else_clause(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'else':
s.next()
return p_suite(s)
def p_while_statement(s):
# s.sy == 'while'
+ _LOG.debug('p_while_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
test = p_test(s)
def p_for_statement(s):
# s.sy == 'for'
+ _LOG.debug('p_for_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
kw = p_for_bounds(s, allow_testlist=True)
return Nodes.ForStatNode(pos, **kw)
def p_for_bounds(s, allow_testlist=True):
+ _LOG.debug('p_for_bounds(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
target = p_for_target(s)
if s.sy == 'in':
s.next()
return {}
def p_for_from_relation(s):
+ _LOG.debug('p_for_from_relation(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy in inequality_relations:
op = s.sy
s.next()
s.error("Expected one of '<', '<=', '>' '>='")
def p_for_from_step(s):
+ _LOG.debug('p_for_from_step(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'by':
s.next()
step = p_bit_expr(s)
inequality_relations = ('<', '<=', '>', '>=')
def p_target(s, terminator):
+ _LOG.debug('p_target(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
expr = p_starred_expr(s)
if s.sy == ',':
return expr
def p_for_target(s):
+ _LOG.debug('p_for_target(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
return p_target(s, 'in')
def p_for_iterator(s, allow_testlist=True):
+ _LOG.debug('p_for_iterator(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
if allow_testlist:
expr = p_testlist(s)
def p_try_statement(s):
# s.sy == 'try'
+ _LOG.debug('p_try_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
body = p_suite(s)
def p_except_clause(s):
# s.sy == 'except'
+ _LOG.debug('p_except_clause(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next()
exc_type = None
pattern = exc_type, target = exc_value, body = body)
def p_include_statement(s, ctx):
+ _LOG.debug('p_include_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
s.next() # 'include'
unicode_include_file_name = p_string_literal(s, 'u')[2]
return Nodes.PassStatNode(pos)
def p_with_statement(s):
+ _LOG.debug('p_with_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
s.next() # 'with'
if s.systring == 'template' and not s.in_python_file:
node = p_with_template(s)
return node
def p_with_items(s):
+ _LOG.debug('p_with_items(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
if not s.in_python_file and s.sy == 'IDENT' and s.systring == 'nogil':
state = s.systring
target = target, body = body)
def p_with_template(s):
+ _LOG.debug('p_with_template(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
pos = s.position()
templates = []
s.next()
def p_simple_statement(s, first_statement = 0):
#print "p_simple_statement:", s.sy, s.systring ###
+ _LOG.debug('p_simple_statement(s=<s sy:%s systring:%s>)' % (s.sy, s.systring))
if s.sy == 'global':
node = p_global_statement(s)
elif s.sy == 'print':
def p_simple_statement_list(s, ctx, first_statement = 0):
# Parse a series of simple statements on one line
# separated by semicolons.
+ _LOG.debug('p_simple_statement_list(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
stat = p_simple_statement(s, first_statement = first_statement)
if s.sy == ';':
stats = [stat]
return stat
def p_compile_time_expr(s):
+ _LOG.debug('p_compile_time_expr(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
old = s.compile_time_expr
s.compile_time_expr = 1
expr = p_testlist(s)
return expr
def p_DEF_statement(s):
+ _LOG.debug('p_DEF_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
denv = s.compile_time_env
s.next() # 'DEF'
return Nodes.PassStatNode(pos)
def p_IF_statement(s, ctx):
+ _LOG.debug('p_IF_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
saved_eval = s.compile_time_eval
current_eval = saved_eval
return result
def p_statement(s, ctx, first_statement = 0):
+ _LOG.debug('p_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
decorators = None
if s.sy == 'ctypedef':
if ctx.level not in ('module', 'module_pxd'):
def p_statement_list(s, ctx, first_statement = 0):
# Parse a series of statements separated by newlines.
+ _LOG.debug('p_statement_list(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
stats = []
while s.sy not in ('DEDENT', 'EOF'):
return Nodes.StatListNode(pos, stats = stats)
def p_suite(s, ctx = Ctx(), with_doc = 0, with_pseudo_doc = 0):
+ _LOG.debug('p_suite(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.expect(':')
doc = None
Returns: (positional_args, keyword_args)
"""
+ _LOG.debug('p_positional_and_keyword_args(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
positional_args = []
keyword_args = []
pos_idx = 0
def p_c_base_type(s, self_flag = 0, nonempty = 0, templates = None):
# If self_flag is true, this is the base type for the
# self argument of a C method of an extension type.
+ _LOG.debug('p_c_base_type(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == '(':
return p_c_complex_base_type(s)
else:
return p_c_simple_base_type(s, self_flag, nonempty = nonempty, templates = templates)
def p_calling_convention(s):
+ _LOG.debug('p_calling_convention(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'IDENT' and s.systring in calling_convention_words:
result = s.systring
s.next()
def p_c_complex_base_type(s):
# s.sy == '('
+ _LOG.debug('p_c_complex_base_type(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next()
base_type = p_c_base_type(s)
base_type = base_type, declarator = declarator)
def p_c_simple_base_type(s, self_flag, nonempty, templates = None):
- #print "p_c_simple_base_type: self_flag =", self_flag, nonempty
+ _LOG.debug('p_c_simple_base_type(s=<s sy:%s systring:%s>, self_flag=%s, nonempty=%s)'
+ % (s.sy, s.systring, self_flag, nonempty))
is_basic = 0
signed = 1
longness = 0
def p_buffer_or_template(s, base_type_node, templates):
# s.sy == '['
+ _LOG.debug('p_buffer_or_template(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next()
# Note that buffer_positional_options_count=1, so the only positional argument is dtype.
def looking_at_name(s):
+ _LOG.debug('looking_at_name(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
return s.sy == 'IDENT' and not s.systring in calling_convention_words
def looking_at_expr(s):
+ _LOG.debug('looking_at_expr(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.systring in base_type_start_words:
return False
elif s.sy == 'IDENT':
return True
def looking_at_base_type(s):
- #print "looking_at_base_type?", s.sy, s.systring, s.position()
+ _LOG.debug('looking_at_base_type(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
return s.sy == 'IDENT' and s.systring in base_type_start_words
def looking_at_dotted_name(s):
+ _LOG.debug('looking_at_dotted_name(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'IDENT':
name = s.systring
s.next()
basic_c_type_names + sign_and_longness_words + tuple(special_basic_c_types)
def p_sign_and_longness(s):
+ _LOG.debug('p_sign_and_longness(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
signed = 1
longness = 0
while s.sy == 'IDENT' and s.systring in sign_and_longness_words:
return signed, longness
def p_opt_cname(s):
+ _LOG.debug('p_opt_cname(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
literal = p_opt_string_literal(s, 'u')
if literal is not None:
cname = EncodedString(literal)
# the declarator must be nonempty. Otherwise we don't care.
# If cmethod_flag is true, then if this declarator declares
# a function, it's a C method of an extension type.
+ _LOG.debug('p_c_declarator(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
if s.sy == '(':
s.next()
return result
def p_c_array_declarator(s, base):
+ _LOG.debug('p_c_array_declarator(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next() # '['
if s.sy != ']':
def p_c_func_declarator(s, pos, ctx, base, cmethod_flag):
# Opening paren has already been skipped
+ _LOG.debug('p_c_func_declarator(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
args = p_c_arg_list(s, ctx, cmethod_flag = cmethod_flag,
nonempty_declarators = 0)
ellipsis = p_optional_ellipsis(s)
def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
assignable, nonempty):
+ _LOG.debug('p_c_simple_declarator(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
calling_convention = p_calling_convention(s)
if s.sy == '*':
return result
def p_nogil(s):
+ _LOG.debug('p_nogil(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'IDENT' and s.systring == 'nogil':
s.next()
return 1
return 0
def p_with_gil(s):
+ _LOG.debug('p_with_gil(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'with':
s.next()
s.expect_keyword('gil')
return 0
def p_exception_value_clause(s):
+ _LOG.debug('p_exception_value_clause(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
exc_val = None
exc_check = 0
if s.sy == 'except':
nonempty_declarators = 0, kw_only = 0, annotated = 1):
# Comma-separated list of C argument declarations, possibly empty.
# May have a trailing comma.
+ _LOG.debug('p_c_arg_list(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
args = []
is_self_arg = cmethod_flag
while s.sy not in c_arg_list_terminators:
return args
def p_optional_ellipsis(s):
+ _LOG.debug('p_optional_ellipsis(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == '.':
expect_ellipsis(s)
return 1
def p_c_arg_decl(s, ctx, in_pyfunc, cmethod_flag = 0, nonempty = 0,
kw_only = 0, annotated = 1):
+ _LOG.debug('p_c_arg_decl(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
not_none = or_none = 0
default = None
kw_only = kw_only)
def p_api(s):
+ _LOG.debug('p_api(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'IDENT' and s.systring == 'api':
s.next()
return 1
return p_suite(s, ctx(cdef_flag = 1))
def p_cdef_extern_block(s, pos, ctx):
+ _LOG.debug('p_cdef_extern_block(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if ctx.overridable:
error(pos, "cdef extern blocks cannot be declared cpdef")
include_file = None
def p_c_enum_definition(s, pos, ctx):
# s.sy == ident 'enum'
+ _LOG.debug('p_c_enum_definition(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
s.next()
if s.sy == 'IDENT':
name = s.systring
in_pxd = ctx.level == 'module_pxd')
def p_c_enum_line(s, ctx, items):
+ _LOG.debug('p_c_enum_line(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy != 'pass':
p_c_enum_item(s, ctx, items)
while s.sy == ',':
s.expect_newline("Syntax error in enum item list")
def p_c_enum_item(s, ctx, items):
+ _LOG.debug('p_c_enum_item(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
ctx = p_c_python_binding(s, ctx)
name = p_ident(s)
in_pxd = ctx.level == 'module_pxd'))
def p_c_struct_or_union_definition(s, pos, ctx):
+ _LOG.debug('p_c_struct_or_union_definition(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
packed = False
if s.systring == 'packed':
packed = True
in_pxd = ctx.level == 'module_pxd', packed = packed)
def p_visibility(s, prev_visibility):
+ _LOG.debug('p_visibility(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
visibility = prev_visibility
if s.sy == 'IDENT' and s.systring in ('extern', 'public', 'readonly'):
return visibility
def p_c_modifiers(s):
+ _LOG.debug('p_c_modifiers(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'IDENT' and s.systring in ('inline',):
modifier = s.systring
s.next()
def p_ctypedef_statement(s, ctx):
# s.sy == 'ctypedef'
+ _LOG.debug('p_ctypedef_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next()
visibility = p_visibility(s, ctx.visibility)
in_pxd = ctx.level == 'module_pxd')
def p_decorators(s):
+ _LOG.debug('p_decorators(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
decorators = []
while s.sy == 'DECORATOR':
pos = s.position()
def p_def_statement(s, decorators=None):
# s.sy == 'def'
+ _LOG.debug('p_def_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next()
name = EncodedString( p_ident(s) )
return_type_annotation = return_type_annotation)
def p_varargslist(s, terminator=')', annotated=1):
+ _LOG.debug('p_varargslist(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
args = p_c_arg_list(s, in_pyfunc = 1, nonempty_declarators = 1,
annotated = annotated)
star_arg = None
return (args, star_arg, starstar_arg)
def p_py_arg_decl(s, annotated = 1):
+ _LOG.debug('p_py_arg_decl(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
name = p_ident(s)
annotation = None
def p_class_statement(s, decorators):
# s.sy == 'class'
+ _LOG.debug('p_class_statement(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next()
class_name = EncodedString( p_ident(s) )
body = body)
def p_c_class_options(s):
+ _LOG.debug('p_c_class_options(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
objstruct_name = None
typeobj_name = None
s.expect('[')
return objstruct_name, typeobj_name
def p_property_decl(s):
+ _LOG.debug('p_property_decl(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
pos = s.position()
s.next() # 'property'
name = p_ident(s)
return Nodes.PropertyNode(pos, name = name, doc = doc, body = body)
def p_doc_string(s):
+ _LOG.debug('p_doc_string(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
if s.sy == 'BEGIN_STRING':
pos = s.position()
kind, bytes_result, unicode_result = p_cat_string_literal(s)
return None
def p_code(s, level=None):
+ _LOG.debug('p_code(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
body = p_statement_list(s, Ctx(level = level), first_statement = 1)
if s.sy != 'EOF':
s.error("Syntax error in statement [%s,%s]" % (
COMPILER_DIRECTIVE_COMMENT_RE = re.compile(r"^#\s*cython:\s*((\w|[.])+\s*=.*)$")
def p_compiler_directive_comments(s):
+ _LOG.debug('p_compiler_directive_comments(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
result = {}
while s.sy == 'commentline':
m = COMPILER_DIRECTIVE_COMMENT_RE.match(s.systring)
return result
def p_module(s, pxd, full_module_name):
+ _LOG.debug('p_module(<s sy:%s systring:%s>, pxd=%s, full_module_name=%s)'
+ % (s.sy, s.systring, pxd, full_module_name))
pos = s.position()
directive_comments = p_compiler_directive_comments(s)
def p_cpp_class_definition(s, pos, ctx):
# s.sy == 'cppclass'
+ _LOG.debug('p_cpp_class_definition(s=<s sy:%s systring:%s>)'
+ % (s.sy, s.systring))
s.next()
module_path = []
class_name = p_ident(s)
f.write("%s]\n" % ind)
return
f.write("%s%s\n" % (ind, node))
-