31
31
"Caml code not allowed in configuration file. Use $$ to escape $." ;
34
module B = Xmllexer.BasicTypes;
37
{ stream : Stream.t (B.token * Loc.t); stack : Stack.t B.token; loc : Loc.t
40
[ EndOfTagExpected of string
42
exception Internal_error of error_msg;
45
(* Stack - the type of s is state *)
47
try ((Stack.pop s.stack), s)
50
let (t, l) = Stream.next s.stream
51
in (t, { stream = s.stream; stack = s.stack; loc = l; }) ];
52
value push t s = Stack.push t s.stack;
54
(* Convert a stream of tokens into an xml tree list *)
55
value rec read_nodes s acc =
57
[ (B.Comment _, s) -> read_nodes s acc
58
| (B.Whitespace _, s) -> read_nodes s acc
59
| (B.PCData pcdata, s) -> read_nodes s [(PCData pcdata)::acc]
60
| (B.Tag (tag, attlist, closed), s) ->
62
[ True -> read_nodes s [Element (tag, (read_attlist s attlist), [])::acc]
63
| False ->read_nodes s
64
[Element (tag, (read_attlist s attlist), (read_elems ~tag s))::acc]
66
| (B.CamlExpr _, _) | (B.CamlString _, _)|(B.CamlList _, _) ->
67
raise (Xml_parser_error nocaml_msg)
68
| (B.Eof, _)|(B.Endtag _,_) as t ->
69
do { push (fst t) s; List.rev acc}
72
and read_elems ?tag s =
73
let elems = read_nodes s [] in
75
[ (B.Endtag s, _) when (Some s) = tag -> elems
76
| (B.Eof, _) when tag = None -> elems
79
[ None -> raise (Internal_error EOFExpected)
80
| Some s -> raise (Internal_error (EndOfTagExpected s)) ] ]
85
| [ `Attribute (`Attr a, `Val v) :: l ] ->
86
[ (a,v) :: (read_attlist s l) ]
87
| [ `Attribute (`CamlAttr _, `Val _) :: _ ] |
88
[ `Attribute (_, `CamlVal _) :: _ ] | [ `CamlList _ :: _ ] ->
89
raise (Xml_parser_error nocaml_msg)];
91
value to_expr_taglist stream loc =
92
let s = { stream = stream; stack = Stack.create (); loc = loc; } in
95
value rawxmlparser_file s =
34
(* We raise this error when parsing Ocsigen configuration files. *)
35
exception ParseException of (Xmllexer.lexing_error * Camlp4.PreCast.Loc.t) ;
37
value parse_error_to_string x loc =
38
Printf.sprintf "%s (%s)"
39
(Xmllexer.lex_error_to_string x) (Loc.to_string loc);
42
module LexerArg = struct
43
value error loc e = ParseException (e, loc);
45
type attr_name = [ = `AttrName of string ];
46
type attr_value = [ = `AttrVal of string ];
47
type attribute = [ = `Attribute of (attr_name * attr_value) ];
50
= `Tag of (string * (list attribute) * bool)
54
| `Whitespace of string
58
value parse_dollar_attrname c loc lexbuf =
59
raise (ParseException (Xmllexer.EAttributeNameExpected, loc));
60
value parse_dollar_attribute c loc lexbuf =
61
raise (ParseException (Xmllexer.EAttributeValueExpected, loc));
62
value parse_dollar_attrvalue = parse_dollar_attrname;
63
value parse_dollar_token c lexbuf = `PCData "$";
67
module Xmllexer = Xmllexer.Make (LexerArg);
70
stream : Stream.t (LexerArg.token * Loc.t);
71
stack : Stack.t LexerArg.token;
75
[ EndOfTagExpected of string
77
exception Internal_error of error_msg;
79
(* Stack - the type of s is state *)
81
try ((Stack.pop s.stack), s)
84
let (t, l) = Stream.next s.stream
85
in (t, { stream = s.stream; stack = s.stack; loc = l; }) ];
87
value push t s = Stack.push t s.stack;
89
(* Convert a stream of tokens into an xml tree list *)
90
value rec read_nodes s acc =
92
[ (`Comment _, s) -> read_nodes s acc
93
| (`Whitespace _, s) -> read_nodes s acc
94
| (`PCData pcdata, s) -> read_nodes s [(PCData pcdata)::acc]
95
| (`Tag ("xi:include",
96
[`Attribute (`AttrName "href", `AttrVal v)], True), s)->
97
let l = rawxmlparser_file v in
98
let acc = List.rev_append l acc in
100
| (`Tag ("xi:include", _, _), s) ->
101
raise (Xml_parser_error "Invalid syntax for inclusion directive")
102
| (`Tag (tag, attlist, closed), s) ->
104
[ True -> read_nodes s [Element (tag, (read_attlist attlist), [])::acc]
105
| False ->read_nodes s
106
[Element (tag, (read_attlist attlist), (read_elems ~tag s))::acc]
108
| (`Eof, _)|(`Endtag _,_) as t ->
109
do { push (fst t) s; List.rev acc}
112
and read_elems ?tag s =
113
let elems = read_nodes s [] in
115
[ (`Endtag s, _) when (Some s) = tag -> elems
116
| (`Eof, _) when tag = None -> elems
119
[ None -> raise (Internal_error EOFExpected)
120
| Some s -> raise (Internal_error (EndOfTagExpected s)) ] ]
122
and read_attlist = List.map (fun [`Attribute (`AttrName a, `AttrVal v) -> (a,v)])
124
and to_expr_taglist stream loc =
125
let s = { stream = stream; stack = Stack.create (); loc = loc; } in
128
and rawxmlparser_file s =
96
129
let chan = open_in s in
97
let loc = Loc.ghost in
98
let tree = to_expr_taglist (Xmllexer.from_stream loc True (Stream.of_channel chan)) loc
99
in do { close_in chan; tree };
131
let loc = Loc.mk s in
132
let tree = to_expr_taglist (Xmllexer.from_stream loc True (Stream.of_channel chan)) loc
133
in do { close_in chan; tree }
137
[ Sys_error s -> raise (Xml_parser_error s)
101
value rawxmlparser_string s =
142
and rawxmlparser_string s =
102
143
let loc = Loc.ghost in
103
144
to_expr_taglist (Xmllexer.from_string loc True s) loc;
105
146
value xmlparser rawxmlparser s = try (rawxmlparser s)
107
[Internal_error EOFExpected -> raise (Xml_parser_error "EOF expected")
108
|Internal_error (EndOfTagExpected s) -> raise (Xml_parser_error ("End of tag expected: "^s))]
148
[ ParseException (e, loc) ->
149
raise (Xml_parser_error (parse_error_to_string e loc))
150
| Internal_error EOFExpected ->
151
raise (Xml_parser_error "EOF expected")
152
| Internal_error (EndOfTagExpected s) ->
153
raise (Xml_parser_error ("End of tag expected: "^s))]
111
156
value xmlparser_file = xmlparser rawxmlparser_file;