123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311(*
Copyright 2012-2025 Codinuum Software Lab <https://codinuum.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)(*
* AST for the Python programming language
*
* ast.ml
*
*)moduleAstloc=Langs_common.AstlocmoduleAst_base=Langs_common.Ast_base(*open Printf*)moduleLoc=structincludeAstlocendtypeloc=Loc.ttypename=loc*stringtypedottedname=namelisttypecomment={c_loc:Loc.t;c_comment:string;}letmake_commentlocc={c_loc=loc;c_comment=c;}typefileinput=Fileinputofloc*statementlistandtestlist={list:exprlist;comma:bool;mutableyield:bool}andstatement={stmt_desc:statement_desc;stmt_loc:loc}andstatement_desc=|Ssimpleofsimplestmtlist|Sasyncofstatement|Sifofexpr*suite*(loc*expr*suite)list*(loc*suite)option|Swhileofexpr*suite*(loc*suite)option|Sforoftargetlist*exprlist*suite*(loc*suite)option|Stryofsuite*(except*suite)list*(loc*suite)option*(loc*suite)option|Stryfinofsuite*(loc*suite)|Swithof(expr*targetoption)list*suite|Sasync_funcdefofdecoratorlist*name*parameters*exproption*suite|Sfuncdefofdecoratorlist*name*parameters*exproption*suite|Sclassdefofdecoratorlist*name*arglist*suite|Smatchofsubject_expr*case_blocklist|Serror|Smarkerofstringandsimplestmt={sstmt_desc:simplestmt_desc;sstmt_loc:loc}andsimplestmt_desc=|SSexprofexprlist|SSassignoftestlistlist*testlist|SSannassignoftargetlist*(loc*expr)*testlistoption|SSaugassignoftargetlist*augop*testlist|SSprintofexprlist|SSprintchevronofexpr*exprlist|SSdeloftargetlist|SSpass|SSbreak|SScontinue|SSreturnofexprlist|SSraise|SSraise1ofexpr|SSraise2ofexpr*expr|SSraise3ofexpr*expr*expr|SSraisefromofexpr*expr|SSyieldofexprlist|SSimportofdottedname_as_namelist|SSfromofdotsoption*dottednameoption*name_as_namelist|SSglobalofnamelist|SSnonlocalofnamelist|SSexecofexpr|SSexec2ofexpr*expr|SSexec3ofexpr*expr*expr|SSassertofexpr|SSassert2ofexpr*expr|SSerroranddottedname_as_name=dottedname*nameoptionandname_as_name=name*nameoptionandexcept=EXofloc|EX1ofloc*expr|EX2ofloc*expr*targetandsuite=loc*statementlistanddots=loc*intandparameters=loc*vararglistandvararg=|VAargoffpdef*exproption|VAargsofloc*nameoption*exproption|VAkwargsofloc*name*exproption|VAsepoflocandfpdef=|Fnameofname|Flistofloc*fpdeflist|Ftypedofloc*name*expranddecorator=loc*dottedname*arglistandexpr={expr_desc:expr_desc;expr_loc:loc}andexpr_desc=|Eprimaryofprimary|Epowerofprimary*expr|Ebopofexpr*bop*expr|Euopofuop*expr|Elambdaofparameters*expr|Econdofexpr*expr*expr|Estarofexpr|Enamedofexpr*expr|Efromofexpr|Eargofexpr*expr|Eerrorandprimary={prim_desc:primary_desc;prim_loc:loc}andprimary_desc=|Pnameofname|Pliteralofliteral|Pexprofexpr|Pparenofexpr|Ptupleofexprlist|Pyieldofexprlist|PcompTofexpr*compfor|PcompLofexpr*compfor|Plistofexprlist|Plistnull|Pdictorsetofdictorsetmaker|Pdictnull|Pstrconvofexprlist|Pattrrefofprimary*name|Psubscriptofprimary*exprlist|Psliceofprimary*sliceitemlist|Pcallofprimary*arglist|Pawaitofprimary|Pellipsisandtrailer=|TRattrrefofname|TRsubscriptofexprlist|TRsliceofsliceitemlist|TRcallofarglistandliteral=|Lintegerofstring|Llongintegerofstring|Lfloatnumberofstring|Limagnumberofstring|Lstringofpystringlist|Lnone|Ltrue|Lfalseandpystring=PSlongofloc*string|PSshortofloc*stringandtarget=exprandlistfor=loc*exprlist*exprlist*listiteroptionandlistif=loc*expr*listiteroptionandlistiter=LIforoflistfor|LIifoflistifanddictorsetmaker=|DSMdictofdictelemlist|DSMdictCofdictelem*compfor|DSMsetofexprlist|DSMsetCofexpr*compforanddictelem={delem_desc:dictelem_desc;delem_loc:loc}anddictelem_desc=|DEkeyValueofexpr*expr|DEstarStarofexprandsliceitem=|SIexprofexpr|SI2ofloc*exproption*exproption|SI3ofloc*exproption*exproption*exproption(*| SIellipsis of loc*)andarglist=loc*argumentlistandargument=|Aargofloc*expr*exproption|Acompofloc*expr*compfor|Aassignofloc*expr*expr|Aargsofloc*expr|Akwargsofloc*exprandcompiter=Cforofcompfor|Cifofcompifandcompif=loc*expr*compiteroptionandcompfor=loc*(exprlist*expr*compiteroption)*boolandaugop=|AaddEq|AsubEq|AmulEq|AdivEq|AmodEq|AandEq|AorEq|AxorEq|AshiftLEq|AshiftREq|ApowEq|AfdivEqandbop=|Bmul|Bdiv|Bfdiv|Bmod|Badd|Bsub|BshiftL|BshiftR|Beq|Bneq|Blt|Bgt|Ble|Bge|BbitAnd|BbitOr|BbitXor|Band|Bor|Bis|BisNot|Bin|BnotInanduop=Upositive|Unegative|Ucomplement|Unotandsubject_expr=|SEstarofloc*expr*exprlist|SEnamedofloc*exprandguard=loc*exprandcomplex_number=|CNplusofloc*literal_expr*string|CNminusofloc*literal_expr*stringandliteral_expr=|LEsignedofloc*literal|LEsignedMinusofloc*literal|LEcmplxPlusofloc*literal_expr*string|LEcmplxMinusofloc*literal_expr*string|LEstringsofloc*pystringlist|LEnoneofloc|LEtrueofloc|LEfalseoflocandkey=|Kliteralofloc*literal_expr|Kattrofloc*namelistandpattern=|PAasofloc*pattern*pattern|PAorofloc*patternlist|PAcaptureofname|PAliteralofliteral_expr|PAwildcardofloc|PAvalueofloc*namelist|PAgroupofloc*pattern|PAseqBofloc*patternoption|PAseqPofloc*patternoption|PAseqOpenofloc*pattern*patternoption|PAseqMaybeofloc*patternlist|PAstarofloc*pattern|PAdblStarofloc*pattern|PAmapofloc*patternlist*patternoption|PAkeyValueofloc*key*pattern|PAkeywordofloc*name*pattern|PAclassofloc*namelist*patternlist*patternlistandcase_block=loc*pattern*guardoption*suiteclassc(fileinput:fileinput)=objectinheritAst_base.cvalmutablecomment_tbl=(Hashtbl.create0:(int,comment)Hashtbl.t)methodset_comment_tblt=comment_tbl<-tmethodcomment_tbl=comment_tblmethodfileinput=fileinputvalmutableblank_regions=([]:(int*int)list)valmutableblank_LOC=0methodset_blank_regionsrs=blank_regions<-rsmethodblank_regions=blank_regionsmethodset_blank_LOCn=blank_LOC<-nmethodblank_LOC=blank_LOCend(* of class AST.c *)(* end of AST *)