1:- module(ml,
2 [parse_bind_context/2, bind_context/3,
3 parse_markup_body/4, parse_markup_text/4, eval_markup_text/2,
4 parse_phrase_save/1,
5 act/3, act/4
6 ]). 7
9
10:- use_module(util(snippets)). 12
13 16
18
19expand_arg(X,Y,Z,U,V):- pac:expand_arg(X, Y, Z, U, V).
20
21parse_markup_text(X, A, B) :- once(parse_markup_text(X, [], A, B)).
22
35
36parse_markup_text(P, P, [], []).
37parse_markup_text([G|P], Q) --> parse_tag(G),
38 parse_markup_text(P, Q).
39parse_markup_text(P, Q) --> line_feed(P, R),
40 parse_markup_text(R, Q).
41
43parse_tag(com(G, Body)) --> "@", skip_markup_open(Tag_codes),
44 parse_markup_body(Body, []),
45 {
46 basic:herbrand_in_context(web, Tag_codes, G0), 47 expand_arg(G0, [], G, Aux, []),
48 maplist(assert, Aux)
49 }.
50
51
55eval_markup_text(X, Y) :- parse_markup_text(L, X, _),
56 once(bind_context((L, []), (Y, _))).
57
59act(G) --> act([], G).
60
62parse_phrase_save(X) :- herbrand_in_context(web, X, G0),
63 64 65 expand_arg(G0, [], G, Aux, []),
66 maplist(assert, Aux),
67 nb_setval(phrase_tag, G).
68
70parse_markup_body(X, A, B):- once(parse_markup_body(X, [], A, B)).
71
73parse_markup_body(X, X) --> markup_close.
74parse_markup_body([C|X], Y) --> parse_tag(C),
75 parse_markup_body(X, Y).
76parse_markup_body(X, Y) --> line_feed(X, Z),
77 parse_markup_body(Z, Y).
78
80line_feed([0'\n|X], X)--> "\n". 81line_feed([A|X], Y) --> [A], line_feed(X, Y).
82line_feed(X, X)--> [].
83
85skip_markup_open(X, A, B):- once(skip_markup_open(X, [], A, B)).
87skip_markup_open(X, X) --> ".\n". 88skip_markup_open([A|X], Y) --> [A], skip_markup_open(X, Y).
89skip_markup_open(X, X) --> [].
90
92markup_close --> "==", line_feed(_,[]).
93markup_close --> "@end", line_feed(_,[]).
94markup_close([], []). 95
104
109
110parse_bind_context(X, Y) :- append(Tag, [0'., 0'\n | Rest], X), !,
111 herbrand_in_context(web, Tag, G0),
112 expand_arg(G0, [], G, Aux, []),
113 maplist(assert, Aux),
114 once(bind_context((com(G, Rest), []), (Y, _))).
115
116act(E, G, X, Y):- once(bind_context(G, (X, E), (Y, _))).
117
118:- meta_predicate use_context(?, 2, ?). 119use_context(C, F, (X, C), (Y, C)):- call(F, X, Y).
120
124
125bind_context(([A|B], E), ([A0|B0], F)):-
126 bind_context((A, E), (A0, E0)),
127 bind_context((B, E0), (B0, F)).
128bind_context((com(F, A), E), (B, D)):-
129 bind_context((A, E), (A0, E0)),
130 bind_context(F, (A0, E0), (B, D)).
131bind_context(X, X).
132
136
137:- meta_predicate bind_context(:, ?, ?). 138
139bind_context(M:G) --> m_bind_context(G, M).
140bind_context(G) --> m_bind_context(G, []).
141
143m_bind_context(M:G, _) --> m_bind_context(G, M).
144m_bind_context((F,G), M)--> m_bind_context(F, M),
145 m_bind_context(G, M).
146m_bind_context({F}, []) --> {once(F)}.
147m_bind_context({F}, M) --> {M:once(F)}.
148m_bind_context(&(F), [])--> phrase(F).
149m_bind_context(&(F), M) --> phrase(M:F).
150m_bind_context(dcl(D), M) --> m_bind_context(obj(push(D)), M).
151m_bind_context(context(E), _, (X, E), (X, E)).
152m_bind_context(context(C, F), M, (X, C), (Y, C)):- once(x_phrase(F, M, X, Y)).
153m_bind_context(obj(F), _, (X, C), (X, D)) :- obj:obj(F, C, D).
154m_bind_context(get(A), _, (X, C), (X, D)) :- obj:obj_get(A, C, D).
155m_bind_context(put(A), _, (X, C), (X, D)) :- obj:obj_put(A, C, D).
156m_bind_context(save, _, (X, C), ([], D)) :- obj:obj_put([acc(X)], C, D).
157m_bind_context(restore, _, (_, C), (X, D)) :- obj:obj_pull([acc(X)], C, D).
158m_bind_context(F, M, (X, C), (Y, C)):- once(x_phrase(F, M, X, Y)).
159
161x_phrase((A,B), M, X, Y):- x_phrase(A, M, X, X0),
162 x_phrase(B, M, X0, Y).
163x_phrase(A;B, M, X, Y):-
164 ( x_phrase(A, M, X, Y)
165 ; x_phrase(B, M, X, Y)).
166x_phrase(\+(A), M, X, Y):- \+ x_phrase(A, M, X, Y).
167x_phrase(once(A), M, X, Y):- once(x_phrase(A, M, X, Y)).
168x_phrase(M:A, _, X, Y):- x_phrase(A, M, X, Y).
169x_phrase(F, [], X, Y):- call(F, X, Y).
170x_phrase(F, M, X, Y):- call(M:F, X, Y)