ocat-lang
Version:
A programming language for the web design and development
107 lines (106 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.tokenize = void 0;
const types_1 = require("./types");
const adapters_1 = require("./adapters/");
const utils_1 = require("./utils");
const error_1 = require("../../error");
const tokenize = (input) => {
const tokens = [];
try {
const words = (0, adapters_1.Lexical)(input);
for (const word of words) {
if (word.length === 0) {
continue;
}
const token = {
type: types_1.TokenType.Null,
value: word,
};
if (word.match(/\b(print|input|output)\b/)) {
token.type = types_1.TokenType.IO;
}
else if (word.match(/\b(int|float|string|bool|void)\b/)) {
token.type = types_1.TokenType.Datatype;
}
else if (word.match(/\b(if|else|while|for|return|break|continue)\b/)) {
token.type = types_1.TokenType.Conditional;
}
else if ((0, utils_1.isValue)(word)) {
token.type = types_1.TokenType.Value;
}
else if (word === "sc" || word === "source-control") {
token.type = types_1.TokenType.SCS;
}
else if (word.startsWith("{") || word.endsWith("}")) {
token.type = types_1.TokenType.Block;
}
else if (word === "[" || word === "]") {
token.type = types_1.TokenType.PageRequest;
}
else if (word === "\n") {
token.type = types_1.TokenType.EOL;
}
else if (word === "(" || word === ")") {
token.type = types_1.TokenType.Shape;
}
else if (word.match(/\b(func)\b/)) {
token.type = types_1.TokenType.Function;
}
else if (word.match(/\b(==|!=|<=|>=|<|>)\b/)) {
token.type = types_1.TokenType.Operator;
}
else if (word.match(/\b(for|while|do|forever|on)\b/)) {
token.type = types_1.TokenType.Loop;
}
else if (word === "=") {
token.type = types_1.TokenType.Assign;
}
else if (word === "meta" || word === "title") {
token.type = types_1.TokenType.Meta;
}
else if (word === "wexport" || word === "out/w") {
token.type = types_1.TokenType.ExportW;
}
else if (word === "func") {
token.type = types_1.TokenType.Function;
}
else if (word === "call") {
token.type = types_1.TokenType.FCall;
}
else if (word === "//" || word === "/<-") {
token.type = types_1.TokenType.Comment;
}
else if (word.match(/\@(.*)/)) {
token.type = types_1.TokenType.ORDER;
}
else if (word === "import") {
token.type = types_1.TokenType.IERQ;
}
else if (word === "component") {
token.type = types_1.TokenType.Component;
}
else if (word === 'layout') {
token.type = types_1.TokenType.Layout;
}
else if (word === 'rest') {
token.type = types_1.TokenType.RestI;
}
else if (word === 'loadComponent' || word === 'loadLayout' || word === "loadTemplate") {
token.type = types_1.TokenType.Load;
}
else {
token.type = types_1.TokenType.Identifier;
}
tokens.push(token);
}
}
catch (e) {
if (e instanceof SyntaxError) {
throw new error_1.CustomError(e.message, error_1.ErrorType.SyntaxError);
}
}
return tokens;
};
exports.tokenize = tokenize;