aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2023-03-28 12:10:35 -0400
committerDavid Robillard <d@drobilla.net>2023-04-05 09:45:15 -0400
commitbd2d7f510576c2185ddb0e9b8872079dba2a05c7 (patch)
treeab02e921f11db296fc77ffbbd981bad51e9648bf
parente970e63146fb5d8de511104eba7aef5319e8653b (diff)
downloadserd-bd2d7f510576c2185ddb0e9b8872079dba2a05c7.tar.gz
serd-bd2d7f510576c2185ddb0e9b8872079dba2a05c7.tar.bz2
serd-bd2d7f510576c2185ddb0e9b8872079dba2a05c7.zip
Fix pretty-printing of anonymous subjects
-rw-r--r--src/writer.c12
-rw-r--r--test/pretty/inline-blank-subject.ttl5
-rw-r--r--test/pretty/inline-blanks-and-lists.ttl61
-rw-r--r--test/pretty/inline-list-subject.ttl6
-rw-r--r--test/pretty/list-subject.ttl7
-rw-r--r--test/pretty/manifest.ttl35
-rw-r--r--test/pretty/nested-list-subject.ttl11
7 files changed, 134 insertions, 3 deletions
diff --git a/src/writer.c b/src/writer.c
index 3433a924..27a6df83 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -43,6 +43,7 @@ typedef enum {
SEP_S_P, ///< Between a subject and predicate (whitespace)
SEP_P_O, ///< Between a predicate and object (whitespace)
SEP_ANON_BEGIN, ///< Start of anonymous node ('[')
+ SEP_ANON_S_P, ///< Between anonymous subject and predicate (whitespace)
SEP_ANON_END, ///< End of anonymous node (']')
SEP_LIST_BEGIN, ///< Start of list ('(')
SEP_LIST_SEP, ///< List separator (whitespace)
@@ -68,6 +69,7 @@ static const SepRule rules[] = {{NULL, 0, 0, 0, 0},
{NULL, 0, 0, 1, 0},
{" ", 1, 0, 0, 0},
{"[", 1, 0, 1, 1},
+ {NULL, 0, 0, 0, 0},
{"]", 1, 1, 0, 0},
{"(", 1, 0, 0, 0},
{NULL, 0, 0, 1, 0},
@@ -692,7 +694,6 @@ write_blank(SerdWriter* const writer,
assert(writer->list_depth == 0);
copy_node(&writer->list_subj, node);
++writer->list_depth;
- ++writer->indent;
return write_sep(writer, SEP_LIST_BEGIN);
}
@@ -892,8 +893,13 @@ serd_writer_write_statement(SerdWriter* writer,
}
write_node(writer, subject, NULL, NULL, FIELD_SUBJECT, flags);
- ++writer->indent;
- write_sep(writer, SEP_S_P);
+ if ((flags & SERD_ANON_S_BEGIN)) {
+ write_sep(writer, SEP_ANON_S_P);
+ } else {
+ ++writer->indent;
+ write_sep(writer, SEP_S_P);
+ }
+
} else {
++writer->indent;
}
diff --git a/test/pretty/inline-blank-subject.ttl b/test/pretty/inline-blank-subject.ttl
new file mode 100644
index 00000000..8f5de39c
--- /dev/null
+++ b/test/pretty/inline-blank-subject.ttl
@@ -0,0 +1,5 @@
+@prefix eg: <http://example.org/eg#> .
+
+[
+ a eg:BlankSubject
+] eg:isA eg:Blank .
diff --git a/test/pretty/inline-blanks-and-lists.ttl b/test/pretty/inline-blanks-and-lists.ttl
new file mode 100644
index 00000000..c57482d3
--- /dev/null
+++ b/test/pretty/inline-blanks-and-lists.ttl
@@ -0,0 +1,61 @@
+@prefix eg: <http://example.org/eg#> .
+
+[
+ a eg:TopBlank
+] .
+
+[
+ a []
+] .
+
+[
+ a ()
+] .
+
+[]
+ a [] ,
+ [] .
+
+[]
+ a [] ,
+ () .
+
+[]
+ a () ,
+ [] .
+
+[]
+ a () ,
+ () .
+
+eg:s2
+ a eg:SparseThing ,
+ [] ,
+ () ,
+ [] ,
+ [] ,
+ () ,
+ () ,
+ <http://example.org/uri21> ,
+ [] ,
+ <http://example.org/uri22> ,
+ () ,
+ <http://example.org/uri23> ,
+ [] ,
+ eg:o21 ,
+ [] ,
+ eg:o22 ,
+ () ,
+ eg:o23 .
+
+eg:s3
+ a eg:Thing ;
+ eg:p1 eg:o1 ,
+ [
+ a eg:SubThing ;
+ eg:p2 eg:o2
+ ] , [
+ a eg:OtherSubThing ;
+ eg:p3 eg:o3
+ ] ;
+ eg:p4 eg:o4 .
diff --git a/test/pretty/inline-list-subject.ttl b/test/pretty/inline-list-subject.ttl
new file mode 100644
index 00000000..a3f8ac18
--- /dev/null
+++ b/test/pretty/inline-list-subject.ttl
@@ -0,0 +1,6 @@
+@prefix eg: <http://example.org/eg#> .
+
+(
+ eg:item1
+ eg:item2
+) eg:isA eg:List .
diff --git a/test/pretty/list-subject.ttl b/test/pretty/list-subject.ttl
new file mode 100644
index 00000000..927f56f3
--- /dev/null
+++ b/test/pretty/list-subject.ttl
@@ -0,0 +1,7 @@
+@prefix eg: <http://example.org/> .
+
+(
+ "apple"
+ "banana"
+ "cherry"
+) a eg:ExampleList .
diff --git a/test/pretty/manifest.ttl b/test/pretty/manifest.ttl
index a78aa943..1f653d65 100644
--- a/test/pretty/manifest.ttl
+++ b/test/pretty/manifest.ttl
@@ -19,9 +19,13 @@
<#empty-list-subject-and-object>
<#ext-named-blank>
<#graph-abbreviation>
+ <#inline-blank-subject>
+ <#inline-blanks-and-lists>
+ <#inline-list-subject>
<#langtags>
<#list-in-object>
<#list-object>
+ <#list-subject>
<#local-name-escapes>
<#long-string-escapes>
<#long-string-quotes>
@@ -30,6 +34,7 @@
<#nested-list-object>
<#short-string-escapes>
<#uri-escapes>
+ <#nested-list-subject>
) .
<#abbreviation>
@@ -104,6 +109,24 @@
mf:name "graph-abbreviation" ;
mf:result <graph-abbreviation.trig> .
+<#inline-blank-subject>
+ a rdft:TestTurtleEval ;
+ mf:action <inline-blank-subject.ttl> ;
+ mf:name "inline-blank-subject" ;
+ mf:result <inline-blank-subject.ttl> .
+
+<#inline-blanks-and-lists>
+ a rdft:TestTurtleEval ;
+ mf:action <inline-blanks-and-lists.ttl> ;
+ mf:name "inline-blanks-and-lists" ;
+ mf:result <inline-blanks-and-lists.ttl> .
+
+<#inline-list-subject>
+ a rdft:TestTurtleEval ;
+ mf:action <inline-list-subject.ttl> ;
+ mf:name "inline-list-subject" ;
+ mf:result <inline-list-subject.ttl> .
+
<#langtags>
a rdft:TestTurtleEval ;
mf:action <langtags.ttl> ;
@@ -122,6 +145,12 @@
mf:name "list-object" ;
mf:result <list-object.ttl> .
+<#list-subject>
+ a rdft:TestTurtleEval ;
+ mf:action <list-subject.ttl> ;
+ mf:name "list-subject" ;
+ mf:result <list-subject.ttl> .
+
<#local-name-escapes>
a rdft:TestTurtleEval ;
mf:action <local-name-escapes.ttl> ;
@@ -158,6 +187,12 @@
mf:name "nested-list-object" ;
mf:result <nested-list-object.ttl> .
+<#nested-list-subject>
+ a rdft:TestTurtleEval ;
+ mf:action <nested-list-subject.ttl> ;
+ mf:name "nested-list-subject" ;
+ mf:result <nested-list-subject.ttl> .
+
<#short-string-escapes>
a rdft:TestTurtleEval ;
mf:action <short-string-escapes.ttl> ;
diff --git a/test/pretty/nested-list-subject.ttl b/test/pretty/nested-list-subject.ttl
new file mode 100644
index 00000000..128197c0
--- /dev/null
+++ b/test/pretty/nested-list-subject.ttl
@@ -0,0 +1,11 @@
+@prefix eg: <http://example.org/> .
+
+(
+ (
+ eg:l1e1
+ eg:l1e2
+ ) (
+ eg:l2e1
+ eg:l2e2
+ )
+) a eg:ExampleList .