refactor: circular deps part 14

This commit is contained in:
Nathan Walker
2025-07-10 15:47:29 -07:00
parent cebc78406b
commit e7ab426ee2
87 changed files with 2667 additions and 3403 deletions

View File

@ -1,8 +0,0 @@
interface CSSValue {
type: string;
string: string;
unit?: string;
value?: number;
}
export function parse(cssValue: string): Array<CSSValue>;

View File

@ -1,113 +0,0 @@
exports.parse = parse;
function parse(str) {
return new Parser(str).parse();
}
function Parser(str) {
this.str = str;
}
Parser.prototype.skip = function(m){
this.str = this.str.slice(m[0].length);
};
Parser.prototype.comma = function(){
var m = /^, */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'comma', string: ',' };
};
Parser.prototype.ident = function(){
var m = /^([\w-]+) */.exec(this.str);
if (!m) return;
this.skip(m);
return {
type: 'ident',
string: m[1]
}
};
Parser.prototype.int = function(){
var m = /^(([-\+]?\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
var n = ~~m[2];
var u = m[3];
return {
type: 'number',
string: m[1],
unit: u || '',
value: n
}
};
Parser.prototype.float = function(){
var m = /^(((?:[-\+]?\d+)?\.\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
var n = parseFloat(m[2]);
var u = m[3];
return {
type: 'number',
string: m[1],
unit: u || '',
value: n
}
};
Parser.prototype.number = function(){
return this.float() || this.int();
};
Parser.prototype.double = function(){
var m = /^"([^"]*)" */.exec(this.str);
if (!m) return m;
this.skip(m);
return {
type: 'string',
quote: '"',
string: '"' + m[1] + '"',
value: m[1]
}
};
Parser.prototype.single = function(){
var m = /^'([^']*)' */.exec(this.str);
if (!m) return m;
this.skip(m);
return {
type: 'string',
quote: "'",
string: "'" + m[1] + "'",
value: m[1]
}
};
Parser.prototype.string = function(){
return this.single() || this.double();
};
Parser.prototype.value = function(){
return this.number()
|| this.ident()
|| this.string()
|| this.comma();
};
Parser.prototype.parse = function(){
var vals = [];
while (this.str.length) {
var obj = this.value();
if (!obj) throw new Error('failed to parse near `' + this.str.slice(0, 10) + '...`');
vals.push(obj);
}
return vals;
};

View File

@ -0,0 +1,131 @@
export interface CSSValue {
type: string;
string: string;
unit?: string;
value?: number;
}
export interface CommaToken {
type: 'comma';
string: ',';
}
export interface IdentToken {
type: 'ident';
string: string;
}
export interface NumberToken {
type: 'number';
string: string;
unit: string;
value: number;
}
export interface StringToken {
type: 'string';
quote: '"' | "'";
string: string;
value: string;
}
export type Token = CommaToken | IdentToken | NumberToken | StringToken;
/**
* Parse a string into an array of tokens.
*/
export function parse(str: string): Token[] {
return new Parser(str).parse();
}
class Parser {
private str: string;
constructor(str: string) {
this.str = str;
}
private skip(match: RegExpExecArray): void {
this.str = this.str.slice(match[0].length);
}
/** Comma tokens: "," */
private comma(): CommaToken | undefined {
const m = /^, */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'comma', string: ',' };
}
/** Identifier tokens: word or dash sequences */
private ident(): IdentToken | undefined {
const m = /^([\w-]+) */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'ident', string: m[1] };
}
/** Integer tokens, possibly with unit */
private int(): NumberToken | undefined {
const m = /^(([-+]?\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
const n = parseInt(m[2], 10);
const u = m[3] || '';
return { type: 'number', string: m[1], unit: u, value: n };
}
/** Float tokens, possibly with unit */
private float(): NumberToken | undefined {
const m = /^(((?:[-+]?\d+)?\.\d+)(\S+)?) */.exec(this.str);
if (!m) return;
this.skip(m);
const n = parseFloat(m[2]);
const u = m[3] || '';
return { type: 'number', string: m[1], unit: u, value: n };
}
/** Number tokens, either float or int */
private number(): NumberToken | undefined {
return this.float() || this.int();
}
/** Double-quoted strings */
private double(): StringToken | undefined {
const m = /^"([^"]*)" */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'string', quote: '"', string: `"${m[1]}"`, value: m[1] };
}
/** Single-quoted strings */
private single(): StringToken | undefined {
const m = /^'([^']*)' */.exec(this.str);
if (!m) return;
this.skip(m);
return { type: 'string', quote: "'", string: `'${m[1]}'`, value: m[1] };
}
/** String tokens: single or double quoted */
private string(): StringToken | undefined {
return this.single() || this.double();
}
/** Attempt to parse any token */
private value(): Token | undefined {
return this.number() || this.ident() || this.string() || this.comma();
}
/** Run the full parse loop */
public parse(): Token[] {
const vals: Token[] = [];
while (this.str.length > 0) {
const obj = this.value();
if (!obj) {
throw new Error(`failed to parse near \`${this.str.slice(0, 10)}...\``);
}
vals.push(obj);
}
return vals;
}
}