-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch back to value, filter accorridng to type.
- Loading branch information
Showing
2 changed files
with
77 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
let rec methods_of_xml = function | ||
| Xml.PCData s -> ("text", Lang.string s) | ||
| Xml.Element (name, params, ([Xml.PCData s] as children)) -> | ||
(name, Lang.meth (Lang.string s) (xml_node ~params ~children)) | ||
| Xml.Element (name, params, children) -> | ||
( name, | ||
Lang.record ( | ||
(List.fold_left | ||
(fun methods el -> | ||
let (name, v) = methods_of_xml el in | ||
let v = | ||
match Methods.find_opt name methods with | ||
| None -> v | ||
| Some (`Tuple {value}) -> Value.make (`Tuple (value @[v])) | ||
| Some value -> (name, Value.make (`Tuple [value; v])) | ||
in | ||
Methods.add name value methods) | ||
Methods.append elements (methods_of_xml el)) | ||
(xml_node ~params ~children) | ||
children) | ||
|
||
and xml_node ~params ~children = | ||
Methods.from_list | ||
[ | ||
( "xml_params", | ||
Term.make | ||
~methods: | ||
(Methods.from_list | ||
(List.map (fun (k, v) -> (k, Term.make (`String v))) params)) | ||
Term.unit ); | ||
( "xml_children", | ||
Term.make (`Tuple (List.map (fun v -> term_of_xml v) children)) ); | ||
] | ||
|
||
and term_of_xml v = Term.make ~methods:(methods_of_xml v) `Null | ||
|
||
let _ = | ||
Lang.add_builtin "_internal_xml_parser_" ~category:`String ~flags:[`Hidden] | ||
~descr:"Internal xml parser" | ||
[ | ||
("type", Value.RuntimeType.t, None, Some "Runtime type"); | ||
("", Lang.string_t, None, None); | ||
] | ||
(Lang.univ_t ()) | ||
(fun p -> | ||
let s = Lang.to_string (List.assoc "" p) in | ||
let ty = Value.RuntimeType.of_value (List.assoc "type" p) in | ||
let ty = Type.fresh ty in | ||
try | ||
let xml = Xml.parse_string s in | ||
let tm = term_of_xml xml in | ||
Typechecking.check ~throw:(fun exn -> raise exn) tm; | ||
Typing.(ty <: tm.Term.t); | ||
Evaluation.eval tm | ||
with exn -> ( | ||
let bt = Printexc.get_raw_backtrace () in | ||
match exn with | ||
| _ -> | ||
Runtime_error.raise ~bt ~pos:(Lang.pos p) | ||
~message: | ||
(Printf.sprintf | ||
"Parse error: xml value cannot be parsed as type: %s" | ||
(Type.to_string ty)) | ||
"xml")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters