Donner SVG main
0.8.0 ยท 382bd0190.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
Parser.h File Reference

C++20 constexpr parser and validator for Donner's bounded WGSL frontend profile. More...

#include <bit>
#include <cstdint>
#include <limits>
#include <optional>
#include <string_view>
#include "donner/gpu/GpuLimits.h"
#include "donner/gpu/shader/wgsl/Module.h"
#include "donner/gpu/shader/wgsl/Number.h"
#include "donner/gpu/shader/wgsl/Uniformity.h"
Include dependency graph for Parser.h:

Classes

struct  donner::gpu::shader::wgsl::Diagnostic
 A fail-closed parser diagnostic. More...
struct  donner::gpu::shader::wgsl::ParseResult
 Result of parsing a bounded WGSL source string. More...
struct  donner::gpu::shader::wgsl::detail::Token
 Lexical token retaining its source spelling and location. More...
struct  donner::gpu::shader::wgsl::detail::PunctuationEntry
 Mapping from one punctuation character to its token category. More...
struct  donner::gpu::shader::wgsl::detail::LexicalTable
 Lexical classes of each byte, plus the token kind of each single-character punctuator. More...
struct  donner::gpu::shader::wgsl::detail::NameBuckets< N >
 Indices of a name table grouped by first byte, so a lookup compares only the names that share the candidate's first byte. More...
class  donner::gpu::shader::wgsl::detail::Parser
 Private parser state. All storage is fixed-size and constexpr-compatible. More...

Namespaces

namespace  donner
 Top-level Donner namespace, which is split into different sub-namespaces such as donner::svg and donner::css.

Enumerations

enum class  donner::gpu::shader::wgsl::ErrorCode : uint8_t {
  None ,
  SourceTooLarge ,
  NonAsciiSource ,
  TokenLimit ,
  IdentifierLimit ,
  StructLimit ,
  StructMemberLimit ,
  BindingLimit ,
  SymbolLimit ,
  ExpressionLimit ,
  StatementLimit ,
  FunctionLimit ,
  NestingLimit ,
  UnexpectedToken ,
  UnsupportedConstruct ,
  InvalidLiteral ,
  InvalidIdentifier ,
  InvalidConstantExpression ,
  DuplicateName ,
  DuplicateBinding ,
  UnknownName ,
  UnknownType ,
  TypeMismatch ,
  InvalidAttribute ,
  InvalidBinding ,
  InvalidLayout ,
  ImmutableAssignment ,
  InvalidCall ,
  InvalidReturn ,
  InvalidCondition ,
  InvalidLoop ,
  MissingReturn ,
  UnreachableStatement ,
  InvalidSwitch ,
  NonUniformControl ,
  UniformityLimit ,
  InvalidStage ,
  InvalidPointer
}
 Reason parsing or validation rejected a WGSL module.
enum class  donner::gpu::shader::wgsl::detail::TokenKind : uint8_t {
  BitAnd ,
  End ,
  Identifier ,
  Number ,
  At ,
  LeftParen ,
  RightParen ,
  LeftBrace ,
  RightBrace ,
  LeftBracket ,
  RightBracket ,
  LeftAngle ,
  RightAngle ,
  Comma ,
  Colon ,
  Semicolon ,
  Dot ,
  Plus ,
  Minus ,
  Star ,
  Slash ,
  Percent ,
  Assign ,
  Equal ,
  NotEqual ,
  Less ,
  LessEqual ,
  Greater ,
  GreaterEqual ,
  Not ,
  And ,
  Or ,
  Arrow ,
  PlusAssign ,
  MinusAssign ,
  ShiftLeft ,
  ShiftRight ,
  ShiftLeftAssign ,
  ShiftRightAssign
}
 Lexical token categories recognized by the WGSL parser.

Functions

constexpr bool donner::gpu::shader::wgsl::detail::IsSpaceByte (int ch)
 Returns whether byte ch separates tokens.
constexpr bool donner::gpu::shader::wgsl::detail::IsIdentifierStartByte (int ch)
 Returns whether byte ch can start an identifier.
constexpr uint8_t donner::gpu::shader::wgsl::detail::ByteClasses (int ch)
 Returns the LexicalTable class bits of byte ch.
constexpr LexicalTable donner::gpu::shader::wgsl::detail::MakeLexicalTable ()
 Builds LexicalTable from the character classes and kSingleCharacterPunctuation.
template<typename Entry, size_t N, typename NameOf>
constexpr NameBuckets< N > donner::gpu::shader::wgsl::detail::BucketNames (const Entry(&entries)[N], NameOf name)
 Groups the names of entries by first byte, keeping table order within each group.
template<typename Entry, size_t N, typename NameOf>
constexpr bool donner::gpu::shader::wgsl::detail::NamesNonEmpty (const Entry(&entries)[N], NameOf name)
 Returns whether every name in entries is non-empty, as BucketNames requires.
constexpr ParseResult donner::gpu::shader::wgsl::Parse (std::string_view source)
 Parses and validates WGSL in the supported frontend profile.

Variables

constexpr PunctuationEntry donner::gpu::shader::wgsl::detail::kSingleCharacterPunctuation []
 Single-character punctuation spellings and their token kinds.
constexpr std::string_view donner::gpu::shader::wgsl::detail::kReservedDeclarationNames []
 Names disallowed for declarations by the supported WGSL grammar.
constexpr LexicalTable donner::gpu::shader::wgsl::detail::kLexicalTable = MakeLexicalTable()
 Byte classes and punctuators for the lexer.
constexpr auto donner::gpu::shader::wgsl::detail::kSelfName = [](std::string_view entry) { return entry; }
 Returns a string table entry itself as its name.
constexpr auto donner::gpu::shader::wgsl::detail::kReservedDeclarationBuckets
 kReservedDeclarationNames grouped by first byte.

Detailed Description

C++20 constexpr parser and validator for Donner's bounded WGSL frontend profile.

Function Documentation

◆ BucketNames()

template<typename Entry, size_t N, typename NameOf>
NameBuckets< N > donner::gpu::shader::wgsl::detail::BucketNames ( const Entry(&) entries[N],
NameOf name )
constexpr

Groups the names of entries by first byte, keeping table order within each group.

Parameters
entriesName table.
nameReturns the name of one entry; names are non-empty.

◆ Parse()

ParseResult donner::gpu::shader::wgsl::Parse ( std::string_view source)
constexpr

Parses and validates WGSL in the supported frontend profile.

The profile accepts flat numeric buffer structures, group-zero resources, helper functions before their callers, compute/vertex/fragment entries, scalar/vector/matrix expressions, structured conditionals and positive-increment for loops, and the builtins represented by Builtin. Break/continue require a loop; discard and derivatives require a fragment entry. Derivatives are restricted to straight-line entry code before any conditional/loop and outside short-circuit RHS. Helpers are pure numeric functions over read-only globals and by-value parameters. Numeric literals include bounded decimal/hexadecimal forms, abstract scalars and scalar module constants. Abstract scalar arithmetic and constant f32 scalar/vector +, -, * and / over literals, their negations and vector constructions of them are evaluated at shader creation, and a result that is not finite fails. Constant f32 arithmetic over other operands, constant matrix arithmetic, constant builtin calls and cross-scalar constant conversions remain unsupported. Static f32 clamp bounds support literals, unary negation and vector construction, with low <= high required in every lane. Floor and sign accept runtime f32 scalars/vectors in this profile. Other unsupported constructs fail explicitly.

Parameters
sourceASCII WGSL bytes to parse.
Returns
A fully validated fixed-arena module or a diagnostic with its offending byte range.

Variable Documentation

◆ kReservedDeclarationBuckets

auto donner::gpu::shader::wgsl::detail::kReservedDeclarationBuckets
inlineconstexpr
Initial value:
=
constexpr auto kSelfName
Returns a string table entry itself as its name.
Definition Parser.h:315
constexpr std::string_view kReservedDeclarationNames[]
Names disallowed for declarations by the supported WGSL grammar.
Definition Parser.h:145
constexpr NameBuckets< N > BucketNames(const Entry(&entries)[N], NameOf name)
Groups the names of entries by first byte, keeping table order within each group.
Definition Parser.h:286

kReservedDeclarationNames grouped by first byte.

◆ kSingleCharacterPunctuation

PunctuationEntry donner::gpu::shader::wgsl::detail::kSingleCharacterPunctuation[]
inlineconstexpr
Initial value:
= {
{'@', TokenKind::At}, {'(', TokenKind::LeftParen}, {')', TokenKind::RightParen},
{'{', TokenKind::LeftBrace}, {'}', TokenKind::RightBrace}, {'[', TokenKind::LeftBracket},
{']', TokenKind::RightBracket}, {',', TokenKind::Comma}, {':', TokenKind::Colon},
{';', TokenKind::Semicolon}, {'.', TokenKind::Dot}, {'*', TokenKind::Star},
{'/', TokenKind::Slash}, {'%', TokenKind::Percent},
}

Single-character punctuation spellings and their token kinds.