Parsing.py 123 KB

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