mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
refactor(lexer): remove PUNCT1_NKNOWN and OPMAP_NOPS consts (#26839)
Pure refactor. `PUNCT1_NKNOWN` and `OPMAP_NOPS` consts are just the length of `PUNCT1` and `OPMAP_OPS` tables. Remove them, and use `PUNCT1.len()` and `OPMAP_OPS.len()` instead. `.len()` on a static array is const-folded by the compiler, so it makes no difference to codegen, but it's clearer to read.
This commit is contained in:
1 parent
935a10e6bb
commit
2f9e30a476
5 files changed
+17
-22
No files matched your search
@@ -64,7 +64,6 @@ TAGS_PEV_FORMATE_DESCRIPTION = "TAGS_PEV_FORMATE_DESCRIPTION"
|
||||
# oxc_lexer
|
||||
tpos = "tpos"
|
||||
vor = "vor"
|
||||
PUNCT1_NKNOWN = "PUNCT1_NKNOWN"
|
||||
# Vendored react_compiler local-variable abbreviations (PrivateName, Optional{Call,Member}Expression, plural of `from`)
|
||||
pn = "pn"
|
||||
oce = "oce"
|
||||
|
||||
@@ -4,7 +4,7 @@ use crate::token::tk;
|
||||
|
||||
use crate::pipeline::{
|
||||
bytes::{is_digit, is_word, is_ws},
|
||||
tables::{PUNCT1, PUNCT1_NKNOWN, Tables, is_kw_init, is_kw_init_ts, is_op_char},
|
||||
tables::{PUNCT1, Tables, is_kw_init, is_kw_init_ts, is_op_char},
|
||||
};
|
||||
|
||||
const FL_WORD: u32 = 0;
|
||||
@@ -21,7 +21,7 @@ static CLS_TS: [u16; 256] = cls_table(true);
|
||||
const fn cls_table(ts: bool) -> [u16; 256] {
|
||||
let mut punct = [tk!(Invalid); 256];
|
||||
let mut i = 0;
|
||||
while i < PUNCT1_NKNOWN {
|
||||
while i < PUNCT1.len() {
|
||||
punct[PUNCT1[i].byte as usize] = PUNCT1[i].kind as u8;
|
||||
i += 1;
|
||||
}
|
||||
|
||||
@@ -9,13 +9,13 @@ mod punct1;
|
||||
pub(super) use keywords::{KwSet, is_kw_init, is_kw_init_ts};
|
||||
pub(super) use operators::is_op_char;
|
||||
|
||||
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx2", target_feature = "bmi2")))]
|
||||
pub(super) use punct1::PUNCT1;
|
||||
#[cfg(all(target_arch = "x86_64", target_feature = "avx2", target_feature = "bmi2"))]
|
||||
pub(super) use punct1::{PH_A, PH_B, PH_T0, PH_T1};
|
||||
#[cfg(not(all(target_arch = "x86_64", target_feature = "avx2", target_feature = "bmi2")))]
|
||||
pub(super) use punct1::{PUNCT1, PUNCT1_NKNOWN};
|
||||
|
||||
use keywords::{KEYWORDS_JS, KEYWORDS_TS, KW_HASH_HINT_JS, KW_HASH_HINT_TS, kwinit_selfcheck};
|
||||
use operators::{OPMAP_NOPS, OPMAP_OPS, OpMap, op_key, opch_selfcheck};
|
||||
use operators::{OPMAP_OPS, OpMap, op_key, opch_selfcheck};
|
||||
use punct1::punct1_hash_selfcheck;
|
||||
|
||||
#[repr(C, align(64))]
|
||||
@@ -128,7 +128,7 @@ impl Tables {
|
||||
fn build_op_pack(&mut self) {
|
||||
self.op2_pack = [0; 256];
|
||||
self.op3_pack = [0; 256];
|
||||
for i in 0..OPMAP_NOPS {
|
||||
for i in 0..OPMAP_OPS.len() {
|
||||
let o = &OPMAP_OPS[i];
|
||||
let c2 = if o.len >= 3 { o.txt[2] } else { 0 };
|
||||
let key = op_key(o.txt[0], o.txt[1], c2, o.len as u32);
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
use crate::token::{OP_KIND_BASE, OP_KIND_MAX, TokenKind, tk};
|
||||
|
||||
use super::punct1::{PUNCT1, PUNCT1_NKNOWN};
|
||||
|
||||
pub const OPMAP_NOPS: usize = 33;
|
||||
use super::punct1::PUNCT1;
|
||||
|
||||
pub(super) struct OpDef {
|
||||
pub txt: &'static [u8],
|
||||
@@ -17,7 +15,7 @@ impl OpDef {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) static OPMAP_OPS: [OpDef; OPMAP_NOPS] = [
|
||||
pub(super) static OPMAP_OPS: [OpDef; 33] = [
|
||||
OpDef::new("<=", TokenKind::Le),
|
||||
OpDef::new(">=", TokenKind::Ge),
|
||||
OpDef::new("==", TokenKind::EqEq),
|
||||
@@ -78,7 +76,7 @@ impl OpMap {
|
||||
}
|
||||
|
||||
fn opmap_init(&mut self) {
|
||||
for i in 0..OPMAP_NOPS {
|
||||
for i in 0..OPMAP_OPS.len() {
|
||||
let a = &OPMAP_OPS[i];
|
||||
assert!(
|
||||
a.len as usize == a.txt.len()
|
||||
@@ -86,7 +84,7 @@ impl OpMap {
|
||||
&& a.kind as u8 <= OP_KIND_MAX,
|
||||
"opmap.rs: bad OpDef {i}"
|
||||
);
|
||||
for j in (i + 1)..OPMAP_NOPS {
|
||||
for j in (i + 1)..OPMAP_OPS.len() {
|
||||
let b = &OPMAP_OPS[j];
|
||||
let a2 = if a.len >= 3 { a.txt[2] } else { 0 };
|
||||
let b2 = if b.len >= 3 { b.txt[2] } else { 0 };
|
||||
@@ -100,7 +98,7 @@ impl OpMap {
|
||||
while m < (1u64 << 28) {
|
||||
let mut used = [0u8; 256];
|
||||
let mut ok = true;
|
||||
for i in 0..OPMAP_NOPS {
|
||||
for i in 0..OPMAP_OPS.len() {
|
||||
let o = &OPMAP_OPS[i];
|
||||
let c2 = if o.len >= 3 { o.txt[2] } else { 0 };
|
||||
let key = op_key(o.txt[0], o.txt[1], c2, o.len as u32);
|
||||
@@ -114,7 +112,7 @@ impl OpMap {
|
||||
if ok {
|
||||
self.opmap_mul = m as u32;
|
||||
self.opmap_slot = [0xFF; 256];
|
||||
for i in 0..OPMAP_NOPS {
|
||||
for i in 0..OPMAP_OPS.len() {
|
||||
let o = &OPMAP_OPS[i];
|
||||
let c2 = if o.len >= 3 { o.txt[2] } else { 0 };
|
||||
let key = op_key(o.txt[0], o.txt[1], c2, o.len as u32);
|
||||
@@ -130,7 +128,7 @@ impl OpMap {
|
||||
|
||||
fn punct1_init(&mut self) {
|
||||
self.punct1_ord = [tk!(Invalid); 256];
|
||||
for i in 0..PUNCT1_NKNOWN {
|
||||
for i in 0..PUNCT1.len() {
|
||||
self.punct1_ord[PUNCT1[i].byte as usize] = PUNCT1[i].kind as u8;
|
||||
}
|
||||
}
|
||||
@@ -161,7 +159,7 @@ impl OpMap {
|
||||
|
||||
fn self_check(&self) {
|
||||
let mut seen_kind = [0u8; 256];
|
||||
for i in 0..OPMAP_NOPS {
|
||||
for i in 0..OPMAP_OPS.len() {
|
||||
let o = &OPMAP_OPS[i];
|
||||
let mut b = [0u8; 4];
|
||||
b[..o.len as usize].copy_from_slice(&o.txt[..o.len as usize]);
|
||||
@@ -183,7 +181,7 @@ impl OpMap {
|
||||
"opmap self-check: op spot-checks failed"
|
||||
);
|
||||
let mut seen = [0u8; 256];
|
||||
for i in 0..PUNCT1_NKNOWN {
|
||||
for i in 0..PUNCT1.len() {
|
||||
let ord = self.punct1_ord[PUNCT1[i].byte as usize];
|
||||
assert!(
|
||||
ord == PUNCT1[i].kind as u8 && seen[ord as usize] == 0,
|
||||
|
||||
@@ -15,12 +15,10 @@ impl Punct1 {
|
||||
}
|
||||
}
|
||||
|
||||
pub const PUNCT1_NKNOWN: usize = 26;
|
||||
|
||||
/// Single-byte punctuators and their [`TokenKind`]s.
|
||||
/// `#` maps to `Invalid` - a bare `#` is invalid on its own
|
||||
/// (private names and hashbangs are resolved earlier).
|
||||
pub const PUNCT1: [Punct1; PUNCT1_NKNOWN] = [
|
||||
pub const PUNCT1: [Punct1; 26] = [
|
||||
Punct1::new('(', TokenKind::LParen),
|
||||
Punct1::new(')', TokenKind::RParen),
|
||||
Punct1::new('[', TokenKind::LBracket),
|
||||
@@ -56,7 +54,7 @@ pub const PH_T1: [u8; 16] = [40, 255, 43, 50, 64, 61, 255, 255, 35, 36, 80, 89,
|
||||
|
||||
pub(super) fn punct1_hash_selfcheck() {
|
||||
let mut punct1_ord = [tk!(Invalid); 256];
|
||||
for i in 0..PUNCT1_NKNOWN {
|
||||
for i in 0..PUNCT1.len() {
|
||||
punct1_ord[PUNCT1[i].byte as usize] = PUNCT1[i].kind as u8;
|
||||
}
|
||||
for c in 0..256usize {
|
||||
|
||||
Reference in new issue
Block a user