Comparing version 0.1.6 to 0.1.7
@@ -7,7 +7,7 @@ // token to split state | ||
exports.STATE_BACK_MARK = '_'; | ||
// RegExp to test the loop state | ||
// reg to test the loop state | ||
exports.REGEXP_TEST = /^:/; | ||
// RegExp to test the loop state | ||
// reg to test the loop state | ||
exports.STATE_CHILD_REG = /^_/; | ||
// the mark of ignore state | ||
exports.IGNORE_TOKEN = ':::'; |
@@ -26,6 +26,6 @@ var configs_1 = require('./configs'); | ||
} | ||
function parseStateMap(state) { | ||
function parseStateSet(state) { | ||
state = state.trim(); | ||
if (state === configs_1.IGNORE_TOKEN || !~state.indexOf(configs_1.STATE_TOKEN)) { | ||
return { prevState: state, nextState: state }; | ||
return { prevState: '', nextState: state }; | ||
} | ||
@@ -39,19 +39,49 @@ else { | ||
} | ||
function symbolSorter(left, right) { | ||
// -1 表示left更靠前 | ||
var leftType = (typeof left === 'string') ? 0 : 1; | ||
var rightType = (typeof right === 'string') ? 0 : 1; | ||
if (leftType - rightType) { | ||
return leftType - rightType; | ||
function getWeight(symbol, symbols) { | ||
if (typeof symbol === 'string') { | ||
var weight = 0; | ||
for (var i = 0, len = symbols.length; i < len; i++) { | ||
var tmpSymbol = symbols[i]; | ||
if (typeof tmpSymbol === 'string' && tmpSymbol.indexOf(symbol) === 0) { | ||
weight++; | ||
} | ||
} | ||
return weight; | ||
} | ||
if (typeof left === 'string' && typeof right === 'string') { | ||
if (left.indexOf(right) === 0) | ||
return -1; | ||
else if (right.indexOf(left) === 0) | ||
return 1; | ||
else | ||
return 0; | ||
else { | ||
return 9999; | ||
} | ||
return 0; | ||
} | ||
function pickSymbol(weights, symbols) { | ||
var min = 10000; | ||
var index = -1; | ||
for (var i = 0, len = weights.length; i < len; i++) { | ||
var w = weights[i]; | ||
if (0 <= w && w < min) { | ||
min = w; | ||
index = i; | ||
} | ||
} | ||
if (index >= 0) { | ||
weights[index] = -1; | ||
return symbols[index]; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
function sortSymbols(symbols) { | ||
var results = []; | ||
var weights = symbols.map(function (item) { return getWeight(item, symbols); }); | ||
while (true) { | ||
var symbol = pickSymbol(weights, symbols); | ||
if (symbol === null) { | ||
break; | ||
} | ||
else { | ||
results.push(symbol); | ||
} | ||
} | ||
return results; | ||
} | ||
function parseTable(source) { | ||
@@ -68,3 +98,3 @@ var symbols = []; | ||
for (var j = 0, lenj = states.length; j < lenj; j++) { | ||
stateMap.set(states[j], parseStateMap(lineStates[j] || '')); | ||
stateMap.set(states[j], parseStateSet(lineStates[j] || '')); | ||
} | ||
@@ -76,4 +106,4 @@ if (symbol) { | ||
} | ||
return { symbols: symbols.sort(symbolSorter), states: states, table: table }; | ||
return { symbols: sortSymbols(symbols), states: states, table: table }; | ||
} | ||
exports.parseTable = parseTable; |
@@ -80,13 +80,21 @@ var configs_1 = require('./configs'); | ||
var isBackState = nextState === configs_1.STATE_BACK_MARK; | ||
if (!isBackState && isNextLoop) { | ||
stateStack.push(currentState); | ||
if (isCurrentLoop && isBackState) { | ||
prevState = prevState || currentState; | ||
nextState = stateStack.pop() || ''; | ||
} | ||
if (!prevState || prevState === configs_1.STATE_BACK_MARK) { | ||
prevState = currentState; | ||
else if (!isBackState && isNextLoop) { | ||
stateStack.push(prevState || currentState); | ||
if (isCurrentLoop) { | ||
prevState = prevState || currentState; | ||
} | ||
else { | ||
prevState = prevState || nextState; | ||
} | ||
} | ||
if (isCurrentLoop && !nextState) { | ||
else if (isCurrentLoop) { | ||
prevState = prevState || currentState; | ||
nextState = currentState; | ||
} | ||
else if (isBackState) { | ||
nextState = stateStack.pop() || ''; | ||
else { | ||
prevState = prevState || nextState; | ||
} | ||
@@ -98,6 +106,8 @@ return { prevState: prevState, nextState: nextState }; | ||
var stateMap = map.get(currentState); | ||
if (!stateMap) | ||
if (!stateMap) { | ||
return { prevState: null, nextState: null }; | ||
else | ||
} | ||
else { | ||
return stateMap; | ||
} | ||
}; | ||
@@ -104,0 +114,0 @@ return Machine; |
{ | ||
"name": "et-parser", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A library to parse string.", | ||
@@ -8,5 +8,4 @@ "main": "es5/parser.js", | ||
"start": "node test/server", | ||
"setup": "tsd install", | ||
"build": "rm -rf es5 && tsc", | ||
"test": "mocha test/fuse" | ||
"test": "standard && tslint src/**/*.ts && mocha test/fuse" | ||
}, | ||
@@ -31,7 +30,12 @@ "repository": { | ||
"should": "^8.1.1", | ||
"standard": "^6.0.4", | ||
"systemjs": "^0.19.16", | ||
"tsd": "^0.6.5", | ||
"tslint": "^3.3.0", | ||
"typescript": "^1.7.5" | ||
}, | ||
"dependencies": {} | ||
"standard": { | ||
"ignore": [ | ||
"es5" | ||
] | ||
} | ||
} |
@@ -11,9 +11,9 @@ | ||
// RegExp to test the loop state | ||
// reg to test the loop state | ||
export const REGEXP_TEST = /^:/; | ||
// RegExp to test the loop state | ||
// reg to test the loop state | ||
export const STATE_CHILD_REG = /^_/; | ||
// the mark of ignore state | ||
export const IGNORE_TOKEN = ':::'; | ||
export const IGNORE_TOKEN = ':::'; |
import {Machine} from './machine'; | ||
import {SPLIT_TOKEN, STATE_TOKEN, REGEXP_TEST, IGNORE_TOKEN} from './configs'; | ||
@@ -32,6 +31,6 @@ | ||
function parseStateMap (state: string) { | ||
function parseStateSet (state: string) { | ||
state = state.trim() | ||
if (state === IGNORE_TOKEN || !~state.indexOf(STATE_TOKEN)) { | ||
return {prevState: state, nextState: state} | ||
return {prevState: '', nextState: state} | ||
} else { | ||
@@ -45,18 +44,49 @@ let states = state.split(STATE_TOKEN); | ||
function symbolSorter (left: string | RegExp, right: string | RegExp) { | ||
// -1 表示left更靠前 | ||
let leftType = (typeof left === 'string') ? 0 : 1; | ||
let rightType = (typeof right === 'string') ? 0 : 1; | ||
function getWeight (symbol: string | RegExp, symbols: (string | RegExp)[]) { | ||
if (typeof symbol === 'string') { | ||
let weight = 0; | ||
for (let i = 0, len = symbols.length; i < len; i++) { | ||
let tmpSymbol = symbols[i]; | ||
if (typeof tmpSymbol === 'string' && tmpSymbol.indexOf(symbol) === 0) { | ||
weight++; | ||
} | ||
} | ||
return weight; | ||
} else { | ||
return 9999; | ||
} | ||
} | ||
if (leftType - rightType) { | ||
return leftType - rightType | ||
function pickSymbol (weights: number[], symbols: (string | RegExp)[]) { | ||
let min = 10000; | ||
let index = -1; | ||
for (let i = 0, len = weights.length; i < len; i++) { | ||
let w = weights[i]; | ||
if (0 <= w && w < min) { | ||
min = w; | ||
index = i; | ||
} | ||
} | ||
if (index >= 0) { | ||
weights[index] = -1; | ||
return symbols[index]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
if (typeof left === 'string' && typeof right === 'string') { | ||
if (left.indexOf(right) === 0) return -1 | ||
else if (right.indexOf(left) === 0) return 1 | ||
else return 0 | ||
function sortSymbols (symbols: (string | RegExp)[]) { | ||
let results: (string | RegExp)[] = []; | ||
let weights: number[] = symbols.map(item => getWeight(item, symbols)); | ||
while (true) { | ||
let symbol = pickSymbol(weights, symbols); | ||
if (symbol === null) { | ||
break; | ||
} else { | ||
results.push(symbol); | ||
} | ||
} | ||
return 0; | ||
return results; | ||
} | ||
@@ -77,3 +107,3 @@ | ||
for (let j = 0, lenj = states.length; j < lenj; j++) { | ||
stateMap.set(states[j], parseStateMap(lineStates[j] || '')); | ||
stateMap.set(states[j], parseStateSet(lineStates[j] || '')); | ||
} | ||
@@ -86,4 +116,4 @@ if (symbol) { | ||
return {symbols: symbols.sort(symbolSorter), states, table}; | ||
return {symbols: sortSymbols(symbols), states, table}; | ||
} | ||
@@ -15,3 +15,3 @@ | ||
let stateStack: string[] = []; | ||
for (let i = 0, len = source.length; i < len;) { | ||
for (let i = 0, len = source.length; i < len; ) { | ||
let {token, prevState, nextState} = this.switchState(source, i, currentState); | ||
@@ -84,15 +84,24 @@ if (!token) break; // no matched token | ||
if (!isBackState && isNextLoop) { | ||
stateStack.push(currentState) | ||
} | ||
if (isCurrentLoop && isBackState) {// 跳出闭合状态 | ||
prevState = prevState || currentState; | ||
nextState = stateStack.pop() || ''; | ||
if (!prevState || prevState === STATE_BACK_MARK) { | ||
prevState = currentState | ||
} else if (!isBackState && isNextLoop) {// 即将进入闭合状态 | ||
stateStack.push(prevState || currentState); | ||
if (isCurrentLoop) { | ||
prevState = prevState || currentState; | ||
} else { | ||
prevState = prevState || nextState; | ||
} | ||
} else if (isCurrentLoop) {// 目前在闭合状态 | ||
prevState = prevState || currentState; | ||
nextState = currentState; | ||
} else { | ||
prevState = prevState || nextState; | ||
} | ||
if (isCurrentLoop && !nextState) { | ||
nextState = currentState | ||
} else if (isBackState) { | ||
nextState = stateStack.pop() || '' | ||
} | ||
return {prevState, nextState} | ||
@@ -103,5 +112,8 @@ } | ||
let stateMap = map.get(currentState) | ||
if (!stateMap) return {prevState: null, nextState: null}; | ||
else return stateMap; | ||
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
17991
474
7
11