Comparing version 0.1.3 to 0.1.4
@@ -1,150 +0,46 @@ | ||
var machine_1 = require('./machine'); | ||
var SPLIT_TOKEN = '|'; | ||
var STATE_TOKEN = ':'; | ||
/* | ||
* | prev | str1 | str2 | reg | after | ||
* ----- | ------ | ----- | ----- | --------- | ----- | ||
* '\'' | prev:str1 | after | str2 | reg | error2 | ||
* '"' | prev:str2 | str1 | after | reg | error2 | ||
* '[' | reg | str1 | str2 | reg | error2 | ||
* ']' | error1 | str1 | str2 | reg:after | error2 | ||
* '\\]' | error1 | str1 | str2 | reg | error2 | ||
* '\\\''| error1 | str1 | str2 | reg | error2 | ||
* '\\"' | error1 | str1 | str2 | reg | error2 | ||
* '|' | end | str1 | str2 | reg | end | ||
* [\\s] | prev | str1 | str2 | reg | after | ||
* | error1 | str1 | str2 | reg | error2 | ||
*/ | ||
var parser = new machine_1.Machine(); | ||
var reg = /[\s]/; | ||
parser.states = ['prev', 'str1', 'str2', 'reg', 'after']; | ||
parser.symbols = ['\'', '"', '[', ']', '\\]', '\\\'', '\\"', '|', reg]; | ||
parser.table = new Map(); | ||
parser.firstState = 'prev'; | ||
var stateMap1 = new Map(); | ||
parser.table.set('\'', stateMap1); | ||
stateMap1.set('prev', { prevState: 'prev', nextState: 'str1' }); | ||
stateMap1.set('str1', { prevState: 'after', nextState: 'after' }); | ||
stateMap1.set('str2', { prevState: 'str2', nextState: 'str2' }); | ||
stateMap1.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap1.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap2 = new Map(); | ||
parser.table.set('"', stateMap2); | ||
stateMap2.set('prev', { prevState: 'prev', nextState: 'str2' }); | ||
stateMap2.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap2.set('str2', { prevState: 'after', nextState: 'after' }); | ||
stateMap2.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap2.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap3 = new Map(); | ||
parser.table.set('[', stateMap3); | ||
stateMap3.set('prev', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap3.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap3.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap3.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap3.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap4 = new Map(); | ||
parser.table.set(']', stateMap4); | ||
stateMap4.set('prev', { prevState: 'error1', nextState: 'error1' }); | ||
stateMap4.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap4.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap4.set('reg', { prevState: 'reg', nextState: 'after' }); | ||
stateMap4.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap5 = new Map(); | ||
parser.table.set('\\]', stateMap5); | ||
stateMap5.set('prev', { prevState: 'error1', nextState: 'error1' }); | ||
stateMap5.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap5.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap5.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap5.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap6 = new Map(); | ||
parser.table.set('\\\'', stateMap6); | ||
stateMap6.set('prev', { prevState: 'error1', nextState: 'error1' }); | ||
stateMap6.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap6.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap6.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap6.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap7 = new Map(); | ||
parser.table.set('\\"', stateMap7); | ||
stateMap7.set('prev', { prevState: 'error1', nextState: 'error1' }); | ||
stateMap7.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap7.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap7.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap7.set('after', { prevState: 'error2', nextState: 'error2' }); | ||
var stateMap8 = new Map(); | ||
parser.table.set('|', stateMap8); | ||
stateMap8.set('prev', { prevState: 'end', nextState: 'end' }); | ||
stateMap8.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap8.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap8.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap8.set('after', { prevState: 'end', nextState: 'end' }); | ||
var stateMap9 = new Map(); | ||
parser.table.set(reg, stateMap9); | ||
stateMap9.set('prev', { prevState: 'prev', nextState: 'prev' }); | ||
stateMap9.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap9.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap9.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap9.set('after', { prevState: 'after', nextState: 'after' }); | ||
var stateMap10 = new Map(); | ||
parser.table.set('', stateMap10); | ||
stateMap10.set('prev', { prevState: 'error1', nextState: 'error1' }); | ||
stateMap10.set('str1', { prevState: 'str1', nextState: 'str1' }); | ||
stateMap10.set('str2', { prevState: 'st2', nextState: 'str2' }); | ||
stateMap10.set('reg', { prevState: 'reg', nextState: 'reg' }); | ||
stateMap10.set('after', { prevState: 'end', nextState: 'end' }); | ||
function parseSymbol(line) { | ||
var isEnd = false; | ||
var isReg = false; | ||
var symbol = ''; | ||
var rest = ''; | ||
parser.parse(line, function (state, token, index) { | ||
// 'prev', 'str1', 'str2', 'reg', 'after', 'end', 'error1', 'error2' | ||
switch (state) { | ||
case 'prev': | ||
case 'after': | ||
break; | ||
case 'str1': | ||
case 'str2': | ||
symbol += token; | ||
break; | ||
case 'reg': | ||
symbol += token; | ||
isReg = true; | ||
break; | ||
case 'end': | ||
isEnd = true; | ||
rest = line.substr(index + token.length); | ||
break; | ||
case 'error1': | ||
throw new Error('Wrong format: Your symbol should start with \' or " or ['); | ||
case 'error2': | ||
throw new Error('Wrong format: Your symbol should end with \' or " or ['); | ||
} | ||
}); | ||
if (!isEnd) | ||
throw new Error('Wrong format: Your symbol is not end'); | ||
return { symbol: isReg ? new RegExp(symbol) : symbol, rest: rest }; | ||
var ESCAPE_MAP = { | ||
'&': '&', | ||
'&line;': '|', | ||
':': ':', | ||
' ': ' ' | ||
}; | ||
function escape(source) { | ||
var regStr = '(?:' + Object.keys(ESCAPE_MAP).join('|') + ')'; | ||
var replacer = new RegExp(regStr, 'g'); | ||
return source.replace(replacer, function (key) { return ESCAPE_MAP[key]; }); | ||
} | ||
function splitStateLine(line) { | ||
var states = line.split(SPLIT_TOKEN); | ||
states.shift(); // remove the first one | ||
return states.map(function (item) { return item.trim(); }); | ||
function translateLine(line) { | ||
var lineStates = line.split(SPLIT_TOKEN).map(function (item) { return item.trim(); }); | ||
var origin = lineStates.shift(); | ||
var symbol; | ||
if (origin.indexOf(STATE_TOKEN) === 0) { | ||
origin = escape(origin.substr(1)); | ||
symbol = new RegExp(origin); | ||
} | ||
else { | ||
symbol = escape(origin); | ||
} | ||
return { symbol: symbol, lineStates: lineStates }; | ||
} | ||
function splitTokenLine(line) { | ||
var list = line.split(SPLIT_TOKEN); | ||
return list.map(function (item) { | ||
var state = item.trim(); | ||
if (~state.indexOf(STATE_TOKEN)) { | ||
var states = item.split(STATE_TOKEN); | ||
var prevState = (states[0] || '').trim(); | ||
var nextState = (states[1] || '').trim(); | ||
return { prevState: prevState, nextState: nextState }; | ||
} | ||
else { | ||
return { prevState: state, nextState: state }; | ||
} | ||
}); | ||
function parseStateMap(state) { | ||
state = state.trim(); | ||
if (~state.indexOf(STATE_TOKEN)) { | ||
var states = state.split(STATE_TOKEN); | ||
var prevState = (states[0] || '').trim(); | ||
var nextState = (states[1] || '').trim(); | ||
return { prevState: prevState, nextState: nextState }; | ||
} | ||
else { | ||
return { prevState: state, nextState: state }; | ||
} | ||
} | ||
function symbolSorter(left, right) { | ||
var isLeftReg = typeof left !== 'string'; | ||
var isRightReg = typeof right !== 'string'; | ||
// -1 表示left更靠前 | ||
var leftType = (typeof left === 'string') ? 0 : 1; | ||
var rightType = (typeof right === 'string') ? 0 : 1; | ||
if (leftType - rightType) { | ||
return leftType - rightType; | ||
} | ||
if (typeof left === 'string' && typeof right === 'string') { | ||
@@ -158,11 +54,3 @@ if (left.indexOf(right) === 0) | ||
} | ||
else if (!isLeftReg && isRightReg) { | ||
return 1; | ||
} | ||
else if (isLeftReg && !isRightReg) { | ||
return -1; | ||
} | ||
else { | ||
return 0; | ||
} | ||
return 0; | ||
} | ||
@@ -174,12 +62,13 @@ function parseTable(source) { | ||
var lines = source.trim().split('\n'); | ||
states = splitStateLine(lines[0]); | ||
states = translateLine(lines[0]).lineStates; | ||
for (var i = 2, len = lines.length; i < len; i++) { | ||
var line = lines[i]; | ||
var _a = parseSymbol(line), symbol = _a.symbol, rest = _a.rest; | ||
var _a = translateLine(line), symbol = _a.symbol, lineStates = _a.lineStates; | ||
var stateMap = new Map(); | ||
var stateList = splitTokenLine(rest); | ||
for (var j = 0, lenj = stateList.length; j < lenj; j++) { | ||
stateMap.set(states[j], stateList[j]); | ||
for (var j = 0, lenj = states.length; j < lenj; j++) { | ||
stateMap.set(states[j], parseStateMap(lineStates[j] || '')); | ||
} | ||
symbols.push(symbol); | ||
if (symbol) { | ||
symbols.push(symbol); | ||
} | ||
table.set(symbol, stateMap); | ||
@@ -186,0 +75,0 @@ } |
{ | ||
"name": "et-parser", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "A library to parse string.", | ||
@@ -5,0 +5,0 @@ "main": "es5/parser.js", |
@@ -16,8 +16,8 @@ # et-parser [![NPM version](https://img.shields.io/npm/v/et-parser.svg?style=flat-square)](https://www.npmjs.com/package/et-parser) | ||
var table = ` | ||
| text | emojiStart | emojiKey | emojiEnd | ||
-------- | ---- | ---------- | -------- | -------- | ||
'[' | emojiStart | emojiStart | emojiStart | emojiStart | ||
']' | text | text | emojiEnd | text | ||
[\\w\\.] | text | emojiKey | emojiKey | text | ||
| text | text | text | text | ||
| text | emojiStart | emojiKey | emojiEnd | ||
-------- | ---- | ---------- | -------- | -------- | ||
[ | emojiStart | emojiStart | emojiStart | emojiStart | ||
] | text | text | emojiEnd | text | ||
:[\\w\\.] | text | emojiKey | emojiKey | text | ||
| text | text | text | text | ||
` | ||
@@ -24,0 +24,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
10
325
13228
1