-
Notifications
You must be signed in to change notification settings - Fork 15
/
Parser.pro
66 lines (56 loc) · 1.13 KB
/
Parser.pro
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
% A root tree is a regular tree with an opening and closing tags and a forest in between
tree(tree(N,AL,TL)) -->
layout,
string("<"),
name(N),
attrs(AL,_),
string(">"),
trees(TL),
string("</"),
name(N),
string(">"),
layout.
% A non-empty forest
trees([H|T]) -->
tree(H),
trees(T).
% An empty forest
trees([]) --> [].
% Attribute list
attrs([H|T],[N|U]) -->
layout,
attr(H),
attrs(T,U),
{
H = [N,_],
not(member(N,U))
}.
attrs([],[]) --> [].
% Attribute
attr([N,V]) -->
name(N),
string("="),
value(V).
% Bare strings
string([],X,X).
string([H|T1],[H|T2],X) :- string(T1,T2,X).
% Consuming spaces
layout --> [0' ], layout. % space '
layout --> [0' ], layout. % tab '
layout --> [10], layout. % newline
layout --> [].
% Only lowcased tag names are allowed
name(N) -->
letter(H), letters(T),
{ atom_codes(N,[H|T]) }.
% Bare letters
letters([H|T]) --> letter(H), letters(T).
letters([]) --> [].
letter(H,[H|T],T) :- H >= 0'a, H =< 0'z.
value(V) -->
char(H), chars(T),
{ atom_codes(V,[H|T]) }.
chars([H|T]) --> char(H), chars(T).
chars([]) --> [].
char(H,[H|T],T) :- H >= 0'a, H =< 0'z.
char(H,[H|T],T) :- H >= 0'0, H =< 0'9.