123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990(* Yoann Padioleau
*
* Copyright (C) 2012 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.
*)openCommon(*****************************************************************************)(* Prelude *)(*****************************************************************************)(* Annotation extraction from comments.
*
* todo: might be very facebook specific and made obsolete by the
* new module features in ES6 (import/export).
*)(*****************************************************************************)(* Types *)(*****************************************************************************)typeannotation=|ProvidesModuleofModule_pre_es6.module_|ProvidesLegacyofModule_pre_es6.module_|RunWhenReady|Otherofstring(*****************************************************************************)(* String -> annotation *)(*****************************************************************************)(* We currently use a line-based and regexp approach to extract annotations.
* Maybe at some point it could be better to have a comment lexer instead.
* But the format of a comment has some line layout. For instance
* annotations usually must be at the beginning of the line (after a possible
* comment esthetic prefix mark), so the line-based approach may be better.
*)letextract_annotationsstr=letlines=Common2.linesstr|>List.mapComment_js.strip_comment_marksinlines|>Common2.map_flatten(funstr->match()with|_whenstr=~"@providesModule[ \t]+\\([A-Za-z-_0-9]+\\)"->[ProvidesModule(Common.matched1str)]|_whenstr=~"@providesLegacy[ \t]+\\([A-Za-z-_0-9]+\\)"->[ProvidesLegacy(Common.matched1str)]|_->letxs=Common2.all_match"\\(@[A-Za-z-]+\\)"strinxs|>List.map(function|"@runWhenReady"->RunWhenReady|s->Others))(*****************************************************************************)(* Main entry point *)(*****************************************************************************)(* The returned parse_info is the one associated with the whole comment.
* We use it in the tag generation. It's not very precise because
* it's the info of the whole comment, and not just of the annotation,
* but this should be good enough for the tags. Being more precise
* would require to lex the comment.
*)letannotations_of_program_with_comments(_,toks)=toks|>List.map(function|Parser_js.TCommenttok->lets=Parse_info.str_of_infotokinletannots=extract_annotationssin(* add location information to the annotation by reusing the
* location information of the comment (that means
* that multiple annotations in one comment will share
* the same location, which is not very precise, but should
* be good enough to locate the annotation when we
* do checks related to annotations or for tags.
*)annots|>List.map(funannot->annot,tok)|_->[])|>List.flatten