summaryrefslogtreecommitdiff
path: root/doc/freestanding/data.html
diff options
context:
space:
mode:
Diffstat (limited to 'doc/freestanding/data.html')
-rw-r--r--doc/freestanding/data.html111
1 files changed, 75 insertions, 36 deletions
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