123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198(****************************************************************************)(* *)(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)(* *)(* Copyright (C) 2017-2019 The MOPSA Project. *)(* *)(* This program is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU Lesser General Public License as published *)(* by the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* This program 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 *)(* GNU Lesser General Public License for more details. *)(* *)(* You should have received a copy of the GNU Lesser General Public License *)(* along with this program. If not, see <http://www.gnu.org/licenses/>. *)(* *)(****************************************************************************)(** Parser of configuration files *)openMopsa_utilsopenYojson.BasicopenYojson.Basic.UtilopenSyntaxopenVisitoropenSig.Abstraction.StackedopenSig.Abstraction.DomainopenSig.Abstraction.SimplifiedopenSig.Abstraction.Simplified_functoropenSig.Abstraction.StatelessopenSig.Abstraction.PartitioningopenSig.Abstraction.ValueopenSig.Abstraction.Value_functoropenSig.Reduction.ExecopenSig.Reduction.EvalopenSig.Reduction.SimplifiedopenSig.Reduction.Value(** {2 Configuration file} *)(** ********************** *)(** Path to the current configuration *)letopt_config=ref""letall_domains()=stacked_domain_names()@standard_domain_names()@simplified_domain_names()@stateless_domain_names()@value_abstraction_names()@simplified_functor_names()@value_functor_names()@partitioning_names()letall_reductions()=simplified_value_reductions()@eval_reductions()@exec_reductions()@simplified_reductions()(** {2 Domain parser} *)(** ***************** *)letdebugfmt=Debug.debug~channel:"framework.config.abstraction.parser"fmtletrecparse_domainjson:domain=json|>visit{leaf=(funsemanticname->letd=tryD_stacked(find_stacked_domainname)withNot_found->tryD_domain(find_standard_domainname)withNot_found->tryD_stateless(find_stateless_domainname)withNot_found->tryD_simplified(find_simplified_domainname)withNot_found->Exceptions.panic"Domain '%s' not found@.Available domains: %a"name(Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt", ")Format.pp_print_string)(all_domains())inmk_domaind~semantic);switch=(funsemanticjsons->mk_domain(D_switch(List.mapparse_domainjsons))~semantic);compose=(funsemanticjsons->mk_domain(D_compose(List.mapparse_domainjsons))~semantic);product=(funsemanticjsonsreductions->mk_domain(D_product(List.mapparse_domainjsons,List.mapparse_domain_reductionreductions))~semantic);nonrel=(funsemanticjson->mk_domain(D_nonrel(parse_valuejson))~semantic);apply=(funsemanticfunctarg->mk_domain(D_functor(parse_domain_functorfunct,parse_domainarg))~semantic);union=(funsemanticjson->assertfalse);}andparse_domain_reduction(name:string):domain_reduction=tryDR_eval(find_eval_reductionname)withNot_found->tryDR_exec(find_exec_reductionname)withNot_found->tryDR_simplified(find_simplified_reductionname)withNot_found->Exceptions.panic"Domain reduction '%s' not found@.Available reductions: %a"name(Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt", ")Format.pp_print_string)(all_reductions())andparse_domain_functor(json:t):domain_functor=json|>visit{leaf=(funsemanticname->tryF_partitioning(find_partitioningname)withNot_found->tryF_simplified(find_simplified_functorname)withNot_found->F_stacked(parse_domainjson));switch=(funsemanticjsons->F_stacked(mk_domain(D_switch(List.mapparse_domainjsons))~semantic));compose=(funsemanticjsons->F_stacked(mk_domain(D_compose(List.mapparse_domainjsons))~semantic));product=(funsemanticjsonsreductions->F_stacked(mk_domain(D_product(List.mapparse_domainjsons,List.mapparse_domain_reductionreductions))~semantic));nonrel=(funsemanticjson->assertfalse);apply=(funsemanticfunctarg->assertfalse);union=(funsemanticjson->assertfalse);}andparse_valuejson:value=json|>visit{leaf=(fun_name->tryV_value(find_value_abstractionname)withNot_found->Exceptions.panic"Value '%s' not found"name);union=(fun_jsons->V_union(List.mapparse_valuejsons));product=(fun_jsonsreductions->V_product(List.mapparse_valuejsons,List.mapparse_value_reductionreductions));apply=(fun_functarg->V_functor(parse_value_functorfunct,parse_valuearg));switch=(fun_jsons->assertfalse);compose=(fun_jsons->assertfalse);nonrel=(fun_json->assertfalse);}andparse_value_reduction(name:string):value_reduction=tryfind_value_reductionnamewithNot_found->Exceptions.panic"Value reduction '%s' not found"nameandparse_value_functorjson:value_functor=letname=to_stringjsonintryfind_value_functornamewithNot_found->Exceptions.panic"Value functor '%s' not found"name(** {2 Toplevel attributes} *)(** *********************** *)letget_languagejson=matchjsonwith|`Assoc(obj)whenList.mem_assoc"language"obj->List.assoc"language"obj|>to_string|_->Exceptions.panic"language declaration not found in configuration file"letget_domain_jsonjson=matchjsonwith|`Assoc(obj)whenList.mem_assoc"domain"obj->List.assoc"domain"obj|_->Exceptions.panic"domain declaration not found in configuration file"(** {2 Entry points} *)(** **************** *)letparsefile:abstraction=letjson=Yojson.Basic.from_filefileinletlanguage=get_languagejsoninletdomain_json=get_domain_jsonjsoninletdomain=parse_domaindomain_jsonin{domain;language}letlanguagefile:string=letjson=Yojson.Basic.from_filefileinget_languagejsonletdomainsfile:stringlist=iffile=""thenall_domains()elseletjson=Yojson.Basic.from_filefileinletdomain=json|>member"domain"inletrecname_visitor=Visitor.{leaf=(fun_name->[name]);switch=(fun_l->List.mapget_namesl|>List.flatten);nonrel=(fun_v->get_namesv);apply=(fun_fd->get_namesf@get_namesd);compose=(fun_l->List.mapget_namesl|>List.flatten);product=(fun_lr->List.mapget_namesl|>List.flatten);union=(fun_l->List.mapget_namesl|>List.flatten);}andget_namesjson=Visitor.visitname_visitorjsoninget_namesdomain