Daniel@0
|
1 # module uripattern
|
Daniel@0
|
2
|
Daniel@0
|
3 by Samer Abdallah, UCL, 2014
|
Daniel@0
|
4
|
Daniel@0
|
5
|
Daniel@0
|
6 This directory contains several alternative implementations of a small URI grammar
|
Daniel@0
|
7 which supports both generation and parsing (pattern-matching) of URIs.
|
Daniel@0
|
8
|
Daniel@0
|
9 There are two variants of the language. The earlier design is quite flexible but
|
Daniel@0
|
10 leads to heavily non-deterministic parsing, which is perhaps not what one wants
|
Daniel@0
|
11 for URI computations which might potentially be called very frequently. The later
|
Daniel@0
|
12 design reduces or eliminates this non-determinism, depending on the implementatation.
|
Daniel@0
|
13
|
Daniel@0
|
14 ### Grammar
|
Daniel@0
|
15
|
Daniel@0
|
16 We describe the pattern grammar in terms of an algebraic datatype for the pattern language.
|
Daniel@0
|
17
|
Daniel@0
|
18
|
Daniel@0
|
19 uri_pattern ---> prefix:uri_part
|
Daniel@0
|
20 ; uri_part.
|
Daniel@0
|
21
|
Daniel@0
|
22 P :: prefix :- rdf_current_prefix(P). % any registered prefix atom
|
Daniel@0
|
23 M :: macro_invocation :- uripattern:def(M,_).
|
Daniel@0
|
24
|
Daniel@0
|
25 uri_part ---> \macro_invocation
|
Daniel@0
|
26 ; uri_part/uri_part % slash separated concatenation
|
Daniel@0
|
27 ; uri_part+uri_part % direct concatenation
|
Daniel@0
|
28 ; num(number) % decimal repn of number
|
Daniel@0
|
29 ; enc(atomic) % HTML form-encoded atom
|
Daniel@0
|
30 ; atm(atomic) % any atomic value
|
Daniel@0
|
31 ; <any ground atom>
|
Daniel@0
|
32 .
|
Daniel@0
|
33
|
Daniel@0
|
34 The more deterministic grammar removes direct concatenation and arbitrary atom parsing
|
Daniel@0
|
35 but adds a tail/1 functor to capture all the remaining characters in the URI:
|
Daniel@0
|
36
|
Daniel@0
|
37 uri_part ---> \macro_invocation
|
Daniel@0
|
38 ; uri_part/uri_part % slash separated concatenation
|
Daniel@0
|
39 ; num(number) % decimal repn of number
|
Daniel@0
|
40 ; enc(atomic) % HTML form-encoded atom
|
Daniel@0
|
41 ; tail(atomic) % matches rest of URI
|
Daniel@0
|
42 ; <any ground atom>
|
Daniel@0
|
43 .
|
Daniel@0
|
44
|
Daniel@0
|
45 This grammar as written is not quite strict enough to enforce the rule that any tail/1
|
Daniel@0
|
46 term must be the last thing in the pattern. The implementations are slightly less strict
|
Daniel@0
|
47 than this this specification.
|
Daniel@0
|
48
|
Daniel@0
|
49
|
Daniel@0
|
50 ### Example
|
Daniel@0
|
51
|
Daniel@0
|
52 Given the following declarations:
|
Daniel@0
|
53
|
Daniel@0
|
54 :- rdf_register_prefix(eg,'http://wwww.example.org/').
|
Daniel@0
|
55 uripattern:def(matrix(X,Y),num(X)/num(Y)).
|
Daniel@0
|
56
|
Daniel@0
|
57 Then URIs can be generated as follows:
|
Daniel@0
|
58
|
Daniel@0
|
59 pattern_uri(eg:home,A).
|
Daniel@0
|
60 ~> A='http://www.example.org/home'
|
Daniel@0
|
61 pattern_uri(eg:files/tail('home/chomsky/.profile'),A).
|
Daniel@0
|
62 ~> A='http://www.example.org/files/home/chomsky/.profile'
|
Daniel@0
|
63 pattern_uri(eg:people/enc('Chomsky, N')/pets,A).
|
Daniel@0
|
64 ~> A='http://www.example.org/people/Chomsky%2c%20N/pets'
|
Daniel@0
|
65 pattern_uri(eg:chessboard/matrix(3,6),A).
|
Daniel@0
|
66 ~> A='http://www.example.org/chessboard/3/6'
|
Daniel@0
|
67
|
Daniel@0
|
68 URIs can also be parsed, eg as follows
|
Daniel@0
|
69
|
Daniel@0
|
70 pattern_uri(eg:files/tail(Path), 'http://www.example.org/files/home/chomsky/.profile').
|
Daniel@0
|
71 ~> Path=
|
Daniel@0
|
72 pattern_uri(eg:people/enc(Name)/pets, 'http://www.example.org/people/Chomsky%2c%20N/pets').
|
Daniel@0
|
73 ~> Name='Chomsky, N'
|
Daniel@0
|
74 pattern_uri(eg:chessboard/matrix(X,Y), 'http://www.example.org/chessboard/3/6').
|
Daniel@0
|
75 ~> X=3, Y=6
|
Daniel@0
|
76
|
Daniel@0
|
77
|
Daniel@0
|
78 ### Implementations
|
Daniel@0
|
79
|
Daniel@0
|
80 There are several implementations:
|
Daniel@0
|
81
|
Daniel@0
|
82 uripattern\_constraint : Works by splicing atoms and uses delayed goals to handle arbitrary modes.
|
Daniel@0
|
83
|
Daniel@0
|
84 uripattern\_string : Works by splicing strings.
|
Daniel@0
|
85
|
Daniel@0
|
86 uripattern\_atom : Works by splicing atoms.
|
Daniel@0
|
87
|
Daniel@0
|
88 uripattern\_dcg : Converts URI to and from a list of codes and uses a DCG.
|
Daniel@0
|
89
|
Daniel@0
|
90 uripattern\_detdcg : A more deterministic DCG
|
Daniel@0
|
91
|
Daniel@0
|
92 uripattern\_detstring : A more deterministic version of string splicing.
|
Daniel@0
|
93
|
Daniel@0
|
94
|