UNPKG

unnestcss

Version:
1 lines 5.7 kB
{"mappings":"AA4BA,OAAO,cAAM,QAAS;AAuCtB,OAAO,cAAM,UAAU;CACrB;CACA;CACA;CACA;CACA;CACA;CACA,YAAY;CAKZ;CAeA;CAeA;CAIA,MAAM,eAAe;CAIrB,QAAQ;CAaR;CAIA,WAAW;CAaX,UAAU,WAAW;CAgBrB,SAAS,eAAe;CAexB,UAAU,WAAW;CA4BrB,UAAU,cAAc;CAaxB,WAAW","names":[],"sources":["../../../../src/lib/stylis/tokenizer.ts"],"sourcesContent":["import {\n ASTERISK,\n AT,\n CARRIAGE_RETURN,\n COLON,\n COMMA,\n DOUBLE_QUOTE,\n EXCLAMATION,\n GREATER_THAN,\n HORIZONTAL_TAB,\n LEFT_CURLY_BRACKET,\n LEFT_PARENTHESIS,\n LEFT_SQUARE_BRACKET,\n LINE_FEED,\n NULL_CHARACTER,\n PLUS,\n REVERSE_SOLIDUS,\n RIGHT_CURLY_BRACKET,\n RIGHT_PARENTHESIS,\n RIGHT_SQUARE_BRACKET,\n SEMICOLON,\n SINGLE_QUOTE,\n SOLIDUS,\n SPACE,\n TILDE,\n} from \"./enum.ts\";\nimport { charCodeAt, isHexChar } from \"./utility.ts\";\n\nexport const token = (type: string): number => {\n switch (type) {\n // \\0 \\t \\n \\r whitespace token\n case NULL_CHARACTER:\n case HORIZONTAL_TAB:\n case LINE_FEED:\n case CARRIAGE_RETURN:\n case SPACE:\n return 5;\n // ! + , / > @ ~ isolate token\n case EXCLAMATION:\n case PLUS:\n case COMMA:\n case SOLIDUS:\n case GREATER_THAN:\n case AT:\n case TILDE:\n // ; { } breakpoint token\n case SEMICOLON:\n case LEFT_CURLY_BRACKET:\n case RIGHT_CURLY_BRACKET:\n return 4;\n // : accompanied token\n case COLON:\n return 3;\n // \" ' ( [ opening delimit token\n case DOUBLE_QUOTE:\n case SINGLE_QUOTE:\n case LEFT_PARENTHESIS:\n case LEFT_SQUARE_BRACKET:\n return 2;\n // ) ] closing delimit token\n case RIGHT_PARENTHESIS:\n case RIGHT_SQUARE_BRACKET:\n return 1;\n }\n return 0;\n};\n\nexport class Tokenizer {\n line = 1;\n column = 1;\n length = 0;\n position = 0;\n character?: string;\n characters: string;\n constructor(value: string) {\n this.characters = value;\n this.length = this.characters.length;\n }\n\n prev(): string {\n if (this.position > 0) {\n this.position--;\n this.character = this.characters[this.position];\n } else {\n this.character = NULL_CHARACTER;\n }\n this.column--;\n if (this.character === LINE_FEED) {\n this.column = 1;\n this.line--;\n }\n return this.character;\n }\n\n next(): string {\n if (this.position < this.length) {\n this.character = this.characters[this.position];\n this.position++;\n } else {\n this.character = NULL_CHARACTER;\n }\n this.column++;\n if (this.character === LINE_FEED) {\n this.column = 1;\n this.line++;\n }\n return this.character;\n }\n\n peek(): string {\n return this.characters[this.position] || NULL_CHARACTER;\n }\n\n slice(begin: number, end: number): string {\n return this.characters.slice(begin, end);\n }\n\n delimit(type: string): string {\n return this.slice(\n this.position - 1,\n this._delimiter(\n type === LEFT_SQUARE_BRACKET\n ? RIGHT_SQUARE_BRACKET\n : type === LEFT_PARENTHESIS\n ? RIGHT_PARENTHESIS\n : type,\n ),\n ).trim();\n }\n\n tokenize(): string[] {\n return this._tokenizer([]);\n }\n\n whitespace(type: string): string {\n this.character = this.peek();\n while (this.character !== NULL_CHARACTER) {\n this.character = this.peek();\n if (charCodeAt(this.character) < 33) {\n this.next();\n } else {\n break;\n }\n }\n return token(type) > 2 || token(this.character) > 3 ? \"\" : SPACE;\n }\n\n protected _tokenizer(children: string[]): string[] {\n while (this.next() !== NULL_CHARACTER) {\n switch (token(this.character)) {\n case 0:\n children.push(this.identifier(this.position - 1));\n break;\n case 2:\n children.push(this.delimit(this.character));\n break;\n default:\n children.push(this.character);\n }\n }\n return children;\n }\n\n escaping(index: number, count: number): string {\n count--;\n while (count && this.next() !== NULL_CHARACTER) {\n count--;\n // not 0-9 A-F a-f\n if (!isHexChar(this.character)) {\n break;\n }\n }\n return this.slice(\n index,\n this.position + +(count < 6 && this.peek() === SPACE && this.next() === SPACE),\n );\n }\n\n protected _delimiter(type: string): number {\n while (this.next() !== NULL_CHARACTER) {\n switch (this.character) {\n // ] ) \" '\n case type:\n return this.position;\n // \" '\n case DOUBLE_QUOTE:\n case SINGLE_QUOTE:\n if (type !== DOUBLE_QUOTE && type !== SINGLE_QUOTE) {\n this._delimiter(this.character);\n }\n break;\n // (\n case LEFT_PARENTHESIS:\n if (type === RIGHT_PARENTHESIS) {\n this._delimiter(type);\n }\n break;\n // \\\n case REVERSE_SOLIDUS:\n this.next();\n break;\n }\n }\n return this.position;\n }\n\n commenter(type: string, index: number): string {\n while (this.next() !== NULL_CHARACTER) {\n // //\n if (type === SOLIDUS && this.character === LINE_FEED) {\n break;\n } // /*\n if (type === ASTERISK && this.character === ASTERISK && this.peek() === SOLIDUS) {\n break;\n }\n }\n return `/*${this.slice(index, this.position - 1)}*${type === SOLIDUS ? type : this.next()}`;\n }\n\n identifier(index: number): string {\n while (!token(this.peek())) {\n this.next();\n }\n return this.slice(index, this.position);\n }\n}\n"],"version":3,"file":"tokenizer.d.ts"}