[m-rev.] for review: declarative debugger command parsing

Zoltan Somogyi zs at cs.mu.OZ.AU
Fri May 3 18:55:02 AEST 2002


For review by anyone (Mark?).

browser/declarative_user.m:
	Require users to either type commands in full or to write the minimum
	abbreviation. Typing nonsense commands that happen to start with the
	same initial letter as a command will no longer "work". This is
	necessary because later changes will add more commands.

	Make the parsing of user commands higher level and more easily
	maintainable.

Zoltan.

cvs diff: Diffing .
Index: declarative_user.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/declarative_user.m,v
retrieving revision 1.18
diff -u -b -r1.18 declarative_user.m
--- declarative_user.m	30 Apr 2002 07:08:01 -0000	1.18
+++ declarative_user.m	3 May 2002 07:47:02 -0000
@@ -264,53 +264,59 @@
 
 get_command(Prompt, Command, User, User) -->
 	util__trace_getline(Prompt, Result, User ^ instr, User ^ outstr),
-	( { Result = ok(String) },
-		{ string__to_char_list(String, Line) },
+	(
+		{ Result = ok(String) },
+		{ Words = string__words(char__is_whitespace, String) },
 		{
-			command_chars(Line, Command0)
+			Words = [CmdWord | CmdArgs],
+			cmd_handler(CmdWord, CmdHandler),
+			CommandPrime = CmdHandler(CmdArgs)
 		->
-			Command = Command0
+			Command = CommandPrime
 		;
 			Command = illegal_command
 		}
-	; { Result = error(Error) },
+	;
+		{ Result = error(Error) },
 		{ io__error_message(Error, Msg) },
 		io__write_string(User ^ outstr, Msg),
 		io__nl(User ^ outstr),
 		{ Command = abort }
-	; { Result = eof },
+	;
+		{ Result = eof },
 		{ Command = abort }
 	).
 
-:- pred command_chars(list(char), user_command).
-:- mode command_chars(in, out) is semidet.
+:- pred cmd_handler(string, func(list(string)) = user_command).
+:- mode cmd_handler(in, out((func(in) = out is semidet))) is semidet.
 
-command_chars(['y' | _], yes).
-command_chars(['n' | _], no).
-command_chars(['i' | _], inadmissible).
-command_chars(['s' | _], skip).
-command_chars(['r' | _], restart).
-command_chars(['a' | _], abort).
-command_chars(['h' | _], help).
-command_chars(['?' | _], help).
-command_chars(['b' | Line0], browse(Arg)) :-
-	(
-		Line0 = ['r','o','w','s','e' | Line1]
-	->
-		Line2 = Line1
-	;
-		Line2 = Line0
-	),
-	list__takewhile(char__is_whitespace, Line2, _, ArgChars),
-	parse_integer(ArgChars, 0, Arg).
-
-:- pred parse_integer(list(char), int, int).
-:- mode parse_integer(in, in, out) is semidet.
-
-parse_integer([], N, N).
-parse_integer([D | Ds], N0, N) :-
-	char__digit_to_int(D, I),
-	parse_integer(Ds, N0 * 10 + I, N).
+cmd_handler("y",	one_word_cmd(yes)).
+cmd_handler("yes",	one_word_cmd(yes)).
+cmd_handler("n",	one_word_cmd(no)).
+cmd_handler("no",	one_word_cmd(no)).
+cmd_handler("in",	one_word_cmd(inadmissible)).
+cmd_handler("inadmissible", one_word_cmd(inadmissible)).
+cmd_handler("s",	one_word_cmd(skip)).
+cmd_handler("skip",	one_word_cmd(skip)).
+cmd_handler("r",	one_word_cmd(restart)).
+cmd_handler("restart",	one_word_cmd(restart)).
+cmd_handler("a",	one_word_cmd(abort)).
+cmd_handler("abort",	one_word_cmd(abort)).
+cmd_handler("?",	one_word_cmd(help)).
+cmd_handler("h",	one_word_cmd(help)).
+cmd_handler("help",	one_word_cmd(help)).
+cmd_handler("b",	browse_cmd).
+cmd_handler("browse",	browse_cmd).
+
+:- func one_word_cmd(user_command::in, list(string)::in) = (user_command::out)
+	is semidet.
+
+one_word_cmd(Cmd, []) = Cmd.
+
+:- func browse_cmd(list(string)::in) = (user_command::out) is semidet.
+
+browse_cmd([Arg]) = browse(ArgNum) :-
+	string__to_int(Arg, ArgNum).
 
 %-----------------------------------------------------------------------------%
 
--------------------------------------------------------------------------
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