From 77f8d52271a12a8b498afe2718d91e4e992b3a31 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 15 May 2022 10:19:00 +0200 Subject: [PATCH 1/5] use phrase_from_file/2 directly on the file name --- src/lib/sgml.pl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/sgml.pl b/src/lib/sgml.pl index 0289278e..bb1b58c3 100644 --- a/src/lib/sgml.pl +++ b/src/lib/sgml.pl @@ -70,9 +70,8 @@ load_structure_([C|Cs], [E], Options, What) :- load_(What, [C|Cs], E, Options). load_structure_(file(Fs), [E], Options, What) :- must_be(list, Options), - must_be(list, Fs), - atom_chars(File, Fs), - once(phrase_from_file(seq(Cs), File)), + must_be(chars, Fs), + once(phrase_from_file(seq(Cs), Fs)), load_(What, Cs, E, Options). load_structure_(stream(Stream), [E], Options, What) :- must_be(list, Options), From dff56643f08b397becedf0681db53525518ee00e Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 15 May 2022 10:23:00 +0200 Subject: [PATCH 2/5] clarify comment --- src/lib/sgml.pl | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib/sgml.pl b/src/lib/sgml.pl index bb1b58c3..26c917f5 100644 --- a/src/lib/sgml.pl +++ b/src/lib/sgml.pl @@ -10,9 +10,11 @@ These predicates parse HTML and XML documents, respectively. - Source must be a stream, specified as stream(S), or a file, - specified as file(Name), where Name is a list of characters, or a - list of characters with the document contents. + Source must be one of: + + - a list of characters with the document contents + - stream(S), specifying a stream S from which to read the content + - file(Name), where Name is a list of characters specifying a file name. Es is unified with the abstract syntax tree of the parsed document, represented as a list of elements where each is of the form: From 609a3a229fbc8a1fa21b78efc4b572b611a2c1b3 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 15 May 2022 10:25:41 +0200 Subject: [PATCH 3/5] strengthen and improve type checks --- src/lib/sgml.pl | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lib/sgml.pl b/src/lib/sgml.pl index 26c917f5..5b5e1acb 100644 --- a/src/lib/sgml.pl +++ b/src/lib/sgml.pl @@ -1,6 +1,6 @@ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Predicates for parsing HTML and XML documents. - Written June 2020 by Markus Triska (triska@metalevel.at) + Written 2020-2022 by Markus Triska (triska@metalevel.at) Part of Scryer Prolog. Currently, two predicates are provided: @@ -63,20 +63,32 @@ :- use_module(library(charsio)). load_html(Source, Es, Options) :- + must_be_source(Source, load_html/3), + must_be(list, Options), load_structure_(Source, Es, Options, html). load_xml(Source, Es, Options) :- + must_be_source(Source, load_xml/3), + must_be(list, Options), load_structure_(Source, Es, Options, xml). +must_be_source(Source, Context) :- + ( var(Source) -> instantiation_error(Context) + ; is_sgml_source(Source) -> true + ; domain_error(sgml_source, Source, Context) + ). + +is_sgml_source(file(Fs)) :- must_be(chars, Fs). +is_sgml_source(stream(_)). +is_sgml_source([]). +is_sgml_source([C|Cs]) :- must_be(chars, [C|Cs]). + load_structure_([], [], _, _). load_structure_([C|Cs], [E], Options, What) :- load_(What, [C|Cs], E, Options). load_structure_(file(Fs), [E], Options, What) :- - must_be(list, Options), - must_be(chars, Fs), once(phrase_from_file(seq(Cs), Fs)), load_(What, Cs, E, Options). load_structure_(stream(Stream), [E], Options, What) :- - must_be(list, Options), get_n_chars(Stream, _, Cs), load_(What, Cs, E, Options). From 1810dabc1436c891246b1ebf15774c1b512ca9f9 Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 15 May 2022 10:55:27 +0200 Subject: [PATCH 4/5] clarify the representation --- src/lib/sgml.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/sgml.pl b/src/lib/sgml.pl index 5b5e1acb..c6095a92 100644 --- a/src/lib/sgml.pl +++ b/src/lib/sgml.pl @@ -21,7 +21,7 @@ * a list of characters, representing text * element(Name, Attrs, Children) - - Name is the name of the tag + - Name, an atom, is the name of the tag - Attrs is a list of Key=Value pairs: Key is an atom, and Value is a list of characters - Children is a list of elements as specified here. From 18c52e076faf4e879519d4dd7a40675832cba3fb Mon Sep 17 00:00:00 2001 From: Markus Triska Date: Sun, 15 May 2022 10:55:56 +0200 Subject: [PATCH 5/5] indent enumerations --- src/lib/sgml.pl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib/sgml.pl b/src/lib/sgml.pl index c6095a92..a0370e8c 100644 --- a/src/lib/sgml.pl +++ b/src/lib/sgml.pl @@ -5,26 +5,26 @@ Currently, two predicates are provided: - - load_html(+Source, -Es, +Options) - - load_xml(+Source, -Es, +Options) + - load_html(+Source, -Es, +Options) + - load_xml(+Source, -Es, +Options) These predicates parse HTML and XML documents, respectively. Source must be one of: - - a list of characters with the document contents - - stream(S), specifying a stream S from which to read the content - - file(Name), where Name is a list of characters specifying a file name. + - a list of characters with the document contents + - stream(S), specifying a stream S from which to read the content + - file(Name), where Name is a list of characters specifying a file name. Es is unified with the abstract syntax tree of the parsed document, represented as a list of elements where each is of the form: - * a list of characters, representing text - * element(Name, Attrs, Children) - - Name, an atom, is the name of the tag - - Attrs is a list of Key=Value pairs: - Key is an atom, and Value is a list of characters - - Children is a list of elements as specified here. + * a list of characters, representing text + * element(Name, Attrs, Children) + - Name, an atom, is the name of the tag + - Attrs is a list of Key=Value pairs: + Key is an atom, and Value is a list of characters + - Children is a list of elements as specified here. Currently, Options are ignored. In the future, more options may be provided to control parsing.