123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147(*
Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2017 Anton Lavrik
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.
*)(*
* Piq AST (abstract syntax tree)
*)moduleU=Piqi_utilopenU.StdmodulerecPiq_ast:sigtypeast=[|`intof(int64*string)|`uintof(int64*string)|`floatof(float*string)|`boolofbool|`wordofstring|`stringof(string*string)|`binaryof(string*string)|`textofstring|`nameofstring|`namedofNamed.t(* TODO: string * ast *)|`typenameofstring|`typedofTyped.t(* TODO: string * ast *)|`listofastlist(* typically, from is represented as (name, args) where name has type
* form_name defined below; however we are also reusing this element for
* single ast elements enclosed in parenthesis: (<ast>); such form is rarely
* used in practice (mostly, for associativity control or for generated
* code), but we decided to support it for consistency *)|`formofast*astlist(* Raw string -- just a sequence of bytes: may be parsed as either binary or
* utf8 string
*
* NOTE: this is used only in several special cases, and can't be represented
* in Piq text format directly *)|`raw_stringofstring(* reference to Piqobj.any object in Piqi_objstore *)|`anyofint]andform_name=[|`wordofstring|`nameofstring|`typenameofstring]end=Piq_astandTyped:sigtypet={mutabletypename:string;mutablevalue:Piq_ast.ast}end=TypedandNamed:sigtypet={mutablename:string;mutablevalue:Piq_ast.ast}end=NamedincludePiq_ast(* these functions are not going to be used; this are just stubs for
* autogenerated Piqi-OCaml type mapping *)letast_of_boolx=`boolxletast_to_bool_=true(* apply function f to the node identified by its path in the tree *)lettransform_astpathf(ast:ast)=letrecauxp=function|`listlwhenp=[]->(* leaf node *)(* f replaces, removes element, or splices elements of the list *)letres=U.flatmapflin`listres|xwhenp=[]->(* leaf node *)(* expecting f to replace the existing value, no other modifications
* such as removal or splicing is allowed in this context *)(matchfxwith[res]->res|_->assertfalse)|`listl->(* haven't reached the leaf node => continue tree traversal *)letres=List.map(auxp)lin`listres|`named{Named.name=n;value=v}whenList.hdp=n->(* found path element => continue tree traversal *)letres={Named.name=n;value=aux(List.tlp)v}in`namedres|x->xinauxpathastletmap_words(ast:ast)f:ast=letrecaux=function|`words->`word(fs)|`names->`name(fs)(* TODO, XXX: apply function to the last segment of the type name? *)|`typenames->`typenames|`named{Named.name=n;Named.value=v}->`named{Named.name=fn;Named.value=auxv}|`typed({Typed.value=ast}asx)->letast=auxastin`typed{xwithTyped.value=ast}|`listl->`list(List.mapauxl)|`form(name,args)->`form(auxname,List.mapauxargs)|x->xinauxastletis_infix_form(form_name:form_name)(args:astlist)=matchform_namewith|`word_->false|`name_|`typename_->(matchargswith|[`name_]|[`named_]|[`typed_]->true|_::_::_->true|_->false)