2 Author: Francis Giraldeau <francis.giraldeau@usherbrooke.ca>
4 Reference: http://www.w3.org/TR/2006/REC-xml11-20060816/
11 (************************************************************************
13 *************************************************************************)
15 let dels (s:string) = del s s
16 let spc = /[ \t\r\n]+/
17 let osp = /[ \t\r\n]*/
18 let sep_spc = del /[ \t\r\n]+/ " "
19 let sep_osp = del /[ \t\r\n]*/ ""
20 let sep_eq = del /[ \t\r\n]*=[ \t\r\n]*/ "="
22 let nmtoken = /[a-zA-Z:_][a-zA-Z0-9:_.-]*/
23 let word = /[a-zA-Z][a-zA-Z0-9._-]*/
24 let char = /.|(\r?\n)/
25 (* if we hide the quotes, then we can only accept single or double quotes *)
26 (* otherwise a put ambiguity is raised *)
27 let sto_dquote = dels "\"" . store /[^"]*/ . dels "\"" (* " *)
28 let sto_squote = dels "'" . store /[^']*/ . dels "'"
30 let comment = [ label "#comment" .
32 store /([^-]|-[^-])*/ .
35 let pi_target = nmtoken - /[Xx][Mm][Ll]/
36 let empty = Util.empty
37 let del_end = del />[\r?\n]?/ ">\n"
38 let del_end_simple = dels ">"
40 (* This is siplified version of processing instruction
41 * pi has to not start or end with a white space and the string
42 * must not contain "?>". We restrict too much by not allowing any
45 let pi = /[^ \r\n\t]|[^ \r\n\t][^?>]*[^ \r\n\t]/
47 (************************************************************************
49 *************************************************************************)
52 let decl = [ label "#decl" . sep_spc .
53 store /[^> \t\n\r]|[^> \t\n\r][^>\t\n\r]*[^> \t\n\r]/ ]
55 let decl_def (r:regexp) (b:lens) = [ dels "<" . key r .
56 sep_spc . store nmtoken .
57 b . sep_osp . del_end_simple ]
59 let elem_def = decl_def /!ELEMENT/ decl
61 let enum = "(" . osp . nmtoken . ( osp . "|" . osp . nmtoken )* . osp . ")"
63 let att_type = /CDATA|ID|IDREF|IDREFS|ENTITY|ENTITIES|NMTOKEN|NMTOKENS/ |
66 let id_def = [ sep_spc . key /PUBLIC/ .
67 [ label "#literal" . sep_spc . sto_dquote ]* ] |
68 [ sep_spc . key /SYSTEM/ . sep_spc . sto_dquote ]
70 let notation_def = decl_def /!NOTATION/ id_def
72 let att_def = counter "att_id" .
73 [ sep_spc . seq "att_id" .
74 [ label "#name" . store word . sep_spc ] .
75 [ label "#type" . store att_type . sep_spc ] .
76 ([ key /#REQUIRED|#IMPLIED/ ] |
77 [ label "#FIXED" . del /#FIXED[ \r\n\t]*|/ "" . sto_dquote ]) ]*
79 let att_list_def = decl_def /!ATTLIST/ att_def
82 let literal (lbl:string) = [ sep_spc . label lbl . sto_dquote ] in
85 | [ sep_spc . key /SYSTEM/ . literal "#systemliteral" ]
86 | [ sep_spc . key /PUBLIC/ . literal "#pubidliteral"
87 . literal "#systemliteral" ] )
89 let decl_def_item = elem_def | entity_def | att_list_def | notation_def
91 let decl_outer = sep_osp . del /\[[ \n\t\r]*/ "[\n" .
92 (decl_def_item . sep_osp )* . dels "]"
94 (* let dtd_def = [ sep_spc . key "SYSTEM" . sep_spc . sto_dquote ] *)
96 let doctype = decl_def /!DOCTYPE/ (decl_outer|id_def)
98 (* General shape of an attribute
99 * q is the regexp matching the quote character for the value
100 * qd is the default quote character
101 * brx is what the actual attribute value must match *)
102 let attval (q:regexp) (qd:string) (brx:regexp) =
103 let quote = del q qd in
104 let body = store brx in
105 [ sep_spc . key nmtoken . sep_eq . square quote body quote ]
107 (* We treat attributes according to one of the following three patterns:
108 attval1 : values that must be quoted with single quotes
109 attval2 : values that must be quoted with double quotes
110 attval3 : values that can be quoted with either *)
112 let attval1 = attval "'" "'" /[^']*"[^']*/ in (* " *)
113 let attval2 = attval "\"" "\"" /[^"]*'[^"]*/ in
114 let attval3 = attval /['"]/ "\"" /(\\\\|[^'\"])*/ in (* " *)
115 [ label "#attribute" . (attval1|attval2|attval3)+ ]
117 let prolog = [ label "#declaration" .
124 (************************************************************************
126 *************************************************************************)
128 (* we consider entities as simple text *)
129 let text_re = /[^<]+/ - /([^<]*\]\]>[^<]*)/
130 let text = [ label "#text" . store text_re ]
131 let cdata = [ label "#CDATA" . dels "<![CDATA[" .
132 store (char* - (char* . "]]>" . char*)) . dels "]]>" ]
134 (* the value of nmtoken_del is always the nmtoken_key string *)
135 let nmtoken_key = key nmtoken
136 let nmtoken_del = del nmtoken "a"
138 let element (body:lens) =
139 let h = attributes? . sep_osp . dels ">" . body* . dels "</" in
140 [ dels "<" . square nmtoken_key h nmtoken_del . sep_osp . del_end ]
142 let empty_element = [ dels "<" . nmtoken_key . value "#empty" .
143 attributes? . sep_osp . del /\/>[\r?\n]?/ "/>\n" ]
145 let pi_instruction = [ dels "<?" . label "#pi" .
146 [ label "#target" . store pi_target ] .
147 [ sep_spc . label "#instruction" . store pi ]? .
148 sep_osp . del /\?>/ "?>" ]
150 (* Typecheck is weaker on rec lens, detected by unfolding *)
152 let content1 = element text
153 let rec content2 = element (content1|text|comment)
156 let rec content = element (text|comment|content|empty_element|pi_instruction|cdata)
158 (* Constraints are weaker here, but it's better than being too strict *)
159 let doc = (sep_osp . (prolog | comment | doctype | pi_instruction))* .
160 ((sep_osp . content) | (sep_osp . empty_element)) .
161 (sep_osp . (comment | pi_instruction ))* . sep_osp
163 let lns = doc | Util.empty?
165 let filter = (incl "/etc/xml/*.xml")
166 . (incl "/etc/xml/catalog")
168 let xfm = transform lns filter