From bca585386877d125019b332190d4868c28acff26 Mon Sep 17 00:00:00 2001 From: eudoxia Date: Fri, 8 Apr 2022 00:39:50 -0400 Subject: software/literary: docs syncing --- software/literary/data.html | 112 ++++++++++++++++++++++++++++-------------- software/literary/log.html | 1 - software/literary/tangle.html | 1 - software/literary/weave.html | 1 - 4 files changed, 75 insertions(+), 40 deletions(-) diff --git a/software/literary/data.html b/software/literary/data.html index 66ecb30..7b5e079 100755 --- a/software/literary/data.html +++ b/software/literary/data.html @@ -10,7 +10,6 @@ data.lp — DistressNetwork° - @@ -40,6 +39,8 @@ (Section searching function) +(Command type detection function) + (Parsing functions) (Code generation functions) @@ -146,7 +147,7 @@ pub const LineRange = struct {

Error set:

-
pub const Errors = error{
+
pub const Errors = error {
     UnexpectedStart,
     UnexpectedEnd,
     DereferenceLimit,
@@ -239,6 +240,27 @@ while (iterator.next()) |line| {
 
 

Parsing

+

We first define a function which, for a given line, determines whether it consists of a formatting command, and which type of command it contains. This is done in order to enable the use of switch statements in later functions using this routine.

+ +

Command type detection function:

+ +
const CommandType = enum { start, add, end, ref, none };
+
+fn command_type(line: []const u8) CommandType {
+    if (std.mem.startsWith(u8, line, k_start)) {
+        return .start;
+    } else if (std.mem.startsWith(u8, line, k_add)) {
+        return .add;
+    } else if (std.mem.eql(u8, line, k_end)) {
+        return .end;
+    } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
+        return .ref;
+    } else {
+        return .none;
+    }
+}
+
+

We then define the parsing functions, consisting of the main parse function which builds the list of Sections, and its auxiliary parse_code subroutine which builds the contents of each CodeSection.

Parsing functions:

@@ -279,15 +301,20 @@ fn parse_code(lines: [][]const u8, index: u32, alloc: Allocator) !CodeReturn {
var i: u32 = 0;
 while (i < lines.len) {
     const line = lines[i];
-    if (std.mem.startsWith(u8, line, k_start)) {
-        (Add new section)
-    } else if (std.mem.startsWith(u8, line, k_add)) {
-        (Append to section)
-    } else if (std.mem.eql(u8, line, k_end)) {
-        log(.err, "line {d}: unexpected section end", .{i + 1});
-        return error.UnexpectedEnd;
-    } else {
-        i += 1;
+    switch (command_type(line)) {
+        .start  => {
+            (Add new section)
+        },
+        .add    => {
+            (Append to section)
+        },
+        .end    => {
+            log(.err, "line {d}: unexpected section end", .{i + 1});
+            return error.UnexpectedEnd;
+        },
+        else    => {
+            i += 1;
+        },
     }
 }
 
@@ -330,15 +357,20 @@ i = section.index;
var i = index;
 while (i < lines.len) {
     const line = lines[i];
-    if (std.mem.startsWith(u8, line, k_start) or std.mem.startsWith(u8, line, k_add)) {
-        log(.err, "line {d}: unexpected section start", .{i + 1});
-        return error.UnexpectedStart;
-    } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
-        (Add reference)
-    } else if (std.mem.eql(u8, line, k_end)) {
-        break;
-    } else {
-        (Add literal range)
+    switch (command_type(line)) {
+        .start, .add    => {
+            log(.err, "line {d}: unexpected section start", .{i + 1});
+            return error.UnexpectedStart;
+        },
+        .ref    => {
+            (Add reference)
+        },
+        .end    => {
+            break;
+        },
+        else    => {
+            (Add literal range)
+        },
     }
 }
 
@@ -445,16 +477,22 @@ try buffer.appendSlice(code); or std.mem.startsWith(u8, line, kc_end) or std.mem.startsWith(u8, line, kc_ref)) { continue; - } else if (std.mem.startsWith(u8, line, k_start)) { - (Format starting command) - } else if (std.mem.startsWith(u8, line, k_add)) { - (Format appending command) - } else if (std.mem.startsWith(u8, line, k_end)) { - (Format ending command) - } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) { - (Format reference command) - } else { - try buffer.append(line); + } else switch (command_type(line)) { + .start => { + (Format starting command) + }, + .add => { + (Format appending command) + }, + .ref => { + (Format reference command) + }, + .end => { + (Format ending command) + }, + else => { + try buffer.append(line); + }, } } @@ -488,13 +526,6 @@ try buffer.append(try std.mem.join(alloc, current_name, conf_start)); try buffer.append(try std.mem.join(alloc, current_name, conf_add));
-

Processing a section ending command, however, does not require updating the current section name.

- -

Format ending command:

- -
try buffer.append(try std.mem.join(alloc, current_name, conf_end));
-
-

To process a reference command, the index of the reference command keyword is first extracted. Then the formatted reference string is created, to which the reference command line’s leading whitespace is prepended (to preserve indentation).

Format reference command:

@@ -503,6 +534,13 @@ try buffer.append(try std.mem.join(alloc, current_name, conf_add)); const ref = try std.mem.join(alloc, line[(start + k_ref.len)..], conf_ref); try buffer.append(try std.mem.concat(alloc, u8, &[_][]const u8{ line[0..start], ref }));
+ +

Processing a section ending command is performed similarly to the starting and appending commands, however it does not require updating the current section name.

+ +

Format ending command:

+ +
try buffer.append(try std.mem.join(alloc, current_name, conf_end));
+
diff --git a/software/literary/log.html b/software/literary/log.html index 9ebf2dc..89f861a 100755 --- a/software/literary/log.html +++ b/software/literary/log.html @@ -10,7 +10,6 @@ log.lp — DistressNetwork° - diff --git a/software/literary/tangle.html b/software/literary/tangle.html index 1559d31..3fd4c02 100755 --- a/software/literary/tangle.html +++ b/software/literary/tangle.html @@ -10,7 +10,6 @@ tangle.lp — DistressNetwork° - diff --git a/software/literary/weave.html b/software/literary/weave.html index 9edd4f1..81ef1d6 100755 --- a/software/literary/weave.html +++ b/software/literary/weave.html @@ -10,7 +10,6 @@ weave.lp — DistressNetwork° - -- cgit v1.2.3