summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreudoxia <uplink@distress.network>2022-04-08 00:32:03 -0400
committereudoxia <uplink@distress.network>2022-04-08 00:32:03 -0400
commitaa372c0ed6ca775f603a535d8bad70f172751a29 (patch)
treef40a5bef7d1d114778bad928c50b60cead8de442
parentb72af93971d38b05bcae92b2246b6014b737cd5e (diff)
control flow restructuring; removed doc timestamps
-rw-r--r--.gitignore1
-rw-r--r--data.lp108
-rwxr-xr-xdoc/data.html112
-rw-r--r--doc/freestanding/data.html111
-rwxr-xr-xdoc/log.html1
-rwxr-xr-xdoc/tangle.html1
-rwxr-xr-xdoc/weave.html1
-rwxr-xr-xgen-docs.sh5
-rw-r--r--src/data.zig162
-rwxr-xr-xtanglebin16376 -> 16416 bytes
-rwxr-xr-xweavebin14672 -> 14672 bytes
11 files changed, 324 insertions, 178 deletions
diff --git a/.gitignore b/.gitignore
index 59f7796..b872cdc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
**/zig-cache
+/misc
diff --git a/data.lp b/data.lp
index 329cd14..ee40f5d 100644
--- a/data.lp
+++ b/data.lp
@@ -19,6 +19,8 @@ This file contains the various data processing-related constants and functions r
@= Section searching function
+@= Command type detection function
+
@= Parsing functions
@= Code generation functions
@@ -111,7 +113,7 @@ We also define the set of errors which may be encountered by the various process
- References to nonexistent section names or configuration commands.
@: Error set
-pub const Errors = error{
+pub const Errors = error {
UnexpectedStart,
UnexpectedEnd,
DereferenceLimit,
@@ -198,6 +200,26 @@ fn search(list: []Section, name: []const u8) !usize {
## 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 `Section`s, and its auxiliary `parse_code` subroutine which builds the contents of each `CodeSection`.
@: Parsing functions
@@ -235,15 +257,20 @@ The main parsing routine iterates over the list of lines, adding code sections w
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;
+ },
}
}
@.
@@ -283,15 +310,20 @@ The code parsing subroutine iterates over the list of lines similarly to the mai
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
+ },
}
}
@.
@@ -392,16 +424,22 @@ pub fn textgen(lines: [][]const u8, alloc: Allocator) ![][]const u8 {
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);
+ },
}
}
@@ -432,12 +470,6 @@ current_name = line[(k_add.len)..];
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
@@ -445,3 +477,9 @@ const start = std.mem.indexOf(u8, line, k_ref).?;
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/doc/data.html b/doc/data.html
index 8d58f42..7b5e079 100755
--- a/doc/data.html
+++ b/doc/data.html
@@ -10,7 +10,6 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<title>data.lp &mdash; DistressNetwork°</title>
-<meta http-equiv="last-modified" content="2022-04-07 11:09:31-04:00" />
<style>h3,.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-size: 1rem; font-weight: normal; font-style: italic;} h3 {margin: 1rem;}</style>
</head>
<body>
@@ -40,6 +39,8 @@
<span class="lp-ref">(Section searching function)</span>
+<span class="lp-ref">(Command type detection function)</span>
+
<span class="lp-ref">(Parsing functions)</span>
<span class="lp-ref">(Code generation functions)</span>
@@ -146,7 +147,7 @@ pub const LineRange = struct {
<h3 id="error-set">Error set:</h3>
-<pre><code>pub const Errors = error{
+<pre><code>pub const Errors = error {
UnexpectedStart,
UnexpectedEnd,
DereferenceLimit,
@@ -239,6 +240,27 @@ while (iterator.next()) |line| {
<h2 id="parsing">Parsing</h2>
+<p>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.</p>
+
+<h3 id="command-type-detection-function">Command type detection function:</h3>
+
+<pre><code>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;
+ }
+}
+</code></pre>
+
<p>We then define the parsing functions, consisting of the main <code>parse</code> function which builds the list of <code>Section</code>s, and its auxiliary <code>parse_code</code> subroutine which builds the contents of each <code>CodeSection</code>.</p>
<h3 id="parsing-functions">Parsing functions:</h3>
@@ -279,15 +301,20 @@ fn parse_code(lines: [][]const u8, index: u32, alloc: Allocator) !CodeReturn {
<pre><code>var i: u32 = 0;
while (i &#60; lines.len) {
const line = lines[i];
- if (std.mem.startsWith(u8, line, k_start)) {
- <span class="lp-ref">(Add new section)</span>
- } else if (std.mem.startsWith(u8, line, k_add)) {
- <span class="lp-ref">(Append to section)</span>
- } 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 =&#62; {
+ <span class="lp-ref">(Add new section)</span>
+ },
+ .add =&#62; {
+ <span class="lp-ref">(Append to section)</span>
+ },
+ .end =&#62; {
+ log(.err, "line {d}: unexpected section end", .{i + 1});
+ return error.UnexpectedEnd;
+ },
+ else =&#62; {
+ i += 1;
+ },
}
}
</code></pre>
@@ -330,15 +357,20 @@ i = section.index;
<pre><code>var i = index;
while (i &#60; 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)) {
- <span class="lp-ref">(Add reference)</span>
- } else if (std.mem.eql(u8, line, k_end)) {
- break;
- } else {
- <span class="lp-ref">(Add literal range)</span>
+ switch (command_type(line)) {
+ .start, .add =&#62; {
+ log(.err, "line {d}: unexpected section start", .{i + 1});
+ return error.UnexpectedStart;
+ },
+ .ref =&#62; {
+ <span class="lp-ref">(Add reference)</span>
+ },
+ .end =&#62; {
+ break;
+ },
+ else =&#62; {
+ <span class="lp-ref">(Add literal range)</span>
+ },
}
}
</code></pre>
@@ -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)) {
- <span class="lp-ref">(Format starting command)</span>
- } else if (std.mem.startsWith(u8, line, k_add)) {
- <span class="lp-ref">(Format appending command)</span>
- } else if (std.mem.startsWith(u8, line, k_end)) {
- <span class="lp-ref">(Format ending command)</span>
- } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
- <span class="lp-ref">(Format reference command)</span>
- } else {
- try buffer.append(line);
+ } else switch (command_type(line)) {
+ .start =&#62; {
+ <span class="lp-ref">(Format starting command)</span>
+ },
+ .add =&#62; {
+ <span class="lp-ref">(Format appending command)</span>
+ },
+ .ref =&#62; {
+ <span class="lp-ref">(Format reference command)</span>
+ },
+ .end =&#62; {
+ <span class="lp-ref">(Format ending command)</span>
+ },
+ else =&#62; {
+ 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));
</code></pre>
-<p>Processing a section ending command, however, does not require updating the current section name.</p>
-
-<h3 id="format-ending-command">Format ending command:</h3>
-
-<pre><code>try buffer.append(try std.mem.join(alloc, current_name, conf_end));
-</code></pre>
-
<p>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&#8217;s leading whitespace is prepended (to preserve indentation). </p>
<h3 id="format-reference-command">Format reference command:</h3>
@@ -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, &#38;[_][]const u8{ line[0..start], ref }));
</code></pre>
+
+<p>Processing a section ending command is performed similarly to the starting and appending commands, however it does not require updating the current section name.</p>
+
+<h3 id="format-ending-command">Format ending command:</h3>
+
+<pre><code>try buffer.append(try std.mem.join(alloc, current_name, conf_end));
+</code></pre>
</main>
<nav>
</nav>
diff --git a/doc/freestanding/data.html b/doc/freestanding/data.html
index a963f99..5217927 100644
--- a/doc/freestanding/data.html
+++ b/doc/freestanding/data.html
@@ -51,6 +51,8 @@ ul ul,ol ol {margin: 0;}
<span class="ref" >(Section searching function)</span>
+<span class="ref" >(Command type detection function)</span>
+
<span class="ref" >(Parsing functions)</span>
<span class="ref" >(Code generation functions)</span>
@@ -157,7 +159,7 @@ pub const LineRange = struct {
<h3 id="error-set">Error set:</h3>
-<pre><code>pub const Errors = error{
+<pre><code>pub const Errors = error {
UnexpectedStart,
UnexpectedEnd,
DereferenceLimit,
@@ -250,6 +252,27 @@ while (iterator.next()) |line| {
<h2 id="parsing">Parsing</h2>
+<p>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.</p>
+
+<h3 id="command-type-detection-function">Command type detection function:</h3>
+
+<pre><code>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;
+ }
+}
+</code></pre>
+
<p>We then define the parsing functions, consisting of the main <code>parse</code> function which builds the list of <code>Section</code>s, and its auxiliary <code>parse_code</code> subroutine which builds the contents of each <code>CodeSection</code>.</p>
<h3 id="parsing-functions">Parsing functions:</h3>
@@ -290,15 +313,20 @@ fn parse_code(lines: [][]const u8, index: u32, alloc: Allocator) !CodeReturn {
<pre><code>var i: u32 = 0;
while (i &#60; lines.len) {
const line = lines[i];
- if (std.mem.startsWith(u8, line, k_start)) {
- <span class="ref" >(Add new section)</span>
- } else if (std.mem.startsWith(u8, line, k_add)) {
- <span class="ref" >(Append to section)</span>
- } 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 =&#62; {
+ <span class="ref" >(Add new section)</span>
+ },
+ .add =&#62; {
+ <span class="ref" >(Append to section)</span>
+ },
+ .end =&#62; {
+ log(.err, "line {d}: unexpected section end", .{i + 1});
+ return error.UnexpectedEnd;
+ },
+ else =&#62; {
+ i += 1;
+ },
}
}
</code></pre>
@@ -341,15 +369,20 @@ i = section.index;
<pre><code>var i = index;
while (i &#60; 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)) {
- <span class="ref" >(Add reference)</span>
- } else if (std.mem.eql(u8, line, k_end)) {
- break;
- } else {
- <span class="ref" >(Add literal range)</span>
+ switch (command_type(line)) {
+ .start, .add =&#62; {
+ log(.err, "line {d}: unexpected section start", .{i + 1});
+ return error.UnexpectedStart;
+ },
+ .ref =&#62; {
+ <span class="ref" >(Add reference)</span>
+ },
+ .end =&#62; {
+ break;
+ },
+ else =&#62; {
+ <span class="ref" >(Add literal range)</span>
+ },
}
}
</code></pre>
@@ -456,16 +489,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)) {
- <span class="ref" >(Format starting command)</span>
- } else if (std.mem.startsWith(u8, line, k_add)) {
- <span class="ref" >(Format appending command)</span>
- } else if (std.mem.startsWith(u8, line, k_end)) {
- <span class="ref" >(Format ending command)</span>
- } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
- <span class="ref" >(Format reference command)</span>
- } else {
- try buffer.append(line);
+ } else switch (command_type(line)) {
+ .start =&#62; {
+ <span class="ref" >(Format starting command)</span>
+ },
+ .add =&#62; {
+ <span class="ref" >(Format appending command)</span>
+ },
+ .ref =&#62; {
+ <span class="ref" >(Format reference command)</span>
+ },
+ .end =&#62; {
+ <span class="ref" >(Format ending command)</span>
+ },
+ else =&#62; {
+ try buffer.append(line);
+ },
}
}
@@ -499,13 +538,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));
</code></pre>
-<p>Processing a section ending command, however, does not require updating the current section name.</p>
-
-<h3 id="format-ending-command">Format ending command:</h3>
-
-<pre><code>try buffer.append(try std.mem.join(alloc, current_name, conf_end));
-</code></pre>
-
<p>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&#8217;s leading whitespace is prepended (to preserve indentation). </p>
<h3 id="format-reference-command">Format reference command:</h3>
@@ -514,5 +546,12 @@ 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, &#38;[_][]const u8{ line[0..start], ref }));
</code></pre>
+
+<p>Processing a section ending command is performed similarly to the starting and appending commands, however it does not require updating the current section name.</p>
+
+<h3 id="format-ending-command">Format ending command:</h3>
+
+<pre><code>try buffer.append(try std.mem.join(alloc, current_name, conf_end));
+</code></pre>
</body>
</html> \ No newline at end of file
diff --git a/doc/log.html b/doc/log.html
index bcfeb56..89f861a 100755
--- a/doc/log.html
+++ b/doc/log.html
@@ -10,7 +10,6 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<title>log.lp &mdash; DistressNetwork°</title>
-<meta http-equiv="last-modified" content="2022-04-07 11:09:31-04:00" />
<style>h3,.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-size: 1rem; font-weight: normal; font-style: italic;} h3 {margin: 1rem;}</style>
</head>
<body>
diff --git a/doc/tangle.html b/doc/tangle.html
index 816fa1b..3fd4c02 100755
--- a/doc/tangle.html
+++ b/doc/tangle.html
@@ -10,7 +10,6 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<title>tangle.lp &mdash; DistressNetwork°</title>
-<meta http-equiv="last-modified" content="2022-04-07 11:09:31-04:00" />
<style>h3,.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-size: 1rem; font-weight: normal; font-style: italic;} h3 {margin: 1rem;}</style>
</head>
<body>
diff --git a/doc/weave.html b/doc/weave.html
index 69be660..81ef1d6 100755
--- a/doc/weave.html
+++ b/doc/weave.html
@@ -10,7 +10,6 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
<link rel="manifest" href="/favicon/site.webmanifest">
<title>weave.lp &mdash; DistressNetwork°</title>
-<meta http-equiv="last-modified" content="2022-04-07 11:09:32-04:00" />
<style>h3,.lp-ref {font-family: neue-haas-grotesk-text, var(--fs-sans); font-size: 1rem; font-weight: normal; font-style: italic;} h3 {margin: 1rem;}</style>
</head>
<body>
diff --git a/gen-docs.sh b/gen-docs.sh
index 8c7e81e..ed371ff 100755
--- a/gen-docs.sh
+++ b/gen-docs.sh
@@ -12,12 +12,13 @@ for file in "data" "log" "tangle" "weave" ; do
pushd "../web" > "/dev/null" ;
sh "./md.sh" "$tmp" "../literary/$out" > "/dev/null" ;
popd > "/dev/null" ;
+ sed "/<meta http-equiv=/d" -i'' "$out" ;
sed "s~</head>~<style>${css}</style>\n&~1" -i'' "$out" ;
sed 's;\(.*\)@= \(.*\) =@;\1<span class="lp-ref">(\2)</span>;1' -i'' "$out" ;
- sh "./../sync.sh" ;
-
echo "[ok] generated text '${file}'" ;
done
+sh "./../sync.sh"
+
rm "$tmp"
diff --git a/src/data.zig b/src/data.zig
index f2ee9d9..824d36c 100644
--- a/src/data.zig
+++ b/src/data.zig
@@ -88,6 +88,22 @@ fn search(list: []Section, name: []const u8) !usize {
}
// TODO return last match instead?
+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;
+ }
+}
+
pub fn parse(lines: [][]const u8, alloc: Allocator) ![]Section {
var sections = std.ArrayList(Section).init(alloc);
defer sections.deinit();
@@ -95,32 +111,37 @@ pub fn parse(lines: [][]const u8, alloc: Allocator) ![]Section {
var i: u32 = 0;
while (i < lines.len) {
const line = lines[i];
- if (std.mem.startsWith(u8, line, k_start)) {
- const name = line[(k_start.len)..];
- log(.debug, "({d}) starting section '{s}'", .{ i + 1, name });
-
- const section = try parse_code(lines, i + 1, alloc);
- try sections.append(.{ .name = name, .content = section.content });
-
- log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
- i = section.index;
- } else if (std.mem.startsWith(u8, line, k_add)) {
- const name = line[(k_add.len)..];
- log(.debug, "({d}) appending to section '{s}'", .{ i + 1, name });
-
- const section = try parse_code(lines, i + 1, alloc);
- const index = try search(sections.items, name);
- const old = &sections.items[index];
- const new = try std.mem.concat(alloc, Content, &[_][]const Content{ old.*.content, section.content });
- old.*.content = new;
-
- log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
- i = section.index;
- } 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 => {
+ const name = line[(k_start.len)..];
+ log(.debug, "({d}) starting section '{s}'", .{ i + 1, name });
+
+ const section = try parse_code(lines, i + 1, alloc);
+ try sections.append(.{ .name = name, .content = section.content });
+
+ log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
+ i = section.index;
+ },
+ .add => {
+ const name = line[(k_add.len)..];
+ log(.debug, "({d}) appending to section '{s}'", .{ i + 1, name });
+
+ const section = try parse_code(lines, i + 1, alloc);
+ const index = try search(sections.items, name);
+ const old = &sections.items[index];
+ const new = try std.mem.concat(alloc, Content, &[_][]const Content{ old.*.content, section.content });
+ old.*.content = new;
+
+ log(.debug, "({d}) ending section '{s}'", .{ section.index, name });
+ i = section.index;
+ },
+ .end => {
+ log(.err, "line {d}: unexpected section end", .{i + 1});
+ return error.UnexpectedEnd;
+ },
+ else => {
+ i += 1;
+ },
}
}
@@ -134,32 +155,37 @@ fn parse_code(lines: [][]const u8, index: u32, alloc: Allocator) !CodeReturn {
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)) {
- const ref_name = std.mem.trimLeft(u8, line, " \t")[(k_ref.len)..];
- try content.append(.{ .reference = ref_name });
- log(.debug, "({d}) \tappended reference '{s}'", .{ i + 1, ref_name });
- i += 1;
- } else if (std.mem.eql(u8, line, k_end)) {
- break;
- } else {
- if (content.items.len > 0) {
- switch (content.items[content.items.len - 1]) {
- .literal => |*range| {
- range.*.end = i;
- },
- .reference => {
- try content.append(.{ .literal = .{ .start = i, .end = i } });
- log(.debug, "({d}) \tappending literal", .{i + 1});
- },
+ switch (command_type(line)) {
+ .start, .add => {
+ log(.err, "line {d}: unexpected section start", .{i + 1});
+ return error.UnexpectedStart;
+ },
+ .ref => {
+ const ref_name = std.mem.trimLeft(u8, line, " \t")[(k_ref.len)..];
+ try content.append(.{ .reference = ref_name });
+ log(.debug, "({d}) \tappended reference '{s}'", .{ i + 1, ref_name });
+ i += 1;
+ },
+ .end => {
+ break;
+ },
+ else => {
+ if (content.items.len > 0) {
+ switch (content.items[content.items.len - 1]) {
+ .literal => |*range| {
+ range.*.end = i;
+ },
+ .reference => {
+ try content.append(.{ .literal = .{ .start = i, .end = i } });
+ log(.debug, "({d}) \tappending literal", .{i + 1});
+ },
+ }
+ } else {
+ try content.append(.{ .literal = .{ .start = i, .end = i } });
+ log(.debug, "({d}) \tappending literal", .{i + 1});
}
- } else {
- try content.append(.{ .literal = .{ .start = i, .end = i } });
- log(.debug, "({d}) \tappending literal", .{i + 1});
- }
- i += 1;
+ i += 1;
+ },
}
}
@@ -214,20 +240,26 @@ pub fn textgen(lines: [][]const u8, alloc: Allocator) ![][]const u8 {
for (lines) |line| {
if (std.mem.startsWith(u8, line, kc_start) or std.mem.startsWith(u8, line, kc_add) 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)) {
- current_name = line[(k_start.len)..];
- try buffer.append(try std.mem.join(alloc, current_name, conf_start));
- } else if (std.mem.startsWith(u8, line, k_add)) {
- current_name = line[(k_add.len)..];
- try buffer.append(try std.mem.join(alloc, current_name, conf_add));
- } else if (std.mem.startsWith(u8, line, k_end)) {
- try buffer.append(try std.mem.join(alloc, current_name, conf_end));
- } else if (std.mem.startsWith(u8, std.mem.trimLeft(u8, line, " \t"), k_ref)) {
- const start = std.mem.indexOf(u8, line, k_ref).?;
- 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 }));
- } else {
- try buffer.append(line);
+ } else switch (command_type(line)) {
+ .start => {
+ current_name = line[(k_start.len)..];
+ try buffer.append(try std.mem.join(alloc, current_name, conf_start));
+ },
+ .add => {
+ current_name = line[(k_add.len)..];
+ try buffer.append(try std.mem.join(alloc, current_name, conf_add));
+ },
+ .ref => {
+ const start = std.mem.indexOf(u8, line, k_ref).?;
+ 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 }));
+ },
+ .end => {
+ try buffer.append(try std.mem.join(alloc, current_name, conf_end));
+ },
+ else => {
+ try buffer.append(line);
+ },
}
}
diff --git a/tangle b/tangle
index e9f3671..9bef0a7 100755
--- a/tangle
+++ b/tangle
Binary files differ
diff --git a/weave b/weave
index 9bf4fc7..40246dd 100755
--- a/weave
+++ b/weave
Binary files differ