123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269(* Yoann Padioleau
*
* Copyright (C) 2019 r2c
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file license.txt.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* license.txt for more details.
*)(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* An Abstract Syntax Tree for OCaml.
*
* See cst_ml.ml for a Concrete Syntax Tree for OCaml (better for program
* transformation purpose).
*)(*****************************************************************************)(* The AST related types *)(*****************************************************************************)(* ------------------------------------------------------------------------- *)(* Token/info *)(* ------------------------------------------------------------------------- *)typetok=Parse_info.t(* with tarzan *)(* a shortcut to annotate some information with token/position information *)type'awrap='a*tok(* with tarzan *)(* ------------------------------------------------------------------------- *)(* Names *)(* ------------------------------------------------------------------------- *)typeident=stringwrap(* with tarzan *)typename=qualifier*identandqualifier=identlist(* TODO: functor? *)(* with tarzan *)(* ------------------------------------------------------------------------- *)(* Types *)(* ------------------------------------------------------------------------- *)typetype_=|TyNameofname(* include builtins *)|TyVarofident|TyFunctionoftype_*type_|TyAppoftype_list*name(* less: could be merged with TyName *)|TyTupleoftype_list(* at least 2 *)(* with tarzan *)(* ------------------------------------------------------------------------- *)(* Expressions *)(* ------------------------------------------------------------------------- *)(* start of big recursive type *)typeexpr=|Lofliteral|Nameofname|Constructorofname*exproption(* special case of Constr *)|Tupleofexprlist|Listofexprlist(* can be empty *)|Sequenceofexprlist|Prefixofstringwrap*expr|Infixofexpr*stringwrap*expr|Callofexpr*argumentlist(* could be factorized with Prefix but it's not a usual prefix operator! *)|RefAccessoftok(* ! *)*expr|RefAssignofexpr*tok(* := *)*expr(* special case of RefAccess and RefAssign *)|FieldAccessofexpr*name|FieldAssignofexpr*name*(* <- *)expr|Recordofexproption(* with *)*(name*expr)list|Newoftok*name|ObjAccessofexpr*ident(* > 1 elt for mutually recursive let (let x and y and z) *)|LetInoflet_bindinglist*expr*rec_opt|Funofparameterlist(* at least one *)*expr(* statement-like expressions *)|Nop(* for empty else *)|Ifofexpr*expr*expr|Matchofexpr*match_caselist|Tryofexpr*match_caselist|Whileofexpr*expr|Forofident*expr*for_direction*expr*exprandliteral=|Intofstringwrap|Floatofstringwrap|Charofstringwrap|Stringofstringwrapandargument=|Argofexpr|ArgKwdofident*expr|ArgQuestionofident*exprandmatch_case=pattern*match_actionandmatch_action=expr*exproption(* when *)andfor_direction=|Tooftok|Downtooftokandrec_opt=tokoption(* ------------------------------------------------------------------------- *)(* Patterns *)(* ------------------------------------------------------------------------- *)andpattern=|PatVarofident|PatLiteralofliteral(* can be signed *)|PatConstructorofname*patternoption(* special cases of PatConstructor *)|PatConsInfixofpattern*tok(* :: *)*pattern|PatTupleofpatternlist|PatListofpatternlist|PatUnderscoreoftok|PatRecordof(name*pattern)list|PatAsofpattern*ident(* ocaml disjunction patterns extension *)|PatDisjofpattern*pattern|PatTypedofpattern*type_(* ------------------------------------------------------------------------- *)(* Let binding (global/local/function definition) *)(* ------------------------------------------------------------------------- *)andlet_binding=|LetClassicoflet_def|LetPatternofpattern*expr(* was called fun_binding in the grammar *)andlet_def={lname:ident;lparams:parameterlist;(* can be empty *)lbody:expr;}andparameter=pattern(* with tarzan *)(* ------------------------------------------------------------------------- *)(* Type declaration *)(* ------------------------------------------------------------------------- *)typetype_declaration={tname:ident;tparams:type_parameterlist;tbody:type_def_kind;}andtype_parameter=ident(* a TyVar, e.g., 'a *)andtype_def_kind=|AbstractType|CoreTypeoftype_(* or type *)|AlgebricTypeof(ident*type_list)list(* and type *)|RecordTypeof(ident*type_*tokoption(* mutable *))list(* with tarzan *)(* ------------------------------------------------------------------------- *)(* Class *)(* ------------------------------------------------------------------------- *)(* ------------------------------------------------------------------------- *)(* Module *)(* ------------------------------------------------------------------------- *)typemodule_declaration={mname:ident;mbody:module_expr;}(* mutually recursive with item *)andmodule_expr=|ModuleNameofname(* alias *)|ModuleStructofitemlist(* ------------------------------------------------------------------------- *)(* Signature/Structure items *)(* ------------------------------------------------------------------------- *)(* could split in sig_item and struct_item but many constructions are
* valid in both contexts.
*)anditem=|Typeoftype_declarationlist(* mutually recursive *)|Exceptionofident*type_list|Externalofident*type_*stringwraplist(* primitive declarations *)|Openofname(* only in sig_item *)|Valofident*type_(* only in struct_item *)|Letofrec_opt*let_bindinglist|Moduleofmodule_declaration(* with tarzan *)typeprogram=itemlist(* with tarzan *)(*****************************************************************************)(* Any *)(*****************************************************************************)typeany=|Toftype_|Eofexpr|Pofpattern|Iofitem|Profprogram(* with tarzan *)(*****************************************************************************)(* Wrappers *)(*****************************************************************************)letstr_of_ident(s,_)=sletinfo_of_ident(_,info)=infoletident_of_name(_,ident)=identletqualifier_of_name(qu,_)=qu|>List.mapstr_of_ident|>Common.join"."