UNPKG

swift-pattern-compiler

Version:

A compiler which transforms SWIFT patterns to an object representation with additional information's.

138 lines (137 loc) 4.28 kB
import { IAst, ILine, INodeLength, INodeLine, IToken } from "./interfaces"; /** * The parser takes an array of tokens and compute into an AST. * * ``` * const tokens = [ * { type: "num", value: "35" }, * { type: "char", value: "a" } * ]; * const ast = parser(tokens); * { * type: "SwiftPattern", * body: [ * { * type: "line", * nodes: [ * { * type: "field", * length: { min: 0, max: 35 }, * char: "a" * }, * ] * } * ] * } * ``` * * @param tokens the SWIFT field pattern string * @returns the tokens array */ export declare const parser: (tokens: IToken[]) => IAst; /** * The parse function takes an array of tokens and compute them into an * array of nodes. * * ``` * const tokens = [ * { type: ETokenType.num, value: "35" }, * { type: ETokenType.char, value: "a" }, * { type: ETokenType.bracket, value: "[" }, * { type: ETokenType.num, value: "3" }, * { type: ETokenType.quant, value: "-" }, * { type: ETokenType.num, value: "20" }, * { type: ETokenType.char, value: "c" }, * { type: ETokenType.bracket, value: "]" }, * ]; * const ast = parser(tokens); * { * type: "line", * nodes: [ * { * type: "field", * length: { min: 0, max: 35 }, * char: "a", * }, * { * type: "optionalField", * nodes: [ * { * type: "field", * length: { min: 3, max: 20 }, * char: "c", * }, * ], * }, * ], * }, * ``` * * @param tokens */ export declare const parse: (tokens: IToken[], optional?: boolean | undefined) => INodeLine; /** * The splitLines function takes an array of tokens and compute into an * array of lines with the corresponding tokens. * * ``` * const tokens = [ * { type: ETokenType.num, value: "35" }, * { type: ETokenType.char, value: "a" }, * { type: ETokenType.bracket, value: "[" }, * { type: ETokenType.num, value: "3" }, * { type: ETokenType.quant, value: "-" }, * { type: ETokenType.num, value: "20" }, * { type: ETokenType.char, value: "c" }, * { type: ETokenType.bracket, value: "]" }, * { type: ETokenType.num, value: "2" }, * { type: ETokenType.multilineQuant, value: "*" }, * { type: ETokenType.num, value: "35" }, * { type: ETokenType.char, value: "a" }, * ]; * const ast = parser(tokens); * [ * { * type: "line", * tokens: [ * { type: "num", value: "35" }, * { type: "char", value: "a" }, * { type: "bracket", value: "[" }, * { type: "num", value: "3" }, * { type: "quant", value: "-" }, * { type: "num", value: "20" }, * { type: "char", value: "c" }, * { type: "bracket", value: "]" }, * ] * }, * { * type: "line", * tokens: [ * { type: "num", value: "35" }, * { type: "char", value: "a" }, * ] * }, * { * type: "line", * tokens: [ * { type: "num", value: "35" }, * { type: "char", value: "a" }, * ] * } * ] * ``` * * @param tokens */ export declare const splitLines: (tokens: IToken[]) => ILine[]; /** * Returns the token when it is defined or an undefined token. * @param token */ export declare const getToken: (token?: IToken | undefined) => IToken; /** * Returns an length object based on the provided quantifier. * @param num1 * @param quant */ export declare const getLengthObj: (num1: string, quant?: string | undefined, num2?: string | undefined) => INodeLength;