Parse whitespace stripping chars
This commit is contained in:
parent
22277beffc
commit
58adc38945
2 changed files with 85 additions and 62 deletions
|
@ -162,6 +162,7 @@ fn parseTemplate(comptime tokens: TokenIter, comptime template_type: TemplateTyp
|
||||||
.pipe => current_text = current_text ++ "|",
|
.pipe => current_text = current_text ++ "|",
|
||||||
.dollar => current_text = current_text ++ "$",
|
.dollar => current_text = current_text ++ "$",
|
||||||
.slash => current_text = current_text ++ "/",
|
.slash => current_text = current_text ++ "/",
|
||||||
|
.equals => current_text = current_text ++ "=",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +183,16 @@ fn parseExpressionOrStatement(
|
||||||
) ParseResult(if (as_statement) Statement else Expression) {
|
) ParseResult(if (as_statement) Statement else Expression) {
|
||||||
comptime {
|
comptime {
|
||||||
var iter = tokens;
|
var iter = tokens;
|
||||||
var stmt: ExecutableStatement = while (iter.next()) |token| switch (token) {
|
var first_token: bool = true;
|
||||||
|
var strip_before: bool = false;
|
||||||
|
var stmt: ExecutableStatement = while (iter.next()) |token| {
|
||||||
|
defer first_token = false;
|
||||||
|
switch (token) {
|
||||||
|
.equals => {
|
||||||
|
if (as_statement and first_token) {
|
||||||
|
strip_before = true;
|
||||||
|
} else @compileError("Unexpected '='");
|
||||||
|
},
|
||||||
.whitespace => {},
|
.whitespace => {},
|
||||||
.pound => {
|
.pound => {
|
||||||
if (!as_statement) @compileError("Unexpected Token");
|
if (!as_statement) @compileError("Unexpected Token");
|
||||||
|
@ -242,18 +252,29 @@ fn parseExpressionOrStatement(
|
||||||
break .{ .expression = .{ .capture_deref = names.item } };
|
break .{ .expression = .{ .capture_deref = names.item } };
|
||||||
},
|
},
|
||||||
else => if (as_statement) @compileError("TODO") else break,
|
else => if (as_statement) @compileError("TODO") else break,
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if (as_statement) {
|
if (as_statement) {
|
||||||
// search for end of statement
|
// search for end of statement
|
||||||
|
var strip_after: bool = false;
|
||||||
while (iter.next()) |token| switch (token) {
|
while (iter.next()) |token| switch (token) {
|
||||||
.whitespace => {},
|
.whitespace => {},
|
||||||
|
.equals => {
|
||||||
|
if (iter.peek()) |t| {
|
||||||
|
if (t == .close_bracket) {
|
||||||
|
strip_after = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@compileError("Unexpected '='");
|
||||||
|
},
|
||||||
.close_bracket => return .{
|
.close_bracket => return .{
|
||||||
.new_iter = iter,
|
.new_iter = iter,
|
||||||
.item = .{
|
.item = .{
|
||||||
.executable = stmt,
|
.executable = stmt,
|
||||||
.strip_before = true,
|
.strip_before = strip_before,
|
||||||
.strip_after = true,
|
.strip_after = strip_after,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
|
@ -425,6 +446,7 @@ const Token = union(enum) {
|
||||||
pipe: void,
|
pipe: void,
|
||||||
dollar: void,
|
dollar: void,
|
||||||
slash: void,
|
slash: void,
|
||||||
|
equals: void,
|
||||||
};
|
};
|
||||||
|
|
||||||
const TokenIter = struct {
|
const TokenIter = struct {
|
||||||
|
@ -453,6 +475,7 @@ const TokenIter = struct {
|
||||||
'|' => return .{ .pipe = {} },
|
'|' => return .{ .pipe = {} },
|
||||||
'$' => return .{ .dollar = {} },
|
'$' => return .{ .dollar = {} },
|
||||||
'/' => return .{ .slash = {} },
|
'/' => return .{ .slash = {} },
|
||||||
|
'=' => return .{ .equals = {} },
|
||||||
' ', '\t', '\n', '\r' => {
|
' ', '\t', '\n', '\r' => {
|
||||||
var idx: usize = 0;
|
var idx: usize = 0;
|
||||||
while (idx < remaining.len and std.mem.indexOfScalar(u8, " \t\n\r", remaining[idx]) != null) : (idx += 1) {}
|
while (idx < remaining.len and std.mem.indexOfScalar(u8, " \t\n\r", remaining[idx]) != null) : (idx += 1) {}
|
||||||
|
@ -464,7 +487,7 @@ const TokenIter = struct {
|
||||||
},
|
},
|
||||||
else => {
|
else => {
|
||||||
var idx: usize = 0;
|
var idx: usize = 0;
|
||||||
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}.#|$/ \t\n\r", remaining[idx]) == null) : (idx += 1) {}
|
while (idx < remaining.len and std.mem.indexOfScalar(u8, "{}.#|$/= \t\n\r", remaining[idx]) == null) : (idx += 1) {}
|
||||||
|
|
||||||
self.start += idx - 1;
|
self.start += idx - 1;
|
||||||
return .{ .text = remaining[0..idx] };
|
return .{ .text = remaining[0..idx] };
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>{.community.name}</title>
|
<title> {= .community.name =} </title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
@ -9,7 +9,7 @@
|
||||||
<h2> {{ REAL BRACKETS }} </h2>
|
<h2> {{ REAL BRACKETS }} </h2>
|
||||||
|
|
||||||
<section>
|
<section>
|
||||||
{#for .baz |$f|}{#for $f |$b|}{$b}:{/for}
|
{= #for .baz |$f|}{#for $f |$b|}{$b}:{/for}
|
||||||
{/for}
|
{/for}
|
||||||
{#if .qux}qux!{/if}
|
{#if .qux}qux!{/if}
|
||||||
{#if .quxx}quxx!{/if}
|
{#if .quxx}quxx!{/if}
|
||||||
|
|
Loading…
Reference in a new issue