Donner
Embeddable browser-grade SVG2 engine
Loading...
Searching...
No Matches
donner::ParseWarningSink Class Reference

Collects parse warnings during parsing. More...

#include "donner/base/ParseWarningSink.h"

Public Member Functions

 ParseWarningSink ()=default
 Construct a sink that collects warnings.
bool isEnabled () const
 Returns true if the sink is enabled (will store warnings).
template<typename Factory>
requires std::invocable<Factory> && std::same_as<std::invoke_result_t<Factory>, ParseDiagnostic>
void add (Factory &&factory)
 Add a warning via a factory callable.
void add (ParseDiagnostic &&warning)
 Add a pre-constructed warning (for cases where the diagnostic is already built).
const std::vector< ParseDiagnostic > & warnings () const
 Access the collected warnings.
bool hasWarnings () const
 Returns true if any warnings have been added.
void merge (ParseWarningSink &&other)
 Merge all warnings from another sink into this one.
void mergeFromSubparser (ParseWarningSink &&other, FileOffset parentOffset)
 Merge warnings from a subparser, remapping source ranges using the given parent offset.

Static Public Member Functions

static ParseWarningSink Disabled ()
 Construct a disabled sink that discards all warnings (no-op).

Detailed Description

Collects parse warnings during parsing.

Always safe to call add() on—when disabled, warnings are silently dropped without invoking the factory callable, implicitly avoiding string formatting overhead.

Replaces the std::vector<ParseDiagnostic>* outWarnings pattern.

Usage:

// The lambda is only invoked if the sink is enabled---no formatting overhead when disabled.
sink.add([&] {
RcString::fromFormat("Unknown attribute '{}'", std::string_view(name)), range);
});
static RcString fromFormat(std::format_string< Args... > fmt, Args &&... args)
Constructs an RcString using std::format-style formatting.
Definition RcString.h:87
static ParseDiagnostic Warning(RcString reason, FileOffset location)
Create a warning diagnostic at a single offset.
Definition ParseDiagnostic.h:53
Examples
custom_css_parser.cc, svg_to_png.cc, and svg_tree_interaction.cc.

Member Function Documentation

◆ add()

template<typename Factory>
requires std::invocable<Factory> && std::same_as<std::invoke_result_t<Factory>, ParseDiagnostic>
void donner::ParseWarningSink::add ( Factory && factory)
inline

Add a warning via a factory callable.

The callable is only invoked when the sink is enabled, implicitly avoiding formatting overhead when warnings are disabled.

Template Parameters
FactoryA callable returning ParseDiagnostic.

◆ mergeFromSubparser()

void donner::ParseWarningSink::mergeFromSubparser ( ParseWarningSink && other,
FileOffset parentOffset )
inline

Merge warnings from a subparser, remapping source ranges using the given parent offset.

Each warning's range.start and range.end are remapped via addParentOffset(parentOffset), translating them from the subparser's local coordinate space to the parent's.

Parameters
otherSubparser's warning sink to merge from.
parentOffsetThe offset of the subparser's input within the parent input.

The documentation for this class was generated from the following file: