UNPKG

@syntaxs/compiler

Version:

Compiler used to compile Syntax Script projects.

256 lines (255 loc) 6.92 kB
/** * Every token type a syntax script declaration file can contain. If something can't be recognized as a token, * it will be tokenized as {@link TokenType.Raw} to let strings include any character. * @author efekos * @version 1.0.0 * @since 0.0.1-alpha */ export var TokenType; (function (TokenType) { /** * `{`. */ TokenType[TokenType["OpenBrace"] = 0] = "OpenBrace"; /** * `}`. */ TokenType[TokenType["CloseBrace"] = 1] = "CloseBrace"; /** * `:::`. */ TokenType[TokenType["DefinitionEnd"] = 2] = "DefinitionEnd"; /** * `;`. */ TokenType[TokenType["Semicolon"] = 3] = "Semicolon"; /** * `,`. */ TokenType[TokenType["Comma"] = 4] = "Comma"; /** * `(`. */ TokenType[TokenType["OpenParen"] = 5] = "OpenParen"; /** * `)`. */ TokenType[TokenType["CloseParen"] = 6] = "CloseParen"; /** * `[`. */ TokenType[TokenType["OpenSquare"] = 7] = "OpenSquare"; /** * `]`. */ TokenType[TokenType["CloseSquare"] = 8] = "CloseSquare"; /** * `operator`. */ TokenType[TokenType["OperatorKeyword"] = 9] = "OperatorKeyword"; /** * `compile`. */ TokenType[TokenType["CompileKeyword"] = 10] = "CompileKeyword"; /** * Anything alphabetical. Can be used for various purposes, such as referencing a keyword or a string value. */ TokenType[TokenType["Identifier"] = 11] = "Identifier"; /** * `<`. */ TokenType[TokenType["OpenDiamond"] = 12] = "OpenDiamond"; /** * `>`. */ TokenType[TokenType["CloseDiamond"] = 13] = "CloseDiamond"; /** * `+s`. */ TokenType[TokenType["WhitespaceIdentifier"] = 14] = "WhitespaceIdentifier"; /** * Any numeric value that does not contain frictional digits, such as `3.14`. */ TokenType[TokenType["IntNumber"] = 15] = "IntNumber"; /** * `'`. */ TokenType[TokenType["SingleQuote"] = 16] = "SingleQuote"; /** * `"`. */ TokenType[TokenType["DoubleQuote"] = 17] = "DoubleQuote"; /** * `import`. */ TokenType[TokenType["ImportKeyword"] = 18] = "ImportKeyword"; /** * `export`. */ TokenType[TokenType["ExportKeyword"] = 19] = "ExportKeyword"; /** * Can be anything. Only characters that can't be assigned to any other token is assigned to a raw token. Using this token instead * of errors allow programmer to use any character they want in the string. */ TokenType[TokenType["Raw"] = 20] = "Raw"; /** * `|`. */ TokenType[TokenType["VarSeperator"] = 21] = "VarSeperator"; /** * `global`. */ TokenType[TokenType["GlobalKeyword"] = 22] = "GlobalKeyword"; /** * `function`. */ TokenType[TokenType["FunctionKeyword"] = 23] = "FunctionKeyword"; /** * `class`. */ TokenType[TokenType["ClassKeyword"] = 24] = "ClassKeyword"; /** * `imports` */ TokenType[TokenType["ImportsKeyword"] = 25] = "ImportsKeyword"; /** * Represents end of the file. There can't be any tokens after this token. It is not an actual token included in a source file, * rather a virtual token added by tokenizers. */ TokenType[TokenType["EndOfFile"] = 26] = "EndOfFile"; /** * `keyword`. */ TokenType[TokenType["KeywordKeyword"] = 27] = "KeywordKeyword"; /** * `rule`. */ TokenType[TokenType["RuleKeyword"] = 28] = "RuleKeyword"; })(TokenType || (TokenType = {})); /** * Every node type a syntax script declaration file can contain. * @author efekos * @since 0.0.1-alpha * @version 1.0.1 */ export var NodeType; (function (NodeType) { /** * {@link ProgramStatement}. */ NodeType[NodeType["Program"] = 0] = "Program"; //.# Statements /** * {@link OperatorStatement}. */ NodeType[NodeType["Operator"] = 1] = "Operator"; /** * {@link CompileStatement}. */ NodeType[NodeType["Compile"] = 2] = "Compile"; /** * {@link ImportStatement}. */ NodeType[NodeType["Import"] = 3] = "Import"; /** * {@link ImportsStatement}. */ NodeType[NodeType["Imports"] = 4] = "Imports"; /** * {@link FunctionStatement}. */ NodeType[NodeType["Function"] = 5] = "Function"; /** * {@link GlobalStatement}. */ NodeType[NodeType["Global"] = 6] = "Global"; /** * {@link KeywordStatement}. */ NodeType[NodeType["Keyword"] = 7] = "Keyword"; /** * {@link RuleStatement}. */ NodeType[NodeType["Rule"] = 8] = "Rule"; //.# Expressions /** * {@link PrimitiveTypeExpression}. */ NodeType[NodeType["PrimitiveType"] = 9] = "PrimitiveType"; /** * {@link WhitespaceIdentifierExpression}. */ NodeType[NodeType["WhitespaceIdentifier"] = 10] = "WhitespaceIdentifier"; /** * {@link VariableExpression}. */ NodeType[NodeType["Variable"] = 11] = "Variable"; /** * {@link StringExpression}. */ NodeType[NodeType["String"] = 12] = "String"; /** * {@link BraceExpression}. */ NodeType[NodeType["Brace"] = 13] = "Brace"; /** * {@link ParenExpression}. */ NodeType[NodeType["Paren"] = 14] = "Paren"; /** * {@link SquareExpression}. */ NodeType[NodeType["Square"] = 15] = "Square"; /** * {@link IdentifierExpression}. */ NodeType[NodeType["Identifier"] = 16] = "Identifier"; })(NodeType || (NodeType = {})); /** * An error that occured while tokenizing, parsing or compiling a file. * @author efekos * @version 1.0.4 * @since 0.0.1-alpha */ export class CompilerError extends Error { range; file; actions = []; /** * An error that occured while tokenizing a file. * @param {Range} range Range where the error is. * @param {string} message Error message. */ constructor(range, message, file, actions) { super(); this.range = range; this.message = message; this.file = file; this.name = 'CompilerError'; if (actions !== undefined) this.actions = actions; } } /** * Checks whether an error is a {@link CompilerError}. * @param {Error} error Any error. * @author efekos * @version 1.0.0 * @since 0.0.1-alpha * @returns Whether it is a {@link CompilerError} or not. */ export function isCompilerError(error) { return error.name === 'CompilerError'; } /** * Determines whether the given node matches the expected node type. * @param {Node} node Any node. * @param {NodeType} nodeType Expected node type. * @returns {boolean} True if the given node is of the expected node type, otherwise false. * @author efekos * @since 0.0.2-alpha * @version 1.0.0 */ export function statementIsA(node, nodeType) { return node.type === nodeType; }