123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149(* Yoann Padioleau
*
* Copyright (C) 2013 Facebook
*
* 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.
*)openCommonmodulePI=Parse_info(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* A few helpers function to build Ast_fuzzy tree from a list of tokens.
* It factorizes the language-independent part of those AST fuzzy builder.
*)(*****************************************************************************)(* Types *)(*****************************************************************************)type'tokhooks={kind:'tok->Parse_info.token_kind;tokf:'tok->Parse_info.info;}exceptionUnclosedofstring*Parse_info.info(* starting point *)(*****************************************************************************)(* Helpers *)(*****************************************************************************)letchar_of_token_kind=function|PI.RAngle->'>'|PI.RBracket->']'|PI.RBrace->'}'|_->raise(Impossible)(*****************************************************************************)(* Entry point *)(*****************************************************************************)(*
* less: I should also factorize with Parse_cpp.parse_fuzzy.
* put here also generic parts of token_views_of_xxx?
*
* less: check that it's consistent with the indentation?
* less: more fault tolerance? if col == 0 and { then reset?
*)letmk_treeshxs=(* filter comment tokens *)letxs=xs+>Common.exclude(funt->letkind=h.kindtinmatchkindwith|PI.Esthet_|PI.Eof->true|_->false)inletrecconsumexxs=matchxwith|tokwhenh.kindtok=PI.LBrace->letbody,closing,rest=look_closePI.RBracex[]xsinAst_fuzzy.Braces(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LBracket->letbody,closing,rest=look_closePI.RBracketx[]xsinAst_fuzzy.Bracket(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LAngle->letbody,closing,rest=look_closePI.RAnglex[]xsinAst_fuzzy.Angle(h.tokfx,body,h.tokfclosing),rest|tokwhenh.kindtok=PI.LPar->letbody,closing,rest=look_close_parenx[]xsinletbody'=split_commabodyinAst_fuzzy.Parens(h.tokfx,body',h.tokfclosing),rest|tok->Ast_fuzzy.Tok(PI.str_of_info(h.tokftok),h.tokfx),xs(*
(match Ast.str_of_info (tokext tok) with
| "..." -> Ast_fuzzy.Dots (tokext tok)
| s when Ast_fuzzy.is_metavar s -> Ast_fuzzy.Metavar (s, tokext tok)
| s -> Ast_fuzzy.Tok (s, tokext tok)
*)andauxxs=matchxswith|[]->[]|x::xs->letx',xs'=consumexxsinx'::auxxs'andlook_closeclose_kindtok_startaccbodyxs=matchxswith|[]->raise(Unclosed(spf"look_close '%c'"(char_of_token_kindclose_kind),h.tokftok_start))|x::xs->(matchxwith|tokwhenh.kindtok=close_kind->List.revaccbody,x,xs|_->let(x',xs')=consumexxsinlook_closeclose_kindtok_start(x'::accbody)xs')(* todo? diff with look_close PI.RPar ? *)andlook_close_parentok_startaccbodyxs=matchxswith|[]->raise(Unclosed("look_close_paren",h.tokftok_start))|x::xs->(matchxwith|tokwhenh.kindtok=PI.RPar->List.revaccbody,x,xs|_->let(x',xs')=consumexxsinlook_close_parentok_start(x'::accbody)xs')andsplit_commaxs=letrecauxaccxs=matchxswith|[]->ifnullaccthen[]else[Left(acc+>List.rev)]|x::xs->(matchxwith|Ast_fuzzy.Tok(",",info)->letbefore=acc+>List.revinifnullbeforethenaux[]xselse(Leftbefore)::(Right(info))::aux[]xs|_->aux(x::acc)xs)inaux[]xsinauxxs