UNPKG

nisp

Version:

A language that for easily build cross-language language

157 lines (156 loc) 5.99 kB
"use strict"; exports.__esModule = true; var context_1 = require("./context"); exports.Context = context_1["default"]; var state_1 = require("./state"); var tokenizer = function (style) { var table = (_a = { router: function (stream, context) { // console.log(context.state.map(i => State[i]), stream.peek()) var state = context.state[context.state.length - 1]; var handle = table[state]; if (!handle) { return null; } var ret = handle(stream, context); if (stream.pos === stream.string.length) { if (context.commitIndent > context.pushIndent) { context.pushIndent = context.commitIndent; ++context.indent; } else if (context.commitIndent < context.pushIndent) { context.pushIndent = context.commitIndent; --context.indent; } } return ret; } }, _a[state_1["default"].start] = function (stream, context) { context.state.pop(); context.state.push(state_1["default"].error); context.state.push(state_1["default"].primary); context.state.push(state_1["default"].empty); return table.router(stream, context); }, _a[state_1["default"].empty] = function (stream, context) { var blank = /^\s+/; if (stream.match(blank)) { return style.blank; } var comment = /^#.*/; if (stream.match(comment)) { return style.comment; } context.state.pop(); if (stream.eol()) { context.state.push(state_1["default"].empty); return style.blank; } return table.router(stream, context); }, _a[state_1["default"].primary] = function (stream, context) { var peek = stream.peek(); context.state.pop(); if (peek === '(') { ++context.commitIndent; context.named = true; context.state.push(state_1["default"].group); context.state.push(state_1["default"].empty); stream.next(); return style.groupStart; } if (peek === '\'' || peek === '"') { context.backed = false; context.string = peek; context.state.push(state_1["default"].string); stream.next(); if (context.named) { return style.name; } return style.string; } var number = /^[-+]?(?:0[xX][\da-fA-F]+|\d+\.?|\d*\.\d+(?:[eE]\d+)?)(?![^)(\x20\t\r\n'"])/; if (stream.match(number)) { context.state.push(state_1["default"].empty); if (context.named) { context.named = false; return style.name; } return style.number; } var bool = /^(?:true|false)\b/; if (stream.match(bool)) { context.state.push(state_1["default"].empty); if (context.named) { context.named = false; return style.name; } return style.boolean; } var nul = /^null\b/; if (stream.match(nul)) { context.state.push(state_1["default"].empty); if (context.named) { context.named = false; return style.name; } return style["null"]; } var identifier = /^[^)(\x20\t\r\n'"]+/; if (stream.match(identifier)) { context.state.push(state_1["default"].empty); if (context.named) { context.named = false; return style.name; } return style.identifier; } context.state.push(state_1["default"].error); return table.router(stream, context); }, _a[state_1["default"].string] = function (stream, context) { if (stream.peek() === context.string) { context.string = ''; context.backed = true; context.state.pop(); context.state.push(state_1["default"].empty); stream.next(); if (context.named) { context.named = false; return style.name; } return style.string; } var regex = new RegExp("^([^" + context.string + "\\\\]+|\\\\.)+"); if (stream.match(regex)) { if (context.named) { return style.name; } return style.string; } context.state.push(state_1["default"].error); return table.router(stream, context); }, _a[state_1["default"].group] = function (stream, context) { var peek = stream.peek(); if (peek === ')') { --context.commitIndent; context.named = false; context.state.pop(); context.state.push(state_1["default"].empty); stream.next(); return style.groupEnd; } context.state.push(state_1["default"].primary); return table.router(stream, context); }, _a[state_1["default"].error] = function (stream, _) { stream.skipToEnd(); return style.error; }, _a); return table; var _a; }; exports.tokenizer = tokenizer;