Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

caesar-parser

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caesar-parser - npm Package Compare versions

Comparing version 0.1.1 to 0.1.2

3

lib/JsonParser.js

@@ -74,3 +74,4 @@ 'use strict';

if (str !== '') {
stream.readStack(indentStack, function (type, substr) {
stream.readStack(indentStack, function (type, preStr, ch) {
var substr = preStr + ch;
substr = substr.trim('');

@@ -77,0 +78,0 @@ switch (type) {

@@ -25,2 +25,4 @@ 'use strict';

var _immutable = require('immutable');
var _Context = require('./Context');

@@ -38,3 +40,3 @@

/**
* @param {string} code
* @param {string|Immutable.List|Array} code
* @param {?object} opts

@@ -57,2 +59,3 @@ * {

this._readyPause = false; // 触发暂停
this._keepOnLines = null; // 前进行数
}

@@ -92,39 +95,40 @@

this._row = opts.startRow || this._row;
this._row = opts.startRow === undefined ? this._row : opts.startRow;
var delayed = opts.delayed ? this._delayed : function (_fn) {
_fn();
};
return new Promise(function (res, rej) {
if (_this._row >= _this._code.length) {
return res(true);
var size = this._code.size;
var ended = function ended() {
_this._readyPause = false;
_this._keepOnLines = null;
};
return new Promise(function (res) {
if (_this._row >= size) {
return res();
}
var errorHandler = function errorHandler(error) {
_this.removeErrorHandler(errorHandler);
rej(error);
};
_this.onError(errorHandler);
var run = function run() {
if (_this._readyPause) {
_this._readyPause = false;
return;
if (_this._readyPause || _this._keepOnLines === 0) {
ended();
return res();
}
var str = _this._code[_this._row];
_this._keepOnLines && _this._keepOnLines--;
var str = _this._code.get(_this._row);
var stream = new _StringStream2['default'](str, { tabSize: _this._tabSize, saveStyle: opts.saveStyle });
while (!stream.eol()) {
// if return true, break the parser
if (_this.token(stream)) {
_this._res = null;
return;
}
_this.token(stream);
}
_this.emit('parse', fn && fn(stream, _this._row), _this._row);
_this._row++;
if (_this._row < _this._code.length) {
if (_this._row < size) {
delayed(run, opts.delayTime);
} else {
_this.removeErrorHandler(errorHandler);
res(true);
ended();
res();
}
};
delayed(run, opts.delayTime);
})['catch'](function (e) {
ended();
_this.emit('error', e);
throw e;
});

@@ -136,3 +140,2 @@ }

* @param {number} col
* @returns {boolean} true
*/

@@ -160,5 +163,5 @@ }, {

var preRow = row - ERR_LIMIT + 1 < 0 ? 0 : row - ERR_LIMIT + 1;
errorMsg = (0, _utils.emptySpace)(4) + colors.black(this._code[row].slice(0, col)) + colors.red(errorMsg + '^ ' + msg);
errorMsg = (0, _utils.emptySpace)(4) + colors.black(this._code.get(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg);
lines = this._code.slice(preRow, row + 1).map(function (text, index, arr) {
return row - arr.length + index + 2 + ' | ' + text;
return row - arr.size + index + 2 + ' | ' + text;
});

@@ -170,9 +173,7 @@ lines.push(errorMsg);

lines = lines.concat(afterLines);
this.emit('error', {
message: colors.bgBlack('\n' + lines.join('\n')),
text: msg,
row: row,
col: col
});
return true;
var err = new Error(colors.bgBlack('\n' + lines.join('\n')));
err.text = msg;
err.row = row;
err.col = col;
throw err;
}

@@ -197,7 +198,7 @@ }, {

key: 'updateCode',
value: function updateCode() {
var code = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
value: function updateCode(code) {
if (typeof code === 'string') {
this._code = code.split(_constants.ENDED_REG);
this._code = new _immutable.List(code.split(_constants.ENDED_REG));
} else if (Array.isArray(code)) {
this._code = new _immutable.List(code);
} else {

@@ -233,3 +234,3 @@ this._code = code;

/**
* @param {?string} code - 需要解析的代码, 可以为空
* @param {string|Immutable.List|Array} code - 需要解析的代码, 可以为空
* @param {object} opts

@@ -249,4 +250,4 @@ * {number} startRow - 开始遍历的位置, 默认之前停留的时间

code && this._initParser(code);
var startRow = opts.startRow || this._row;
if (this._ctxList.length !== 0 && opts.startRow > 0) {
var startRow = opts.startRow === undefined ? this._row : opts.startRow;
if (this._ctxList.length !== 0 && startRow > 0) {
// 获取之前的状态

@@ -270,3 +271,2 @@ this.ctx = this._ctxList[startRow - 1] && this._ctxList[startRow - 1].context;

_this2._res = _this2.ctx.toJS();
_this2._row = 0;
return _this2._ctxList.map(function (item) {

@@ -302,5 +302,14 @@ return item.styleStack;

}
/**
* @param {number} lines - 前进行数, 如果不传递或为0, 则前进到行尾
* @param {object} opts - 参进highlight
* @returns {Promise}
*/
}, {
key: 'keepOn',
value: function keepOn(opts) {
value: function keepOn(lines, opts) {
if (lines) {
this._keepOnLines = lines;
}
return this.highlight(null, opts);

@@ -310,3 +319,16 @@ }

/**
* @param {string} code
* 回滚只适用于highlight有缓存ctxList的, parse没有缓存无法使用
* @param {number} num - 回滚的行数
*/
}, {
key: 'rollBack',
value: function rollBack() {
var num = arguments.length <= 0 || arguments[0] === undefined ? 0 : arguments[0];
var newRow = this._row - num;
newRow < 0 ? this._row = 0 : this._row = newRow;
}
/**
* @param {string|Immutable.List|Array} code
* @returns {Promise}

@@ -325,4 +347,2 @@ */

_this3._res = _this3.ctx.toJS();
// clear ctx
_this3.clearContext();
return _this3._res;

@@ -329,0 +349,0 @@ });

@@ -13,2 +13,4 @@ 'use strict';

var _utils = require('./utils');
var StringStream = (function () {

@@ -214,15 +216,3 @@ /**

value: function readStack(stack, fn) {
var _this = this;
var pre = 0;
var substr = undefined;
stack.forEach(function (pos, key) {
if (key % 2 === 0) {
substr = _this._string.slice(pre, pos + 1);
pre = pos + 1;
} else {
fn(pos, substr);
}
});
if (pre < this._string.length - 1) fn(null, this._string.slice(pre));
(0, _utils.readStack)(this._string, stack, fn);
}

@@ -229,0 +219,0 @@ }, {

@@ -22,6 +22,44 @@ 'use strict';

}
function readStack(str, stack, fn) {
var pre = 0;
var substr = undefined;
stack.forEach(function (pos, key) {
if (key % 2 === 0) {
substr = str.slice(pre, pos);
pre = pos + 1;
} else {
fn(pos, substr, str.slice(pre - 1, pre));
}
});
if (pre < str.length - 1) fn(null, str.slice(pre), '');
}
function addStyle(str, stack, fn) {
var res = [];
var key = null;
var keyCh = '';
readStack(str, stack, function (_key, preStr, ch) {
if (key) {
res.push(fn(key, keyCh + preStr));
} else {
res.push(preStr);
}
if (_key) {
key = _key;
keyCh = ch;
} else {
key = null;
}
});
if (key) {
res.push(fn(key, keyCh));
}
return res;
}
module.exports = {
Delayed: Delayed,
emptySpace: emptySpace
emptySpace: emptySpace,
readStack: readStack,
addStyle: addStyle
};
{
"name": "caesar-parser",
"version": "0.1.1",
"version": "0.1.2",
"description": "",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -42,3 +42,4 @@ import Parser from './Parser'

if (str !== '') {
stream.readStack(indentStack, (type, substr)=> {
stream.readStack(indentStack, (type, preStr, ch)=> {
let substr = preStr + ch
substr = substr.trim('')

@@ -45,0 +46,0 @@ switch (type) {

import {EventEmitter} from 'events'
import {ENDED_REG} from './constants'
import StringStream from './StringStream'
import {List} from 'immutable'
import Context from './Context'

@@ -10,3 +11,3 @@ import {Delayed, emptySpace} from './utils'

/**
* @param {string} code
* @param {string|Immutable.List|Array} code
* @param {?object} opts

@@ -22,3 +23,4 @@ * {

this._delayed = new Delayed
this._readyPause = false // 触发暂停
this._readyPause = false // 触发暂停
this._keepOnLines = null // 前进行数
}

@@ -47,37 +49,38 @@ /**

_iter(fn, opts = {}) {
this._row = opts.startRow || this._row
this._row = opts.startRow === undefined ? this._row : opts.startRow
const delayed = opts.delayed ? this._delayed : function(_fn) { _fn() }
return new Promise((res, rej)=> {
if (this._row >= this._code.length) {
return res(true)
const size = this._code.size
const ended = () => {
this._readyPause = false
this._keepOnLines = null
}
return new Promise((res)=> {
if (this._row >= size) {
return res()
}
const errorHandler = (error)=> {
this.removeErrorHandler(errorHandler)
rej(error)
}
this.onError(errorHandler)
const run = ()=> {
if (this._readyPause) {
this._readyPause = false
return
if (this._readyPause || this._keepOnLines === 0) {
ended()
return res()
}
const str = this._code[this._row]
this._keepOnLines && (this._keepOnLines --)
const str = this._code.get(this._row)
const stream = new StringStream(str, {tabSize: this._tabSize, saveStyle: opts.saveStyle})
while (!stream.eol()) {
// if return true, break the parser
if (this.token(stream)) {
this._res = null
return
}
this.token(stream)
}
this.emit('parse', fn && fn(stream, this._row), this._row)
this._row ++
if (this._row < this._code.length) {
if (this._row < size) {
delayed(run, opts.delayTime)
} else {
this.removeErrorHandler(errorHandler)
res(true)
ended()
res()
}
}
delayed(run, opts.delayTime)
}).catch((e) => {
ended()
this.emit('error', e)
throw e
})

@@ -88,3 +91,2 @@ }

* @param {number} col
* @returns {boolean} true
*/

@@ -105,14 +107,12 @@ error(msg, col) {

const preRow = row - ERR_LIMIT + 1 < 0 ? 0 : row - ERR_LIMIT + 1
errorMsg = emptySpace(4) + colors.black(this._code[row].slice(0, col)) + colors.red(errorMsg + '^ ' + msg)
lines = this._code.slice(preRow, row + 1).map((text, index, arr) => (row - arr.length + index + 2) + ' | ' + text )
errorMsg = emptySpace(4) + colors.black(this._code.get(row).slice(0, col)) + colors.red(errorMsg + '^ ' + msg)
lines = this._code.slice(preRow, row + 1).map((text, index, arr) => (row - arr.size + index + 2) + ' | ' + text )
lines.push(errorMsg)
afterLines = this._code.slice(row + 1, row + 1 + ERR_LIMIT).map((text, index) => (row + index + 2) + ' | ' + text )
lines = lines.concat(afterLines)
this.emit('error', {
message: colors.bgBlack('\n' + lines.join('\n')),
text: msg,
row: row,
col: col,
})
return true
const err = new Error(colors.bgBlack('\n' + lines.join('\n')))
err.text = msg
err.row = row
err.col = col
throw err
}

@@ -131,5 +131,7 @@ clearContext() {

}
updateCode(code = []) {
updateCode(code) {
if (typeof code === 'string') {
this._code = code.split(ENDED_REG)
this._code = new List(code.split(ENDED_REG))
} else if (Array.isArray(code)) {
this._code = new List(code)
} else {

@@ -155,3 +157,3 @@ this._code = code

/**
* @param {?string} code - 需要解析的代码, 可以为空
* @param {string|Immutable.List|Array} code - 需要解析的代码, 可以为空
* @param {object} opts

@@ -165,4 +167,4 @@ * {number} startRow - 开始遍历的位置, 默认之前停留的时间

code && this._initParser(code)
const startRow = opts.startRow || this._row
if (this._ctxList.length !== 0 && opts.startRow > 0) {
const startRow = opts.startRow === undefined ? this._row : opts.startRow
if (this._ctxList.length !== 0 && startRow > 0) {
// 获取之前的状态

@@ -186,3 +188,2 @@ this.ctx = this._ctxList[startRow - 1] && this._ctxList[startRow - 1].context

this._res = this.ctx.toJS()
this._row = 0
return this._ctxList.map((item) => item.styleStack)

@@ -208,7 +209,23 @@ })

}
keepOn(opts) {
/**
* @param {number} lines - 前进行数, 如果不传递或为0, 则前进到行尾
* @param {object} opts - 参进highlight
* @returns {Promise}
*/
keepOn(lines, opts) {
if (lines) {
this._keepOnLines = lines
}
return this.highlight(null, opts)
}
/**
* @param {string} code
* 回滚只适用于highlight有缓存ctxList的, parse没有缓存无法使用
* @param {number} num - 回滚的行数
*/
rollBack(num = 0) {
const newRow = this._row - num
newRow < 0 ? this._row = 0 : this._row = newRow
}
/**
* @param {string|Immutable.List|Array} code
* @returns {Promise}

@@ -221,4 +238,2 @@ */

this._res = this.ctx.toJS()
// clear ctx
this.clearContext()
return this._res

@@ -225,0 +240,0 @@ })

import {SPACE_REG} from './constants'
import {readStack} from './utils'

@@ -152,13 +153,3 @@ class StringStream {

readStack(stack, fn) {
let pre = 0
let substr
stack.forEach((pos, key)=> {
if (key % 2 === 0) {
substr = this._string.slice(pre, pos + 1)
pre = pos + 1
} else {
fn(pos, substr)
}
})
if (pre < this._string.length - 1) fn(null, this._string.slice(pre))
readStack(this._string, stack, fn)
}

@@ -165,0 +156,0 @@ get styleStack() {

@@ -16,6 +16,44 @@ function Delayed(ms = 0) {

}
function readStack(str, stack, fn) {
let pre = 0
let substr
stack.forEach((pos, key)=> {
if (key % 2 === 0) {
substr = str.slice(pre, pos)
pre = pos + 1
} else {
fn(pos, substr, str.slice(pre - 1, pre))
}
})
if (pre < str.length - 1) fn(null, str.slice(pre), '')
}
function addStyle(str, stack, fn) {
const res = []
let key = null
let keyCh = ''
readStack(str, stack, (_key, preStr, ch)=> {
if (key) {
res.push(fn(key, keyCh + preStr))
} else {
res.push(preStr)
}
if (_key) {
key = _key
keyCh = ch
} else {
key = null
}
})
if (key) {
res.push(fn(key, keyCh))
}
return res
}
module.exports = {
Delayed,
emptySpace,
readStack,
addStyle,
}

@@ -6,2 +6,3 @@ ### 程序猿的玩具

- 行分段
- 数组和对象的逗号问题
- 数组和对象的逗号问题
- JsonParser 单侧改为promise all
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc