Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file pleac.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232(* Yoann Padioleau
*
* Copyright (C) 2010 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 *)(*****************************************************************************)(*
* PLEAC, the Programming Language Examples Alike Cookbook,
* is a great resource when learning a new language. This module
* provides some functions to parse pleac data and to generate
* regular code that can then be analyzed and indexed and
* then visualized like any other code.
*
* See http://pleac.sourceforge.net/
*
* Important files:
* - skeleton.sgml
* - *.data, language implementations
*)(*****************************************************************************)(* Types *)(*****************************************************************************)typecode_excerpt=stringlisttypesection=stringtypecomment_style=string(* comment_start *)*string(* comment_end *)typesections=(section,code_excerpt)Common.assoctypeskeleton=(string(* section1 *)*((string(* section2 title *)*section)list))list(*****************************************************************************)(* Helpers *)(*****************************************************************************)letskip_no_headingxs=xs+>Common.exclude(fun(s,_)->s=$=Common2.split_list_regexp_noheading)letmangle_to_generate_filenames=Str.global_replace(Str.regexp"[- /.,':()]")"_"s(*****************************************************************************)(* Parsing *)(*****************************************************************************)(* ex: (* @@PLEAC@@_1.0 *), or # @@PLEAC@@_1.0 *)letregexp_section_pleac_data="\\(.*\\) @@PLEAC@@_\\([0-9\\.]+\\)\\(.*\\)"letparse_data_filefile=file+>Common.cat+>Common2.split_list_regexpregexp_section_pleac_data+>skip_no_heading+>List.map(fun(s,group)->ifs=~regexp_section_pleac_datathenlet(_,section,_)=Common.matched3sinsection,groupelsefailwith("Pleac.parse_data_file: impossible: "^s))letdetect_comment_stylefile=file+>Common.cat+>Common2.return_when(funs->ifs=~regexp_section_pleac_datathenlet(s1,_s2,s3)=Common.matched3sinSome(s1,s3)elseNone)(* ex: <sect1 id="strings" label="1"><title>Strings</title> *)letregexp_skeleton_section1="<sect1 .*<title>\\(.*\\)</title>"(* ex: <sect2><title>Short Sleeps</title> *)letregexp_skeleton_section2="<sect2><title>\\(.*\\)</title>"(* ex: PLEAC:3.9:CAELP *)letregexp_skeleton_section_number="PLEAC:\\(.*\\):"(* It's a sgml file so we could parse it using pxp and then visiting it
* but using regexps is probably ok.
*)letparse_skeleton_filefile=file+>Common.cat+>Common2.split_list_regexpregexp_skeleton_section1+>skip_no_heading+>List.map(fun(s,group)->ifs=~regexp_skeleton_section1thenletsection1=Common.matched1sinsection1,group+>Common2.split_list_regexpregexp_skeleton_section2+>skip_no_heading+>List.map(fun(s2,group)->ifs2=~regexp_skeleton_section2thenletsection2=Common.matched1s2insection2,group+>Common2.return_when(funs3->ifs3=~regexp_skeleton_section_numberthenSome(Common.matched1s3)elseNone)elsefailwith("Pleac.parse_data_file: impossible: "^s))elsefailwith("Pleac.parse_data_file: impossible: "^s))(*****************************************************************************)(* Main entry point *)(*****************************************************************************)(* todo? could also split using class with sections and static methods with
* subsections. So could use M-x Pleac_Strings::TAB :)
*)typegen_mode=|OneFilePerSection|OneDirPerSectionletgen_source_filesskeletonsections(comment_start,comment_end)~gen_mode~output_dir~ext_file~hook_start_section2~hook_line_body~hook_end_section2=ifnot(Common2.command2_y_or_no("rm -rf "^output_dir))thenfailwith"ok we stop";Common.command2("mkdir -p "^output_dir);lethsections=Common.hash_of_listsectionsinletestet_sect1=(Common2.repeat"*"70)+>Common.join""inletestet_sect2=(Common2.repeat"-"70)+>Common.join""in(matchgen_modewith|OneFilePerSection->skeleton+>List.iter(fun(section1,xs)->letfile=Filename.concatoutput_dir(mangle_to_generate_filenamesection1)^"."^ext_fileinCommon.with_open_outfilefile(fun(pr_no_nl,_chan)->letprs=pr_no_nl(s^"\n")inpr(spf"%s %s %s"comment_startestet_sect1comment_end);pr(spf"%s %s %s"comment_startsection1comment_end);pr(spf"%s %s %s"comment_startestet_sect1comment_end);xs+>List.iter(fun(section2,secnumber)->letcode_opt=trySome(Hashtbl.findhsectionssecnumber)withNot_found->pr2(spf"Section %s was not found in data file"secnumber);Noneinpr(spf"%s %s %s"comment_startestet_sect2comment_end);pr(spf"%s %s %s"comment_startsection2comment_end);pr(spf"%s %s %s"comment_startestet_sect2comment_end);code_opt+>Common.do_option(funcode->code+>List.iterpr))))|OneDirPerSection->skeleton+>List.iter(fun(section1,xs)->letdir=Filename.concatoutput_dir(mangle_to_generate_filenamesection1)inCommon.command2("mkdir -p "^dir);xs+>List.iter(fun(section2,secnumber)->letfile=Filename.concatdir(mangle_to_generate_filenamesection2)^"."^ext_fileinletcode_opt=trySome(Hashtbl.findhsectionssecnumber)withNot_found->pr2(spf"Section %s was not found in data file"secnumber);Noneincode_opt+>Common.do_option(funcode->Common.with_open_outfilefile(fun(pr_no_nl,_chan)->letprs=pr_no_nl(s^"\n")inpr(spf"%s %s %s"comment_startestet_sect1comment_end);pr(spf"%s %s %s"comment_startsection2comment_end);pr(spf"%s %s %s"comment_startestet_sect1comment_end);pr(hook_start_section2(mangle_to_generate_filenamesection2));code+>List.iter(funs->pr(hook_line_bodys));pr(hook_end_section2(mangle_to_generate_filenamesection2));)))))