Skip to content

Conversation

@thevilledev
Copy link
Contributor

@thevilledev thevilledev commented Dec 25, 2025

Motivation

Implements bytes literals, which return []byte directly and interpret \x and octal escapes as raw byte values (0-255) rather than Unicode codepoints.

Fixes #457.

Changes

Components:

  • lexer/token: add Bytes token kind
  • lexer/state: detect [bB]'...' and [bB]"..." prefix, emit Bytes token
  • lexer/utils: add unescapeBytes and unescapeByteChar functions for byte-level escape handling (no \u/\U support, 0-255 limit)
  • parser: parse Bytes token into BytesNode
  • ast/node: add BytesNode struct with []byte value
  • ast/visitor: handle BytesNode in AST walker
  • ast/print: add String() method
  • checker: type inference returns []byte
  • compiler: emit push for BytesNode value

Updated docs.

Further comments

This is not exactly CEL-compatible:

  • CEL allows \uXXXX in bytes literals (only \U is string-only). We reject both, requiring \x for arbitrary byte values instead.
  • CEL supports b\... and triple-quoted bytes. We only support single-line b'...' and b"..."

Non-ASCII characters like b"ÿ" are UTF-8 encoded to []byte{0xC3, 0xBF}, which does match CEL behavior.

Open to discussion whether we want to go for this compatibility or stay at this level!

Implements bytes literals, which return []byte directly and
interpret \x and octal escapes as raw byte values (0-255) rather
than Unicode codepoints.

Components:

- lexer/token: add Bytes token kind
- lexer/state: detect [bB]'...' and [bB]"..." prefix, emit Bytes token
- lexer/utils: add unescapeBytes and unescapeByteChar functions
  for byte-level escape handling ((no \u/\U support, 0-255 limit)
- parser: parse Bytes token into BytesNode
- ast/node: add BytesNode struct with []byte value
- ast/visitor: handle BytesNode in AST walker
- ast/print: add String() method
- checker: type inference returns []byte
- compiler: emit push for BytesNode value

Updated docs.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add b'' form to represent bytes

1 participant