-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathxml.doc.m
80 lines (62 loc) · 2.28 KB
/
xml.doc.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% Copyright (C) 2000, 2011 The University of Melbourne.
% Copyright (C) 2018, 2022 The Mercury team.
% This file is distributed under the terms specified in COPYING.LIB.
%---------------------------------------------------------------------------%
%
% Main author: [email protected].
%
%---------------------------------------------------------------------------%
:- module xml.doc.
:- interface.
:- import_module array.
:- import_module list.
:- import_module map.
%---------------------------------------------------------------------------%
:- type document
---> doc(
prestuff :: list(ref(content)),
root :: ref(content),
poststuff :: list(ref(content)),
content :: array(content)
).
:- type content
---> element(element)
; pi(string, string)
; comment(string)
; data(string).
:- type content_store
---> content(
e_next :: ref(content),
e_map :: map(ref(content), content)
).
:- type element
---> element(
elt_name :: string,
elt_attrs :: list(attribute),
elt_content :: list(ref(content))
).
:- type attribute
---> attribute(
attr_name :: string,
attr_value :: string
).
:- type ref(T) == int.
:- func ref(content_store, ref(content)) = content.
:- pred add(content::in, ref(content)::out,
content_store::in, content_store::out) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module int.
%---------------------------------------------------------------------------%
ref(Elems, Ref) = Elem :-
map.lookup(Elems ^ e_map, Ref, Elem).
add(Elem, Ref, !Elems) :-
Ref = !.Elems ^ e_next,
!Elems ^ e_next := Ref + 1,
Map0 = !.Elems ^ e_map,
map.set(Ref, Elem, Map0, Map),
!Elems ^ e_map := Map.