Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /gen/expand-file.sml
ViewVC logotype

View of /gen/expand-file.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 244 - (download) (annotate)
Wed Sep 24 08:26:44 2008 UTC (11 years, 3 months ago) by jhr
File size: 2656 byte(s)
  Add signature
(* expand-file.sml
 *
 * COPYRIGHT (c) 2008 John Reppy (http://cs.uchicago.edu/~jhr)
 * All rights reserved.
 *
 * Expand the tags in a template file.  A tag has the form "@group:extension@".
 *)

structure ExpandFile : sig

    val expand : {src : string, dst : string, tree : Extensions.group list} -> unit

  end = struct

    structure Ext = Extensions
    structure SS = Substring
    structure M = MatchTree

    structure RE = RegExpFn (
      structure P = AwkSyntax
      structure E = BackTrackEngine);

    structure GenStdFunc = GenFuncFn(struct val indirect = false fun cName x = x end)
    structure GenGleeFunc = GenFuncFn(struct val indirect = true fun cName x = "p"^x end)

  (* find an extension by group and name *)
    fun findExtension (tree, group, ext) = let
	  fun findGrp (Ext.Group{name, exts}::r) =
		if (name = group) then findExt exts
		else findGrp r
	    | findGrp [] = NONE
	  and findExt (Ext.Ext{name, consts, funcs}::r) =
		if (name = ext) then SOME{consts=consts, funcs=funcs}
		else findExt r
	    | findExt [] = NONE
	  in
	    findGrp tree
	  end

  (* expand tag to output le *)
    fun expandTag (outS, consts, funcs) = ()

    val placeholderRE = RE.compileString "[\\t ]*@([a-zA-Z][-a-zA-Z0-9_]*):([a-zA-Z][a-zA-Z0-9_]*)@[\\t ]*"
    val prefixPlaceholder = RE.prefix placeholderRE SS.getc

    fun findPlaceholder s = (case prefixPlaceholder(SS.full s)
	   of SOME(M.Match(_, [M.Match({pos=p1, len=l1}, _), M.Match({pos=p2, len=l2}, _)]), _) =>
		SOME(SS.string(SS.slice(p1, 0, SOME l1)), SS.string(SS.slice(p2, 0, SOME l2)))
	    | _ => NONE
	  (* end case *))

  (* copy from inStrm to outStrm expanding placeholders *)
    fun copy (tree, inStrm, outStrm) = let
	  fun lp () = (case TextIO.inputLine inStrm
		 of NONE => ()
		  | SOME s => (
		    case findPlaceholder s
		     of NONE => TextIO.output (outStrm, s)
		      | SOME(group, ext) => (case findExtension (tree, group, ext)
			   of SOME{consts, funcs} => expandTag (outStrm, consts, funcs)
			    | NONE => raise Fail "bogus placeholder"
			  (* end case *))
		    (* end case *);
		    lp())
		(* end case *))
	  in
	    lp()
	  end

    exception OpenOut

    fun expand {src, dst, tree} = (let
	  val srcStrm = TextIO.openIn src
	  val dstStrm = TextIO.openOut dst
		handle ex => (
		  TextIO.closeIn srcStrm;
		  TextIO.output(TextIO.stdErr, concat[
		      "Warning: unable to open output file \"",
		      dst, "\"\n"
		    ]);
		  raise OpenOut)
	  fun done () = (TextIO.closeIn srcStrm; TextIO.closeOut dstStrm)
	  in
	    copy (tree, srcStrm, dstStrm) handle ex => (done(); raise ex);
	    done()
	  end
	    handle OpenOut => ())

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0