Comparing version 0.1.4 to 0.1.5
var STATE_BACK_MARK = '_'; | ||
var STATE_CHILD_REG = /^_/; | ||
var IGNORE_TOKEN = ':::'; | ||
var Machine = (function () { | ||
@@ -15,6 +16,10 @@ function Machine() { | ||
for (var i = 0, len = source.length; i < len;) { | ||
var _a = this.getToken(source, i), token = _a.token, symbol = _a.symbol; | ||
var _b = this.switchState(symbol, currentState, stateStack), prevState = _b.prevState, nextState = _b.nextState; | ||
var _a = this.switchState(source, i, currentState), token = _a.token, prevState = _a.prevState, nextState = _a.nextState; | ||
if (!token) | ||
break; // no matched token | ||
if (prevState === null && nextState === null) | ||
break; // no matched state | ||
var set = this.chargeLoopState(currentState, prevState, nextState, stateStack); | ||
prevState = set.prevState; | ||
nextState = set.nextState; | ||
var fnState = callbackFn(prevState, token, i); | ||
@@ -30,13 +35,20 @@ if (fnState && typeof fnState === 'string') { | ||
}; | ||
Machine.prototype.getToken = function (source, index) { | ||
Machine.prototype.switchState = function (source, index, currentState) { | ||
var symbols = this.symbols; | ||
var char = source[index]; | ||
var token = char; | ||
var kick = ''; | ||
var prevStateOut = ''; | ||
var nextStateOut = ''; | ||
for (var i = 0, len = symbols.length; i < len; i++) { | ||
var symbol = symbols[i]; | ||
var _a = this.getState(symbol, currentState), prevState = _a.prevState, nextState = _a.nextState; | ||
if (nextState === IGNORE_TOKEN) { | ||
continue; | ||
} | ||
if (typeof symbol === 'string') { | ||
var tmp = source.substr(index, symbol.length); | ||
if (tmp === symbol) { | ||
kick = token = symbol; | ||
token = symbol; | ||
prevStateOut = prevState; | ||
nextStateOut = nextState; | ||
break; | ||
@@ -48,3 +60,4 @@ } | ||
token = char; | ||
kick = symbol; | ||
prevStateOut = prevState; | ||
nextStateOut = nextState; | ||
break; | ||
@@ -54,10 +67,5 @@ } | ||
} | ||
return { token: token, symbol: kick }; | ||
return { token: token, prevState: prevStateOut, nextState: nextStateOut }; | ||
}; | ||
Machine.prototype.switchState = function (symbol, currentState, stateStack) { | ||
var map = this.table.get(symbol); | ||
var stateMap = map.get(currentState); | ||
if (!stateMap) | ||
return { prevState: null, nextState: null }; | ||
var prevState = stateMap.prevState, nextState = stateMap.nextState; | ||
Machine.prototype.chargeLoopState = function (currentState, prevState, nextState, stateStack) { | ||
var isCurrentLoop = STATE_CHILD_REG.test(currentState); | ||
@@ -80,4 +88,12 @@ var isNextLoop = STATE_CHILD_REG.test(nextState); | ||
}; | ||
Machine.prototype.getState = function (symbol, currentState) { | ||
var map = this.table.get(symbol); | ||
var stateMap = map.get(currentState); | ||
if (!stateMap) | ||
return { prevState: null, nextState: null }; | ||
else | ||
return stateMap; | ||
}; | ||
return Machine; | ||
})(); | ||
exports.Machine = Machine; |
{ | ||
"name": "et-parser", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "A library to parse string.", | ||
@@ -5,0 +5,0 @@ "main": "es5/parser.js", |
const STATE_BACK_MARK = '_'; | ||
const STATE_CHILD_REG = /^_/; | ||
const IGNORE_TOKEN = ':::'; | ||
@@ -17,6 +18,10 @@ export class Machine { | ||
for (let i = 0, len = source.length; i < len;) { | ||
let {token, symbol} = this.getToken(source, i) | ||
let {prevState, nextState} = this.switchState(symbol, currentState, stateStack); | ||
let {token, prevState, nextState} = this.switchState(source, i, currentState); | ||
if (!token) break; // no matched token | ||
if (prevState === null && nextState === null) break; // no matched state | ||
let set = this.chargeLoopState(currentState, prevState, nextState, stateStack); | ||
prevState = set.prevState; | ||
nextState = set.nextState; | ||
let fnState = callbackFn(prevState, token, i); | ||
@@ -28,17 +33,24 @@ if (fnState && typeof fnState === 'string') { | ||
} | ||
i += token.length | ||
i += token.length; | ||
} | ||
} | ||
getToken (source: string, index: number) { | ||
switchState (source: string, index: number, currentState: string) { | ||
let symbols = this.symbols; | ||
let char = source[index]; | ||
let token = char; | ||
let kick: string | RegExp = ''; | ||
let prevStateOut = ''; | ||
let nextStateOut = ''; | ||
for (let i = 0, len = symbols.length; i < len; i++) { | ||
let symbol = symbols[i] | ||
let {prevState, nextState} = this.getState(symbol, currentState); | ||
if (nextState === IGNORE_TOKEN) { | ||
continue; | ||
} | ||
if (typeof symbol === 'string') { | ||
let tmp = source.substr(index, symbol.length); | ||
if (tmp === symbol) { | ||
kick = token = symbol; | ||
token = symbol; | ||
prevStateOut = prevState; | ||
nextStateOut = nextState; | ||
break; | ||
@@ -49,3 +61,4 @@ } | ||
token = char; | ||
kick = symbol; | ||
prevStateOut = prevState; | ||
nextStateOut = nextState; | ||
break; | ||
@@ -55,10 +68,5 @@ } | ||
} | ||
return {token, symbol: kick} | ||
return {token, prevState: prevStateOut, nextState: nextStateOut} | ||
} | ||
switchState (symbol: string | RegExp, currentState: string, stateStack: string[]) { | ||
let map = this.table.get(symbol) | ||
let stateMap = map.get(currentState) | ||
if (!stateMap) return {prevState: null, nextState: null}; | ||
let {prevState, nextState} = stateMap; | ||
chargeLoopState (currentState: string, prevState: string, nextState: string, stateStack: string[]) { | ||
let isCurrentLoop = STATE_CHILD_REG.test(currentState) | ||
@@ -81,5 +89,10 @@ let isNextLoop = STATE_CHILD_REG.test(nextState) | ||
} | ||
return {prevState, nextState} | ||
} | ||
getState (symbol: string | RegExp, currentState: string) { | ||
let map = this.table.get(symbol) | ||
let stateMap = map.get(currentState) | ||
if (!stateMap) return {prevState: null, nextState: null}; | ||
else return stateMap; | ||
} | ||
} |
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
14466
355
0