diff --git a/src/compilers/FormatProperties.ts b/src/compilers/FormatProperties.ts index 3243e9a..2edf2d6 100644 --- a/src/compilers/FormatProperties.ts +++ b/src/compilers/FormatProperties.ts @@ -1,13 +1,13 @@ import { Binding } from "../types/properties/value.js" export function FormatProperties(properties: any) { - const property_bags: Record = {} + const property_bag: Record = {} for (const key in properties) { const value = properties[key] if (key.startsWith("#")) { - property_bags[key] = value + property_bag[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 } } diff --git a/src/compilers/bindings/Lexer.ts b/src/compilers/bindings/Lexer.ts index 29be94f..2d0637d 100644 --- a/src/compilers/bindings/Lexer.ts +++ b/src/compilers/bindings/Lexer.ts @@ -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 } diff --git a/src/compilers/bindings/Parser.ts b/src/compilers/bindings/Parser.ts index 537267d..0a169e0 100644 --- a/src/compilers/bindings/Parser.ts +++ b/src/compilers/bindings/Parser.ts @@ -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 }) => { diff --git a/src/compilers/bindings/types.ts b/src/compilers/bindings/types.ts index 31e0195..183f809 100644 --- a/src/compilers/bindings/types.ts +++ b/src/compilers/bindings/types.ts @@ -11,7 +11,7 @@ export enum TokenKind { OPERATOR, COMMA, - EOF + EOF, } export enum TSTokenKind { @@ -55,9 +55,9 @@ export function makeToken( 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 } } } diff --git a/src/index.ts b/src/index.ts index 6fbd040..9fb78aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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" diff --git a/test/app.ts b/test/app.ts index 9fc8cd6..b351844 100644 --- a/test/app.ts +++ b/test/app.ts @@ -1,14 +1,10 @@ -import { BagBinding, f, ItemAuxID, Label } from ".." +import { Panel } from ".." -const text = Label({ - text: "#text", - [BagBinding.ITEM_ID_AUX]: ItemAuxID.DIAMOND, - "#x": 2, +const panel = Panel() + +panel.addBindings({ + source_property_name: `[ $abc ]`, + target_property_name: "#text", }) -text.addBindings({ - source_property_name: f(`Test: #{ -(${BagBinding.ITEM_ID_AUX} % #x) == 0 }`), - target_property_name: BagBinding.TEXT, -}) - -console.log(text) +console.log(panel)