From 0bea22d3ca0f09f1019eee37ba4f8ab63ad49782 Mon Sep 17 00:00:00 2001 From: Nikolay Kostyurin Date: Thu, 12 Jul 2018 23:21:22 +0200 Subject: [PATCH] fix parse tags in brackets --- packages/bbob-parser/lib/Tokenizer.js | 26 +++++++++++++++++---- packages/bbob-parser/test/Tokenizer.test.js | 17 ++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/packages/bbob-parser/lib/Tokenizer.js b/packages/bbob-parser/lib/Tokenizer.js index 7ee8c6e..f88133a 100644 --- a/packages/bbob-parser/lib/Tokenizer.js +++ b/packages/bbob-parser/lib/Tokenizer.js @@ -177,14 +177,30 @@ class Tokenizer { this.colPos = 0; } - charOPENBRAKET() { - this.flushWord(); - this.tagToken = this.createTagToken(''); + charOPENBRAKET(charCode) { + const nextCharCode = this.seekChar(1); + const isNextSpace = nextCharCode === SPACE || nextCharCode === TAB; + + if (isNextSpace) { + this.createWord(); + this.wordToken[Token.VALUE_ID] += getChar(charCode); + } else { + this.flushWord(); + + this.tagToken = this.createTagToken(''); + } this.nextCol(); } - charCLOSEBRAKET() { + charCLOSEBRAKET(charCode) { + const prevCharCode = this.seekChar(-1); + const isPrevSpace = prevCharCode === SPACE || prevCharCode === TAB; + + if (isPrevSpace) { + this.wordToken[Token.VALUE_ID] += getChar(charCode); + } + this.nextCol(); this.flushTag(); this.flushAttrNames(); @@ -328,6 +344,8 @@ class Tokenizer { } } +new Tokenizer('[ [g]G[/g] ]').tokenize() + module.exports = Tokenizer; module.exports.createTokenOfType = createTokenOfType; module.exports.TYPE = { diff --git a/packages/bbob-parser/test/Tokenizer.test.js b/packages/bbob-parser/test/Tokenizer.test.js index ac6e222..afa2e18 100644 --- a/packages/bbob-parser/test/Tokenizer.test.js +++ b/packages/bbob-parser/test/Tokenizer.test.js @@ -53,6 +53,23 @@ describe('Tokenizer', () => { expectOutput(output, tokens); }); + test('tokenize tags in brakets', () => { + const input = '[ [h1]G[/h1] ]'; + const tokens = tokenize(input); + + const output = [ + [TYPE.WORD, '[', '0', '0'], + [TYPE.SPACE, ' ', '1', '0'], + [TYPE.TAG, 'h1', '2', '0'], + [TYPE.WORD, 'G', '1', '0'], + [TYPE.TAG, '/h1', '7', '0'], + [TYPE.SPACE, ' ', '12', '0'], + [TYPE.WORD, ']', '7', '0'], + ]; + + expectOutput(output, tokens); + }); + test('tokenize tag as param', () => { const input = '[color="#ff0000"]Text[/color]'; const tokens = tokenize(input);