[m-rev.] for review: brace expansion

Zoltan Somogyi zs at cs.mu.OZ.AU
Tue May 6 12:15:17 AEST 2003


For review by anyone. Also, whoever caused the command line below
to have two copies of --linkage shared should fix the duplication.
I'll fix the duplicate --c-include-directory ../browser in Mmake.workspace
in the diff that will start to use brace expansion.

Zoltan.

The objective of this change is to make bootcheck output more readable. The
current output, containing lines such as the following, isn't readable due
in large part to all the directory specifications.

home/aral/public/mercury-latest/i686-pc-linux-gnu/bin/mmc --no-unboxed-float
--conf-low-tag-bits 2 --bits-per-word 32 --bytes-per-word 4 --compile-to-c
--grade asm_fast.gc      --config-file ../scripts/Mercury.config
--no-mercury-stdlib-dir -I../library --c-include-directory ../boehm_gc
--c-include-directory ../boehm_gc/include --c-include-directory ../mps_gc/code
--c-include-directory ../runtime --c-include-directory ../library
--c-include-directory ../library/ --c-include-directory ../browser
--c-include-directory ../browser/ --c-include-directory ../trace --no-infer-all
--halt-at-warn --no-warn-inferred-erroneous -I ../browser -I ../analysis
--c-include-directory ../analysis --no-main --linkage shared --no-main
--linkage shared     hlds.hlds_out > hlds.hlds_out.err 2>&1

This diff add a mechanism to replace about five lines of the above with

--c-include-directory ../{boehm_gc,boehm_gc/include,mps_gc/code,runtime,library,browser,trace,analysis}

This won't make the output readable, but at least it is a start.

The change to bootcheck itself to use this capability will have to wait
until this change has been installed.

compiler/handle_options.m:
	Perform brace expansion on all options that specify lists of
	directories.

doc/user_guide.texi:
	Document the brace expansion of the values of the affected options.

library/dir.m:
	Add a function expand_braces.

NEWS:
	Mention the new function.

tests/hard_coded/brace.{m,exp}:
	New test case to test the new function.

tests/hard_coded/Mmakefile:
	Enable the new test case.

cvs diff: Diffing .
Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.310
diff -u -b -r1.310 NEWS
--- NEWS	30 Apr 2003 18:15:37 -0000	1.310
+++ NEWS	5 May 2003 02:38:39 -0000
@@ -86,7 +86,9 @@
 
 * We've added a new library module, `array2d'.
 
-* We've added a predicate, map_fold, to the set module.
+* We've added a predicate, map_fold, to set.m.
+
+* We've added a predicate, expand_braces, to dir.m.
 
 * We've added the three predicates, `is_nan/1', `is_inf/1' and
   `is_nan_or_inf/1' to float.m.  These predicates are for use only on
cvs diff: Diffing analysis
cvs diff: Diffing bindist
cvs diff: Diffing boehm_gc
cvs diff: Diffing boehm_gc/Mac_files
cvs diff: Diffing boehm_gc/cord
cvs diff: Diffing boehm_gc/cord/private
cvs diff: Diffing boehm_gc/doc
cvs diff: Diffing boehm_gc/include
cvs diff: Diffing boehm_gc/include/private
cvs diff: Diffing boehm_gc/tests
cvs diff: Diffing browser
cvs diff: Diffing bytecode
cvs diff: Diffing compiler
Index: compiler/handle_options.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/handle_options.m,v
retrieving revision 1.180
diff -u -b -r1.180 handle_options.m
--- compiler/handle_options.m	15 Apr 2003 08:32:51 -0000	1.180
+++ compiler/handle_options.m	6 May 2003 01:53:49 -0000
@@ -272,6 +272,12 @@
 	globals__io_init(OptionTable1, Target, GC_Method, TagsMethod,
 		TermNorm, TraceLevel, TraceSuppress),
 
+	list__foldl(brace_expand_accumulating_option,
+		[c_include_directory, link_library_directories,
+		runtime_link_library_directories, mercury_library_directories,
+		init_file_directories, options_search_directories,
+		search_directories, intermod_directories]),
+
 	% Conservative GC implies --no-reclaim-heap-*
 	( { gc_is_conservative(GC_Method) = yes } ->
 		globals__io_set_option(
@@ -1236,6 +1242,14 @@
 	;
 		[]
 	).
+
+:- pred brace_expand_accumulating_option(option::in,
+	io__state::di, io__state::uo) is det.
+
+brace_expand_accumulating_option(Option) -->
+	globals__io_lookup_accumulating_option(Option, Strings0),
+	{ list__condense(list__map(expand_braces, Strings0), Strings) },
+	globals__io_set_option(Option, accumulating(Strings)).
 
 	% These option implications only affect the low-level (LLDS) code
 	% generator.  They may in fact be harmful if set for the high-level
cvs diff: Diffing compiler/notes
cvs diff: Diffing debian
cvs diff: Diffing deep_profiler
cvs diff: Diffing deep_profiler/notes
cvs diff: Diffing doc
Index: doc/user_guide.texi
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/doc/user_guide.texi,v
retrieving revision 1.364
diff -u -b -r1.364 user_guide.texi
--- doc/user_guide.texi	16 Apr 2003 12:36:24 -0000	1.364
+++ doc/user_guide.texi	6 May 2003 02:00:44 -0000
@@ -6390,6 +6390,8 @@
 @findex --options-search-directory
 Add @var{dir} to the list of directories to be searched for
 options files.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item --mercury-configuration-directory @var{dir}
@@ -6407,6 +6409,8 @@
 @cindex Search path
 Append @var{dir} to the list of directories to be searched for
 imported modules.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item --intermod-directory @var{dir}
@@ -6415,6 +6419,8 @@
 @cindex Search path
 Append @var{dir} to the list of directories to be searched for
 @samp{.opt} files.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item --use-search-directories-for-intermod
@@ -6528,6 +6534,8 @@
 C header files.  Note that if you want to override this list, rather than
 append to it, then you can set the @samp{MERCURY_MC_ALL_C_INCL_DIRS}
 environment variable to a sequence of @samp{--c-include-directory} options.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item @code{--c-debug}
@@ -6654,6 +6662,8 @@
 @cindex Search path for libraries
 Append @var{directory} to the list of directories in which
 to search for libraries.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item -R @var{directory}
@@ -6662,6 +6672,8 @@
 @findex --runtime-library-directory
 Append @var{directory} to the list of directories in which
 to search for shared libraries at runtime.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @item -l @var{library}
 @itemx --library @var{library}
@@ -6719,6 +6731,8 @@
 @findex --init-file-directory
 Append @var{directory} to the list of directories to
 be searched for @samp{.init} files by @samp{c2init}.
+If @var{dir} contains braces,
+it may specify more than one directory via brace expansion.
 
 @sp 1
 @item --init-file @var{file}
cvs diff: Diffing extras
cvs diff: Diffing extras/aditi
cvs diff: Diffing extras/cgi
cvs diff: Diffing extras/complex_numbers
cvs diff: Diffing extras/complex_numbers/samples
cvs diff: Diffing extras/complex_numbers/tests
cvs diff: Diffing extras/concurrency
cvs diff: Diffing extras/curs
cvs diff: Diffing extras/curs/samples
cvs diff: Diffing extras/curses
cvs diff: Diffing extras/curses/sample
cvs diff: Diffing extras/dynamic_linking
cvs diff: Diffing extras/error
cvs diff: Diffing extras/graphics
cvs diff: Diffing extras/graphics/mercury_opengl
cvs diff: Diffing extras/graphics/mercury_tcltk
cvs diff: Diffing extras/graphics/samples
cvs diff: Diffing extras/graphics/samples/calc
cvs diff: Diffing extras/graphics/samples/maze
cvs diff: Diffing extras/graphics/samples/pent
cvs diff: Diffing extras/lazy_evaluation
cvs diff: Diffing extras/lex
cvs diff: Diffing extras/lex/samples
cvs diff: Diffing extras/lex/tests
cvs diff: Diffing extras/logged_output
cvs diff: Diffing extras/moose
cvs diff: Diffing extras/moose/samples
cvs diff: Diffing extras/morphine
cvs diff: Diffing extras/morphine/non-regression-tests
cvs diff: Diffing extras/morphine/scripts
cvs diff: Diffing extras/morphine/source
cvs diff: Diffing extras/odbc
cvs diff: Diffing extras/posix
cvs diff: Diffing extras/quickcheck
cvs diff: Diffing extras/quickcheck/tutes
cvs diff: Diffing extras/references
cvs diff: Diffing extras/references/samples
cvs diff: Diffing extras/references/tests
cvs diff: Diffing extras/stream
cvs diff: Diffing extras/trailed_update
cvs diff: Diffing extras/trailed_update/samples
cvs diff: Diffing extras/trailed_update/tests
cvs diff: Diffing extras/xml
cvs diff: Diffing extras/xml/samples
cvs diff: Diffing java
cvs diff: Diffing java/library
cvs diff: Diffing java/runtime
cvs diff: Diffing library
Index: library/dir.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/dir.m,v
retrieving revision 1.14
diff -u -b -r1.14 dir.m
--- library/dir.m	10 Jul 2002 06:49:35 -0000	1.14
+++ library/dir.m	5 May 2003 01:43:50 -0000
@@ -15,6 +15,8 @@
 :- module dir.
 :- interface.
 
+:- import_module list.
+
 	% predicates to isolate system dependencies 
 
 :- func dir__directory_separator = character.
@@ -43,10 +45,32 @@
 :- func dir__make_path_name(string, string) = string.
 :- func string / string = string.
 
+       % Implement brace expansion, as in sh: return the sequence of strings
+       % generated from the given input string. Throw an exception if the
+       % input string contains mismatched braces.
+       %
+       % The following is the documentation of brace expansion from the sh
+       % manual:
+       %
+       %	Brace expansion is a mechanism by which arbitrary strings may
+       %	be generated. This mechanism is similar to pathname expansion,
+       %	but the filenames generated need not exist. Patterns to be
+       %	brace expanded take the form of an optional preamble, followed
+       %	by a series of comma-separated strings between a pair of
+       %	braces, followed by an optional postscript. The preamble is
+       %	prefixed to each string contained within the braces, and the
+       %	postscript is then appended to each resulting string, expanding
+       %	left to right.
+       %
+       %	Brace expansions may be nested. The results of each expanded
+       %	string are not sorted; left to right order is preserved.
+       %	For example, a{d,c,b}e expands into `ade ace abe'.
+:- func expand_braces(string) = list(string).
+
 %-----------------------------------------------------------------------------%
 
 :- implementation.
-:- import_module int, list, require, string.
+:- import_module int, char, list, require, exception, string.
 
 dir__directory_separator('/').
 
@@ -98,6 +122,85 @@
 
 DirName / FileName = dir__make_path_name(DirName, FileName).
 
+expand_braces(ArgStr) = ExpandStrs :-
+	ArgChar = string__to_char_list(ArgStr),
+	ExpandChars = expand(ArgChar),
+	ExpandStrs = list__map(string__from_char_list, ExpandChars).
+
+:- func expand(list(char)) = list(list(char)).
+
+expand(Chars) = expand_acc(Chars, [[]]).
+
+:- func expand_acc(list(char), list(list(char))) = list(list(char)).
+
+expand_acc([], Prefixes) = Prefixes.
+expand_acc([Char | Chars], Prefixes0) = Strings :-
+	( Char = '{' ->
+		find_matching_brace(Chars, Alternatives0, Left),
+		AlternativeLists = list__map(expand, Alternatives0),
+		Alternatives = list__condense(AlternativeLists),
+		PrefixLists = list__map(add_alternatives(Alternatives),
+			Prefixes0),
+		Prefixes1 = list__condense(PrefixLists),
+		expand_acc(Left, Prefixes1) = Strings
+	;
+		Prefixes1 = list__map(add_char_at_end(Char), Prefixes0),
+		Strings = expand_acc(Chars, Prefixes1)
+	).
+
+:- func add_alternatives(list(list(char)), list(char)) = list(list(char)).
+
+add_alternatives(Alternatives, Prefix) =
+	list__map(list__append(Prefix), Alternatives).
+
+:- func add_char_at_end(char, list(char)) = list(char).
+
+add_char_at_end(Char, Prefix) = list__append(Prefix, [Char]).
+
+:- pred find_matching_brace(list(char)::in, list(list(char))::out,
+	list(char)::out) is det.
+
+find_matching_brace(Chars, Alternatives, Left) :-
+	find_matching_brace_or_comma(Chars, [], [], 0, Alternatives, Left).
+
+:- pred find_matching_brace_or_comma(list(char)::in, list(list(char))::in,
+	list(char)::in, int::in, list(list(char))::out, list(char)::out)
+	is det.
+
+find_matching_brace_or_comma([], _, _, _, _, _) :-
+	throw("no matching brace").
+find_matching_brace_or_comma([Char | Chars], Alternatives0, CurAlternative,
+		BraceLevel, Alternatives, Left) :-
+	( Char = '}' ->
+		( BraceLevel = 0 ->
+			list__append(Alternatives0, [CurAlternative],
+				Alternatives),
+			Left = Chars
+		;
+			find_matching_brace_or_comma(Chars, Alternatives0,
+				list__append(CurAlternative, [Char]),
+				BraceLevel - 1, Alternatives, Left)
+		)
+	; Char = '{' ->
+		find_matching_brace_or_comma(Chars, Alternatives0,
+			list__append(CurAlternative, [Char]),
+			BraceLevel + 1, Alternatives, Left)
+	; Char = (',') ->
+		( BraceLevel = 0 ->
+			list__append(Alternatives0, [CurAlternative],
+				Alternatives1),
+			find_matching_brace_or_comma(Chars, Alternatives1,
+				[], BraceLevel, Alternatives, Left)
+		;
+			find_matching_brace_or_comma(Chars, Alternatives0,
+				list__append(CurAlternative, [Char]),
+				BraceLevel, Alternatives, Left)
+		)
+	;
+		find_matching_brace_or_comma(Chars, Alternatives0,
+			list__append(CurAlternative, [Char]),
+			BraceLevel, Alternatives, Left)
+	).
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 % Ralph Becket <rwab1 at cl.cam.ac.uk> 27/04/99
cvs diff: Diffing profiler
cvs diff: Diffing robdd
cvs diff: Diffing runtime
cvs diff: Diffing runtime/GETOPT
cvs diff: Diffing runtime/machdeps
cvs diff: Diffing samples
cvs diff: Diffing samples/c_interface
cvs diff: Diffing samples/c_interface/c_calls_mercury
cvs diff: Diffing samples/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/c_interface/mercury_calls_c
cvs diff: Diffing samples/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/diff
cvs diff: Diffing samples/muz
cvs diff: Diffing samples/rot13
cvs diff: Diffing samples/solutions
cvs diff: Diffing samples/tests
cvs diff: Diffing samples/tests/c_interface
cvs diff: Diffing samples/tests/c_interface/c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/cplusplus_calls_mercury
cvs diff: Diffing samples/tests/c_interface/mercury_calls_c
cvs diff: Diffing samples/tests/c_interface/mercury_calls_cplusplus
cvs diff: Diffing samples/tests/c_interface/mercury_calls_fortran
cvs diff: Diffing samples/tests/c_interface/simpler_c_calls_mercury
cvs diff: Diffing samples/tests/c_interface/simpler_cplusplus_calls_mercury
cvs diff: Diffing samples/tests/diff
cvs diff: Diffing samples/tests/muz
cvs diff: Diffing samples/tests/rot13
cvs diff: Diffing samples/tests/solutions
cvs diff: Diffing samples/tests/toplevel
cvs diff: Diffing scripts
cvs diff: Diffing tests
cvs diff: Diffing tests/benchmarks
cvs diff: Diffing tests/debugger
cvs diff: Diffing tests/debugger/declarative
cvs diff: Diffing tests/dppd
cvs diff: Diffing tests/general
cvs diff: Diffing tests/general/accumulator
cvs diff: Diffing tests/general/string_format
cvs diff: Diffing tests/general/structure_reuse
cvs diff: Diffing tests/grade_subdirs
cvs diff: Diffing tests/hard_coded
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.196
diff -u -b -r1.196 Mmakefile
--- tests/hard_coded/Mmakefile	20 Mar 2003 06:25:54 -0000	1.196
+++ tests/hard_coded/Mmakefile	5 May 2003 01:34:19 -0000
@@ -8,6 +8,7 @@
 	address_of_builtins \
 	agg \
 	bidirectional \
+	brace \
 	builtin_inst_rename \
 	boyer \
 	c_write_string \
Index: tests/hard_coded/brace.exp
===================================================================
RCS file: tests/hard_coded/brace.exp
diff -N tests/hard_coded/brace.exp
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/brace.exp	5 May 2003 03:22:58 -0000
@@ -0,0 +1,50 @@
+abc
+  abc
+{a,b}
+  a
+  b
+a{b,c}d
+  abd
+  acd
+aa{bb,cc}dd
+  aabbdd
+  aaccdd
+aa{bb,cc,{d,e}}ff
+  aabbff
+  aaccff
+  aadff
+  aaeff
+aa{{b1,b2,b3},cc,{d,e}}ff
+  aab1ff
+  aab2ff
+  aab3ff
+  aaccff
+  aadff
+  aaeff
+aa{{b1,{b21,b22,b23},b3},cc,{d,e}}ff
+  aab1ff
+  aab21ff
+  aab22ff
+  aab23ff
+  aab3ff
+  aaccff
+  aadff
+  aaeff
+a{b,c}d{e,f,g}h
+  abdeh
+  abdfh
+  abdgh
+  acdeh
+  acdfh
+  acdgh
+aa{bb,cc,{{d1,d2},e}}ff{g,hh}
+  aabbffg
+  aabbffhh
+  aaccffg
+  aaccffhh
+  aad1ffg
+  aad1ffhh
+  aad2ffg
+  aad2ffhh
+  aaeffg
+  aaeffhh
Index: tests/hard_coded/brace.m
===================================================================
RCS file: tests/hard_coded/brace.m
diff -N tests/hard_coded/brace.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/hard_coded/brace.m	5 May 2003 01:33:26 -0000
@@ -0,0 +1,44 @@
+:- module brace.
+
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module dir, list.
+
+main(!IO) :-
+	list__foldl(expand_print, test_inputs, !IO).
+
+:- pred expand_print(string::in, io::di, io::uo) is det.
+
+expand_print(Arg, !IO) :-
+	io__write_string(Arg, !IO),
+	io__nl(!IO),
+	write_expansions(expand_braces(Arg), !IO).
+
+:- pred write_expansions(list(string)::in, io::di, io::uo) is det.
+
+write_expansions([], !IO).
+write_expansions([Str | Strs], !IO) :-
+	io__write_string("  ", !IO),
+	io__write_string(Str, !IO),
+	io__nl(!IO),
+	write_expansions(Strs, !IO).
+
+:- func test_inputs = list(string).
+
+test_inputs = [
+	"abc",
+	"{a,b}",
+	"a{b,c}d",
+	"aa{bb,cc}dd",
+	"aa{bb,cc,{d,e}}ff",
+	"aa{{b1,b2,b3},cc,{d,e}}ff",
+	"aa{{b1,{b21,b22,b23},b3},cc,{d,e}}ff",
+	"a{b,c}d{e,f,g}h",
+	"aa{bb,cc,{{d1,d2},e}}ff{g,hh}"
+].
cvs diff: Diffing tests/hard_coded/exceptions
cvs diff: Diffing tests/hard_coded/purity
cvs diff: Diffing tests/hard_coded/sub-modules
cvs diff: Diffing tests/hard_coded/typeclasses
cvs diff: Diffing tests/invalid
cvs diff: Diffing tests/invalid/purity
cvs diff: Diffing tests/misc_tests
cvs diff: Diffing tests/mmc_make
cvs diff: Diffing tests/mmc_make/lib
cvs diff: Diffing tests/recompilation
cvs diff: Diffing tests/tabling
cvs diff: Diffing tests/term
cvs diff: Diffing tests/valid
cvs diff: Diffing tests/warnings
cvs diff: Diffing tools
cvs diff: Diffing trace
cvs diff: Diffing util
cvs diff: Diffing vim
cvs diff: Diffing vim/after
cvs diff: Diffing vim/ftplugin
cvs diff: Diffing vim/syntax
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list