Parsing.py 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867
  1. # cython: auto_cpdef=True, infer_types=True, language_level=3, py2_import=True
  2. #
  3. # Parser
  4. #
  5. from __future__ import absolute_import
  6. # This should be done automatically
  7. import cython
  8. cython.declare(Nodes=object, ExprNodes=object, EncodedString=object,
  9. bytes_literal=object, StringEncoding=object,
  10. FileSourceDescriptor=object, lookup_unicodechar=object, unicode_category=object,
  11. Future=object, Options=object, error=object, warning=object,
  12. Builtin=object, ModuleNode=object, Utils=object, _unicode=object, _bytes=object,
  13. re=object, sys=object, _parse_escape_sequences=object, _parse_escape_sequences_raw=object,
  14. partial=object, reduce=object, _IS_PY3=cython.bint, _IS_2BYTE_UNICODE=cython.bint,
  15. _CDEF_MODIFIERS=tuple)
  16. from io import StringIO
  17. import re
  18. import sys
  19. from unicodedata import lookup as lookup_unicodechar, category as unicode_category
  20. from functools import partial, reduce
  21. from .Scanning import PyrexScanner, FileSourceDescriptor, StringSourceDescriptor
  22. from . import Nodes
  23. from . import ExprNodes
  24. from . import Builtin
  25. from . import StringEncoding
  26. from .StringEncoding import EncodedString, bytes_literal, _unicode, _bytes
  27. from .ModuleNode import ModuleNode
  28. from .Errors import error, warning
  29. from .. import Utils
  30. from . import Future
  31. from . import Options
  32. _IS_PY3 = sys.version_info[0] >= 3
  33. _IS_2BYTE_UNICODE = sys.maxunicode == 0xffff
  34. _CDEF_MODIFIERS = ('inline', 'nogil', 'api')
  35. class Ctx(object):
  36. # Parsing context
  37. level = 'other'
  38. visibility = 'private'
  39. cdef_flag = 0
  40. typedef_flag = 0
  41. api = 0
  42. overridable = 0
  43. nogil = 0
  44. namespace = None
  45. templates = None
  46. allow_struct_enum_decorator = False
  47. def __init__(self, **kwds):
  48. self.__dict__.update(kwds)
  49. def __call__(self, **kwds):
  50. ctx = Ctx()
  51. d = ctx.__dict__
  52. d.update(self.__dict__)
  53. d.update(kwds)
  54. return ctx
  55. def p_ident(s, message="Expected an identifier"):
  56. if s.sy == 'IDENT':
  57. name = s.systring
  58. s.next()
  59. return name
  60. else:
  61. s.error(message)
  62. def p_ident_list(s):
  63. names = []
  64. while s.sy == 'IDENT':
  65. names.append(s.systring)
  66. s.next()
  67. if s.sy != ',':
  68. break
  69. s.next()
  70. return names
  71. #------------------------------------------
  72. #
  73. # Expressions
  74. #
  75. #------------------------------------------
  76. def p_binop_operator(s):
  77. pos = s.position()
  78. op = s.sy
  79. s.next()
  80. return op, pos
  81. def p_binop_expr(s, ops, p_sub_expr):
  82. n1 = p_sub_expr(s)
  83. while s.sy in ops:
  84. op, pos = p_binop_operator(s)
  85. n2 = p_sub_expr(s)
  86. n1 = ExprNodes.binop_node(pos, op, n1, n2)
  87. if op == '/':
  88. if Future.division in s.context.future_directives:
  89. n1.truedivision = True
  90. else:
  91. n1.truedivision = None # unknown
  92. return n1
  93. #lambdef: 'lambda' [varargslist] ':' test
  94. def p_lambdef(s, allow_conditional=True):
  95. # s.sy == 'lambda'
  96. pos = s.position()
  97. s.next()
  98. if s.sy == ':':
  99. args = []
  100. star_arg = starstar_arg = None
  101. else:
  102. args, star_arg, starstar_arg = p_varargslist(
  103. s, terminator=':', annotated=False)
  104. s.expect(':')
  105. if allow_conditional:
  106. expr = p_test(s)
  107. else:
  108. expr = p_test_nocond(s)
  109. return ExprNodes.LambdaNode(
  110. pos, args = args,
  111. star_arg = star_arg, starstar_arg = starstar_arg,
  112. result_expr = expr)
  113. #lambdef_nocond: 'lambda' [varargslist] ':' test_nocond
  114. def p_lambdef_nocond(s):
  115. return p_lambdef(s, allow_conditional=False)
  116. #test: or_test ['if' or_test 'else' test] | lambdef
  117. def p_test(s):
  118. if s.sy == 'lambda':
  119. return p_lambdef(s)
  120. pos = s.position()
  121. expr = p_or_test(s)
  122. if s.sy == 'if':
  123. s.next()
  124. test = p_or_test(s)
  125. s.expect('else')
  126. other = p_test(s)
  127. return ExprNodes.CondExprNode(pos, test=test, true_val=expr, false_val=other)
  128. else:
  129. return expr
  130. #test_nocond: or_test | lambdef_nocond
  131. def p_test_nocond(s):
  132. if s.sy == 'lambda':
  133. return p_lambdef_nocond(s)
  134. else:
  135. return p_or_test(s)
  136. #or_test: and_test ('or' and_test)*
  137. def p_or_test(s):
  138. return p_rassoc_binop_expr(s, ('or',), p_and_test)
  139. def p_rassoc_binop_expr(s, ops, p_subexpr):
  140. n1 = p_subexpr(s)
  141. if s.sy in ops:
  142. pos = s.position()
  143. op = s.sy
  144. s.next()
  145. n2 = p_rassoc_binop_expr(s, ops, p_subexpr)
  146. n1 = ExprNodes.binop_node(pos, op, n1, n2)
  147. return n1
  148. #and_test: not_test ('and' not_test)*
  149. def p_and_test(s):
  150. #return p_binop_expr(s, ('and',), p_not_test)
  151. return p_rassoc_binop_expr(s, ('and',), p_not_test)
  152. #not_test: 'not' not_test | comparison
  153. def p_not_test(s):
  154. if s.sy == 'not':
  155. pos = s.position()
  156. s.next()
  157. return ExprNodes.NotNode(pos, operand = p_not_test(s))
  158. else:
  159. return p_comparison(s)
  160. #comparison: expr (comp_op expr)*
  161. #comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
  162. def p_comparison(s):
  163. n1 = p_starred_expr(s)
  164. if s.sy in comparison_ops:
  165. pos = s.position()
  166. op = p_cmp_op(s)
  167. n2 = p_starred_expr(s)
  168. n1 = ExprNodes.PrimaryCmpNode(pos,
  169. operator = op, operand1 = n1, operand2 = n2)
  170. if s.sy in comparison_ops:
  171. n1.cascade = p_cascaded_cmp(s)
  172. return n1
  173. def p_test_or_starred_expr(s):
  174. if s.sy == '*':
  175. return p_starred_expr(s)
  176. else:
  177. return p_test(s)
  178. def p_starred_expr(s):
  179. pos = s.position()
  180. if s.sy == '*':
  181. starred = True
  182. s.next()
  183. else:
  184. starred = False
  185. expr = p_bit_expr(s)
  186. if starred:
  187. expr = ExprNodes.StarredUnpackingNode(pos, expr)
  188. return expr
  189. def p_cascaded_cmp(s):
  190. pos = s.position()
  191. op = p_cmp_op(s)
  192. n2 = p_starred_expr(s)
  193. result = ExprNodes.CascadedCmpNode(pos,
  194. operator = op, operand2 = n2)
  195. if s.sy in comparison_ops:
  196. result.cascade = p_cascaded_cmp(s)
  197. return result
  198. def p_cmp_op(s):
  199. if s.sy == 'not':
  200. s.next()
  201. s.expect('in')
  202. op = 'not_in'
  203. elif s.sy == 'is':
  204. s.next()
  205. if s.sy == 'not':
  206. s.next()
  207. op = 'is_not'
  208. else:
  209. op = 'is'
  210. else:
  211. op = s.sy
  212. s.next()
  213. if op == '<>':
  214. op = '!='
  215. return op
  216. comparison_ops = cython.declare(set, set([
  217. '<', '>', '==', '>=', '<=', '<>', '!=',
  218. 'in', 'is', 'not'
  219. ]))
  220. #expr: xor_expr ('|' xor_expr)*
  221. def p_bit_expr(s):
  222. return p_binop_expr(s, ('|',), p_xor_expr)
  223. #xor_expr: and_expr ('^' and_expr)*
  224. def p_xor_expr(s):
  225. return p_binop_expr(s, ('^',), p_and_expr)
  226. #and_expr: shift_expr ('&' shift_expr)*
  227. def p_and_expr(s):
  228. return p_binop_expr(s, ('&',), p_shift_expr)
  229. #shift_expr: arith_expr (('<<'|'>>') arith_expr)*
  230. def p_shift_expr(s):
  231. return p_binop_expr(s, ('<<', '>>'), p_arith_expr)
  232. #arith_expr: term (('+'|'-') term)*
  233. def p_arith_expr(s):
  234. return p_binop_expr(s, ('+', '-'), p_term)
  235. #term: factor (('*'|'@'|'/'|'%'|'//') factor)*
  236. def p_term(s):
  237. return p_binop_expr(s, ('*', '@', '/', '%', '//'), p_factor)
  238. #factor: ('+'|'-'|'~'|'&'|typecast|sizeof) factor | power
  239. def p_factor(s):
  240. # little indirection for C-ification purposes
  241. return _p_factor(s)
  242. def _p_factor(s):
  243. sy = s.sy
  244. if sy in ('+', '-', '~'):
  245. op = s.sy
  246. pos = s.position()
  247. s.next()
  248. return ExprNodes.unop_node(pos, op, p_factor(s))
  249. elif not s.in_python_file:
  250. if sy == '&':
  251. pos = s.position()
  252. s.next()
  253. arg = p_factor(s)
  254. return ExprNodes.AmpersandNode(pos, operand = arg)
  255. elif sy == "<":
  256. return p_typecast(s)
  257. elif sy == 'IDENT' and s.systring == "sizeof":
  258. return p_sizeof(s)
  259. return p_power(s)
  260. def p_typecast(s):
  261. # s.sy == "<"
  262. pos = s.position()
  263. s.next()
  264. base_type = p_c_base_type(s)
  265. is_memslice = isinstance(base_type, Nodes.MemoryViewSliceTypeNode)
  266. is_template = isinstance(base_type, Nodes.TemplatedTypeNode)
  267. is_const = isinstance(base_type, Nodes.CConstTypeNode)
  268. if (not is_memslice and not is_template and not is_const
  269. and base_type.name is None):
  270. s.error("Unknown type")
  271. declarator = p_c_declarator(s, empty = 1)
  272. if s.sy == '?':
  273. s.next()
  274. typecheck = 1
  275. else:
  276. typecheck = 0
  277. s.expect(">")
  278. operand = p_factor(s)
  279. if is_memslice:
  280. return ExprNodes.CythonArrayNode(pos, base_type_node=base_type,
  281. operand=operand)
  282. return ExprNodes.TypecastNode(pos,
  283. base_type = base_type,
  284. declarator = declarator,
  285. operand = operand,
  286. typecheck = typecheck)
  287. def p_sizeof(s):
  288. # s.sy == ident "sizeof"
  289. pos = s.position()
  290. s.next()
  291. s.expect('(')
  292. # Here we decide if we are looking at an expression or type
  293. # If it is actually a type, but parsable as an expression,
  294. # we treat it as an expression here.
  295. if looking_at_expr(s):
  296. operand = p_test(s)
  297. node = ExprNodes.SizeofVarNode(pos, operand = operand)
  298. else:
  299. base_type = p_c_base_type(s)
  300. declarator = p_c_declarator(s, empty = 1)
  301. node = ExprNodes.SizeofTypeNode(pos,
  302. base_type = base_type, declarator = declarator)
  303. s.expect(')')
  304. return node
  305. def p_yield_expression(s):
  306. # s.sy == "yield"
  307. pos = s.position()
  308. s.next()
  309. is_yield_from = False
  310. if s.sy == 'from':
  311. is_yield_from = True
  312. s.next()
  313. if s.sy != ')' and s.sy not in statement_terminators:
  314. # "yield from" does not support implicit tuples, but "yield" does ("yield 1,2")
  315. arg = p_test(s) if is_yield_from else p_testlist(s)
  316. else:
  317. if is_yield_from:
  318. s.error("'yield from' requires a source argument",
  319. pos=pos, fatal=False)
  320. arg = None
  321. if is_yield_from:
  322. return ExprNodes.YieldFromExprNode(pos, arg=arg)
  323. else:
  324. return ExprNodes.YieldExprNode(pos, arg=arg)
  325. def p_yield_statement(s):
  326. # s.sy == "yield"
  327. yield_expr = p_yield_expression(s)
  328. return Nodes.ExprStatNode(yield_expr.pos, expr=yield_expr)
  329. def p_async_statement(s, ctx, decorators):
  330. # s.sy >> 'async' ...
  331. if s.sy == 'def':
  332. # 'async def' statements aren't allowed in pxd files
  333. if 'pxd' in ctx.level:
  334. s.error('def statement not allowed here')
  335. s.level = ctx.level
  336. return p_def_statement(s, decorators, is_async_def=True)
  337. elif decorators:
  338. s.error("Decorators can only be followed by functions or classes")
  339. elif s.sy == 'for':
  340. return p_for_statement(s, is_async=True)
  341. elif s.sy == 'with':
  342. s.next()
  343. return p_with_items(s, is_async=True)
  344. else:
  345. s.error("expected one of 'def', 'for', 'with' after 'async'")
  346. #power: atom_expr ('**' factor)*
  347. #atom_expr: ['await'] atom trailer*
  348. def p_power(s):
  349. if s.systring == 'new' and s.peek()[0] == 'IDENT':
  350. return p_new_expr(s)
  351. await_pos = None
  352. if s.sy == 'await':
  353. await_pos = s.position()
  354. s.next()
  355. n1 = p_atom(s)
  356. while s.sy in ('(', '[', '.'):
  357. n1 = p_trailer(s, n1)
  358. if await_pos:
  359. n1 = ExprNodes.AwaitExprNode(await_pos, arg=n1)
  360. if s.sy == '**':
  361. pos = s.position()
  362. s.next()
  363. n2 = p_factor(s)
  364. n1 = ExprNodes.binop_node(pos, '**', n1, n2)
  365. return n1
  366. def p_new_expr(s):
  367. # s.systring == 'new'.
  368. pos = s.position()
  369. s.next()
  370. cppclass = p_c_base_type(s)
  371. return p_call(s, ExprNodes.NewExprNode(pos, cppclass = cppclass))
  372. #trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
  373. def p_trailer(s, node1):
  374. pos = s.position()
  375. if s.sy == '(':
  376. return p_call(s, node1)
  377. elif s.sy == '[':
  378. return p_index(s, node1)
  379. else: # s.sy == '.'
  380. s.next()
  381. name = p_ident(s)
  382. return ExprNodes.AttributeNode(pos,
  383. obj=node1, attribute=name)
  384. # arglist: argument (',' argument)* [',']
  385. # argument: [test '='] test # Really [keyword '='] test
  386. # since PEP 448:
  387. # argument: ( test [comp_for] |
  388. # test '=' test |
  389. # '**' expr |
  390. # star_expr )
  391. def p_call_parse_args(s, allow_genexp=True):
  392. # s.sy == '('
  393. pos = s.position()
  394. s.next()
  395. positional_args = []
  396. keyword_args = []
  397. starstar_seen = False
  398. last_was_tuple_unpack = False
  399. while s.sy != ')':
  400. if s.sy == '*':
  401. if starstar_seen:
  402. s.error("Non-keyword arg following keyword arg", pos=s.position())
  403. s.next()
  404. positional_args.append(p_test(s))
  405. last_was_tuple_unpack = True
  406. elif s.sy == '**':
  407. s.next()
  408. keyword_args.append(p_test(s))
  409. starstar_seen = True
  410. else:
  411. arg = p_test(s)
  412. if s.sy == '=':
  413. s.next()
  414. if not arg.is_name:
  415. s.error("Expected an identifier before '='",
  416. pos=arg.pos)
  417. encoded_name = s.context.intern_ustring(arg.name)
  418. keyword = ExprNodes.IdentifierStringNode(
  419. arg.pos, value=encoded_name)
  420. arg = p_test(s)
  421. keyword_args.append((keyword, arg))
  422. else:
  423. if keyword_args:
  424. s.error("Non-keyword arg following keyword arg", pos=arg.pos)
  425. if positional_args and not last_was_tuple_unpack:
  426. positional_args[-1].append(arg)
  427. else:
  428. positional_args.append([arg])
  429. last_was_tuple_unpack = False
  430. if s.sy != ',':
  431. break
  432. s.next()
  433. if s.sy in ('for', 'async'):
  434. if not keyword_args and not last_was_tuple_unpack:
  435. if len(positional_args) == 1 and len(positional_args[0]) == 1:
  436. positional_args = [[p_genexp(s, positional_args[0][0])]]
  437. s.expect(')')
  438. return positional_args or [[]], keyword_args
  439. def p_call_build_packed_args(pos, positional_args, keyword_args):
  440. keyword_dict = None
  441. subtuples = [
  442. ExprNodes.TupleNode(pos, args=arg) if isinstance(arg, list) else ExprNodes.AsTupleNode(pos, arg=arg)
  443. for arg in positional_args
  444. ]
  445. # TODO: implement a faster way to join tuples than creating each one and adding them
  446. arg_tuple = reduce(partial(ExprNodes.binop_node, pos, '+'), subtuples)
  447. if keyword_args:
  448. kwargs = []
  449. dict_items = []
  450. for item in keyword_args:
  451. if isinstance(item, tuple):
  452. key, value = item
  453. dict_items.append(ExprNodes.DictItemNode(pos=key.pos, key=key, value=value))
  454. elif item.is_dict_literal:
  455. # unpack "**{a:b}" directly
  456. dict_items.extend(item.key_value_pairs)
  457. else:
  458. if dict_items:
  459. kwargs.append(ExprNodes.DictNode(
  460. dict_items[0].pos, key_value_pairs=dict_items, reject_duplicates=True))
  461. dict_items = []
  462. kwargs.append(item)
  463. if dict_items:
  464. kwargs.append(ExprNodes.DictNode(
  465. dict_items[0].pos, key_value_pairs=dict_items, reject_duplicates=True))
  466. if kwargs:
  467. if len(kwargs) == 1 and kwargs[0].is_dict_literal:
  468. # only simple keyword arguments found -> one dict
  469. keyword_dict = kwargs[0]
  470. else:
  471. # at least one **kwargs
  472. keyword_dict = ExprNodes.MergedDictNode(pos, keyword_args=kwargs)
  473. return arg_tuple, keyword_dict
  474. def p_call(s, function):
  475. # s.sy == '('
  476. pos = s.position()
  477. positional_args, keyword_args = p_call_parse_args(s)
  478. if not keyword_args and len(positional_args) == 1 and isinstance(positional_args[0], list):
  479. return ExprNodes.SimpleCallNode(pos, function=function, args=positional_args[0])
  480. else:
  481. arg_tuple, keyword_dict = p_call_build_packed_args(pos, positional_args, keyword_args)
  482. return ExprNodes.GeneralCallNode(
  483. pos, function=function, positional_args=arg_tuple, keyword_args=keyword_dict)
  484. #lambdef: 'lambda' [varargslist] ':' test
  485. #subscriptlist: subscript (',' subscript)* [',']
  486. def p_index(s, base):
  487. # s.sy == '['
  488. pos = s.position()
  489. s.next()
  490. subscripts, is_single_value = p_subscript_list(s)
  491. if is_single_value and len(subscripts[0]) == 2:
  492. start, stop = subscripts[0]
  493. result = ExprNodes.SliceIndexNode(pos,
  494. base = base, start = start, stop = stop)
  495. else:
  496. indexes = make_slice_nodes(pos, subscripts)
  497. if is_single_value:
  498. index = indexes[0]
  499. else:
  500. index = ExprNodes.TupleNode(pos, args = indexes)
  501. result = ExprNodes.IndexNode(pos,
  502. base = base, index = index)
  503. s.expect(']')
  504. return result
  505. def p_subscript_list(s):
  506. is_single_value = True
  507. items = [p_subscript(s)]
  508. while s.sy == ',':
  509. is_single_value = False
  510. s.next()
  511. if s.sy == ']':
  512. break
  513. items.append(p_subscript(s))
  514. return items, is_single_value
  515. #subscript: '.' '.' '.' | test | [test] ':' [test] [':' [test]]
  516. def p_subscript(s):
  517. # Parse a subscript and return a list of
  518. # 1, 2 or 3 ExprNodes, depending on how
  519. # many slice elements were encountered.
  520. pos = s.position()
  521. start = p_slice_element(s, (':',))
  522. if s.sy != ':':
  523. return [start]
  524. s.next()
  525. stop = p_slice_element(s, (':', ',', ']'))
  526. if s.sy != ':':
  527. return [start, stop]
  528. s.next()
  529. step = p_slice_element(s, (':', ',', ']'))
  530. return [start, stop, step]
  531. def p_slice_element(s, follow_set):
  532. # Simple expression which may be missing iff
  533. # it is followed by something in follow_set.
  534. if s.sy not in follow_set:
  535. return p_test(s)
  536. else:
  537. return None
  538. def expect_ellipsis(s):
  539. s.expect('.')
  540. s.expect('.')
  541. s.expect('.')
  542. def make_slice_nodes(pos, subscripts):
  543. # Convert a list of subscripts as returned
  544. # by p_subscript_list into a list of ExprNodes,
  545. # creating SliceNodes for elements with 2 or
  546. # more components.
  547. result = []
  548. for subscript in subscripts:
  549. if len(subscript) == 1:
  550. result.append(subscript[0])
  551. else:
  552. result.append(make_slice_node(pos, *subscript))
  553. return result
  554. def make_slice_node(pos, start, stop = None, step = None):
  555. if not start:
  556. start = ExprNodes.NoneNode(pos)
  557. if not stop:
  558. stop = ExprNodes.NoneNode(pos)
  559. if not step:
  560. step = ExprNodes.NoneNode(pos)
  561. return ExprNodes.SliceNode(pos,
  562. start = start, stop = stop, step = step)
  563. #atom: '(' [yield_expr|testlist_comp] ')' | '[' [listmaker] ']' | '{' [dict_or_set_maker] '}' | '`' testlist '`' | NAME | NUMBER | STRING+
  564. def p_atom(s):
  565. pos = s.position()
  566. sy = s.sy
  567. if sy == '(':
  568. s.next()
  569. if s.sy == ')':
  570. result = ExprNodes.TupleNode(pos, args = [])
  571. elif s.sy == 'yield':
  572. result = p_yield_expression(s)
  573. else:
  574. result = p_testlist_comp(s)
  575. s.expect(')')
  576. return result
  577. elif sy == '[':
  578. return p_list_maker(s)
  579. elif sy == '{':
  580. return p_dict_or_set_maker(s)
  581. elif sy == '`':
  582. return p_backquote_expr(s)
  583. elif sy == '.':
  584. expect_ellipsis(s)
  585. return ExprNodes.EllipsisNode(pos)
  586. elif sy == 'INT':
  587. return p_int_literal(s)
  588. elif sy == 'FLOAT':
  589. value = s.systring
  590. s.next()
  591. return ExprNodes.FloatNode(pos, value = value)
  592. elif sy == 'IMAG':
  593. value = s.systring[:-1]
  594. s.next()
  595. return ExprNodes.ImagNode(pos, value = value)
  596. elif sy == 'BEGIN_STRING':
  597. kind, bytes_value, unicode_value = p_cat_string_literal(s)
  598. if kind == 'c':
  599. return ExprNodes.CharNode(pos, value = bytes_value)
  600. elif kind == 'u':
  601. return ExprNodes.UnicodeNode(pos, value = unicode_value, bytes_value = bytes_value)
  602. elif kind == 'b':
  603. return ExprNodes.BytesNode(pos, value = bytes_value)
  604. elif kind == 'f':
  605. return ExprNodes.JoinedStrNode(pos, values = unicode_value)
  606. elif kind == '':
  607. return ExprNodes.StringNode(pos, value = bytes_value, unicode_value = unicode_value)
  608. else:
  609. s.error("invalid string kind '%s'" % kind)
  610. elif sy == 'IDENT':
  611. name = s.systring
  612. if name == "None":
  613. result = ExprNodes.NoneNode(pos)
  614. elif name == "True":
  615. result = ExprNodes.BoolNode(pos, value=True)
  616. elif name == "False":
  617. result = ExprNodes.BoolNode(pos, value=False)
  618. elif name == "NULL" and not s.in_python_file:
  619. result = ExprNodes.NullNode(pos)
  620. else:
  621. result = p_name(s, name)
  622. s.next()
  623. return result
  624. else:
  625. s.error("Expected an identifier or literal")
  626. def p_int_literal(s):
  627. pos = s.position()
  628. value = s.systring
  629. s.next()
  630. unsigned = ""
  631. longness = ""
  632. while value[-1] in u"UuLl":
  633. if value[-1] in u"Ll":
  634. longness += "L"
  635. else:
  636. unsigned += "U"
  637. value = value[:-1]
  638. # '3L' is ambiguous in Py2 but not in Py3. '3U' and '3LL' are
  639. # illegal in Py2 Python files. All suffixes are illegal in Py3
  640. # Python files.
  641. is_c_literal = None
  642. if unsigned:
  643. is_c_literal = True
  644. elif longness:
  645. if longness == 'LL' or s.context.language_level >= 3:
  646. is_c_literal = True
  647. if s.in_python_file:
  648. if is_c_literal:
  649. error(pos, "illegal integer literal syntax in Python source file")
  650. is_c_literal = False
  651. return ExprNodes.IntNode(pos,
  652. is_c_literal = is_c_literal,
  653. value = value,
  654. unsigned = unsigned,
  655. longness = longness)
  656. def p_name(s, name):
  657. pos = s.position()
  658. if not s.compile_time_expr and name in s.compile_time_env:
  659. value = s.compile_time_env.lookup_here(name)
  660. node = wrap_compile_time_constant(pos, value)
  661. if node is not None:
  662. return node
  663. return ExprNodes.NameNode(pos, name=name)
  664. def wrap_compile_time_constant(pos, value):
  665. rep = repr(value)
  666. if value is None:
  667. return ExprNodes.NoneNode(pos)
  668. elif value is Ellipsis:
  669. return ExprNodes.EllipsisNode(pos)
  670. elif isinstance(value, bool):
  671. return ExprNodes.BoolNode(pos, value=value)
  672. elif isinstance(value, int):
  673. return ExprNodes.IntNode(pos, value=rep, constant_result=value)
  674. elif isinstance(value, float):
  675. return ExprNodes.FloatNode(pos, value=rep, constant_result=value)
  676. elif isinstance(value, complex):
  677. node = ExprNodes.ImagNode(pos, value=repr(value.imag), constant_result=complex(0.0, value.imag))
  678. if value.real:
  679. # FIXME: should we care about -0.0 ?
  680. # probably not worth using the '-' operator for negative imag values
  681. node = ExprNodes.binop_node(
  682. pos, '+', ExprNodes.FloatNode(pos, value=repr(value.real), constant_result=value.real), node,
  683. constant_result=value)
  684. return node
  685. elif isinstance(value, _unicode):
  686. return ExprNodes.UnicodeNode(pos, value=EncodedString(value))
  687. elif isinstance(value, _bytes):
  688. bvalue = bytes_literal(value, 'ascii') # actually: unknown encoding, but BytesLiteral requires one
  689. return ExprNodes.BytesNode(pos, value=bvalue, constant_result=value)
  690. elif isinstance(value, tuple):
  691. args = [wrap_compile_time_constant(pos, arg)
  692. for arg in value]
  693. if None not in args:
  694. return ExprNodes.TupleNode(pos, args=args)
  695. else:
  696. # error already reported
  697. return None
  698. elif not _IS_PY3 and isinstance(value, long):
  699. return ExprNodes.IntNode(pos, value=rep.rstrip('L'), constant_result=value)
  700. error(pos, "Invalid type for compile-time constant: %r (type %s)"
  701. % (value, value.__class__.__name__))
  702. return None
  703. def p_cat_string_literal(s):
  704. # A sequence of one or more adjacent string literals.
  705. # Returns (kind, bytes_value, unicode_value)
  706. # where kind in ('b', 'c', 'u', 'f', '')
  707. pos = s.position()
  708. kind, bytes_value, unicode_value = p_string_literal(s)
  709. if kind == 'c' or s.sy != 'BEGIN_STRING':
  710. return kind, bytes_value, unicode_value
  711. bstrings, ustrings, positions = [bytes_value], [unicode_value], [pos]
  712. bytes_value = unicode_value = None
  713. while s.sy == 'BEGIN_STRING':
  714. pos = s.position()
  715. next_kind, next_bytes_value, next_unicode_value = p_string_literal(s)
  716. if next_kind == 'c':
  717. error(pos, "Cannot concatenate char literal with another string or char literal")
  718. continue
  719. elif next_kind != kind:
  720. # concatenating f strings and normal strings is allowed and leads to an f string
  721. if set([kind, next_kind]) in (set(['f', 'u']), set(['f', ''])):
  722. kind = 'f'
  723. else:
  724. error(pos, "Cannot mix string literals of different types, expected %s'', got %s''" % (
  725. kind, next_kind))
  726. continue
  727. bstrings.append(next_bytes_value)
  728. ustrings.append(next_unicode_value)
  729. positions.append(pos)
  730. # join and rewrap the partial literals
  731. if kind in ('b', 'c', '') or kind == 'u' and None not in bstrings:
  732. # Py3 enforced unicode literals are parsed as bytes/unicode combination
  733. bytes_value = bytes_literal(StringEncoding.join_bytes(bstrings), s.source_encoding)
  734. if kind in ('u', ''):
  735. unicode_value = EncodedString(u''.join([u for u in ustrings if u is not None]))
  736. if kind == 'f':
  737. unicode_value = []
  738. for u, pos in zip(ustrings, positions):
  739. if isinstance(u, list):
  740. unicode_value += u
  741. else:
  742. # non-f-string concatenated into the f-string
  743. unicode_value.append(ExprNodes.UnicodeNode(pos, value=EncodedString(u)))
  744. return kind, bytes_value, unicode_value
  745. def p_opt_string_literal(s, required_type='u'):
  746. if s.sy != 'BEGIN_STRING':
  747. return None
  748. pos = s.position()
  749. kind, bytes_value, unicode_value = p_string_literal(s, required_type)
  750. if required_type == 'u':
  751. if kind == 'f':
  752. s.error("f-string not allowed here", pos)
  753. return unicode_value
  754. elif required_type == 'b':
  755. return bytes_value
  756. else:
  757. s.error("internal parser configuration error")
  758. def check_for_non_ascii_characters(string):
  759. for c in string:
  760. if c >= u'\x80':
  761. return True
  762. return False
  763. def p_string_literal(s, kind_override=None):
  764. # A single string or char literal. Returns (kind, bvalue, uvalue)
  765. # where kind in ('b', 'c', 'u', 'f', ''). The 'bvalue' is the source
  766. # code byte sequence of the string literal, 'uvalue' is the
  767. # decoded Unicode string. Either of the two may be None depending
  768. # on the 'kind' of string, only unprefixed strings have both
  769. # representations. In f-strings, the uvalue is a list of the Unicode
  770. # strings and f-string expressions that make up the f-string.
  771. # s.sy == 'BEGIN_STRING'
  772. pos = s.position()
  773. is_python3_source = s.context.language_level >= 3
  774. has_non_ascii_literal_characters = False
  775. string_start_pos = (pos[0], pos[1], pos[2] + len(s.systring))
  776. kind_string = s.systring.rstrip('"\'').lower()
  777. if len(kind_string) > 1:
  778. if len(set(kind_string)) != len(kind_string):
  779. error(pos, 'Duplicate string prefix character')
  780. if 'b' in kind_string and 'u' in kind_string:
  781. error(pos, 'String prefixes b and u cannot be combined')
  782. if 'b' in kind_string and 'f' in kind_string:
  783. error(pos, 'String prefixes b and f cannot be combined')
  784. if 'u' in kind_string and 'f' in kind_string:
  785. error(pos, 'String prefixes u and f cannot be combined')
  786. is_raw = 'r' in kind_string
  787. if 'c' in kind_string:
  788. # this should never happen, since the lexer does not allow combining c
  789. # with other prefix characters
  790. if len(kind_string) != 1:
  791. error(pos, 'Invalid string prefix for character literal')
  792. kind = 'c'
  793. elif 'f' in kind_string:
  794. kind = 'f' # u is ignored
  795. is_raw = True # postpone the escape resolution
  796. elif 'b' in kind_string:
  797. kind = 'b'
  798. elif 'u' in kind_string:
  799. kind = 'u'
  800. else:
  801. kind = ''
  802. if kind == '' and kind_override is None and Future.unicode_literals in s.context.future_directives:
  803. chars = StringEncoding.StrLiteralBuilder(s.source_encoding)
  804. kind = 'u'
  805. else:
  806. if kind_override is not None and kind_override in 'ub':
  807. kind = kind_override
  808. if kind in ('u', 'f'): # f-strings are scanned exactly like Unicode literals, but are parsed further later
  809. chars = StringEncoding.UnicodeLiteralBuilder()
  810. elif kind == '':
  811. chars = StringEncoding.StrLiteralBuilder(s.source_encoding)
  812. else:
  813. chars = StringEncoding.BytesLiteralBuilder(s.source_encoding)
  814. while 1:
  815. s.next()
  816. sy = s.sy
  817. systr = s.systring
  818. # print "p_string_literal: sy =", sy, repr(s.systring) ###
  819. if sy == 'CHARS':
  820. chars.append(systr)
  821. if is_python3_source and not has_non_ascii_literal_characters and check_for_non_ascii_characters(systr):
  822. has_non_ascii_literal_characters = True
  823. elif sy == 'ESCAPE':
  824. # in Py2, 'ur' raw unicode strings resolve unicode escapes but nothing else
  825. if is_raw and (is_python3_source or kind != 'u' or systr[1] not in u'Uu'):
  826. chars.append(systr)
  827. if is_python3_source and not has_non_ascii_literal_characters and check_for_non_ascii_characters(systr):
  828. has_non_ascii_literal_characters = True
  829. else:
  830. _append_escape_sequence(kind, chars, systr, s)
  831. elif sy == 'NEWLINE':
  832. chars.append(u'\n')
  833. elif sy == 'END_STRING':
  834. break
  835. elif sy == 'EOF':
  836. s.error("Unclosed string literal", pos=pos)
  837. else:
  838. s.error("Unexpected token %r:%r in string literal" % (
  839. sy, s.systring))
  840. if kind == 'c':
  841. unicode_value = None
  842. bytes_value = chars.getchar()
  843. if len(bytes_value) != 1:
  844. error(pos, u"invalid character literal: %r" % bytes_value)
  845. else:
  846. bytes_value, unicode_value = chars.getstrings()
  847. if (has_non_ascii_literal_characters
  848. and is_python3_source and Future.unicode_literals in s.context.future_directives):
  849. # Python 3 forbids literal non-ASCII characters in byte strings
  850. if kind == 'b':
  851. s.error("bytes can only contain ASCII literal characters.", pos=pos)
  852. bytes_value = None
  853. if kind == 'f':
  854. unicode_value = p_f_string(s, unicode_value, string_start_pos, is_raw='r' in kind_string)
  855. s.next()
  856. return (kind, bytes_value, unicode_value)
  857. def _append_escape_sequence(kind, builder, escape_sequence, s):
  858. c = escape_sequence[1]
  859. if c in u"01234567":
  860. builder.append_charval(int(escape_sequence[1:], 8))
  861. elif c in u"'\"\\":
  862. builder.append(c)
  863. elif c in u"abfnrtv":
  864. builder.append(StringEncoding.char_from_escape_sequence(escape_sequence))
  865. elif c == u'\n':
  866. pass # line continuation
  867. elif c == u'x': # \xXX
  868. if len(escape_sequence) == 4:
  869. builder.append_charval(int(escape_sequence[2:], 16))
  870. else:
  871. s.error("Invalid hex escape '%s'" % escape_sequence, fatal=False)
  872. elif c in u'NUu' and kind in ('u', 'f', ''): # \uxxxx, \Uxxxxxxxx, \N{...}
  873. chrval = -1
  874. if c == u'N':
  875. uchar = None
  876. try:
  877. uchar = lookup_unicodechar(escape_sequence[3:-1])
  878. chrval = ord(uchar)
  879. except KeyError:
  880. s.error("Unknown Unicode character name %s" %
  881. repr(escape_sequence[3:-1]).lstrip('u'), fatal=False)
  882. except TypeError:
  883. # 2-byte unicode build of CPython?
  884. if (uchar is not None and _IS_2BYTE_UNICODE and len(uchar) == 2 and
  885. unicode_category(uchar[0]) == 'Cs' and unicode_category(uchar[1]) == 'Cs'):
  886. # surrogate pair instead of single character
  887. chrval = 0x10000 + (ord(uchar[0]) - 0xd800) >> 10 + (ord(uchar[1]) - 0xdc00)
  888. else:
  889. raise
  890. elif len(escape_sequence) in (6, 10):
  891. chrval = int(escape_sequence[2:], 16)
  892. if chrval > 1114111: # sys.maxunicode:
  893. s.error("Invalid unicode escape '%s'" % escape_sequence)
  894. chrval = -1
  895. else:
  896. s.error("Invalid unicode escape '%s'" % escape_sequence, fatal=False)
  897. if chrval >= 0:
  898. builder.append_uescape(chrval, escape_sequence)
  899. else:
  900. builder.append(escape_sequence)
  901. _parse_escape_sequences_raw, _parse_escape_sequences = [re.compile((
  902. # escape sequences:
  903. br'(\\(?:' +
  904. (br'\\?' if is_raw else (
  905. br'[\\abfnrtv"\'{]|'
  906. br'[0-7]{2,3}|'
  907. br'N\{[^}]*\}|'
  908. br'x[0-9a-fA-F]{2}|'
  909. br'u[0-9a-fA-F]{4}|'
  910. br'U[0-9a-fA-F]{8}|'
  911. br'[NxuU]|' # detect invalid escape sequences that do not match above
  912. )) +
  913. br')?|'
  914. # non-escape sequences:
  915. br'\{\{?|'
  916. br'\}\}?|'
  917. br'[^\\{}]+)'
  918. ).decode('us-ascii')).match
  919. for is_raw in (True, False)]
  920. def _f_string_error_pos(pos, string, i):
  921. return (pos[0], pos[1], pos[2] + i + 1) # FIXME: handle newlines in string
  922. def p_f_string(s, unicode_value, pos, is_raw):
  923. # Parses a PEP 498 f-string literal into a list of nodes. Nodes are either UnicodeNodes
  924. # or FormattedValueNodes.
  925. values = []
  926. next_start = 0
  927. size = len(unicode_value)
  928. builder = StringEncoding.UnicodeLiteralBuilder()
  929. _parse_seq = _parse_escape_sequences_raw if is_raw else _parse_escape_sequences
  930. while next_start < size:
  931. end = next_start
  932. match = _parse_seq(unicode_value, next_start)
  933. if match is None:
  934. error(_f_string_error_pos(pos, unicode_value, next_start), "Invalid escape sequence")
  935. next_start = match.end()
  936. part = match.group()
  937. c = part[0]
  938. if c == '\\':
  939. if not is_raw and len(part) > 1:
  940. _append_escape_sequence('f', builder, part, s)
  941. else:
  942. builder.append(part)
  943. elif c == '{':
  944. if part == '{{':
  945. builder.append('{')
  946. else:
  947. # start of an expression
  948. if builder.chars:
  949. values.append(ExprNodes.UnicodeNode(pos, value=builder.getstring()))
  950. builder = StringEncoding.UnicodeLiteralBuilder()
  951. next_start, expr_node = p_f_string_expr(s, unicode_value, pos, next_start, is_raw)
  952. values.append(expr_node)
  953. elif c == '}':
  954. if part == '}}':
  955. builder.append('}')
  956. else:
  957. error(_f_string_error_pos(pos, unicode_value, end),
  958. "f-string: single '}' is not allowed")
  959. else:
  960. builder.append(part)
  961. if builder.chars:
  962. values.append(ExprNodes.UnicodeNode(pos, value=builder.getstring()))
  963. return values
  964. def p_f_string_expr(s, unicode_value, pos, starting_index, is_raw):
  965. # Parses a {}-delimited expression inside an f-string. Returns a FormattedValueNode
  966. # and the index in the string that follows the expression.
  967. i = starting_index
  968. size = len(unicode_value)
  969. conversion_char = terminal_char = format_spec = None
  970. format_spec_str = None
  971. NO_CHAR = 2**30
  972. nested_depth = 0
  973. quote_char = NO_CHAR
  974. in_triple_quotes = False
  975. backslash_reported = False
  976. while True:
  977. if i >= size:
  978. break # error will be reported below
  979. c = unicode_value[i]
  980. if quote_char != NO_CHAR:
  981. if c == '\\':
  982. # avoid redundant error reports along '\' sequences
  983. if not backslash_reported:
  984. error(_f_string_error_pos(pos, unicode_value, i),
  985. "backslashes not allowed in f-strings")
  986. backslash_reported = True
  987. elif c == quote_char:
  988. if in_triple_quotes:
  989. if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
  990. in_triple_quotes = False
  991. quote_char = NO_CHAR
  992. i += 2
  993. else:
  994. quote_char = NO_CHAR
  995. elif c in '\'"':
  996. quote_char = c
  997. if i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
  998. in_triple_quotes = True
  999. i += 2
  1000. elif c in '{[(':
  1001. nested_depth += 1
  1002. elif nested_depth != 0 and c in '}])':
  1003. nested_depth -= 1
  1004. elif c == '#':
  1005. error(_f_string_error_pos(pos, unicode_value, i),
  1006. "format string cannot include #")
  1007. elif nested_depth == 0 and c in '!:}':
  1008. # allow != as a special case
  1009. if c == '!' and i + 1 < size and unicode_value[i + 1] == '=':
  1010. i += 1
  1011. continue
  1012. terminal_char = c
  1013. break
  1014. i += 1
  1015. # normalise line endings as the parser expects that
  1016. expr_str = unicode_value[starting_index:i].replace('\r\n', '\n').replace('\r', '\n')
  1017. expr_pos = (pos[0], pos[1], pos[2] + starting_index + 2) # TODO: find exact code position (concat, multi-line, ...)
  1018. if not expr_str.strip():
  1019. error(_f_string_error_pos(pos, unicode_value, starting_index),
  1020. "empty expression not allowed in f-string")
  1021. if terminal_char == '!':
  1022. i += 1
  1023. if i + 2 > size:
  1024. pass # error will be reported below
  1025. else:
  1026. conversion_char = unicode_value[i]
  1027. i += 1
  1028. terminal_char = unicode_value[i]
  1029. if terminal_char == ':':
  1030. in_triple_quotes = False
  1031. in_string = False
  1032. nested_depth = 0
  1033. start_format_spec = i + 1
  1034. while True:
  1035. if i >= size:
  1036. break # error will be reported below
  1037. c = unicode_value[i]
  1038. if not in_triple_quotes and not in_string:
  1039. if c == '{':
  1040. nested_depth += 1
  1041. elif c == '}':
  1042. if nested_depth > 0:
  1043. nested_depth -= 1
  1044. else:
  1045. terminal_char = c
  1046. break
  1047. if c in '\'"':
  1048. if not in_string and i + 2 < size and unicode_value[i + 1] == c and unicode_value[i + 2] == c:
  1049. in_triple_quotes = not in_triple_quotes
  1050. i += 2
  1051. elif not in_triple_quotes:
  1052. in_string = not in_string
  1053. i += 1
  1054. format_spec_str = unicode_value[start_format_spec:i]
  1055. if terminal_char != '}':
  1056. error(_f_string_error_pos(pos, unicode_value, i),
  1057. "missing '}' in format string expression" + (
  1058. ", found '%s'" % terminal_char if terminal_char else ""))
  1059. # parse the expression as if it was surrounded by parentheses
  1060. buf = StringIO('(%s)' % expr_str)
  1061. scanner = PyrexScanner(buf, expr_pos[0], parent_scanner=s, source_encoding=s.source_encoding, initial_pos=expr_pos)
  1062. expr = p_testlist(scanner) # TODO is testlist right here?
  1063. # validate the conversion char
  1064. if conversion_char is not None and not ExprNodes.FormattedValueNode.find_conversion_func(conversion_char):
  1065. error(expr_pos, "invalid conversion character '%s'" % conversion_char)
  1066. # the format spec is itself treated like an f-string
  1067. if format_spec_str:
  1068. format_spec = ExprNodes.JoinedStrNode(pos, values=p_f_string(s, format_spec_str, pos, is_raw))
  1069. return i + 1, ExprNodes.FormattedValueNode(
  1070. pos, value=expr, conversion_char=conversion_char, format_spec=format_spec)
  1071. # since PEP 448:
  1072. # list_display ::= "[" [listmaker] "]"
  1073. # listmaker ::= (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
  1074. # comp_iter ::= comp_for | comp_if
  1075. # comp_for ::= ["async"] "for" expression_list "in" testlist [comp_iter]
  1076. # comp_if ::= "if" test [comp_iter]
  1077. def p_list_maker(s):
  1078. # s.sy == '['
  1079. pos = s.position()
  1080. s.next()
  1081. if s.sy == ']':
  1082. s.expect(']')
  1083. return ExprNodes.ListNode(pos, args=[])
  1084. expr = p_test_or_starred_expr(s)
  1085. if s.sy in ('for', 'async'):
  1086. if expr.is_starred:
  1087. s.error("iterable unpacking cannot be used in comprehension")
  1088. append = ExprNodes.ComprehensionAppendNode(pos, expr=expr)
  1089. loop = p_comp_for(s, append)
  1090. s.expect(']')
  1091. return ExprNodes.ComprehensionNode(
  1092. pos, loop=loop, append=append, type=Builtin.list_type,
  1093. # list comprehensions leak their loop variable in Py2
  1094. has_local_scope=s.context.language_level >= 3)
  1095. # (merged) list literal
  1096. if s.sy == ',':
  1097. s.next()
  1098. exprs = p_test_or_starred_expr_list(s, expr)
  1099. else:
  1100. exprs = [expr]
  1101. s.expect(']')
  1102. return ExprNodes.ListNode(pos, args=exprs)
  1103. def p_comp_iter(s, body):
  1104. if s.sy in ('for', 'async'):
  1105. return p_comp_for(s, body)
  1106. elif s.sy == 'if':
  1107. return p_comp_if(s, body)
  1108. else:
  1109. # insert the 'append' operation into the loop
  1110. return body
  1111. def p_comp_for(s, body):
  1112. pos = s.position()
  1113. # [async] for ...
  1114. is_async = False
  1115. if s.sy == 'async':
  1116. is_async = True
  1117. s.next()
  1118. # s.sy == 'for'
  1119. s.expect('for')
  1120. kw = p_for_bounds(s, allow_testlist=False, is_async=is_async)
  1121. kw.update(else_clause=None, body=p_comp_iter(s, body), is_async=is_async)
  1122. return Nodes.ForStatNode(pos, **kw)
  1123. def p_comp_if(s, body):
  1124. # s.sy == 'if'
  1125. pos = s.position()
  1126. s.next()
  1127. test = p_test_nocond(s)
  1128. return Nodes.IfStatNode(pos,
  1129. if_clauses = [Nodes.IfClauseNode(pos, condition = test,
  1130. body = p_comp_iter(s, body))],
  1131. else_clause = None )
  1132. # since PEP 448:
  1133. #dictorsetmaker: ( ((test ':' test | '**' expr)
  1134. # (comp_for | (',' (test ':' test | '**' expr))* [','])) |
  1135. # ((test | star_expr)
  1136. # (comp_for | (',' (test | star_expr))* [','])) )
  1137. def p_dict_or_set_maker(s):
  1138. # s.sy == '{'
  1139. pos = s.position()
  1140. s.next()
  1141. if s.sy == '}':
  1142. s.next()
  1143. return ExprNodes.DictNode(pos, key_value_pairs=[])
  1144. parts = []
  1145. target_type = 0
  1146. last_was_simple_item = False
  1147. while True:
  1148. if s.sy in ('*', '**'):
  1149. # merged set/dict literal
  1150. if target_type == 0:
  1151. target_type = 1 if s.sy == '*' else 2 # 'stars'
  1152. elif target_type != len(s.sy):
  1153. s.error("unexpected %sitem found in %s literal" % (
  1154. s.sy, 'set' if target_type == 1 else 'dict'))
  1155. s.next()
  1156. if s.sy == '*':
  1157. s.error("expected expression, found '*'")
  1158. item = p_starred_expr(s)
  1159. parts.append(item)
  1160. last_was_simple_item = False
  1161. else:
  1162. item = p_test(s)
  1163. if target_type == 0:
  1164. target_type = 2 if s.sy == ':' else 1 # dict vs. set
  1165. if target_type == 2:
  1166. # dict literal
  1167. s.expect(':')
  1168. key = item
  1169. value = p_test(s)
  1170. item = ExprNodes.DictItemNode(key.pos, key=key, value=value)
  1171. if last_was_simple_item:
  1172. parts[-1].append(item)
  1173. else:
  1174. parts.append([item])
  1175. last_was_simple_item = True
  1176. if s.sy == ',':
  1177. s.next()
  1178. if s.sy == '}':
  1179. break
  1180. else:
  1181. break
  1182. if s.sy in ('for', 'async'):
  1183. # dict/set comprehension
  1184. if len(parts) == 1 and isinstance(parts[0], list) and len(parts[0]) == 1:
  1185. item = parts[0][0]
  1186. if target_type == 2:
  1187. assert isinstance(item, ExprNodes.DictItemNode), type(item)
  1188. comprehension_type = Builtin.dict_type
  1189. append = ExprNodes.DictComprehensionAppendNode(
  1190. item.pos, key_expr=item.key, value_expr=item.value)
  1191. else:
  1192. comprehension_type = Builtin.set_type
  1193. append = ExprNodes.ComprehensionAppendNode(item.pos, expr=item)
  1194. loop = p_comp_for(s, append)
  1195. s.expect('}')
  1196. return ExprNodes.ComprehensionNode(pos, loop=loop, append=append, type=comprehension_type)
  1197. else:
  1198. # syntax error, try to find a good error message
  1199. if len(parts) == 1 and not isinstance(parts[0], list):
  1200. s.error("iterable unpacking cannot be used in comprehension")
  1201. else:
  1202. # e.g. "{1,2,3 for ..."
  1203. s.expect('}')
  1204. return ExprNodes.DictNode(pos, key_value_pairs=[])
  1205. s.expect('}')
  1206. if target_type == 1:
  1207. # (merged) set literal
  1208. items = []
  1209. set_items = []
  1210. for part in parts:
  1211. if isinstance(part, list):
  1212. set_items.extend(part)
  1213. else:
  1214. if set_items:
  1215. items.append(ExprNodes.SetNode(set_items[0].pos, args=set_items))
  1216. set_items = []
  1217. items.append(part)
  1218. if set_items:
  1219. items.append(ExprNodes.SetNode(set_items[0].pos, args=set_items))
  1220. if len(items) == 1 and items[0].is_set_literal:
  1221. return items[0]
  1222. return ExprNodes.MergedSequenceNode(pos, args=items, type=Builtin.set_type)
  1223. else:
  1224. # (merged) dict literal
  1225. items = []
  1226. dict_items = []
  1227. for part in parts:
  1228. if isinstance(part, list):
  1229. dict_items.extend(part)
  1230. else:
  1231. if dict_items:
  1232. items.append(ExprNodes.DictNode(dict_items[0].pos, key_value_pairs=dict_items))
  1233. dict_items = []
  1234. items.append(part)
  1235. if dict_items:
  1236. items.append(ExprNodes.DictNode(dict_items[0].pos, key_value_pairs=dict_items))
  1237. if len(items) == 1 and items[0].is_dict_literal:
  1238. return items[0]
  1239. return ExprNodes.MergedDictNode(pos, keyword_args=items, reject_duplicates=False)
  1240. # NOTE: no longer in Py3 :)
  1241. def p_backquote_expr(s):
  1242. # s.sy == '`'
  1243. pos = s.position()
  1244. s.next()
  1245. args = [p_test(s)]
  1246. while s.sy == ',':
  1247. s.next()
  1248. args.append(p_test(s))
  1249. s.expect('`')
  1250. if len(args) == 1:
  1251. arg = args[0]
  1252. else:
  1253. arg = ExprNodes.TupleNode(pos, args = args)
  1254. return ExprNodes.BackquoteNode(pos, arg = arg)
  1255. def p_simple_expr_list(s, expr=None):
  1256. exprs = expr is not None and [expr] or []
  1257. while s.sy not in expr_terminators:
  1258. exprs.append( p_test(s) )
  1259. if s.sy != ',':
  1260. break
  1261. s.next()
  1262. return exprs
  1263. def p_test_or_starred_expr_list(s, expr=None):
  1264. exprs = expr is not None and [expr] or []
  1265. while s.sy not in expr_terminators:
  1266. exprs.append(p_test_or_starred_expr(s))
  1267. if s.sy != ',':
  1268. break
  1269. s.next()
  1270. return exprs
  1271. #testlist: test (',' test)* [',']
  1272. def p_testlist(s):
  1273. pos = s.position()
  1274. expr = p_test(s)
  1275. if s.sy == ',':
  1276. s.next()
  1277. exprs = p_simple_expr_list(s, expr)
  1278. return ExprNodes.TupleNode(pos, args = exprs)
  1279. else:
  1280. return expr
  1281. # testlist_star_expr: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
  1282. def p_testlist_star_expr(s):
  1283. pos = s.position()
  1284. expr = p_test_or_starred_expr(s)
  1285. if s.sy == ',':
  1286. s.next()
  1287. exprs = p_test_or_starred_expr_list(s, expr)
  1288. return ExprNodes.TupleNode(pos, args = exprs)
  1289. else:
  1290. return expr
  1291. # testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
  1292. def p_testlist_comp(s):
  1293. pos = s.position()
  1294. expr = p_test_or_starred_expr(s)
  1295. if s.sy == ',':
  1296. s.next()
  1297. exprs = p_test_or_starred_expr_list(s, expr)
  1298. return ExprNodes.TupleNode(pos, args = exprs)
  1299. elif s.sy in ('for', 'async'):
  1300. return p_genexp(s, expr)
  1301. else:
  1302. return expr
  1303. def p_genexp(s, expr):
  1304. # s.sy == 'async' | 'for'
  1305. loop = p_comp_for(s, Nodes.ExprStatNode(
  1306. expr.pos, expr = ExprNodes.YieldExprNode(expr.pos, arg=expr)))
  1307. return ExprNodes.GeneratorExpressionNode(expr.pos, loop=loop)
  1308. expr_terminators = cython.declare(set, set([
  1309. ')', ']', '}', ':', '=', 'NEWLINE']))
  1310. #-------------------------------------------------------
  1311. #
  1312. # Statements
  1313. #
  1314. #-------------------------------------------------------
  1315. def p_global_statement(s):
  1316. # assume s.sy == 'global'
  1317. pos = s.position()
  1318. s.next()
  1319. names = p_ident_list(s)
  1320. return Nodes.GlobalNode(pos, names = names)
  1321. def p_nonlocal_statement(s):
  1322. pos = s.position()
  1323. s.next()
  1324. names = p_ident_list(s)
  1325. return Nodes.NonlocalNode(pos, names = names)
  1326. def p_expression_or_assignment(s):
  1327. expr = p_testlist_star_expr(s)
  1328. if s.sy == ':' and (expr.is_name or expr.is_subscript or expr.is_attribute):
  1329. s.next()
  1330. expr.annotation = p_test(s)
  1331. if s.sy == '=' and expr.is_starred:
  1332. # This is a common enough error to make when learning Cython to let
  1333. # it fail as early as possible and give a very clear error message.
  1334. s.error("a starred assignment target must be in a list or tuple"
  1335. " - maybe you meant to use an index assignment: var[0] = ...",
  1336. pos=expr.pos)
  1337. expr_list = [expr]
  1338. while s.sy == '=':
  1339. s.next()
  1340. if s.sy == 'yield':
  1341. expr = p_yield_expression(s)
  1342. else:
  1343. expr = p_testlist_star_expr(s)
  1344. expr_list.append(expr)
  1345. if len(expr_list) == 1:
  1346. if re.match(r"([-+*/%^&|]|<<|>>|\*\*|//|@)=", s.sy):
  1347. lhs = expr_list[0]
  1348. if isinstance(lhs, ExprNodes.SliceIndexNode):
  1349. # implementation requires IndexNode
  1350. lhs = ExprNodes.IndexNode(
  1351. lhs.pos,
  1352. base=lhs.base,
  1353. index=make_slice_node(lhs.pos, lhs.start, lhs.stop))
  1354. elif not isinstance(lhs, (ExprNodes.AttributeNode, ExprNodes.IndexNode, ExprNodes.NameNode)):
  1355. error(lhs.pos, "Illegal operand for inplace operation.")
  1356. operator = s.sy[:-1]
  1357. s.next()
  1358. if s.sy == 'yield':
  1359. rhs = p_yield_expression(s)
  1360. else:
  1361. rhs = p_testlist(s)
  1362. return Nodes.InPlaceAssignmentNode(lhs.pos, operator=operator, lhs=lhs, rhs=rhs)
  1363. expr = expr_list[0]
  1364. return Nodes.ExprStatNode(expr.pos, expr=expr)
  1365. rhs = expr_list[-1]
  1366. if len(expr_list) == 2:
  1367. return Nodes.SingleAssignmentNode(rhs.pos, lhs=expr_list[0], rhs=rhs)
  1368. else:
  1369. return Nodes.CascadedAssignmentNode(rhs.pos, lhs_list=expr_list[:-1], rhs=rhs)
  1370. def p_print_statement(s):
  1371. # s.sy == 'print'
  1372. pos = s.position()
  1373. ends_with_comma = 0
  1374. s.next()
  1375. if s.sy == '>>':
  1376. s.next()
  1377. stream = p_test(s)
  1378. if s.sy == ',':
  1379. s.next()
  1380. ends_with_comma = s.sy in ('NEWLINE', 'EOF')
  1381. else:
  1382. stream = None
  1383. args = []
  1384. if s.sy not in ('NEWLINE', 'EOF'):
  1385. args.append(p_test(s))
  1386. while s.sy == ',':
  1387. s.next()
  1388. if s.sy in ('NEWLINE', 'EOF'):
  1389. ends_with_comma = 1
  1390. break
  1391. args.append(p_test(s))
  1392. arg_tuple = ExprNodes.TupleNode(pos, args=args)
  1393. return Nodes.PrintStatNode(pos,
  1394. arg_tuple=arg_tuple, stream=stream,
  1395. append_newline=not ends_with_comma)
  1396. def p_exec_statement(s):
  1397. # s.sy == 'exec'
  1398. pos = s.position()
  1399. s.next()
  1400. code = p_bit_expr(s)
  1401. if isinstance(code, ExprNodes.TupleNode):
  1402. # Py3 compatibility syntax
  1403. tuple_variant = True
  1404. args = code.args
  1405. if len(args) not in (2, 3):
  1406. s.error("expected tuple of length 2 or 3, got length %d" % len(args),
  1407. pos=pos, fatal=False)
  1408. args = [code]
  1409. else:
  1410. tuple_variant = False
  1411. args = [code]
  1412. if s.sy == 'in':
  1413. if tuple_variant:
  1414. s.error("tuple variant of exec does not support additional 'in' arguments",
  1415. fatal=False)
  1416. s.next()
  1417. args.append(p_test(s))
  1418. if s.sy == ',':
  1419. s.next()
  1420. args.append(p_test(s))
  1421. return Nodes.ExecStatNode(pos, args=args)
  1422. def p_del_statement(s):
  1423. # s.sy == 'del'
  1424. pos = s.position()
  1425. s.next()
  1426. # FIXME: 'exprlist' in Python
  1427. args = p_simple_expr_list(s)
  1428. return Nodes.DelStatNode(pos, args = args)
  1429. def p_pass_statement(s, with_newline = 0):
  1430. pos = s.position()
  1431. s.expect('pass')
  1432. if with_newline:
  1433. s.expect_newline("Expected a newline", ignore_semicolon=True)
  1434. return Nodes.PassStatNode(pos)
  1435. def p_break_statement(s):
  1436. # s.sy == 'break'
  1437. pos = s.position()
  1438. s.next()
  1439. return Nodes.BreakStatNode(pos)
  1440. def p_continue_statement(s):
  1441. # s.sy == 'continue'
  1442. pos = s.position()
  1443. s.next()
  1444. return Nodes.ContinueStatNode(pos)
  1445. def p_return_statement(s):
  1446. # s.sy == 'return'
  1447. pos = s.position()
  1448. s.next()
  1449. if s.sy not in statement_terminators:
  1450. value = p_testlist(s)
  1451. else:
  1452. value = None
  1453. return Nodes.ReturnStatNode(pos, value = value)
  1454. def p_raise_statement(s):
  1455. # s.sy == 'raise'
  1456. pos = s.position()
  1457. s.next()
  1458. exc_type = None
  1459. exc_value = None
  1460. exc_tb = None
  1461. cause = None
  1462. if s.sy not in statement_terminators:
  1463. exc_type = p_test(s)
  1464. if s.sy == ',':
  1465. s.next()
  1466. exc_value = p_test(s)
  1467. if s.sy == ',':
  1468. s.next()
  1469. exc_tb = p_test(s)
  1470. elif s.sy == 'from':
  1471. s.next()
  1472. cause = p_test(s)
  1473. if exc_type or exc_value or exc_tb:
  1474. return Nodes.RaiseStatNode(pos,
  1475. exc_type = exc_type,
  1476. exc_value = exc_value,
  1477. exc_tb = exc_tb,
  1478. cause = cause)
  1479. else:
  1480. return Nodes.ReraiseStatNode(pos)
  1481. def p_import_statement(s):
  1482. # s.sy in ('import', 'cimport')
  1483. pos = s.position()
  1484. kind = s.sy
  1485. s.next()
  1486. items = [p_dotted_name(s, as_allowed=1)]
  1487. while s.sy == ',':
  1488. s.next()
  1489. items.append(p_dotted_name(s, as_allowed=1))
  1490. stats = []
  1491. is_absolute = Future.absolute_import in s.context.future_directives
  1492. for pos, target_name, dotted_name, as_name in items:
  1493. if kind == 'cimport':
  1494. stat = Nodes.CImportStatNode(
  1495. pos,
  1496. module_name=dotted_name,
  1497. as_name=as_name,
  1498. is_absolute=is_absolute)
  1499. else:
  1500. if as_name and "." in dotted_name:
  1501. name_list = ExprNodes.ListNode(pos, args=[
  1502. ExprNodes.IdentifierStringNode(pos, value=s.context.intern_ustring("*"))])
  1503. else:
  1504. name_list = None
  1505. stat = Nodes.SingleAssignmentNode(
  1506. pos,
  1507. lhs=ExprNodes.NameNode(pos, name=as_name or target_name),
  1508. rhs=ExprNodes.ImportNode(
  1509. pos,
  1510. module_name=ExprNodes.IdentifierStringNode(pos, value=dotted_name),
  1511. level=0 if is_absolute else None,
  1512. name_list=name_list))
  1513. stats.append(stat)
  1514. return Nodes.StatListNode(pos, stats=stats)
  1515. def p_from_import_statement(s, first_statement = 0):
  1516. # s.sy == 'from'
  1517. pos = s.position()
  1518. s.next()
  1519. if s.sy == '.':
  1520. # count relative import level
  1521. level = 0
  1522. while s.sy == '.':
  1523. level += 1
  1524. s.next()
  1525. else:
  1526. level = None
  1527. if level is not None and s.sy in ('import', 'cimport'):
  1528. # we are dealing with "from .. import foo, bar"
  1529. dotted_name_pos, dotted_name = s.position(), s.context.intern_ustring('')
  1530. else:
  1531. if level is None and Future.absolute_import in s.context.future_directives:
  1532. level = 0
  1533. (dotted_name_pos, _, dotted_name, _) = p_dotted_name(s, as_allowed=False)
  1534. if s.sy not in ('import', 'cimport'):
  1535. s.error("Expected 'import' or 'cimport'")
  1536. kind = s.sy
  1537. s.next()
  1538. is_cimport = kind == 'cimport'
  1539. is_parenthesized = False
  1540. if s.sy == '*':
  1541. imported_names = [(s.position(), s.context.intern_ustring("*"), None, None)]
  1542. s.next()
  1543. else:
  1544. if s.sy == '(':
  1545. is_parenthesized = True
  1546. s.next()
  1547. imported_names = [p_imported_name(s, is_cimport)]
  1548. while s.sy == ',':
  1549. s.next()
  1550. if is_parenthesized and s.sy == ')':
  1551. break
  1552. imported_names.append(p_imported_name(s, is_cimport))
  1553. if is_parenthesized:
  1554. s.expect(')')
  1555. if dotted_name == '__future__':
  1556. if not first_statement:
  1557. s.error("from __future__ imports must occur at the beginning of the file")
  1558. elif level:
  1559. s.error("invalid syntax")
  1560. else:
  1561. for (name_pos, name, as_name, kind) in imported_names:
  1562. if name == "braces":
  1563. s.error("not a chance", name_pos)
  1564. break
  1565. try:
  1566. directive = getattr(Future, name)
  1567. except AttributeError:
  1568. s.error("future feature %s is not defined" % name, name_pos)
  1569. break
  1570. s.context.future_directives.add(directive)
  1571. return Nodes.PassStatNode(pos)
  1572. elif kind == 'cimport':
  1573. return Nodes.FromCImportStatNode(
  1574. pos, module_name=dotted_name,
  1575. relative_level=level,
  1576. imported_names=imported_names)
  1577. else:
  1578. imported_name_strings = []
  1579. items = []
  1580. for (name_pos, name, as_name, kind) in imported_names:
  1581. imported_name_strings.append(
  1582. ExprNodes.IdentifierStringNode(name_pos, value=name))
  1583. items.append(
  1584. (name, ExprNodes.NameNode(name_pos, name=as_name or name)))
  1585. import_list = ExprNodes.ListNode(
  1586. imported_names[0][0], args=imported_name_strings)
  1587. return Nodes.FromImportStatNode(pos,
  1588. module = ExprNodes.ImportNode(dotted_name_pos,
  1589. module_name = ExprNodes.IdentifierStringNode(pos, value = dotted_name),
  1590. level = level,
  1591. name_list = import_list),
  1592. items = items)
  1593. imported_name_kinds = cython.declare(set, set(['class', 'struct', 'union']))
  1594. def p_imported_name(s, is_cimport):
  1595. pos = s.position()
  1596. kind = None
  1597. if is_cimport and s.systring in imported_name_kinds:
  1598. kind = s.systring
  1599. warning(pos, 'the "from module cimport %s name" syntax is deprecated and '
  1600. 'will be removed in Cython 3.0' % kind, 2)
  1601. s.next()
  1602. name = p_ident(s)
  1603. as_name = p_as_name(s)
  1604. return (pos, name, as_name, kind)
  1605. def p_dotted_name(s, as_allowed):
  1606. pos = s.position()
  1607. target_name = p_ident(s)
  1608. as_name = None
  1609. names = [target_name]
  1610. while s.sy == '.':
  1611. s.next()
  1612. names.append(p_ident(s))
  1613. if as_allowed:
  1614. as_name = p_as_name(s)
  1615. return (pos, target_name, s.context.intern_ustring(u'.'.join(names)), as_name)
  1616. def p_as_name(s):
  1617. if s.sy == 'IDENT' and s.systring == 'as':
  1618. s.next()
  1619. return p_ident(s)
  1620. else:
  1621. return None
  1622. def p_assert_statement(s):
  1623. # s.sy == 'assert'
  1624. pos = s.position()
  1625. s.next()
  1626. cond = p_test(s)
  1627. if s.sy == ',':
  1628. s.next()
  1629. value = p_test(s)
  1630. else:
  1631. value = None
  1632. return Nodes.AssertStatNode(pos, cond = cond, value = value)
  1633. statement_terminators = cython.declare(set, set([';', 'NEWLINE', 'EOF']))
  1634. def p_if_statement(s):
  1635. # s.sy == 'if'
  1636. pos = s.position()
  1637. s.next()
  1638. if_clauses = [p_if_clause(s)]
  1639. while s.sy == 'elif':
  1640. s.next()
  1641. if_clauses.append(p_if_clause(s))
  1642. else_clause = p_else_clause(s)
  1643. return Nodes.IfStatNode(pos,
  1644. if_clauses = if_clauses, else_clause = else_clause)
  1645. def p_if_clause(s):
  1646. pos = s.position()
  1647. test = p_test(s)
  1648. body = p_suite(s)
  1649. return Nodes.IfClauseNode(pos,
  1650. condition = test, body = body)
  1651. def p_else_clause(s):
  1652. if s.sy == 'else':
  1653. s.next()
  1654. return p_suite(s)
  1655. else:
  1656. return None
  1657. def p_while_statement(s):
  1658. # s.sy == 'while'
  1659. pos = s.position()
  1660. s.next()
  1661. test = p_test(s)
  1662. body = p_suite(s)
  1663. else_clause = p_else_clause(s)
  1664. return Nodes.WhileStatNode(pos,
  1665. condition = test, body = body,
  1666. else_clause = else_clause)
  1667. def p_for_statement(s, is_async=False):
  1668. # s.sy == 'for'
  1669. pos = s.position()
  1670. s.next()
  1671. kw = p_for_bounds(s, allow_testlist=True, is_async=is_async)
  1672. body = p_suite(s)
  1673. else_clause = p_else_clause(s)
  1674. kw.update(body=body, else_clause=else_clause, is_async=is_async)
  1675. return Nodes.ForStatNode(pos, **kw)
  1676. def p_for_bounds(s, allow_testlist=True, is_async=False):
  1677. target = p_for_target(s)
  1678. if s.sy == 'in':
  1679. s.next()
  1680. iterator = p_for_iterator(s, allow_testlist, is_async=is_async)
  1681. return dict(target=target, iterator=iterator)
  1682. elif not s.in_python_file and not is_async:
  1683. if s.sy == 'from':
  1684. s.next()
  1685. bound1 = p_bit_expr(s)
  1686. else:
  1687. # Support shorter "for a <= x < b" syntax
  1688. bound1, target = target, None
  1689. rel1 = p_for_from_relation(s)
  1690. name2_pos = s.position()
  1691. name2 = p_ident(s)
  1692. rel2_pos = s.position()
  1693. rel2 = p_for_from_relation(s)
  1694. bound2 = p_bit_expr(s)
  1695. step = p_for_from_step(s)
  1696. if target is None:
  1697. target = ExprNodes.NameNode(name2_pos, name = name2)
  1698. else:
  1699. if not target.is_name:
  1700. error(target.pos,
  1701. "Target of for-from statement must be a variable name")
  1702. elif name2 != target.name:
  1703. error(name2_pos,
  1704. "Variable name in for-from range does not match target")
  1705. if rel1[0] != rel2[0]:
  1706. error(rel2_pos,
  1707. "Relation directions in for-from do not match")
  1708. return dict(target = target,
  1709. bound1 = bound1,
  1710. relation1 = rel1,
  1711. relation2 = rel2,
  1712. bound2 = bound2,
  1713. step = step,
  1714. )
  1715. else:
  1716. s.expect('in')
  1717. return {}
  1718. def p_for_from_relation(s):
  1719. if s.sy in inequality_relations:
  1720. op = s.sy
  1721. s.next()
  1722. return op
  1723. else:
  1724. s.error("Expected one of '<', '<=', '>' '>='")
  1725. def p_for_from_step(s):
  1726. if s.sy == 'IDENT' and s.systring == 'by':
  1727. s.next()
  1728. step = p_bit_expr(s)
  1729. return step
  1730. else:
  1731. return None
  1732. inequality_relations = cython.declare(set, set(['<', '<=', '>', '>=']))
  1733. def p_target(s, terminator):
  1734. pos = s.position()
  1735. expr = p_starred_expr(s)
  1736. if s.sy == ',':
  1737. s.next()
  1738. exprs = [expr]
  1739. while s.sy != terminator:
  1740. exprs.append(p_starred_expr(s))
  1741. if s.sy != ',':
  1742. break
  1743. s.next()
  1744. return ExprNodes.TupleNode(pos, args = exprs)
  1745. else:
  1746. return expr
  1747. def p_for_target(s):
  1748. return p_target(s, 'in')
  1749. def p_for_iterator(s, allow_testlist=True, is_async=False):
  1750. pos = s.position()
  1751. if allow_testlist:
  1752. expr = p_testlist(s)
  1753. else:
  1754. expr = p_or_test(s)
  1755. return (ExprNodes.AsyncIteratorNode if is_async else ExprNodes.IteratorNode)(pos, sequence=expr)
  1756. def p_try_statement(s):
  1757. # s.sy == 'try'
  1758. pos = s.position()
  1759. s.next()
  1760. body = p_suite(s)
  1761. except_clauses = []
  1762. else_clause = None
  1763. if s.sy in ('except', 'else'):
  1764. while s.sy == 'except':
  1765. except_clauses.append(p_except_clause(s))
  1766. if s.sy == 'else':
  1767. s.next()
  1768. else_clause = p_suite(s)
  1769. body = Nodes.TryExceptStatNode(pos,
  1770. body = body, except_clauses = except_clauses,
  1771. else_clause = else_clause)
  1772. if s.sy != 'finally':
  1773. return body
  1774. # try-except-finally is equivalent to nested try-except/try-finally
  1775. if s.sy == 'finally':
  1776. s.next()
  1777. finally_clause = p_suite(s)
  1778. return Nodes.TryFinallyStatNode(pos,
  1779. body = body, finally_clause = finally_clause)
  1780. else:
  1781. s.error("Expected 'except' or 'finally'")
  1782. def p_except_clause(s):
  1783. # s.sy == 'except'
  1784. pos = s.position()
  1785. s.next()
  1786. exc_type = None
  1787. exc_value = None
  1788. is_except_as = False
  1789. if s.sy != ':':
  1790. exc_type = p_test(s)
  1791. # normalise into list of single exception tests
  1792. if isinstance(exc_type, ExprNodes.TupleNode):
  1793. exc_type = exc_type.args
  1794. else:
  1795. exc_type = [exc_type]
  1796. if s.sy == ',' or (s.sy == 'IDENT' and s.systring == 'as'
  1797. and s.context.language_level == 2):
  1798. s.next()
  1799. exc_value = p_test(s)
  1800. elif s.sy == 'IDENT' and s.systring == 'as':
  1801. # Py3 syntax requires a name here
  1802. s.next()
  1803. pos2 = s.position()
  1804. name = p_ident(s)
  1805. exc_value = ExprNodes.NameNode(pos2, name = name)
  1806. is_except_as = True
  1807. body = p_suite(s)
  1808. return Nodes.ExceptClauseNode(pos,
  1809. pattern = exc_type, target = exc_value,
  1810. body = body, is_except_as=is_except_as)
  1811. def p_include_statement(s, ctx):
  1812. pos = s.position()
  1813. s.next() # 'include'
  1814. unicode_include_file_name = p_string_literal(s, 'u')[2]
  1815. s.expect_newline("Syntax error in include statement")
  1816. if s.compile_time_eval:
  1817. include_file_name = unicode_include_file_name
  1818. include_file_path = s.context.find_include_file(include_file_name, pos)
  1819. if include_file_path:
  1820. s.included_files.append(include_file_name)
  1821. with Utils.open_source_file(include_file_path) as f:
  1822. if Options.source_root:
  1823. import os
  1824. rel_path = os.path.relpath(include_file_path, Options.source_root)
  1825. else:
  1826. rel_path = None
  1827. source_desc = FileSourceDescriptor(include_file_path, rel_path)
  1828. s2 = PyrexScanner(f, source_desc, s, source_encoding=f.encoding, parse_comments=s.parse_comments)
  1829. tree = p_statement_list(s2, ctx)
  1830. return tree
  1831. else:
  1832. return None
  1833. else:
  1834. return Nodes.PassStatNode(pos)
  1835. def p_with_statement(s):
  1836. s.next() # 'with'
  1837. if s.systring == 'template' and not s.in_python_file:
  1838. node = p_with_template(s)
  1839. else:
  1840. node = p_with_items(s)
  1841. return node
  1842. def p_with_items(s, is_async=False):
  1843. pos = s.position()
  1844. if not s.in_python_file and s.sy == 'IDENT' and s.systring in ('nogil', 'gil'):
  1845. if is_async:
  1846. s.error("with gil/nogil cannot be async")
  1847. state = s.systring
  1848. s.next()
  1849. if s.sy == ',':
  1850. s.next()
  1851. body = p_with_items(s)
  1852. else:
  1853. body = p_suite(s)
  1854. return Nodes.GILStatNode(pos, state=state, body=body)
  1855. else:
  1856. manager = p_test(s)
  1857. target = None
  1858. if s.sy == 'IDENT' and s.systring == 'as':
  1859. s.next()
  1860. target = p_starred_expr(s)
  1861. if s.sy == ',':
  1862. s.next()
  1863. body = p_with_items(s, is_async=is_async)
  1864. else:
  1865. body = p_suite(s)
  1866. return Nodes.WithStatNode(pos, manager=manager, target=target, body=body, is_async=is_async)
  1867. def p_with_template(s):
  1868. pos = s.position()
  1869. templates = []
  1870. s.next()
  1871. s.expect('[')
  1872. templates.append(s.systring)
  1873. s.next()
  1874. while s.systring == ',':
  1875. s.next()
  1876. templates.append(s.systring)
  1877. s.next()
  1878. s.expect(']')
  1879. if s.sy == ':':
  1880. s.next()
  1881. s.expect_newline("Syntax error in template function declaration")
  1882. s.expect_indent()
  1883. body_ctx = Ctx()
  1884. body_ctx.templates = templates
  1885. func_or_var = p_c_func_or_var_declaration(s, pos, body_ctx)
  1886. s.expect_dedent()
  1887. return func_or_var
  1888. else:
  1889. error(pos, "Syntax error in template function declaration")
  1890. def p_simple_statement(s, first_statement = 0):
  1891. #print "p_simple_statement:", s.sy, s.systring ###
  1892. if s.sy == 'global':
  1893. node = p_global_statement(s)
  1894. elif s.sy == 'nonlocal':
  1895. node = p_nonlocal_statement(s)
  1896. elif s.sy == 'print':
  1897. node = p_print_statement(s)
  1898. elif s.sy == 'exec':
  1899. node = p_exec_statement(s)
  1900. elif s.sy == 'del':
  1901. node = p_del_statement(s)
  1902. elif s.sy == 'break':
  1903. node = p_break_statement(s)
  1904. elif s.sy == 'continue':
  1905. node = p_continue_statement(s)
  1906. elif s.sy == 'return':
  1907. node = p_return_statement(s)
  1908. elif s.sy == 'raise':
  1909. node = p_raise_statement(s)
  1910. elif s.sy in ('import', 'cimport'):
  1911. node = p_import_statement(s)
  1912. elif s.sy == 'from':
  1913. node = p_from_import_statement(s, first_statement = first_statement)
  1914. elif s.sy == 'yield':
  1915. node = p_yield_statement(s)
  1916. elif s.sy == 'assert':
  1917. node = p_assert_statement(s)
  1918. elif s.sy == 'pass':
  1919. node = p_pass_statement(s)
  1920. else:
  1921. node = p_expression_or_assignment(s)
  1922. return node
  1923. def p_simple_statement_list(s, ctx, first_statement = 0):
  1924. # Parse a series of simple statements on one line
  1925. # separated by semicolons.
  1926. stat = p_simple_statement(s, first_statement = first_statement)
  1927. pos = stat.pos
  1928. stats = []
  1929. if not isinstance(stat, Nodes.PassStatNode):
  1930. stats.append(stat)
  1931. while s.sy == ';':
  1932. #print "p_simple_statement_list: maybe more to follow" ###
  1933. s.next()
  1934. if s.sy in ('NEWLINE', 'EOF'):
  1935. break
  1936. stat = p_simple_statement(s, first_statement = first_statement)
  1937. if isinstance(stat, Nodes.PassStatNode):
  1938. continue
  1939. stats.append(stat)
  1940. first_statement = False
  1941. if not stats:
  1942. stat = Nodes.PassStatNode(pos)
  1943. elif len(stats) == 1:
  1944. stat = stats[0]
  1945. else:
  1946. stat = Nodes.StatListNode(pos, stats = stats)
  1947. if s.sy not in ('NEWLINE', 'EOF'):
  1948. # provide a better error message for users who accidentally write Cython code in .py files
  1949. if isinstance(stat, Nodes.ExprStatNode):
  1950. if stat.expr.is_name and stat.expr.name == 'cdef':
  1951. s.error("The 'cdef' keyword is only allowed in Cython files (pyx/pxi/pxd)", pos)
  1952. s.expect_newline("Syntax error in simple statement list")
  1953. return stat
  1954. def p_compile_time_expr(s):
  1955. old = s.compile_time_expr
  1956. s.compile_time_expr = 1
  1957. expr = p_testlist(s)
  1958. s.compile_time_expr = old
  1959. return expr
  1960. def p_DEF_statement(s):
  1961. pos = s.position()
  1962. denv = s.compile_time_env
  1963. s.next() # 'DEF'
  1964. name = p_ident(s)
  1965. s.expect('=')
  1966. expr = p_compile_time_expr(s)
  1967. if s.compile_time_eval:
  1968. value = expr.compile_time_value(denv)
  1969. #print "p_DEF_statement: %s = %r" % (name, value) ###
  1970. denv.declare(name, value)
  1971. s.expect_newline("Expected a newline", ignore_semicolon=True)
  1972. return Nodes.PassStatNode(pos)
  1973. def p_IF_statement(s, ctx):
  1974. pos = s.position()
  1975. saved_eval = s.compile_time_eval
  1976. current_eval = saved_eval
  1977. denv = s.compile_time_env
  1978. result = None
  1979. while 1:
  1980. s.next() # 'IF' or 'ELIF'
  1981. expr = p_compile_time_expr(s)
  1982. s.compile_time_eval = current_eval and bool(expr.compile_time_value(denv))
  1983. body = p_suite(s, ctx)
  1984. if s.compile_time_eval:
  1985. result = body
  1986. current_eval = 0
  1987. if s.sy != 'ELIF':
  1988. break
  1989. if s.sy == 'ELSE':
  1990. s.next()
  1991. s.compile_time_eval = current_eval
  1992. body = p_suite(s, ctx)
  1993. if current_eval:
  1994. result = body
  1995. if not result:
  1996. result = Nodes.PassStatNode(pos)
  1997. s.compile_time_eval = saved_eval
  1998. return result
  1999. def p_statement(s, ctx, first_statement = 0):
  2000. cdef_flag = ctx.cdef_flag
  2001. decorators = None
  2002. if s.sy == 'ctypedef':
  2003. if ctx.level not in ('module', 'module_pxd'):
  2004. s.error("ctypedef statement not allowed here")
  2005. #if ctx.api:
  2006. # error(s.position(), "'api' not allowed with 'ctypedef'")
  2007. return p_ctypedef_statement(s, ctx)
  2008. elif s.sy == 'DEF':
  2009. return p_DEF_statement(s)
  2010. elif s.sy == 'IF':
  2011. return p_IF_statement(s, ctx)
  2012. elif s.sy == '@':
  2013. if ctx.level not in ('module', 'class', 'c_class', 'function', 'property', 'module_pxd', 'c_class_pxd', 'other'):
  2014. s.error('decorator not allowed here')
  2015. s.level = ctx.level
  2016. decorators = p_decorators(s)
  2017. if not ctx.allow_struct_enum_decorator and s.sy not in ('def', 'cdef', 'cpdef', 'class', 'async'):
  2018. if s.sy == 'IDENT' and s.systring == 'async':
  2019. pass # handled below
  2020. else:
  2021. s.error("Decorators can only be followed by functions or classes")
  2022. elif s.sy == 'pass' and cdef_flag:
  2023. # empty cdef block
  2024. return p_pass_statement(s, with_newline=1)
  2025. overridable = 0
  2026. if s.sy == 'cdef':
  2027. cdef_flag = 1
  2028. s.next()
  2029. elif s.sy == 'cpdef':
  2030. cdef_flag = 1
  2031. overridable = 1
  2032. s.next()
  2033. if cdef_flag:
  2034. if ctx.level not in ('module', 'module_pxd', 'function', 'c_class', 'c_class_pxd'):
  2035. s.error('cdef statement not allowed here')
  2036. s.level = ctx.level
  2037. node = p_cdef_statement(s, ctx(overridable=overridable))
  2038. if decorators is not None:
  2039. tup = (Nodes.CFuncDefNode, Nodes.CVarDefNode, Nodes.CClassDefNode)
  2040. if ctx.allow_struct_enum_decorator:
  2041. tup += (Nodes.CStructOrUnionDefNode, Nodes.CEnumDefNode)
  2042. if not isinstance(node, tup):
  2043. s.error("Decorators can only be followed by functions or classes")
  2044. node.decorators = decorators
  2045. return node
  2046. else:
  2047. if ctx.api:
  2048. s.error("'api' not allowed with this statement", fatal=False)
  2049. elif s.sy == 'def':
  2050. # def statements aren't allowed in pxd files, except
  2051. # as part of a cdef class
  2052. if ('pxd' in ctx.level) and (ctx.level != 'c_class_pxd'):
  2053. s.error('def statement not allowed here')
  2054. s.level = ctx.level
  2055. return p_def_statement(s, decorators)
  2056. elif s.sy == 'class':
  2057. if ctx.level not in ('module', 'function', 'class', 'other'):
  2058. s.error("class definition not allowed here")
  2059. return p_class_statement(s, decorators)
  2060. elif s.sy == 'include':
  2061. if ctx.level not in ('module', 'module_pxd'):
  2062. s.error("include statement not allowed here")
  2063. return p_include_statement(s, ctx)
  2064. elif ctx.level == 'c_class' and s.sy == 'IDENT' and s.systring == 'property':
  2065. return p_property_decl(s)
  2066. elif s.sy == 'pass' and ctx.level != 'property':
  2067. return p_pass_statement(s, with_newline=True)
  2068. else:
  2069. if ctx.level in ('c_class_pxd', 'property'):
  2070. node = p_ignorable_statement(s)
  2071. if node is not None:
  2072. return node
  2073. s.error("Executable statement not allowed here")
  2074. if s.sy == 'if':
  2075. return p_if_statement(s)
  2076. elif s.sy == 'while':
  2077. return p_while_statement(s)
  2078. elif s.sy == 'for':
  2079. return p_for_statement(s)
  2080. elif s.sy == 'try':
  2081. return p_try_statement(s)
  2082. elif s.sy == 'with':
  2083. return p_with_statement(s)
  2084. elif s.sy == 'async':
  2085. s.next()
  2086. return p_async_statement(s, ctx, decorators)
  2087. else:
  2088. if s.sy == 'IDENT' and s.systring == 'async':
  2089. ident_name = s.systring
  2090. # PEP 492 enables the async/await keywords when it spots "async def ..."
  2091. s.next()
  2092. if s.sy == 'def':
  2093. return p_async_statement(s, ctx, decorators)
  2094. elif decorators:
  2095. s.error("Decorators can only be followed by functions or classes")
  2096. s.put_back('IDENT', ident_name) # re-insert original token
  2097. return p_simple_statement_list(s, ctx, first_statement=first_statement)
  2098. def p_statement_list(s, ctx, first_statement = 0):
  2099. # Parse a series of statements separated by newlines.
  2100. pos = s.position()
  2101. stats = []
  2102. while s.sy not in ('DEDENT', 'EOF'):
  2103. stat = p_statement(s, ctx, first_statement = first_statement)
  2104. if isinstance(stat, Nodes.PassStatNode):
  2105. continue
  2106. stats.append(stat)
  2107. first_statement = False
  2108. if not stats:
  2109. return Nodes.PassStatNode(pos)
  2110. elif len(stats) == 1:
  2111. return stats[0]
  2112. else:
  2113. return Nodes.StatListNode(pos, stats = stats)
  2114. def p_suite(s, ctx=Ctx()):
  2115. return p_suite_with_docstring(s, ctx, with_doc_only=False)[1]
  2116. def p_suite_with_docstring(s, ctx, with_doc_only=False):
  2117. s.expect(':')
  2118. doc = None
  2119. if s.sy == 'NEWLINE':
  2120. s.next()
  2121. s.expect_indent()
  2122. if with_doc_only:
  2123. doc = p_doc_string(s)
  2124. body = p_statement_list(s, ctx)
  2125. s.expect_dedent()
  2126. else:
  2127. if ctx.api:
  2128. s.error("'api' not allowed with this statement", fatal=False)
  2129. if ctx.level in ('module', 'class', 'function', 'other'):
  2130. body = p_simple_statement_list(s, ctx)
  2131. else:
  2132. body = p_pass_statement(s)
  2133. s.expect_newline("Syntax error in declarations", ignore_semicolon=True)
  2134. if not with_doc_only:
  2135. doc, body = _extract_docstring(body)
  2136. return doc, body
  2137. def p_positional_and_keyword_args(s, end_sy_set, templates = None):
  2138. """
  2139. Parses positional and keyword arguments. end_sy_set
  2140. should contain any s.sy that terminate the argument list.
  2141. Argument expansion (* and **) are not allowed.
  2142. Returns: (positional_args, keyword_args)
  2143. """
  2144. positional_args = []
  2145. keyword_args = []
  2146. pos_idx = 0
  2147. while s.sy not in end_sy_set:
  2148. if s.sy == '*' or s.sy == '**':
  2149. s.error('Argument expansion not allowed here.', fatal=False)
  2150. parsed_type = False
  2151. if s.sy == 'IDENT' and s.peek()[0] == '=':
  2152. ident = s.systring
  2153. s.next() # s.sy is '='
  2154. s.next()
  2155. if looking_at_expr(s):
  2156. arg = p_test(s)
  2157. else:
  2158. base_type = p_c_base_type(s, templates = templates)
  2159. declarator = p_c_declarator(s, empty = 1)
  2160. arg = Nodes.CComplexBaseTypeNode(base_type.pos,
  2161. base_type = base_type, declarator = declarator)
  2162. parsed_type = True
  2163. keyword_node = ExprNodes.IdentifierStringNode(arg.pos, value=ident)
  2164. keyword_args.append((keyword_node, arg))
  2165. was_keyword = True
  2166. else:
  2167. if looking_at_expr(s):
  2168. arg = p_test(s)
  2169. else:
  2170. base_type = p_c_base_type(s, templates = templates)
  2171. declarator = p_c_declarator(s, empty = 1)
  2172. arg = Nodes.CComplexBaseTypeNode(base_type.pos,
  2173. base_type = base_type, declarator = declarator)
  2174. parsed_type = True
  2175. positional_args.append(arg)
  2176. pos_idx += 1
  2177. if len(keyword_args) > 0:
  2178. s.error("Non-keyword arg following keyword arg",
  2179. pos=arg.pos)
  2180. if s.sy != ',':
  2181. if s.sy not in end_sy_set:
  2182. if parsed_type:
  2183. s.error("Unmatched %s" % " or ".join(end_sy_set))
  2184. break
  2185. s.next()
  2186. return positional_args, keyword_args
  2187. def p_c_base_type(s, self_flag = 0, nonempty = 0, templates = None):
  2188. # If self_flag is true, this is the base type for the
  2189. # self argument of a C method of an extension type.
  2190. if s.sy == '(':
  2191. return p_c_complex_base_type(s, templates = templates)
  2192. else:
  2193. return p_c_simple_base_type(s, self_flag, nonempty = nonempty, templates = templates)
  2194. def p_calling_convention(s):
  2195. if s.sy == 'IDENT' and s.systring in calling_convention_words:
  2196. result = s.systring
  2197. s.next()
  2198. return result
  2199. else:
  2200. return ""
  2201. calling_convention_words = cython.declare(
  2202. set, set(["__stdcall", "__cdecl", "__fastcall"]))
  2203. def p_c_complex_base_type(s, templates = None):
  2204. # s.sy == '('
  2205. pos = s.position()
  2206. s.next()
  2207. base_type = p_c_base_type(s, templates=templates)
  2208. declarator = p_c_declarator(s, empty=True)
  2209. type_node = Nodes.CComplexBaseTypeNode(
  2210. pos, base_type=base_type, declarator=declarator)
  2211. if s.sy == ',':
  2212. components = [type_node]
  2213. while s.sy == ',':
  2214. s.next()
  2215. if s.sy == ')':
  2216. break
  2217. base_type = p_c_base_type(s, templates=templates)
  2218. declarator = p_c_declarator(s, empty=True)
  2219. components.append(Nodes.CComplexBaseTypeNode(
  2220. pos, base_type=base_type, declarator=declarator))
  2221. type_node = Nodes.CTupleBaseTypeNode(pos, components = components)
  2222. s.expect(')')
  2223. if s.sy == '[':
  2224. if is_memoryviewslice_access(s):
  2225. type_node = p_memoryviewslice_access(s, type_node)
  2226. else:
  2227. type_node = p_buffer_or_template(s, type_node, templates)
  2228. return type_node
  2229. def p_c_simple_base_type(s, self_flag, nonempty, templates = None):
  2230. #print "p_c_simple_base_type: self_flag =", self_flag, nonempty
  2231. is_basic = 0
  2232. signed = 1
  2233. longness = 0
  2234. complex = 0
  2235. module_path = []
  2236. pos = s.position()
  2237. if not s.sy == 'IDENT':
  2238. error(pos, "Expected an identifier, found '%s'" % s.sy)
  2239. if s.systring == 'const':
  2240. s.next()
  2241. base_type = p_c_base_type(s, self_flag=self_flag, nonempty=nonempty, templates=templates)
  2242. if isinstance(base_type, Nodes.MemoryViewSliceTypeNode):
  2243. # reverse order to avoid having to write "(const int)[:]"
  2244. base_type.base_type_node = Nodes.CConstTypeNode(pos, base_type=base_type.base_type_node)
  2245. return base_type
  2246. return Nodes.CConstTypeNode(pos, base_type=base_type)
  2247. if looking_at_base_type(s):
  2248. #print "p_c_simple_base_type: looking_at_base_type at", s.position()
  2249. is_basic = 1
  2250. if s.sy == 'IDENT' and s.systring in special_basic_c_types:
  2251. signed, longness = special_basic_c_types[s.systring]
  2252. name = s.systring
  2253. s.next()
  2254. else:
  2255. signed, longness = p_sign_and_longness(s)
  2256. if s.sy == 'IDENT' and s.systring in basic_c_type_names:
  2257. name = s.systring
  2258. s.next()
  2259. else:
  2260. name = 'int' # long [int], short [int], long [int] complex, etc.
  2261. if s.sy == 'IDENT' and s.systring == 'complex':
  2262. complex = 1
  2263. s.next()
  2264. elif looking_at_dotted_name(s):
  2265. #print "p_c_simple_base_type: looking_at_type_name at", s.position()
  2266. name = s.systring
  2267. s.next()
  2268. while s.sy == '.':
  2269. module_path.append(name)
  2270. s.next()
  2271. name = p_ident(s)
  2272. else:
  2273. name = s.systring
  2274. s.next()
  2275. if nonempty and s.sy != 'IDENT':
  2276. # Make sure this is not a declaration of a variable or function.
  2277. if s.sy == '(':
  2278. s.next()
  2279. if (s.sy == '*' or s.sy == '**' or s.sy == '&'
  2280. or (s.sy == 'IDENT' and s.systring in calling_convention_words)):
  2281. s.put_back('(', '(')
  2282. else:
  2283. s.put_back('(', '(')
  2284. s.put_back('IDENT', name)
  2285. name = None
  2286. elif s.sy not in ('*', '**', '[', '&'):
  2287. s.put_back('IDENT', name)
  2288. name = None
  2289. type_node = Nodes.CSimpleBaseTypeNode(pos,
  2290. name = name, module_path = module_path,
  2291. is_basic_c_type = is_basic, signed = signed,
  2292. complex = complex, longness = longness,
  2293. is_self_arg = self_flag, templates = templates)
  2294. # declarations here.
  2295. if s.sy == '[':
  2296. if is_memoryviewslice_access(s):
  2297. type_node = p_memoryviewslice_access(s, type_node)
  2298. else:
  2299. type_node = p_buffer_or_template(s, type_node, templates)
  2300. if s.sy == '.':
  2301. s.next()
  2302. name = p_ident(s)
  2303. type_node = Nodes.CNestedBaseTypeNode(pos, base_type = type_node, name = name)
  2304. return type_node
  2305. def p_buffer_or_template(s, base_type_node, templates):
  2306. # s.sy == '['
  2307. pos = s.position()
  2308. s.next()
  2309. # Note that buffer_positional_options_count=1, so the only positional argument is dtype.
  2310. # For templated types, all parameters are types.
  2311. positional_args, keyword_args = (
  2312. p_positional_and_keyword_args(s, (']',), templates)
  2313. )
  2314. s.expect(']')
  2315. if s.sy == '[':
  2316. base_type_node = p_buffer_or_template(s, base_type_node, templates)
  2317. keyword_dict = ExprNodes.DictNode(pos,
  2318. key_value_pairs = [
  2319. ExprNodes.DictItemNode(pos=key.pos, key=key, value=value)
  2320. for key, value in keyword_args
  2321. ])
  2322. result = Nodes.TemplatedTypeNode(pos,
  2323. positional_args = positional_args,
  2324. keyword_args = keyword_dict,
  2325. base_type_node = base_type_node)
  2326. return result
  2327. def p_bracketed_base_type(s, base_type_node, nonempty, empty):
  2328. # s.sy == '['
  2329. if empty and not nonempty:
  2330. # sizeof-like thing. Only anonymous C arrays allowed (int[SIZE]).
  2331. return base_type_node
  2332. elif not empty and nonempty:
  2333. # declaration of either memoryview slice or buffer.
  2334. if is_memoryviewslice_access(s):
  2335. return p_memoryviewslice_access(s, base_type_node)
  2336. else:
  2337. return p_buffer_or_template(s, base_type_node, None)
  2338. # return p_buffer_access(s, base_type_node)
  2339. elif not empty and not nonempty:
  2340. # only anonymous C arrays and memoryview slice arrays here. We
  2341. # disallow buffer declarations for now, due to ambiguity with anonymous
  2342. # C arrays.
  2343. if is_memoryviewslice_access(s):
  2344. return p_memoryviewslice_access(s, base_type_node)
  2345. else:
  2346. return base_type_node
  2347. def is_memoryviewslice_access(s):
  2348. # s.sy == '['
  2349. # a memoryview slice declaration is distinguishable from a buffer access
  2350. # declaration by the first entry in the bracketed list. The buffer will
  2351. # not have an unnested colon in the first entry; the memoryview slice will.
  2352. saved = [(s.sy, s.systring)]
  2353. s.next()
  2354. retval = False
  2355. if s.systring == ':':
  2356. retval = True
  2357. elif s.sy == 'INT':
  2358. saved.append((s.sy, s.systring))
  2359. s.next()
  2360. if s.sy == ':':
  2361. retval = True
  2362. for sv in saved[::-1]:
  2363. s.put_back(*sv)
  2364. return retval
  2365. def p_memoryviewslice_access(s, base_type_node):
  2366. # s.sy == '['
  2367. pos = s.position()
  2368. s.next()
  2369. subscripts, _ = p_subscript_list(s)
  2370. # make sure each entry in subscripts is a slice
  2371. for subscript in subscripts:
  2372. if len(subscript) < 2:
  2373. s.error("An axis specification in memoryview declaration does not have a ':'.")
  2374. s.expect(']')
  2375. indexes = make_slice_nodes(pos, subscripts)
  2376. result = Nodes.MemoryViewSliceTypeNode(pos,
  2377. base_type_node = base_type_node,
  2378. axes = indexes)
  2379. return result
  2380. def looking_at_name(s):
  2381. return s.sy == 'IDENT' and not s.systring in calling_convention_words
  2382. def looking_at_expr(s):
  2383. if s.systring in base_type_start_words:
  2384. return False
  2385. elif s.sy == 'IDENT':
  2386. is_type = False
  2387. name = s.systring
  2388. dotted_path = []
  2389. s.next()
  2390. while s.sy == '.':
  2391. s.next()
  2392. dotted_path.append(s.systring)
  2393. s.expect('IDENT')
  2394. saved = s.sy, s.systring
  2395. if s.sy == 'IDENT':
  2396. is_type = True
  2397. elif s.sy == '*' or s.sy == '**':
  2398. s.next()
  2399. is_type = s.sy in (')', ']')
  2400. s.put_back(*saved)
  2401. elif s.sy == '(':
  2402. s.next()
  2403. is_type = s.sy == '*'
  2404. s.put_back(*saved)
  2405. elif s.sy == '[':
  2406. s.next()
  2407. is_type = s.sy == ']' or not looking_at_expr(s) # could be a nested template type
  2408. s.put_back(*saved)
  2409. dotted_path.reverse()
  2410. for p in dotted_path:
  2411. s.put_back('IDENT', p)
  2412. s.put_back('.', '.')
  2413. s.put_back('IDENT', name)
  2414. return not is_type and saved[0]
  2415. else:
  2416. return True
  2417. def looking_at_base_type(s):
  2418. #print "looking_at_base_type?", s.sy, s.systring, s.position()
  2419. return s.sy == 'IDENT' and s.systring in base_type_start_words
  2420. def looking_at_dotted_name(s):
  2421. if s.sy == 'IDENT':
  2422. name = s.systring
  2423. s.next()
  2424. result = s.sy == '.'
  2425. s.put_back('IDENT', name)
  2426. return result
  2427. else:
  2428. return 0
  2429. def looking_at_call(s):
  2430. "See if we're looking at a.b.c("
  2431. # Don't mess up the original position, so save and restore it.
  2432. # Unfortunately there's no good way to handle this, as a subsequent call
  2433. # to next() will not advance the position until it reads a new token.
  2434. position = s.start_line, s.start_col
  2435. result = looking_at_expr(s) == u'('
  2436. if not result:
  2437. s.start_line, s.start_col = position
  2438. return result
  2439. basic_c_type_names = cython.declare(
  2440. set, set(["void", "char", "int", "float", "double", "bint"]))
  2441. special_basic_c_types = cython.declare(dict, {
  2442. # name : (signed, longness)
  2443. "Py_UNICODE" : (0, 0),
  2444. "Py_UCS4" : (0, 0),
  2445. "Py_hash_t" : (2, 0),
  2446. "Py_ssize_t" : (2, 0),
  2447. "ssize_t" : (2, 0),
  2448. "size_t" : (0, 0),
  2449. "ptrdiff_t" : (2, 0),
  2450. "Py_tss_t" : (1, 0),
  2451. })
  2452. sign_and_longness_words = cython.declare(
  2453. set, set(["short", "long", "signed", "unsigned"]))
  2454. base_type_start_words = cython.declare(
  2455. set,
  2456. basic_c_type_names
  2457. | sign_and_longness_words
  2458. | set(special_basic_c_types))
  2459. struct_enum_union = cython.declare(
  2460. set, set(["struct", "union", "enum", "packed"]))
  2461. def p_sign_and_longness(s):
  2462. signed = 1
  2463. longness = 0
  2464. while s.sy == 'IDENT' and s.systring in sign_and_longness_words:
  2465. if s.systring == 'unsigned':
  2466. signed = 0
  2467. elif s.systring == 'signed':
  2468. signed = 2
  2469. elif s.systring == 'short':
  2470. longness = -1
  2471. elif s.systring == 'long':
  2472. longness += 1
  2473. s.next()
  2474. return signed, longness
  2475. def p_opt_cname(s):
  2476. literal = p_opt_string_literal(s, 'u')
  2477. if literal is not None:
  2478. cname = EncodedString(literal)
  2479. cname.encoding = s.source_encoding
  2480. else:
  2481. cname = None
  2482. return cname
  2483. def p_c_declarator(s, ctx = Ctx(), empty = 0, is_type = 0, cmethod_flag = 0,
  2484. assignable = 0, nonempty = 0,
  2485. calling_convention_allowed = 0):
  2486. # If empty is true, the declarator must be empty. If nonempty is true,
  2487. # the declarator must be nonempty. Otherwise we don't care.
  2488. # If cmethod_flag is true, then if this declarator declares
  2489. # a function, it's a C method of an extension type.
  2490. pos = s.position()
  2491. if s.sy == '(':
  2492. s.next()
  2493. if s.sy == ')' or looking_at_name(s):
  2494. base = Nodes.CNameDeclaratorNode(pos, name=s.context.intern_ustring(u""), cname=None)
  2495. result = p_c_func_declarator(s, pos, ctx, base, cmethod_flag)
  2496. else:
  2497. result = p_c_declarator(s, ctx, empty = empty, is_type = is_type,
  2498. cmethod_flag = cmethod_flag,
  2499. nonempty = nonempty,
  2500. calling_convention_allowed = 1)
  2501. s.expect(')')
  2502. else:
  2503. result = p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
  2504. assignable, nonempty)
  2505. if not calling_convention_allowed and result.calling_convention and s.sy != '(':
  2506. error(s.position(), "%s on something that is not a function"
  2507. % result.calling_convention)
  2508. while s.sy in ('[', '('):
  2509. pos = s.position()
  2510. if s.sy == '[':
  2511. result = p_c_array_declarator(s, result)
  2512. else: # sy == '('
  2513. s.next()
  2514. result = p_c_func_declarator(s, pos, ctx, result, cmethod_flag)
  2515. cmethod_flag = 0
  2516. return result
  2517. def p_c_array_declarator(s, base):
  2518. pos = s.position()
  2519. s.next() # '['
  2520. if s.sy != ']':
  2521. dim = p_testlist(s)
  2522. else:
  2523. dim = None
  2524. s.expect(']')
  2525. return Nodes.CArrayDeclaratorNode(pos, base = base, dimension = dim)
  2526. def p_c_func_declarator(s, pos, ctx, base, cmethod_flag):
  2527. # Opening paren has already been skipped
  2528. args = p_c_arg_list(s, ctx, cmethod_flag = cmethod_flag,
  2529. nonempty_declarators = 0)
  2530. ellipsis = p_optional_ellipsis(s)
  2531. s.expect(')')
  2532. nogil = p_nogil(s)
  2533. exc_val, exc_check = p_exception_value_clause(s)
  2534. # TODO - warning to enforce preferred exception specification order
  2535. nogil = nogil or p_nogil(s)
  2536. with_gil = p_with_gil(s)
  2537. return Nodes.CFuncDeclaratorNode(pos,
  2538. base = base, args = args, has_varargs = ellipsis,
  2539. exception_value = exc_val, exception_check = exc_check,
  2540. nogil = nogil or ctx.nogil or with_gil, with_gil = with_gil)
  2541. supported_overloaded_operators = cython.declare(set, set([
  2542. '+', '-', '*', '/', '%',
  2543. '++', '--', '~', '|', '&', '^', '<<', '>>', ',',
  2544. '==', '!=', '>=', '>', '<=', '<',
  2545. '[]', '()', '!', '=',
  2546. 'bool',
  2547. ]))
  2548. def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
  2549. assignable, nonempty):
  2550. pos = s.position()
  2551. calling_convention = p_calling_convention(s)
  2552. if s.sy == '*':
  2553. s.next()
  2554. if s.systring == 'const':
  2555. const_pos = s.position()
  2556. s.next()
  2557. const_base = p_c_declarator(s, ctx, empty = empty,
  2558. is_type = is_type,
  2559. cmethod_flag = cmethod_flag,
  2560. assignable = assignable,
  2561. nonempty = nonempty)
  2562. base = Nodes.CConstDeclaratorNode(const_pos, base = const_base)
  2563. else:
  2564. base = p_c_declarator(s, ctx, empty = empty, is_type = is_type,
  2565. cmethod_flag = cmethod_flag,
  2566. assignable = assignable, nonempty = nonempty)
  2567. result = Nodes.CPtrDeclaratorNode(pos,
  2568. base = base)
  2569. elif s.sy == '**': # scanner returns this as a single token
  2570. s.next()
  2571. base = p_c_declarator(s, ctx, empty = empty, is_type = is_type,
  2572. cmethod_flag = cmethod_flag,
  2573. assignable = assignable, nonempty = nonempty)
  2574. result = Nodes.CPtrDeclaratorNode(pos,
  2575. base = Nodes.CPtrDeclaratorNode(pos,
  2576. base = base))
  2577. elif s.sy == '&':
  2578. s.next()
  2579. base = p_c_declarator(s, ctx, empty = empty, is_type = is_type,
  2580. cmethod_flag = cmethod_flag,
  2581. assignable = assignable, nonempty = nonempty)
  2582. result = Nodes.CReferenceDeclaratorNode(pos, base = base)
  2583. else:
  2584. rhs = None
  2585. if s.sy == 'IDENT':
  2586. name = s.systring
  2587. if empty:
  2588. error(s.position(), "Declarator should be empty")
  2589. s.next()
  2590. cname = p_opt_cname(s)
  2591. if name != 'operator' and s.sy == '=' and assignable:
  2592. s.next()
  2593. rhs = p_test(s)
  2594. else:
  2595. if nonempty:
  2596. error(s.position(), "Empty declarator")
  2597. name = ""
  2598. cname = None
  2599. if cname is None and ctx.namespace is not None and nonempty:
  2600. cname = ctx.namespace + "::" + name
  2601. if name == 'operator' and ctx.visibility == 'extern' and nonempty:
  2602. op = s.sy
  2603. if [1 for c in op if c in '+-*/<=>!%&|([^~,']:
  2604. s.next()
  2605. # Handle diphthong operators.
  2606. if op == '(':
  2607. s.expect(')')
  2608. op = '()'
  2609. elif op == '[':
  2610. s.expect(']')
  2611. op = '[]'
  2612. elif op in ('-', '+', '|', '&') and s.sy == op:
  2613. op *= 2 # ++, --, ...
  2614. s.next()
  2615. elif s.sy == '=':
  2616. op += s.sy # +=, -=, ...
  2617. s.next()
  2618. if op not in supported_overloaded_operators:
  2619. s.error("Overloading operator '%s' not yet supported." % op,
  2620. fatal=False)
  2621. name += op
  2622. elif op == 'IDENT':
  2623. op = s.systring;
  2624. if op not in supported_overloaded_operators:
  2625. s.error("Overloading operator '%s' not yet supported." % op,
  2626. fatal=False)
  2627. name = name + ' ' + op
  2628. s.next()
  2629. result = Nodes.CNameDeclaratorNode(pos,
  2630. name = name, cname = cname, default = rhs)
  2631. result.calling_convention = calling_convention
  2632. return result
  2633. def p_nogil(s):
  2634. if s.sy == 'IDENT' and s.systring == 'nogil':
  2635. s.next()
  2636. return 1
  2637. else:
  2638. return 0
  2639. def p_with_gil(s):
  2640. if s.sy == 'with':
  2641. s.next()
  2642. s.expect_keyword('gil')
  2643. return 1
  2644. else:
  2645. return 0
  2646. def p_exception_value_clause(s):
  2647. exc_val = None
  2648. exc_check = 0
  2649. if s.sy == 'IDENT' and s.systring == 'noexcept':
  2650. s.next()
  2651. exc_check = False # No-op in Cython 0.29.x
  2652. elif s.sy == 'except':
  2653. s.next()
  2654. if s.sy == '*':
  2655. exc_check = 1
  2656. s.next()
  2657. elif s.sy == '+':
  2658. exc_check = '+'
  2659. plus_char_pos = s.position()[2]
  2660. s.next()
  2661. if s.sy == 'IDENT':
  2662. name = s.systring
  2663. if name == 'nogil':
  2664. if s.position()[2] == plus_char_pos + 1:
  2665. error(s.position(),
  2666. "'except +nogil' defines an exception handling function. Use 'except + nogil' for the 'nogil' modifier.")
  2667. # 'except + nogil' is parsed outside
  2668. else:
  2669. exc_val = p_name(s, name)
  2670. s.next()
  2671. elif s.sy == '*':
  2672. exc_val = ExprNodes.CharNode(s.position(), value=u'*')
  2673. s.next()
  2674. else:
  2675. if s.sy == '?':
  2676. exc_check = 1
  2677. s.next()
  2678. exc_val = p_test(s)
  2679. return exc_val, exc_check
  2680. c_arg_list_terminators = cython.declare(set, set(['*', '**', '.', ')', ':']))
  2681. def p_c_arg_list(s, ctx = Ctx(), in_pyfunc = 0, cmethod_flag = 0,
  2682. nonempty_declarators = 0, kw_only = 0, annotated = 1):
  2683. # Comma-separated list of C argument declarations, possibly empty.
  2684. # May have a trailing comma.
  2685. args = []
  2686. is_self_arg = cmethod_flag
  2687. while s.sy not in c_arg_list_terminators:
  2688. args.append(p_c_arg_decl(s, ctx, in_pyfunc, is_self_arg,
  2689. nonempty = nonempty_declarators, kw_only = kw_only,
  2690. annotated = annotated))
  2691. if s.sy != ',':
  2692. break
  2693. s.next()
  2694. is_self_arg = 0
  2695. return args
  2696. def p_optional_ellipsis(s):
  2697. if s.sy == '.':
  2698. expect_ellipsis(s)
  2699. return 1
  2700. else:
  2701. return 0
  2702. def p_c_arg_decl(s, ctx, in_pyfunc, cmethod_flag = 0, nonempty = 0,
  2703. kw_only = 0, annotated = 1):
  2704. pos = s.position()
  2705. not_none = or_none = 0
  2706. default = None
  2707. annotation = None
  2708. if s.in_python_file:
  2709. # empty type declaration
  2710. base_type = Nodes.CSimpleBaseTypeNode(pos,
  2711. name = None, module_path = [],
  2712. is_basic_c_type = 0, signed = 0,
  2713. complex = 0, longness = 0,
  2714. is_self_arg = cmethod_flag, templates = None)
  2715. else:
  2716. base_type = p_c_base_type(s, cmethod_flag, nonempty = nonempty)
  2717. declarator = p_c_declarator(s, ctx, nonempty = nonempty)
  2718. if s.sy in ('not', 'or') and not s.in_python_file:
  2719. kind = s.sy
  2720. s.next()
  2721. if s.sy == 'IDENT' and s.systring == 'None':
  2722. s.next()
  2723. else:
  2724. s.error("Expected 'None'")
  2725. if not in_pyfunc:
  2726. error(pos, "'%s None' only allowed in Python functions" % kind)
  2727. or_none = kind == 'or'
  2728. not_none = kind == 'not'
  2729. if annotated and s.sy == ':':
  2730. s.next()
  2731. annotation = p_test(s)
  2732. if s.sy == '=':
  2733. s.next()
  2734. if 'pxd' in ctx.level:
  2735. if s.sy in ['*', '?']:
  2736. # TODO(github/1736): Make this an error for inline declarations.
  2737. default = ExprNodes.NoneNode(pos)
  2738. s.next()
  2739. elif 'inline' in ctx.modifiers:
  2740. default = p_test(s)
  2741. else:
  2742. error(pos, "default values cannot be specified in pxd files, use ? or *")
  2743. else:
  2744. default = p_test(s)
  2745. return Nodes.CArgDeclNode(pos,
  2746. base_type = base_type,
  2747. declarator = declarator,
  2748. not_none = not_none,
  2749. or_none = or_none,
  2750. default = default,
  2751. annotation = annotation,
  2752. kw_only = kw_only)
  2753. def p_api(s):
  2754. if s.sy == 'IDENT' and s.systring == 'api':
  2755. s.next()
  2756. return 1
  2757. else:
  2758. return 0
  2759. def p_cdef_statement(s, ctx):
  2760. pos = s.position()
  2761. ctx.visibility = p_visibility(s, ctx.visibility)
  2762. ctx.api = ctx.api or p_api(s)
  2763. if ctx.api:
  2764. if ctx.visibility not in ('private', 'public'):
  2765. error(pos, "Cannot combine 'api' with '%s'" % ctx.visibility)
  2766. if (ctx.visibility == 'extern') and s.sy == 'from':
  2767. return p_cdef_extern_block(s, pos, ctx)
  2768. elif s.sy == 'import':
  2769. s.next()
  2770. return p_cdef_extern_block(s, pos, ctx)
  2771. elif p_nogil(s):
  2772. ctx.nogil = 1
  2773. if ctx.overridable:
  2774. error(pos, "cdef blocks cannot be declared cpdef")
  2775. return p_cdef_block(s, ctx)
  2776. elif s.sy == ':':
  2777. if ctx.overridable:
  2778. error(pos, "cdef blocks cannot be declared cpdef")
  2779. return p_cdef_block(s, ctx)
  2780. elif s.sy == 'class':
  2781. if ctx.level not in ('module', 'module_pxd'):
  2782. error(pos, "Extension type definition not allowed here")
  2783. if ctx.overridable:
  2784. error(pos, "Extension types cannot be declared cpdef")
  2785. return p_c_class_definition(s, pos, ctx)
  2786. elif s.sy == 'IDENT' and s.systring == 'cppclass':
  2787. return p_cpp_class_definition(s, pos, ctx)
  2788. elif s.sy == 'IDENT' and s.systring in struct_enum_union:
  2789. if ctx.level not in ('module', 'module_pxd'):
  2790. error(pos, "C struct/union/enum definition not allowed here")
  2791. if ctx.overridable:
  2792. if s.systring != 'enum':
  2793. error(pos, "C struct/union cannot be declared cpdef")
  2794. return p_struct_enum(s, pos, ctx)
  2795. elif s.sy == 'IDENT' and s.systring == 'fused':
  2796. return p_fused_definition(s, pos, ctx)
  2797. else:
  2798. return p_c_func_or_var_declaration(s, pos, ctx)
  2799. def p_cdef_block(s, ctx):
  2800. return p_suite(s, ctx(cdef_flag = 1))
  2801. def p_cdef_extern_block(s, pos, ctx):
  2802. if ctx.overridable:
  2803. error(pos, "cdef extern blocks cannot be declared cpdef")
  2804. include_file = None
  2805. s.expect('from')
  2806. if s.sy == '*':
  2807. s.next()
  2808. else:
  2809. include_file = p_string_literal(s, 'u')[2]
  2810. ctx = ctx(cdef_flag = 1, visibility = 'extern')
  2811. if s.systring == "namespace":
  2812. s.next()
  2813. ctx.namespace = p_string_literal(s, 'u')[2]
  2814. if p_nogil(s):
  2815. ctx.nogil = 1
  2816. # Use "docstring" as verbatim string to include
  2817. verbatim_include, body = p_suite_with_docstring(s, ctx, True)
  2818. return Nodes.CDefExternNode(pos,
  2819. include_file = include_file,
  2820. verbatim_include = verbatim_include,
  2821. body = body,
  2822. namespace = ctx.namespace)
  2823. def p_c_enum_definition(s, pos, ctx):
  2824. # s.sy == ident 'enum'
  2825. s.next()
  2826. if s.sy == 'IDENT':
  2827. name = s.systring
  2828. s.next()
  2829. cname = p_opt_cname(s)
  2830. if cname is None and ctx.namespace is not None:
  2831. cname = ctx.namespace + "::" + name
  2832. else:
  2833. name = None
  2834. cname = None
  2835. items = None
  2836. s.expect(':')
  2837. items = []
  2838. if s.sy != 'NEWLINE':
  2839. p_c_enum_line(s, ctx, items)
  2840. else:
  2841. s.next() # 'NEWLINE'
  2842. s.expect_indent()
  2843. while s.sy not in ('DEDENT', 'EOF'):
  2844. p_c_enum_line(s, ctx, items)
  2845. s.expect_dedent()
  2846. return Nodes.CEnumDefNode(
  2847. pos, name = name, cname = cname, items = items,
  2848. typedef_flag = ctx.typedef_flag, visibility = ctx.visibility,
  2849. create_wrapper = ctx.overridable,
  2850. api = ctx.api, in_pxd = ctx.level == 'module_pxd')
  2851. def p_c_enum_line(s, ctx, items):
  2852. if s.sy != 'pass':
  2853. p_c_enum_item(s, ctx, items)
  2854. while s.sy == ',':
  2855. s.next()
  2856. if s.sy in ('NEWLINE', 'EOF'):
  2857. break
  2858. p_c_enum_item(s, ctx, items)
  2859. else:
  2860. s.next()
  2861. s.expect_newline("Syntax error in enum item list")
  2862. def p_c_enum_item(s, ctx, items):
  2863. pos = s.position()
  2864. name = p_ident(s)
  2865. cname = p_opt_cname(s)
  2866. if cname is None and ctx.namespace is not None:
  2867. cname = ctx.namespace + "::" + name
  2868. value = None
  2869. if s.sy == '=':
  2870. s.next()
  2871. value = p_test(s)
  2872. items.append(Nodes.CEnumDefItemNode(pos,
  2873. name = name, cname = cname, value = value))
  2874. def p_c_struct_or_union_definition(s, pos, ctx):
  2875. packed = False
  2876. if s.systring == 'packed':
  2877. packed = True
  2878. s.next()
  2879. if s.sy != 'IDENT' or s.systring != 'struct':
  2880. s.expected('struct')
  2881. # s.sy == ident 'struct' or 'union'
  2882. kind = s.systring
  2883. s.next()
  2884. name = p_ident(s)
  2885. cname = p_opt_cname(s)
  2886. if cname is None and ctx.namespace is not None:
  2887. cname = ctx.namespace + "::" + name
  2888. attributes = None
  2889. if s.sy == ':':
  2890. s.next()
  2891. s.expect('NEWLINE')
  2892. s.expect_indent()
  2893. attributes = []
  2894. body_ctx = Ctx()
  2895. while s.sy != 'DEDENT':
  2896. if s.sy != 'pass':
  2897. attributes.append(
  2898. p_c_func_or_var_declaration(s, s.position(), body_ctx))
  2899. else:
  2900. s.next()
  2901. s.expect_newline("Expected a newline")
  2902. s.expect_dedent()
  2903. else:
  2904. s.expect_newline("Syntax error in struct or union definition")
  2905. return Nodes.CStructOrUnionDefNode(pos,
  2906. name = name, cname = cname, kind = kind, attributes = attributes,
  2907. typedef_flag = ctx.typedef_flag, visibility = ctx.visibility,
  2908. api = ctx.api, in_pxd = ctx.level == 'module_pxd', packed = packed)
  2909. def p_fused_definition(s, pos, ctx):
  2910. """
  2911. c(type)def fused my_fused_type:
  2912. ...
  2913. """
  2914. # s.systring == 'fused'
  2915. if ctx.level not in ('module', 'module_pxd'):
  2916. error(pos, "Fused type definition not allowed here")
  2917. s.next()
  2918. name = p_ident(s)
  2919. s.expect(":")
  2920. s.expect_newline()
  2921. s.expect_indent()
  2922. types = []
  2923. while s.sy != 'DEDENT':
  2924. if s.sy != 'pass':
  2925. #types.append(p_c_declarator(s))
  2926. types.append(p_c_base_type(s)) #, nonempty=1))
  2927. else:
  2928. s.next()
  2929. s.expect_newline()
  2930. s.expect_dedent()
  2931. if not types:
  2932. error(pos, "Need at least one type")
  2933. return Nodes.FusedTypeNode(pos, name=name, types=types)
  2934. def p_struct_enum(s, pos, ctx):
  2935. if s.systring == 'enum':
  2936. return p_c_enum_definition(s, pos, ctx)
  2937. else:
  2938. return p_c_struct_or_union_definition(s, pos, ctx)
  2939. def p_visibility(s, prev_visibility):
  2940. pos = s.position()
  2941. visibility = prev_visibility
  2942. if s.sy == 'IDENT' and s.systring in ('extern', 'public', 'readonly'):
  2943. visibility = s.systring
  2944. if prev_visibility != 'private' and visibility != prev_visibility:
  2945. s.error("Conflicting visibility options '%s' and '%s'"
  2946. % (prev_visibility, visibility), fatal=False)
  2947. s.next()
  2948. return visibility
  2949. def p_c_modifiers(s):
  2950. if s.sy == 'IDENT' and s.systring in ('inline',):
  2951. modifier = s.systring
  2952. s.next()
  2953. return [modifier] + p_c_modifiers(s)
  2954. return []
  2955. def p_c_func_or_var_declaration(s, pos, ctx):
  2956. cmethod_flag = ctx.level in ('c_class', 'c_class_pxd')
  2957. modifiers = p_c_modifiers(s)
  2958. base_type = p_c_base_type(s, nonempty = 1, templates = ctx.templates)
  2959. declarator = p_c_declarator(s, ctx(modifiers=modifiers), cmethod_flag = cmethod_flag,
  2960. assignable = 1, nonempty = 1)
  2961. declarator.overridable = ctx.overridable
  2962. if s.sy == 'IDENT' and s.systring == 'const' and ctx.level == 'cpp_class':
  2963. s.next()
  2964. is_const_method = 1
  2965. else:
  2966. is_const_method = 0
  2967. if s.sy == '->':
  2968. # Special enough to give a better error message and keep going.
  2969. s.error(
  2970. "Return type annotation is not allowed in cdef/cpdef signatures. "
  2971. "Please define it before the function name, as in C signatures.",
  2972. fatal=False)
  2973. s.next()
  2974. p_test(s) # Keep going, but ignore result.
  2975. if s.sy == ':':
  2976. if ctx.level not in ('module', 'c_class', 'module_pxd', 'c_class_pxd', 'cpp_class') and not ctx.templates:
  2977. s.error("C function definition not allowed here")
  2978. doc, suite = p_suite_with_docstring(s, Ctx(level='function'))
  2979. result = Nodes.CFuncDefNode(pos,
  2980. visibility = ctx.visibility,
  2981. base_type = base_type,
  2982. declarator = declarator,
  2983. body = suite,
  2984. doc = doc,
  2985. modifiers = modifiers,
  2986. api = ctx.api,
  2987. overridable = ctx.overridable,
  2988. is_const_method = is_const_method)
  2989. else:
  2990. #if api:
  2991. # s.error("'api' not allowed with variable declaration")
  2992. if is_const_method:
  2993. declarator.is_const_method = is_const_method
  2994. declarators = [declarator]
  2995. while s.sy == ',':
  2996. s.next()
  2997. if s.sy == 'NEWLINE':
  2998. break
  2999. declarator = p_c_declarator(s, ctx, cmethod_flag = cmethod_flag,
  3000. assignable = 1, nonempty = 1)
  3001. declarators.append(declarator)
  3002. doc_line = s.start_line + 1
  3003. s.expect_newline("Syntax error in C variable declaration", ignore_semicolon=True)
  3004. if ctx.level in ('c_class', 'c_class_pxd') and s.start_line == doc_line:
  3005. doc = p_doc_string(s)
  3006. else:
  3007. doc = None
  3008. result = Nodes.CVarDefNode(pos,
  3009. visibility = ctx.visibility,
  3010. base_type = base_type,
  3011. declarators = declarators,
  3012. in_pxd = ctx.level in ('module_pxd', 'c_class_pxd'),
  3013. doc = doc,
  3014. api = ctx.api,
  3015. modifiers = modifiers,
  3016. overridable = ctx.overridable)
  3017. return result
  3018. def p_ctypedef_statement(s, ctx):
  3019. # s.sy == 'ctypedef'
  3020. pos = s.position()
  3021. s.next()
  3022. visibility = p_visibility(s, ctx.visibility)
  3023. api = p_api(s)
  3024. ctx = ctx(typedef_flag = 1, visibility = visibility)
  3025. if api:
  3026. ctx.api = 1
  3027. if s.sy == 'class':
  3028. return p_c_class_definition(s, pos, ctx)
  3029. elif s.sy == 'IDENT' and s.systring in struct_enum_union:
  3030. return p_struct_enum(s, pos, ctx)
  3031. elif s.sy == 'IDENT' and s.systring == 'fused':
  3032. return p_fused_definition(s, pos, ctx)
  3033. else:
  3034. base_type = p_c_base_type(s, nonempty = 1)
  3035. declarator = p_c_declarator(s, ctx, is_type = 1, nonempty = 1)
  3036. s.expect_newline("Syntax error in ctypedef statement", ignore_semicolon=True)
  3037. return Nodes.CTypeDefNode(
  3038. pos, base_type = base_type,
  3039. declarator = declarator,
  3040. visibility = visibility, api = api,
  3041. in_pxd = ctx.level == 'module_pxd')
  3042. def p_decorators(s):
  3043. decorators = []
  3044. while s.sy == '@':
  3045. pos = s.position()
  3046. s.next()
  3047. decstring = p_dotted_name(s, as_allowed=0)[2]
  3048. names = decstring.split('.')
  3049. decorator = ExprNodes.NameNode(pos, name=s.context.intern_ustring(names[0]))
  3050. for name in names[1:]:
  3051. decorator = ExprNodes.AttributeNode(
  3052. pos, attribute=s.context.intern_ustring(name), obj=decorator)
  3053. if s.sy == '(':
  3054. decorator = p_call(s, decorator)
  3055. decorators.append(Nodes.DecoratorNode(pos, decorator=decorator))
  3056. s.expect_newline("Expected a newline after decorator")
  3057. return decorators
  3058. def _reject_cdef_modifier_in_py(s, name):
  3059. """Step over incorrectly placed cdef modifiers (@see _CDEF_MODIFIERS) to provide a good error message for them.
  3060. """
  3061. if s.sy == 'IDENT' and name in _CDEF_MODIFIERS:
  3062. # Special enough to provide a good error message.
  3063. s.error("Cannot use cdef modifier '%s' in Python function signature. Use a decorator instead." % name, fatal=False)
  3064. return p_ident(s) # Keep going, in case there are other errors.
  3065. return name
  3066. def p_def_statement(s, decorators=None, is_async_def=False):
  3067. # s.sy == 'def'
  3068. pos = s.position()
  3069. # PEP 492 switches the async/await keywords on in "async def" functions
  3070. if is_async_def:
  3071. s.enter_async()
  3072. s.next()
  3073. name = _reject_cdef_modifier_in_py(s, p_ident(s))
  3074. s.expect(
  3075. '(',
  3076. "Expected '(', found '%s'. Did you use cdef syntax in a Python declaration? "
  3077. "Use decorators and Python type annotations instead." % (
  3078. s.systring if s.sy == 'IDENT' else s.sy))
  3079. args, star_arg, starstar_arg = p_varargslist(s, terminator=')')
  3080. s.expect(')')
  3081. _reject_cdef_modifier_in_py(s, s.systring)
  3082. return_type_annotation = None
  3083. if s.sy == '->':
  3084. s.next()
  3085. return_type_annotation = p_test(s)
  3086. _reject_cdef_modifier_in_py(s, s.systring)
  3087. doc, body = p_suite_with_docstring(s, Ctx(level='function'))
  3088. if is_async_def:
  3089. s.exit_async()
  3090. return Nodes.DefNode(
  3091. pos, name=name, args=args, star_arg=star_arg, starstar_arg=starstar_arg,
  3092. doc=doc, body=body, decorators=decorators, is_async_def=is_async_def,
  3093. return_type_annotation=return_type_annotation)
  3094. def p_varargslist(s, terminator=')', annotated=1):
  3095. args = p_c_arg_list(s, in_pyfunc = 1, nonempty_declarators = 1,
  3096. annotated = annotated)
  3097. star_arg = None
  3098. starstar_arg = None
  3099. if s.sy == '*':
  3100. s.next()
  3101. if s.sy == 'IDENT':
  3102. star_arg = p_py_arg_decl(s, annotated=annotated)
  3103. if s.sy == ',':
  3104. s.next()
  3105. args.extend(p_c_arg_list(s, in_pyfunc = 1,
  3106. nonempty_declarators = 1, kw_only = 1, annotated = annotated))
  3107. elif s.sy != terminator:
  3108. s.error("Syntax error in Python function argument list")
  3109. if s.sy == '**':
  3110. s.next()
  3111. starstar_arg = p_py_arg_decl(s, annotated=annotated)
  3112. if s.sy == ',':
  3113. s.next()
  3114. return (args, star_arg, starstar_arg)
  3115. def p_py_arg_decl(s, annotated = 1):
  3116. pos = s.position()
  3117. name = p_ident(s)
  3118. annotation = None
  3119. if annotated and s.sy == ':':
  3120. s.next()
  3121. annotation = p_test(s)
  3122. return Nodes.PyArgDeclNode(pos, name = name, annotation = annotation)
  3123. def p_class_statement(s, decorators):
  3124. # s.sy == 'class'
  3125. pos = s.position()
  3126. s.next()
  3127. class_name = EncodedString(p_ident(s))
  3128. class_name.encoding = s.source_encoding # FIXME: why is this needed?
  3129. arg_tuple = None
  3130. keyword_dict = None
  3131. if s.sy == '(':
  3132. positional_args, keyword_args = p_call_parse_args(s, allow_genexp=False)
  3133. arg_tuple, keyword_dict = p_call_build_packed_args(pos, positional_args, keyword_args)
  3134. if arg_tuple is None:
  3135. # XXX: empty arg_tuple
  3136. arg_tuple = ExprNodes.TupleNode(pos, args=[])
  3137. doc, body = p_suite_with_docstring(s, Ctx(level='class'))
  3138. return Nodes.PyClassDefNode(
  3139. pos, name=class_name,
  3140. bases=arg_tuple,
  3141. keyword_args=keyword_dict,
  3142. doc=doc, body=body, decorators=decorators,
  3143. force_py3_semantics=s.context.language_level >= 3)
  3144. def p_c_class_definition(s, pos, ctx):
  3145. # s.sy == 'class'
  3146. s.next()
  3147. module_path = []
  3148. class_name = p_ident(s)
  3149. while s.sy == '.':
  3150. s.next()
  3151. module_path.append(class_name)
  3152. class_name = p_ident(s)
  3153. if module_path and ctx.visibility != 'extern':
  3154. error(pos, "Qualified class name only allowed for 'extern' C class")
  3155. if module_path and s.sy == 'IDENT' and s.systring == 'as':
  3156. s.next()
  3157. as_name = p_ident(s)
  3158. else:
  3159. as_name = class_name
  3160. objstruct_name = None
  3161. typeobj_name = None
  3162. bases = None
  3163. check_size = None
  3164. if s.sy == '(':
  3165. positional_args, keyword_args = p_call_parse_args(s, allow_genexp=False)
  3166. if keyword_args:
  3167. s.error("C classes cannot take keyword bases.")
  3168. bases, _ = p_call_build_packed_args(pos, positional_args, keyword_args)
  3169. if bases is None:
  3170. bases = ExprNodes.TupleNode(pos, args=[])
  3171. if s.sy == '[':
  3172. if ctx.visibility not in ('public', 'extern') and not ctx.api:
  3173. error(s.position(), "Name options only allowed for 'public', 'api', or 'extern' C class")
  3174. objstruct_name, typeobj_name, check_size = p_c_class_options(s)
  3175. if s.sy == ':':
  3176. if ctx.level == 'module_pxd':
  3177. body_level = 'c_class_pxd'
  3178. else:
  3179. body_level = 'c_class'
  3180. doc, body = p_suite_with_docstring(s, Ctx(level=body_level))
  3181. else:
  3182. s.expect_newline("Syntax error in C class definition")
  3183. doc = None
  3184. body = None
  3185. if ctx.visibility == 'extern':
  3186. if not module_path:
  3187. error(pos, "Module name required for 'extern' C class")
  3188. if typeobj_name:
  3189. error(pos, "Type object name specification not allowed for 'extern' C class")
  3190. elif ctx.visibility == 'public':
  3191. if not objstruct_name:
  3192. error(pos, "Object struct name specification required for 'public' C class")
  3193. if not typeobj_name:
  3194. error(pos, "Type object name specification required for 'public' C class")
  3195. elif ctx.visibility == 'private':
  3196. if ctx.api:
  3197. if not objstruct_name:
  3198. error(pos, "Object struct name specification required for 'api' C class")
  3199. if not typeobj_name:
  3200. error(pos, "Type object name specification required for 'api' C class")
  3201. else:
  3202. error(pos, "Invalid class visibility '%s'" % ctx.visibility)
  3203. return Nodes.CClassDefNode(pos,
  3204. visibility = ctx.visibility,
  3205. typedef_flag = ctx.typedef_flag,
  3206. api = ctx.api,
  3207. module_name = ".".join(module_path),
  3208. class_name = class_name,
  3209. as_name = as_name,
  3210. bases = bases,
  3211. objstruct_name = objstruct_name,
  3212. typeobj_name = typeobj_name,
  3213. check_size = check_size,
  3214. in_pxd = ctx.level == 'module_pxd',
  3215. doc = doc,
  3216. body = body)
  3217. def p_c_class_options(s):
  3218. objstruct_name = None
  3219. typeobj_name = None
  3220. check_size = None
  3221. s.expect('[')
  3222. while 1:
  3223. if s.sy != 'IDENT':
  3224. break
  3225. if s.systring == 'object':
  3226. s.next()
  3227. objstruct_name = p_ident(s)
  3228. elif s.systring == 'type':
  3229. s.next()
  3230. typeobj_name = p_ident(s)
  3231. elif s.systring == 'check_size':
  3232. s.next()
  3233. check_size = p_ident(s)
  3234. if check_size not in ('ignore', 'warn', 'error'):
  3235. s.error("Expected one of ignore, warn or error, found %r" % check_size)
  3236. if s.sy != ',':
  3237. break
  3238. s.next()
  3239. s.expect(']', "Expected 'object', 'type' or 'check_size'")
  3240. return objstruct_name, typeobj_name, check_size
  3241. def p_property_decl(s):
  3242. pos = s.position()
  3243. s.next() # 'property'
  3244. name = p_ident(s)
  3245. doc, body = p_suite_with_docstring(
  3246. s, Ctx(level='property'), with_doc_only=True)
  3247. return Nodes.PropertyNode(pos, name=name, doc=doc, body=body)
  3248. def p_ignorable_statement(s):
  3249. """
  3250. Parses any kind of ignorable statement that is allowed in .pxd files.
  3251. """
  3252. if s.sy == 'BEGIN_STRING':
  3253. pos = s.position()
  3254. string_node = p_atom(s)
  3255. s.expect_newline("Syntax error in string", ignore_semicolon=True)
  3256. return Nodes.ExprStatNode(pos, expr=string_node)
  3257. return None
  3258. def p_doc_string(s):
  3259. if s.sy == 'BEGIN_STRING':
  3260. pos = s.position()
  3261. kind, bytes_result, unicode_result = p_cat_string_literal(s)
  3262. s.expect_newline("Syntax error in doc string", ignore_semicolon=True)
  3263. if kind in ('u', ''):
  3264. return unicode_result
  3265. warning(pos, "Python 3 requires docstrings to be unicode strings")
  3266. return bytes_result
  3267. else:
  3268. return None
  3269. def _extract_docstring(node):
  3270. """
  3271. Extract a docstring from a statement or from the first statement
  3272. in a list. Remove the statement if found. Return a tuple
  3273. (plain-docstring or None, node).
  3274. """
  3275. doc_node = None
  3276. if node is None:
  3277. pass
  3278. elif isinstance(node, Nodes.ExprStatNode):
  3279. if node.expr.is_string_literal:
  3280. doc_node = node.expr
  3281. node = Nodes.StatListNode(node.pos, stats=[])
  3282. elif isinstance(node, Nodes.StatListNode) and node.stats:
  3283. stats = node.stats
  3284. if isinstance(stats[0], Nodes.ExprStatNode):
  3285. if stats[0].expr.is_string_literal:
  3286. doc_node = stats[0].expr
  3287. del stats[0]
  3288. if doc_node is None:
  3289. doc = None
  3290. elif isinstance(doc_node, ExprNodes.BytesNode):
  3291. warning(node.pos,
  3292. "Python 3 requires docstrings to be unicode strings")
  3293. doc = doc_node.value
  3294. elif isinstance(doc_node, ExprNodes.StringNode):
  3295. doc = doc_node.unicode_value
  3296. if doc is None:
  3297. doc = doc_node.value
  3298. else:
  3299. doc = doc_node.value
  3300. return doc, node
  3301. def p_code(s, level=None, ctx=Ctx):
  3302. body = p_statement_list(s, ctx(level = level), first_statement = 1)
  3303. if s.sy != 'EOF':
  3304. s.error("Syntax error in statement [%s,%s]" % (
  3305. repr(s.sy), repr(s.systring)))
  3306. return body
  3307. _match_compiler_directive_comment = cython.declare(object, re.compile(
  3308. r"^#\s*cython\s*:\s*((\w|[.])+\s*=.*)$").match)
  3309. def p_compiler_directive_comments(s):
  3310. result = {}
  3311. while s.sy == 'commentline':
  3312. pos = s.position()
  3313. m = _match_compiler_directive_comment(s.systring)
  3314. if m:
  3315. directives_string = m.group(1).strip()
  3316. try:
  3317. new_directives = Options.parse_directive_list(directives_string, ignore_unknown=True)
  3318. except ValueError as e:
  3319. s.error(e.args[0], fatal=False)
  3320. s.next()
  3321. continue
  3322. for name in new_directives:
  3323. if name not in result:
  3324. pass
  3325. elif new_directives[name] == result[name]:
  3326. warning(pos, "Duplicate directive found: %s" % (name,))
  3327. else:
  3328. s.error("Conflicting settings found for top-level directive %s: %r and %r" % (
  3329. name, result[name], new_directives[name]), pos=pos)
  3330. if 'language_level' in new_directives:
  3331. # Make sure we apply the language level already to the first token that follows the comments.
  3332. s.context.set_language_level(new_directives['language_level'])
  3333. result.update(new_directives)
  3334. s.next()
  3335. return result
  3336. def p_module(s, pxd, full_module_name, ctx=Ctx):
  3337. pos = s.position()
  3338. directive_comments = p_compiler_directive_comments(s)
  3339. s.parse_comments = False
  3340. if s.context.language_level is None:
  3341. s.context.set_language_level(2) # Arcadia default.
  3342. if s.context.language_level is None:
  3343. s.context.set_language_level(2)
  3344. if pos[0].filename:
  3345. import warnings
  3346. warnings.warn(
  3347. "Cython directive 'language_level' not set, using 2 for now (Py2). "
  3348. "This will change in a later release! File: %s" % pos[0].filename,
  3349. FutureWarning,
  3350. stacklevel=1 if cython.compiled else 2,
  3351. )
  3352. doc = p_doc_string(s)
  3353. if pxd:
  3354. level = 'module_pxd'
  3355. else:
  3356. level = 'module'
  3357. body = p_statement_list(s, ctx(level=level), first_statement = 1)
  3358. if s.sy != 'EOF':
  3359. s.error("Syntax error in statement [%s,%s]" % (
  3360. repr(s.sy), repr(s.systring)))
  3361. return ModuleNode(pos, doc = doc, body = body,
  3362. full_module_name = full_module_name,
  3363. directive_comments = directive_comments)
  3364. def p_template_definition(s):
  3365. name = p_ident(s)
  3366. if s.sy == '=':
  3367. s.expect('=')
  3368. s.expect('*')
  3369. required = False
  3370. else:
  3371. required = True
  3372. return name, required
  3373. def p_cpp_class_definition(s, pos, ctx):
  3374. # s.sy == 'cppclass'
  3375. s.next()
  3376. module_path = []
  3377. class_name = p_ident(s)
  3378. cname = p_opt_cname(s)
  3379. if cname is None and ctx.namespace is not None:
  3380. cname = ctx.namespace + "::" + class_name
  3381. if s.sy == '.':
  3382. error(pos, "Qualified class name not allowed C++ class")
  3383. if s.sy == '[':
  3384. s.next()
  3385. templates = [p_template_definition(s)]
  3386. while s.sy == ',':
  3387. s.next()
  3388. templates.append(p_template_definition(s))
  3389. s.expect(']')
  3390. template_names = [name for name, required in templates]
  3391. else:
  3392. templates = None
  3393. template_names = None
  3394. if s.sy == '(':
  3395. s.next()
  3396. base_classes = [p_c_base_type(s, templates = template_names)]
  3397. while s.sy == ',':
  3398. s.next()
  3399. base_classes.append(p_c_base_type(s, templates = template_names))
  3400. s.expect(')')
  3401. else:
  3402. base_classes = []
  3403. if s.sy == '[':
  3404. error(s.position(), "Name options not allowed for C++ class")
  3405. nogil = p_nogil(s)
  3406. if s.sy == ':':
  3407. s.next()
  3408. s.expect('NEWLINE')
  3409. s.expect_indent()
  3410. attributes = []
  3411. body_ctx = Ctx(visibility = ctx.visibility, level='cpp_class', nogil=nogil or ctx.nogil)
  3412. body_ctx.templates = template_names
  3413. while s.sy != 'DEDENT':
  3414. if s.sy != 'pass':
  3415. attributes.append(p_cpp_class_attribute(s, body_ctx))
  3416. else:
  3417. s.next()
  3418. s.expect_newline("Expected a newline")
  3419. s.expect_dedent()
  3420. else:
  3421. attributes = None
  3422. s.expect_newline("Syntax error in C++ class definition")
  3423. return Nodes.CppClassNode(pos,
  3424. name = class_name,
  3425. cname = cname,
  3426. base_classes = base_classes,
  3427. visibility = ctx.visibility,
  3428. in_pxd = ctx.level == 'module_pxd',
  3429. attributes = attributes,
  3430. templates = templates)
  3431. def p_cpp_class_attribute(s, ctx):
  3432. decorators = None
  3433. if s.sy == '@':
  3434. decorators = p_decorators(s)
  3435. if s.systring == 'cppclass':
  3436. return p_cpp_class_definition(s, s.position(), ctx)
  3437. elif s.systring == 'ctypedef':
  3438. return p_ctypedef_statement(s, ctx)
  3439. elif s.sy == 'IDENT' and s.systring in struct_enum_union:
  3440. if s.systring != 'enum':
  3441. return p_cpp_class_definition(s, s.position(), ctx)
  3442. else:
  3443. return p_struct_enum(s, s.position(), ctx)
  3444. else:
  3445. node = p_c_func_or_var_declaration(s, s.position(), ctx)
  3446. if decorators is not None:
  3447. tup = Nodes.CFuncDefNode, Nodes.CVarDefNode, Nodes.CClassDefNode
  3448. if ctx.allow_struct_enum_decorator:
  3449. tup += Nodes.CStructOrUnionDefNode, Nodes.CEnumDefNode
  3450. if not isinstance(node, tup):
  3451. s.error("Decorators can only be followed by functions or classes")
  3452. node.decorators = decorators
  3453. return node
  3454. #----------------------------------------------
  3455. #
  3456. # Debugging
  3457. #
  3458. #----------------------------------------------
  3459. def print_parse_tree(f, node, level, key = None):
  3460. ind = " " * level
  3461. if node:
  3462. f.write(ind)
  3463. if key:
  3464. f.write("%s: " % key)
  3465. t = type(node)
  3466. if t is tuple:
  3467. f.write("(%s @ %s\n" % (node[0], node[1]))
  3468. for i in range(2, len(node)):
  3469. print_parse_tree(f, node[i], level+1)
  3470. f.write("%s)\n" % ind)
  3471. return
  3472. elif isinstance(node, Nodes.Node):
  3473. try:
  3474. tag = node.tag
  3475. except AttributeError:
  3476. tag = node.__class__.__name__
  3477. f.write("%s @ %s\n" % (tag, node.pos))
  3478. for name, value in node.__dict__.items():
  3479. if name != 'tag' and name != 'pos':
  3480. print_parse_tree(f, value, level+1, name)
  3481. return
  3482. elif t is list:
  3483. f.write("[\n")
  3484. for i in range(len(node)):
  3485. print_parse_tree(f, node[i], level+1)
  3486. f.write("%s]\n" % ind)
  3487. return
  3488. f.write("%s%s\n" % (ind, node))