@bbob/parser
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -1,8 +0,5 @@ | ||
const Tokenizer = require('./Tokenizer'); | ||
const Parser = require('./Parser'); | ||
module.exports = function parse(input, options) { | ||
const tokenizer = new Tokenizer(input); | ||
const tokens = tokenizer.tokenize(); | ||
const parser = new Parser(tokens, options); | ||
const parser = new Parser(input, options); | ||
const ast = parser.parse(); | ||
@@ -9,0 +6,0 @@ |
@@ -13,3 +13,3 @@ const { | ||
isTagEnd, | ||
} = require('./token'); | ||
} = require('./Token'); | ||
@@ -21,2 +21,4 @@ const { | ||
const Tokenizer = require('./Tokenizer'); | ||
const createTagNode = (tag, attrs = {}, content = []) => ({ tag, attrs, content }); | ||
@@ -34,8 +36,12 @@ | ||
*/ | ||
module.exports = class Parser { | ||
constructor(tokens, options = {}) { | ||
this.tokens = tokens; | ||
class Parser { | ||
constructor(input, options = {}) { | ||
this.tokenizer = new Tokenizer(input, { | ||
onToken: (token) => { | ||
this.parseToken(token); | ||
}, | ||
}); | ||
this.options = options; | ||
this.closableTags = this.findNestedTags(); | ||
this.nodes = []; | ||
@@ -48,3 +54,3 @@ this.nestedNodes = []; | ||
isNestedTag(token) { | ||
return this.closableTags.indexOf(getTokenValue(token)) >= 0; | ||
return this.tokenizer.isTokenNested(token); | ||
} | ||
@@ -161,14 +167,21 @@ | ||
parseToken(token) { | ||
this.handleTagToken(token); | ||
this.handleCurTag(token); | ||
} | ||
parse() { | ||
let token; | ||
// eslint-disable-next-line no-cond-assign | ||
while (token = this.tokens.shift()) { | ||
if (!token) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
if (this.tokens) { | ||
let token; | ||
// eslint-disable-next-line no-cond-assign | ||
while (token = this.tokens.shift()) { | ||
if (!token) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
this.parseToken(token); | ||
} | ||
this.handleTagToken(token); | ||
this.handleCurTag(token); | ||
} else { | ||
this.tokens = this.tokenizer.tokenize(); | ||
} | ||
@@ -180,3 +193,3 @@ | ||
findNestedTags() { | ||
const tags = this.tokens.filter(isTagToken).reduce((acc, token) => { | ||
const tags = (this.tokens || []).filter(isTagToken).reduce((acc, token) => { | ||
acc[getTokenValue(token)] = true; | ||
@@ -205,4 +218,7 @@ | ||
} | ||
}; | ||
} | ||
new Parser('[Verse 2]').parse(); | ||
module.exports = Parser; | ||
module.exports.createTagNode = createTagNode; |
@@ -6,7 +6,10 @@ const { | ||
PLACEHOLDER_SPACE, PLACEHOLDER_SPACE_TAB, | ||
SLASH, | ||
} = require('./char'); | ||
const TOKEN = require('./token'); | ||
const Token = require('./Token'); | ||
const createTokenOfType = (type, value, line, row) => new Token(type, value, line, row); | ||
class Tokenizer { | ||
constructor(input) { | ||
constructor(input, options = {}) { | ||
this.buffer = input; | ||
@@ -19,14 +22,23 @@ this.colPos = 0; | ||
this.tokens = new Array(Math.floor(this.buffer.length)); | ||
this.dummyArray = ['', '', '', '']; | ||
this.dummyToken = createTokenOfType('', '', '', ''); | ||
this.wordToken = this.dummyArray; | ||
this.tagToken = this.dummyArray; | ||
this.attrNameToken = this.dummyArray; | ||
this.attrValueToken = this.dummyArray; | ||
this.wordToken = this.dummyToken; | ||
this.tagToken = this.dummyToken; | ||
this.attrNameToken = this.dummyToken; | ||
this.attrValueToken = this.dummyToken; | ||
this.attrTokens = []; | ||
this.options = options; | ||
} | ||
emitToken(token) { | ||
if (this.options.onToken) { | ||
this.options.onToken(token); | ||
} | ||
} | ||
appendToken(token) { | ||
this.tokenIndex += 1; | ||
this.tokens[this.tokenIndex] = token; | ||
this.emitToken(token); | ||
} | ||
@@ -43,3 +55,3 @@ | ||
flushWord() { | ||
if (this.wordToken[TOKEN.TYPE_ID] && this.wordToken[TOKEN.VALUE_ID]) { | ||
if (this.wordToken[Token.TYPE_ID] && this.wordToken[Token.VALUE_ID]) { | ||
this.appendToken(this.wordToken); | ||
@@ -51,3 +63,3 @@ this.wordToken = this.createWordToken(''); | ||
createWord(value, line, row) { | ||
if (this.wordToken[TOKEN.TYPE_ID] === '') { | ||
if (this.wordToken[Token.TYPE_ID] === '') { | ||
this.wordToken = this.createWordToken(value, line, row); | ||
@@ -58,15 +70,15 @@ } | ||
flushTag() { | ||
if (this.tagToken[TOKEN.TYPE_ID]) { | ||
if (this.tagToken[Token.TYPE_ID]) { | ||
// [] and [=] tag case | ||
if (this.tagToken[TOKEN.VALUE_ID] === '') { | ||
const value = this.attrValueToken[TOKEN.TYPE_ID] ? getChar(EQ) : ''; | ||
if (this.tagToken[Token.VALUE_ID] === '') { | ||
const value = this.attrValueToken[Token.TYPE_ID] ? getChar(EQ) : ''; | ||
const word = getChar(OPEN_BRAKET) + value + getChar(CLOSE_BRAKET); | ||
this.createWord('', 0, 0); | ||
this.wordToken[TOKEN.VALUE_ID] += word; | ||
this.wordToken[Token.VALUE_ID] += word; | ||
this.tagToken = this.dummyArray; | ||
this.tagToken = this.dummyToken; | ||
if (this.attrValueToken[TOKEN.TYPE_ID]) { | ||
this.attrValueToken = this.dummyArray; | ||
if (this.attrValueToken[Token.TYPE_ID]) { | ||
this.attrValueToken = this.dummyToken; | ||
} | ||
@@ -77,9 +89,9 @@ | ||
if (this.attrNameToken[TOKEN.TYPE_ID] && !this.attrValueToken[TOKEN.TYPE_ID]) { | ||
this.tagToken[TOKEN.VALUE_ID] += PLACEHOLDER_SPACE + this.attrNameToken[TOKEN.VALUE_ID]; | ||
this.attrNameToken = this.dummyArray; | ||
if (this.attrNameToken[Token.TYPE_ID] && !this.attrValueToken[Token.TYPE_ID]) { | ||
this.tagToken[Token.VALUE_ID] += PLACEHOLDER_SPACE + this.attrNameToken[Token.VALUE_ID]; | ||
this.attrNameToken = this.dummyToken; | ||
} | ||
this.appendToken(this.tagToken); | ||
this.tagToken = this.dummyArray; | ||
this.tagToken = this.dummyToken; | ||
} | ||
@@ -89,14 +101,14 @@ } | ||
flushUnclosedTag() { | ||
if (this.tagToken[TOKEN.TYPE_ID]) { | ||
const value = this.tagToken[TOKEN.VALUE_ID] + (this.attrValueToken[TOKEN.VALUE_ID] ? getChar(EQ) : ''); | ||
if (this.tagToken[Token.TYPE_ID]) { | ||
const value = this.tagToken[Token.VALUE_ID] + (this.attrValueToken[Token.VALUE_ID] ? getChar(EQ) : ''); | ||
this.tagToken[TOKEN.TYPE_ID] = TOKEN.TYPE_WORD; | ||
this.tagToken[TOKEN.VALUE_ID] = getChar(OPEN_BRAKET) + value; | ||
this.tagToken[Token.TYPE_ID] = Token.TYPE_WORD; | ||
this.tagToken[Token.VALUE_ID] = getChar(OPEN_BRAKET) + value; | ||
this.appendToken(this.tagToken); | ||
this.tagToken = this.dummyArray; | ||
this.tagToken = this.dummyToken; | ||
if (this.attrValueToken[TOKEN.TYPE_ID]) { | ||
this.attrValueToken = this.dummyArray; | ||
if (this.attrValueToken[Token.TYPE_ID]) { | ||
this.attrValueToken = this.dummyToken; | ||
} | ||
@@ -107,10 +119,10 @@ } | ||
flushAttrNames() { | ||
if (this.attrNameToken[TOKEN.TYPE_ID]) { | ||
if (this.attrNameToken[Token.TYPE_ID]) { | ||
this.attrTokens.push(this.attrNameToken); | ||
this.attrNameToken = this.dummyArray; | ||
this.attrNameToken = this.dummyToken; | ||
} | ||
if (this.attrValueToken[TOKEN.TYPE_ID]) { | ||
if (this.attrValueToken[Token.TYPE_ID]) { | ||
this.attrTokens.push(this.attrValueToken); | ||
this.attrValueToken = this.dummyArray; | ||
this.attrValueToken = this.dummyToken; | ||
} | ||
@@ -129,3 +141,3 @@ } | ||
if (this.tagToken[TOKEN.TYPE_ID]) { | ||
if (this.tagToken[Token.TYPE_ID]) { | ||
this.attrNameToken = this.createAttrNameToken(''); | ||
@@ -164,6 +176,6 @@ } else { | ||
charEQ(charCode) { | ||
if (this.tagToken[TOKEN.TYPE_ID]) { | ||
if (this.tagToken[Token.TYPE_ID]) { | ||
this.attrValueToken = this.createAttrValueToken(''); | ||
} else { | ||
this.wordToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
this.wordToken[Token.VALUE_ID] += getChar(charCode); | ||
} | ||
@@ -175,6 +187,6 @@ | ||
charQUOTEMARK(charCode) { | ||
if (this.attrValueToken[TOKEN.TYPE_ID] && this.attrValueToken[TOKEN.VALUE_ID] > 0) { | ||
if (this.attrValueToken[Token.TYPE_ID] && this.attrValueToken[Token.VALUE_ID] > 0) { | ||
this.flushAttrNames(); | ||
} else if (this.tagToken[TOKEN.TYPE_ID] === '') { | ||
this.wordToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
} else if (this.tagToken[Token.TYPE_ID] === '') { | ||
this.wordToken[Token.VALUE_ID] += getChar(charCode); | ||
} | ||
@@ -186,12 +198,12 @@ | ||
charWORD(charCode) { | ||
if (this.tagToken[TOKEN.TYPE_ID] && this.attrValueToken[TOKEN.TYPE_ID]) { | ||
this.attrValueToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
} else if (this.tagToken[TOKEN.TYPE_ID] && this.attrNameToken[TOKEN.TYPE_ID]) { | ||
this.attrNameToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
} else if (this.tagToken[TOKEN.TYPE_ID]) { | ||
this.tagToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
if (this.tagToken[Token.TYPE_ID] && this.attrValueToken[Token.TYPE_ID]) { | ||
this.attrValueToken[Token.VALUE_ID] += getChar(charCode); | ||
} else if (this.tagToken[Token.TYPE_ID] && this.attrNameToken[Token.TYPE_ID]) { | ||
this.attrNameToken[Token.VALUE_ID] += getChar(charCode); | ||
} else if (this.tagToken[Token.TYPE_ID]) { | ||
this.tagToken[Token.VALUE_ID] += getChar(charCode); | ||
} else { | ||
this.createWord(); | ||
this.wordToken[TOKEN.VALUE_ID] += getChar(charCode); | ||
this.wordToken[Token.VALUE_ID] += getChar(charCode); | ||
} | ||
@@ -249,27 +261,28 @@ | ||
createWordToken(value = '', line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_WORD, value, line, row); | ||
return createTokenOfType(Token.TYPE_WORD, value, line, row); | ||
} | ||
createTagToken(value, line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_TAG, value, line, row); | ||
return createTokenOfType(Token.TYPE_TAG, value, line, row); | ||
} | ||
createAttrNameToken(value, line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_ATTR_NAME, value, line, row); | ||
return createTokenOfType(Token.TYPE_ATTR_NAME, value, line, row); | ||
} | ||
createAttrValueToken(value, line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_ATTR_VALUE, value, line, row); | ||
return createTokenOfType(Token.TYPE_ATTR_VALUE, value, line, row); | ||
} | ||
createSpaceToken(value, line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_SPACE, value, line, row); | ||
return createTokenOfType(Token.TYPE_SPACE, value, line, row); | ||
} | ||
createNewLineToken(value, line = this.colPos, row = this.rowPos) { | ||
return this.createTokenOfType(TOKEN.TYPE_NEW_LINE, value, line, row); | ||
return createTokenOfType(Token.TYPE_NEW_LINE, value, line, row); | ||
} | ||
createTokenOfType(type, value, line = this.colPos, row = this.rowPos) { | ||
return [String(type), String(value), String(line), String(row)]; | ||
isTokenNested(token) { | ||
const value = getChar(OPEN_BRAKET) + getChar(SLASH) + Token.getTokenValue(token); | ||
return this.buffer.indexOf(value) > -1; | ||
} | ||
@@ -282,16 +295,17 @@ } | ||
module.exports = Tokenizer; | ||
module.exports.createTokenOfType = createTokenOfType; | ||
module.exports.TYPE = { | ||
WORD: TOKEN.TYPE_WORD, | ||
TAG: TOKEN.TYPE_TAG, | ||
ATTR_NAME: TOKEN.TYPE_ATTR_NAME, | ||
ATTR_VALUE: TOKEN.TYPE_ATTR_VALUE, | ||
SPACE: TOKEN.TYPE_SPACE, | ||
NEW_LINE: TOKEN.TYPE_NEW_LINE, | ||
WORD: Token.TYPE_WORD, | ||
TAG: Token.TYPE_TAG, | ||
ATTR_NAME: Token.TYPE_ATTR_NAME, | ||
ATTR_VALUE: Token.TYPE_ATTR_VALUE, | ||
SPACE: Token.TYPE_SPACE, | ||
NEW_LINE: Token.TYPE_NEW_LINE, | ||
}; | ||
module.exports.TOKEN = { | ||
TYPE_ID: TOKEN.TYPE_ID, | ||
VALUE_ID: TOKEN.VALUE_ID, | ||
LINE_ID: TOKEN.LINE_ID, | ||
COLUMN_ID: TOKEN.COLUMN_ID, | ||
TYPE_ID: Token.TYPE_ID, | ||
VALUE_ID: Token.VALUE_ID, | ||
LINE_ID: Token.LINE_ID, | ||
COLUMN_ID: Token.COLUMN_ID, | ||
}; | ||
{ | ||
"name": "@bbob/parser", | ||
"version": "1.0.4", | ||
"description": "Fast BB Code parser ", | ||
"version": "1.0.5", | ||
"description": "Fast, flexible, and lean implementation of BBcode parser", | ||
"homepage": "https://github.com/JiLiZART/bbob", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17045
547
0