Skip to content

Commit

Permalink
fixup! Implement skipping for any field with known size.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 16, 2024
1 parent 620edb4 commit 1b175d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,15 @@ class ParserBuilder {
*/
void waitForEod();

/*
* Generates code which waits for given input length to be available to
* immediately consume and trim it.
*
* @param size an unsigned integer specifying the length of the input to skip
* @param location location associated with the operation
*/
void skip(const Expression& size, const Meta& location = {});

/** Returns a boolean expression that's true if EOD has been reached. */
Expression atEod();

Expand Down
33 changes: 17 additions & 16 deletions spicy/toolchain/src/compiler/codegen/parser-builder.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.

#include "compiler/detail/codegen/parser-builder.h"

#include <algorithm>
#include <numeric>
#include <optional>
Expand Down Expand Up @@ -30,7 +32,6 @@
#include <spicy/ast/types/unit-items/sink.h>
#include <spicy/compiler/detail/codegen/codegen.h>
#include <spicy/compiler/detail/codegen/grammar.h>
#include <spicy/compiler/detail/codegen/parser-builder.h>
#include <spicy/compiler/detail/codegen/production.h>
#include <spicy/compiler/detail/codegen/productions/all.h>

Expand Down Expand Up @@ -1699,20 +1700,6 @@ struct ProductionVisitor
}

void operator()(const production::Skip& p) {
auto consumeFixedSize = [&](const Expression& size) {
assert(size.type().template isA<type::UnsignedInteger>());

auto n = builder()->addTmp("skip", size);
auto loop = builder()->addWhile(builder::greater(n, builder::integer(0U)));
pushBuilder(loop, [&]() {
pb->waitForInput(builder::integer(1U), "not enough bytes for skipping", p.location());
auto consume = builder()->addTmp("consume", builder::min(builder::size(state().cur), size));
pb->advanceInput(consume);
builder()->addAssign(n, builder::difference(n, consume));
builder()->addDebugMsg("spicy-verbose", "- skipped %u bytes (%u left to skip)", {consume, n});
});
};

if ( auto c = p.field().condition() )
pushBuilder(builder()->addIf(*c));

Expand All @@ -1721,7 +1708,7 @@ struct ProductionVisitor
}

else if ( const auto& size = p.field().size() )
consumeFixedSize(*size);
pb->skip(*size, p.location());

else if ( p.field().parseType().isA<type::Bytes>() ) {
// Bytes with fixed size already handled above.
Expand Down Expand Up @@ -2403,6 +2390,20 @@ void ParserBuilder::waitForEod() {
builder()->addCall("spicy_rt::waitForEod", {state().data, state().cur, _filters(state())});
}

void ParserBuilder::skip(const Expression& size, const Meta& location) {
assert(size.type().template isA<type::UnsignedInteger>());

auto n = builder()->addTmp("skip", size);
auto loop = builder()->addWhile(builder::greater(n, builder::integer(0U)));
pushBuilder(loop, [&]() {
waitForInput(builder::integer(1U), "not enough bytes for skipping", location);
auto consume = builder()->addTmp("consume", builder::min(builder::size(state().cur), size));
advanceInput(consume);
builder()->addAssign(n, builder::difference(n, consume));
builder()->addDebugMsg("spicy-verbose", "- skipped %u bytes (%u left to skip)", {consume, n});
});
}

void ParserBuilder::parseError(const Expression& error_msg, const Meta& location) {
builder()->addThrow(builder::exception(builder::typeByID("spicy_rt::ParseError"), error_msg, location), location);
}
Expand Down

0 comments on commit 1b175d1

Please sign in to comment.