Add hex escape sequences (\xNN) to string literals #279

Closed
opened 2026-01-19 04:34:06 +00:00 by navicore · 1 comment
navicore commented 2026-01-19 04:34:06 +00:00 (Migrated from github.com)

Problem

Seq string literals only support a limited set of escape sequences: \" \\ \n \r \t

This makes it awkward to write terminal control codes (ANSI escapes) which require the ESC character (0x1B):

# Current workaround - verbose and runtime allocation
: esc ( -- String ) 27 string.from-codepoint ;
: clear-line ( -- ) esc "[2K" string.concat io.write ;

# Desired - clean and compile-time
: clear-line ( -- ) "\x1b[2K" io.write ;

Proposal

Add \xNN hex escape sequences to string literals:

"\x1b[2K"   # ESC [ 2 K - clear line
"\x1b[D"    # ESC [ D - cursor left  
"\x00"      # null byte
"\x7f"      # DEL character

Use Cases

  1. Terminal applications - ANSI escape codes for cursor movement, colors, clearing
  2. The new terminal FFI - vim-style editors, TUI apps need these codes
  3. Binary protocols - any low-level work with non-printable characters

Implementation

In the lexer, when parsing string literals and encountering \x:

  • Read exactly 2 hex digits
  • Convert to byte value 0x00-0xFF
  • Emit that byte into the string

Optional: Unicode Escapes

Could also add \u{NNNN} for Unicode code points:

"\u{1F600}"  # 😀
"\u{03BB}"   # λ

But hex escapes alone would unblock terminal work.

This came up while implementing vim-style line editing for seq-lisp (#52). The terminal FFI (patch-seq#274) enables raw terminal access, but writing escape codes is cumbersome without \x support.

## Problem Seq string literals only support a limited set of escape sequences: `\" \\ \n \r \t` This makes it awkward to write terminal control codes (ANSI escapes) which require the ESC character (0x1B): ```seq # Current workaround - verbose and runtime allocation : esc ( -- String ) 27 string.from-codepoint ; : clear-line ( -- ) esc "[2K" string.concat io.write ; # Desired - clean and compile-time : clear-line ( -- ) "\x1b[2K" io.write ; ``` ## Proposal Add `\xNN` hex escape sequences to string literals: ```seq "\x1b[2K" # ESC [ 2 K - clear line "\x1b[D" # ESC [ D - cursor left "\x00" # null byte "\x7f" # DEL character ``` ## Use Cases 1. **Terminal applications** - ANSI escape codes for cursor movement, colors, clearing 2. **The new terminal FFI** - vim-style editors, TUI apps need these codes 3. **Binary protocols** - any low-level work with non-printable characters ## Implementation In the lexer, when parsing string literals and encountering `\x`: - Read exactly 2 hex digits - Convert to byte value 0x00-0xFF - Emit that byte into the string ## Optional: Unicode Escapes Could also add `\u{NNNN}` for Unicode code points: ```seq "\u{1F600}" # 😀 "\u{03BB}" # λ ``` But hex escapes alone would unblock terminal work. ## Related This came up while implementing vim-style line editing for seq-lisp (#52). The terminal FFI (patch-seq#274) enables raw terminal access, but writing escape codes is cumbersome without `\x` support.
navicore commented 2026-01-19 04:48:48 +00:00 (Migrated from github.com)
https://github.com/navicore/patch-seq/pull/280
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
navicore/patch-seq#279
No description provided.