Logos.Char (Logos v0.2.0)

Copy Markdown

A Logos character literal: \a, \newline, \space, \tab, \u0041.

Wrapped in a dedicated struct (rather than represented as a bare integer codepoint) purely so it's tagged: Logos also has plain integers as a first-class type, and \a must never be confused with the integer 97 even though they share a codepoint -- Logos.Form.t() needs character and integer to be genuinely distinct cases, the same reasoning as Logos.Ratio's auto-demotion note about integer vs ratio.

Summary

Functions

Builds a char directly from a codepoint.

Parses the text after the leading \ of a CHAR token (e.g. "newline", "a", "u0041") into a %Logos.Char{}.

The full printed form of a char, e.g. \\newline or \\a.

Types

t()

@type t() :: %Logos.Char{codepoint: non_neg_integer()}

Functions

new(codepoint)

@spec new(non_neg_integer()) :: t()

Builds a char directly from a codepoint.

parse(text)

@spec parse(String.t()) :: {:ok, t()} | {:error, String.t()}

Parses the text after the leading \ of a CHAR token (e.g. "newline", "a", "u0041") into a %Logos.Char{}.

Recognizes the named chars (newline/space/tab/return/ backspace/formfeed), the uHHHH codepoint-escape form, and falls back to "a single literal grapheme" otherwise. A multi-grapheme escape body that's neither a known name nor a u-escape (e.g. a typo like \nnewline) is a genuine reader error.

to_string(char)

@spec to_string(t()) :: String.t()

The full printed form of a char, e.g. \\newline or \\a.