2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-05-15 11:59:37 +03:00

feat(parser): better line and column counting support in tokens

This commit is contained in:
Nikolay Kostyurin
2018-10-07 22:20:49 +02:00
parent d610630801
commit 1c3bebef22
2 changed files with 30 additions and 5 deletions
+6
View File
@@ -52,6 +52,12 @@ const convertTagToText = (token) => {
};
class Token {
/**
* @param {String} type
* @param {String} value
* @param line
* @param row
*/
constructor(type, value, line, row) {
this[TOKEN_TYPE_ID] = String(type);
this[TOKEN_VALUE_ID] = String(value);
+24 -5
View File
@@ -15,11 +15,15 @@ import { Token, TYPE_ATTR_NAME, TYPE_ATTR_VALUE, TYPE_NEW_LINE, TYPE_SPACE, TYPE
const EM = '!';
const createCharGrabber = (source) => {
const createCharGrabber = (source, { onSkip } = {}) => {
let idx = 0;
const skip = () => {
idx += 1;
if (onSkip) {
onSkip();
}
};
const hasNext = () => source.length > idx;
const getRest = () => source.substr(idx);
@@ -40,9 +44,6 @@ const createCharGrabber = (source) => {
getNext: () => source[idx + 1],
getPrev: () => source[idx - 1],
getCurr: () => source[idx],
moveIdxTo: (val) => {
idx += val;
},
getRest,
substrUntilChar: (char) => {
const restStr = getRest();
@@ -72,6 +73,20 @@ const trimChar = (str, charToRemove) => {
const unquote = str => str.replace(BACKSLASH + QUOTEMARK, QUOTEMARK);
const createToken = (type, value, r = 0, cl = 0) => new Token(type, value, r, cl);
/**
* @typedef {Object} Lexer
* @property {Function} tokenize
* @property {Function} isTokenNested
*/
/**
* @param {String} buffer
* @param {Object} options
* @param {Function} options.onToken
* @param {String} options.openTag
* @param {String} options.closeTag
* @return {Lexer}
*/
function createLexer(buffer, options = {}) {
let row = 0;
let col = 0;
@@ -158,7 +173,11 @@ function createLexer(buffer, options = {}) {
return { tag: tagName, attrs: attrTokens };
};
const bufferGrabber = createCharGrabber(buffer);
const bufferGrabber = createCharGrabber(buffer, {
onSkip: () => {
col++;
},
});
const next = () => {
const char = bufferGrabber.getCurr();