This commit is contained in:
Asaki Yuki 2026-01-24 21:42:11 +07:00
parent 99c294a9ba
commit 3719a3f6b3
6 changed files with 57 additions and 111 deletions

View file

@ -1,13 +1,13 @@
import { Binding } from "../types/properties/value.js"
export function FormatProperties(properties: any) {
const property_bags: Record<Binding, any> = {}
const property_bag: Record<Binding, any> = {}
for (const key in properties) {
const value = properties[key]
if (key.startsWith("#")) {
property_bags[<Binding>key] = value
property_bag[<Binding>key] = value
delete properties[key]
}
}
@ -17,11 +17,11 @@ export function FormatProperties(properties: any) {
delete properties.anchor
}
if (Object.keys(property_bags).length) {
if (properties.property_bags) {
properties.property_bags = { ...property_bags, ...properties.property_bags }
if (Object.keys(property_bag).length) {
if (properties.property_bag) {
properties.property_bag = { ...property_bag, ...properties.property_bag }
} else {
properties.property_bags = property_bags
properties.property_bag = property_bag
}
}

View file

@ -1,10 +1,13 @@
import { makeToken, TokenKind, Token, TSToken, TSTokenKind } from "./types.js"
import * as Checker from "./Checker.js"
export function Lexer(input: string, start: number = 0, end?: number) {
export function Lexer(input: string, start: number = 0, length?: number) {
const tokens: Token[] = []
if (input.length === 0) return tokens
length ||= input.length
console.log(input.slice(start, length))
let index = start
do {
@ -17,11 +20,18 @@ export function Lexer(input: string, start: number = 0, end?: number) {
case "#":
case "$": {
const start = index++
while (index < input.length) {
while (index < length) {
const token = input[index]
if (Checker.isWordChar(token)) index++
else break
else {
if (start + 1 === index) {
console.error(
`\x1b[31merror: ${input + "\n" + " ".repeat(index + 6) + "^"}\nInvalid character.\x1b[0m`,
)
throw new Error()
}
break
}
}
tokens.push(makeToken(input, TokenKind.VARIABLE, start, index-- - start))
@ -29,19 +39,6 @@ export function Lexer(input: string, start: number = 0, end?: number) {
break
}
case "'": {
const start = index++
do {
const token = input[index]
if (token === "'") break
} while (++index < input.length)
tokens.push(makeToken(input, TokenKind.STRING, start, index - start + 1))
break
}
case ",":
tokens.push(makeToken(input, TokenKind.COMMA, index))
break
@ -78,88 +75,39 @@ export function Lexer(input: string, start: number = 0, end?: number) {
else tokens.push(makeToken(input, TokenKind.OPERATOR, index))
break
case "f":
case "F": {
// string
case "'": {
const start = index++
do {
const token = input[index]
if (token === "'") break
} while (++index < length)
tokens.push(makeToken(input, TokenKind.STRING, start, index - start + 1))
break
}
// template string
case "f": {
if (input[index + 1] === "'") {
const tsTokens: TSToken[] = []
const start = ++index
const start = index
const tokenization = (start: number) => {
while (index < input.length) {
const templateStringTokens = (start: number) => {
while (index < length) {
const char = input[index]
if (char === "'") {
index++
eatString()
} else if (char === "}") {
tsTokens.push({
kind: TSTokenKind.EXPRESSION,
tokens: Lexer(input, start + 1, index),
})
break
}
index++
}
}
const stringification = (start: number) => {
while (index < input.length) {
const char = input[index]
if (char === "'") {
if (start + 1 !== index)
tsTokens.push({
kind: TSTokenKind.STRING,
tokens: {
kind: TokenKind.STRING,
start: start + 1,
length: index - start + 1,
value: `'${input.slice(start + 1, index)}'`,
},
})
break
} else if (char === "#" && input[index + 1] === "{") {
tsTokens.push({
kind: TSTokenKind.STRING,
tokens: {
value: `'${input.slice(start + 1, index)}'`,
kind: TokenKind.STRING,
length: index - start + 1,
start,
},
})
tokenization(++index)
start = index
}
index++
}
}
const eatString = () => {
while (index < input.length) {
const char = input[index]
if (char === "'") {
break
} else if (char === "#" && input[index + 1] === "{") {
index++
eatTemplate()
}
index++
}
}
const eatTemplate = () => {
while (index < input.length) {
const char = input[index]
if (char === "'") {
eatString()
} else if (char === "}") {
break
}
index++
}
}
stringification(index++)
tokens.push(makeToken(tsTokens, TokenKind.TEMPLATE_STRING, start - 1, index - start + 1))
templateStringTokens(index)
tokens.push(makeToken(tsTokens, TokenKind.TEMPLATE_STRING, start, index - start))
break
}
}
@ -181,7 +129,7 @@ export function Lexer(input: string, start: number = 0, end?: number) {
}
}
}
} while (++index < (end || input.length))
} while (++index < length)
return tokens
}

View file

@ -292,7 +292,7 @@ export class Parser {
out(): { gen?: BindingItem[]; out: Expression } {
return {
out: this.output,
out: `(${this.output})`,
gen: this.genBindings.map(
({ source, target }) =>
<BindingItem>{

View file

@ -11,7 +11,7 @@ export enum TokenKind {
OPERATOR,
COMMA,
EOF
EOF,
}
export enum TSTokenKind {
@ -55,9 +55,9 @@ export function makeToken<T extends TokenKind>(
length: number = 1,
): Token {
if (kind === TokenKind.TEMPLATE_STRING) {
return { value: input as TSToken[], kind: kind, start, length }
return { kind: kind, start, length, value: input as TSToken[] }
} else {
return { value: input.slice(start, start + length) as string, kind, start, length }
return { kind, start, length, value: input.slice(start, start + length) as string }
}
}

View file

@ -9,3 +9,5 @@ export * from "./types/enums/index.js"
export * as Properties from "./types/properties/index.js"
export { ItemAuxID } from "./types/enums/Items.js"
export * from "./compilers/bindings/index.js"