lib: drop non-Hare extras (ascii.digitval, bytes.copy, fmt.errpos)

ascii.digitval/isidstart/isidpart are lexer-private, not Hare-stdlib.
Moved into lib/ww/lex/lex.ww as fn (renamed digitval to hexval since
its sole job is the \\xHH escape decoder).

bytes.copy and fmt.errpos have no Hare counterpart and no external
callers; gone.
This commit is contained in:
2026-05-13 03:24:33 +09:00
parent f4743dc5d5
commit 9bc973dc96
8 changed files with 110 additions and 169 deletions

View File

@@ -1000,33 +1000,6 @@ export fn isxdigit(c: rune) bool = {
return false;
};
// digitval — value of `c` as a hex/decimal digit. void variant means
// `c` isn't a hex digit. Useful when scanning numeric literals.
export fn digitval(c: rune) (i32 | void) = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
@@ -1057,6 +1030,34 @@ use ascii;
use mem;
use tok;
// isidstart / isidpart — identifier classification. Lexer-local
// because the "alpha or '_' / alnum or '_'" set isn't part of Hare's
// ascii::; ascii::isalpha + the '_' check live here instead.
fn isidstart(c: rune) bool = {
if (ascii.isalpha(c)) { return true; };
if (c == 95) { return true; }; // '_'
return false;
};
fn isidpart(c: rune) bool = {
if (ascii.isalnum(c)) { return true; };
if (c == 95) { return true; };
return false;
};
// hexval — value of `c` as a hex digit (0..15) or void if not a hex
// digit. Used by string-literal `\xHH` escapes.
fn hexval(c: rune) (i32 | void) = {
if (ascii.isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; }; // 'A'..'F'
};
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; }; // 'a'..'f'
};
return;
};
type lex = struct {
file: str,
src: *u8, // raw bytes; not necessarily NUL-terminated
@@ -1286,8 +1287,8 @@ fn escape(l: *lex, out: *i32) bool = {
// keeps the explicit "return false on impossible-void" path
// for symmetry with the other lexer error sites. Use `!`
// once we have a panic-with-position helper.
let h: i32 = ascii.digitval(hi: rune)!;
let lv: i32 = ascii.digitval(lo: rune)!;
let h: i32 = hexval(hi: rune)!;
let lv: i32 = hexval(lo: rune)!;
*out = (h << 4) | lv;
return true;
};
@@ -1530,12 +1531,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: rune)) {
if (isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
if (!isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -1579,7 +1580,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: rune)) { break; };
if (!isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -1728,7 +1729,7 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};

View File

@@ -1000,33 +1000,6 @@ export fn isxdigit(c: rune) bool = {
return false;
};
// digitval — value of `c` as a hex/decimal digit. void variant means
// `c` isn't a hex digit. Useful when scanning numeric literals.
export fn digitval(c: rune) (i32 | void) = {
if (isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; };
};
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; };
};
return;
};
// isidstart / isidpart — identifier classes used by the lexer.
// Alpha or '_' starts; alnum or '_' continues.
export fn isidstart(c: rune) bool = {
if (isalpha(c)) { return true; };
if (c == 95) { return true; }; // '_'
return false;
};
export fn isidpart(c: rune) bool = {
if (isalnum(c)) { return true; };
if (c == 95) { return true; };
return false;
};
// tolower / toupper — fold ASCII case. Non-letters pass through.
export fn tolower(c: rune) rune = {
if (isupper(c)) { return c + 32; };
@@ -1057,6 +1030,34 @@ use ascii;
use mem;
use tok;
// isidstart / isidpart — identifier classification. Lexer-local
// because the "alpha or '_' / alnum or '_'" set isn't part of Hare's
// ascii::; ascii::isalpha + the '_' check live here instead.
fn isidstart(c: rune) bool = {
if (ascii.isalpha(c)) { return true; };
if (c == 95) { return true; }; // '_'
return false;
};
fn isidpart(c: rune) bool = {
if (ascii.isalnum(c)) { return true; };
if (c == 95) { return true; };
return false;
};
// hexval — value of `c` as a hex digit (0..15) or void if not a hex
// digit. Used by string-literal `\xHH` escapes.
fn hexval(c: rune) (i32 | void) = {
if (ascii.isdigit(c)) { return (c - 48): i32; };
if (c >= 65) {
if (c <= 70) { return ((c - 65) + 10): i32; }; // 'A'..'F'
};
if (c >= 97) {
if (c <= 102) { return ((c - 97) + 10): i32; }; // 'a'..'f'
};
return;
};
type lex = struct {
file: str,
src: *u8, // raw bytes; not necessarily NUL-terminated
@@ -1286,8 +1287,8 @@ fn escape(l: *lex, out: *i32) bool = {
// keeps the explicit "return false on impossible-void" path
// for symmetry with the other lexer error sites. Use `!`
// once we have a panic-with-position helper.
let h: i32 = ascii.digitval(hi: rune)!;
let lv: i32 = ascii.digitval(lo: rune)!;
let h: i32 = hexval(hi: rune)!;
let lv: i32 = hexval(lo: rune)!;
*out = (h << 4) | lv;
return true;
};
@@ -1530,12 +1531,12 @@ fn lexnum(l: *lex, start: *pos, out: *tok) void = {
let pc: i32 = lpeek(l, 0u64);
if (pc >= 0) {
if (ascii.isidstart(pc: rune)) {
if (isidstart(pc: rune)) {
let sb: u64 = l.lpos;
for (true) {
let cc: i32 = lpeek(l, 0u64);
if (cc < 0) { break; };
if (!ascii.isidpart(cc: rune)) { break; };
if (!isidpart(cc: rune)) { break; };
lget(l);
};
let sl: u64 = l.lpos - sb;
@@ -1579,7 +1580,7 @@ fn lexident(l: *lex, start: *pos, out: *tok) void = {
for (true) {
let c: i32 = lpeek(l, 0u64);
if (c < 0) { break; };
if (!ascii.isidpart(c: rune)) { break; };
if (!isidpart(c: rune)) { break; };
lget(l);
};
let n: u64 = l.lpos - begin;
@@ -1728,7 +1729,7 @@ export fn lexnext(l: *lex, out: *tok) void = {
let c: i32 = lpeek(l, 0u64);
if (c >= 0) {
if (ascii.isidstart(c: rune)) { lexident(l, &start, out); return; };
if (isidstart(c: rune)) { lexident(l, &start, out); return; };
if (ascii.isdigit(c: rune)) { lexnum(l, &start, out); return; };
};