home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.yorku.ca 2015
/
ftp.cs.yorku.ca.tar
/
ftp.cs.yorku.ca
/
pub
/
peter
/
SVT
/
clean.prolog
< prev
next >
Wrap
Text File
|
2002-07-29
|
3KB
|
99 lines
/*
clean.prolog cleans up a code fragment so that when converted to
term, Prolog will parse it.
contains various utilities used by both symbex and wp.
Version 1.1, March 28, 1998: added matchP, fixed clean so op can begin string.
1.4 brackets/3 turns array index expressions into terms.
string_to_term moved here (for both symbex & wp).
1.4.1, March 27: matchP and charsNoComm repaired, fixComment added.
:- module(cleanup, [clean/2, matchP/5, upto/5, break/2,
string_to_term/2]).
*/
:- ensure_loaded([library(basics), library(charsio), library(change_arg),
library(ctypes), library(lists)]).
clean(Old, New) :-
parseOld(Prefix, NewSubstring, Old, Rest),
clean(Rest, NewRest),
append([Prefix, NewSubstring, NewRest], New).
clean(Old, Old).
parseOld(Prefix, New) --> chars(Prefix), {matchOp(Old, New)}, Old.
string_to_term(String, Term) :-
clean(String, Cleaned),
brackets(Indexed, Cleaned, []),
chars_to_term(Indexed, Term1),
deIndex(Term1, Term).
deIndex(Expr, Final) :-
path_arg(Path, Expr, Term),
Term =.. [F, 'INDEX'(Index)],
change_path_arg(Path, Expr, Modified, array(F, Index)),
!,
deIndex(Modified, Final).
deIndex(Expr, Expr).
% common grammatical rules
chars([C]) --> [C].
chars([C | Rest]) --> [C], chars(Rest).
chars([]) --> [].
upto([], C, Stop) --> C, ! ; Stop, !, {fail}.
upto([X|Rest], C, Stop) --> [X], upto(Rest, C, Stop).
% matches up to C inclusive
% requires a C in the string being matched to succeed
% requires a Stop to prevent scanning to end of string
% before failing.
skip([X | Rest] , C) -->
[X], { [X] \== C }, skip(Rest, C).
% chars up to and not including a stop character C
% scans to end of the string if no C.
skip([], _) --> [].
break --> [C], {is_space(C)}, break ; [].
%==============================================
matchOp("!=", "<>").
matchOp("!", " not ").
matchOp("||", " or ").
matchOp("&&", " and ").
matchOp("==", " = ").
matchOp("0x", "16'").
matchP(LB, RB, String) --> LB, !, matched(LB, RB, String), RB.
matched(LB, RB, String) -->
matchP(LB, RB, S),
(
{append([LB, S, RB], String)}
;
matched(LB, RB, S1), {append([LB, S, RB, S1], String)}
).
matched(_, _, []) --> [].
matched(LB, RB, [C | Rest]) --> [C], matched(LB, RB, Rest).
bracket(E) -->
"[", matched("[", "]", Exp), "]",
{ phrase(brackets(E), Exp)}.
brackets(S) -->
chars(Pre), bracket(E), brackets(Post),
{append([Pre, "('INDEX'(", E, "))", Post], S)}.
brackets(S) --> chars(S).
fixComment(Fixed) -->
upto(Pre, "//", "//"), break, "{",
upto(Comment, "}", "}"),
fixComment(F1),
{ append([Pre, "//", [12], Comment, [12], F1], Fixed)}.
fixComment(Fixed) --> chars(Fixed).