Skip to content

Seal mustachio nodes #3433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/src/mustachio/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ class MustachioParser {
}

/// An interface for various types of node in a Mustache template.
@sealed
abstract class MustachioNode {
sealed class MustachioNode {
SourceSpan get span;
}

Expand All @@ -434,7 +433,7 @@ mixin HasMultiNamedKey {

/// A Text node, representing literal text.
@immutable
class Text implements MustachioNode {
final class Text implements MustachioNode {
final String content;

@override
Expand All @@ -448,7 +447,7 @@ class Text implements MustachioNode {

/// A Variable node, representing a variable to be resolved.
@immutable
class Variable with HasMultiNamedKey implements MustachioNode {
final class Variable with HasMultiNamedKey implements MustachioNode {
@override
final List<String> key;

Expand All @@ -470,7 +469,7 @@ class Variable with HasMultiNamedKey implements MustachioNode {
/// A Section node, representing either a Conditional Section, a Repeated
/// Section, or a Value Section, possibly inverted.
@immutable
class Section with HasMultiNamedKey implements MustachioNode {
final class Section with HasMultiNamedKey implements MustachioNode {
@override
final List<String> key;

Expand All @@ -493,7 +492,7 @@ class Section with HasMultiNamedKey implements MustachioNode {

/// A Partial node, representing a partial to be resolved.
@immutable
class Partial implements MustachioNode {
final class Partial implements MustachioNode {
final String key;

@override
Expand Down
72 changes: 36 additions & 36 deletions lib/src/mustachio/renderer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,30 +100,29 @@ class Template {

// Walk the Mustache syntax tree, looking for Partial nodes.
while (nodeQueue.isNotEmpty) {
var node = nodeQueue.removeFirst();
if (node is Text) {
// Nothing to do.
} else if (node is Variable) {
// Nothing to do.
} else if (node is Section) {
nodeQueue.addAll(node.children);
} else if (node is Partial) {
var key = node.key;
if (!partials.containsKey(key)) {
partials[key] = await partialResolver(key);
}
var partialFile = partials[key]!;
if (!partialTemplates.containsKey(partialFile)) {
try {
var partialTemplate = await Template.parse(partialFile,
partialResolver: partialResolver,
partialTemplates: {...partialTemplates});
partialTemplates[partialFile] = partialTemplate;
} on FileSystemException catch (e) {
throw MustachioResolutionError(node.span.message(
'FileSystemException (${e.message}) when reading partial:'));
switch (nodeQueue.removeFirst()) {
case Text():
break;
case Variable():
break;
case Section(:var children):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SOOO NICE!

nodeQueue.addAll(children);
case Partial(:var span, :var key):
if (!partials.containsKey(key)) {
partials[key] = await partialResolver(key);
}
var partialFile = partials[key]!;
if (!partialTemplates.containsKey(partialFile)) {
try {
var partialTemplate = await Template.parse(partialFile,
partialResolver: partialResolver,
partialTemplates: {...partialTemplates});
partialTemplates[partialFile] = partialTemplate;
} on FileSystemException catch (e) {
throw MustachioResolutionError(span.message(
'FileSystemException (${e.message}) when reading partial:'));
}
}
}
}
}

Expand Down Expand Up @@ -218,19 +217,20 @@ abstract class RendererBase<T extends Object?> {
/// Renders a block of Mustache template, the [ast], into [sink].
void renderBlock(List<MustachioNode> ast) {
for (var node in ast) {
if (node is Text) {
write(node.content);
} else if (node is Variable) {
var content = getFields(node);
if (node.escape) {
write(htmlEscape.convert(content));
} else {
write(content);
}
} else if (node is Section) {
section(node);
} else if (node is Partial) {
partial(node);
switch (node) {
case Text():
write(node.content);
case Variable():
var content = getFields(node);
if (node.escape) {
write(htmlEscape.convert(content));
} else {
write(content);
}
case Section():
section(node);
case Partial():
partial(node);
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions tool/mustachio/codegen_aot_compiler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,16 @@ class _BlockCompiler {

Future<void> _compile(List<MustachioNode> syntaxTree) async {
for (var node in syntaxTree) {
if (node is Text) {
_writeText(node.content);
} else if (node is Variable) {
var variableLookup = _lookUpGetter(node);
_writeGetter(variableLookup, escape: node.escape);
} else if (node is Section) {
await _compileSection(node);
} else if (node is Partial) {
await _compilePartial(node);
switch (node) {
case Text():
_writeText(node.content);
case Variable():
var variableLookup = _lookUpGetter(node);
_writeGetter(variableLookup, escape: node.escape);
case Section():
await _compileSection(node);
case Partial():
await _compilePartial(node);
}
}
}
Expand Down