Skip to content

Commit

Permalink
fix(single function): comment reg and function body loop
Browse files Browse the repository at this point in the history
  • Loading branch information
子文 committed Jun 18, 2020
1 parent 05517ef commit 9acba89
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 53 deletions.
67 changes: 42 additions & 25 deletions docs/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions entry/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const presetVal = {
}
}
}
new Parser().parseString(`function abs(a,b,c) {
a = 5;
{};
return(a+b);
}`)
export default (): JSX.Element => {
const parser = React.useRef((() => {
const instance = new Parser({
Expand All @@ -46,6 +51,8 @@ export default (): JSX.Element => {
return instance
})());


console.log(parser.current.getCurrentValues())
const [result, setResult] = React.useState(() => parser.current.getOptions().defaultReturnValues)
const [time, setTime] = React.useState(0)
const values = React.useRef(parser.current.getCurrentValues())
Expand Down
16 changes: 8 additions & 8 deletions entry/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<html>
<head>
<title> cEval </title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/antd/4.2.5/antd.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/5.1.1/react-router-dom.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/5.1.1/react-router.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/antd/4.2.5/antd.js"></script>
<link href="https://g.alicdn.com/code/lib/antd/4.3.0/antd.css" rel="stylesheet">
<script src="https://g.alicdn.com/code/lib/react/16.13.1/umd/react.development.js"></script>
<script src="https://g.alicdn.com/code/lib/react-dom/16.13.1/umd/react-dom.development.js"></script>
<script src="https://g.alicdn.com/code/lib/react-router-dom/5.1.2/react-router-dom.js"></script>
<!-- <script src="https://g.alicdn.com/code/lib/react-router/6.0.0-alpha.5/react-router.development.js"></script> -->
<script src="https://g.alicdn.com/code/lib/lodash.js/4.17.15/lodash.js"></script>
<script src="https://g.alicdn.com/code/lib/moment.js/2.26.0/moment.js"></script>
<script src="https://g.alicdn.com/code/lib/antd/4.3.0/antd.js"></script>
</head>

<body>
Expand Down
4 changes: 1 addition & 3 deletions entry/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import { name } from '../package.json';
import menuDataList from './ui-json';
import './index.less';

document.addEventListener('DOMContentLoaded', () => {
render(<Material routes={menuDataList} title={name} />, document.getElementById('root'))
})
render(<Material routes={menuDataList} basePath={name} title={name} />, document.getElementById('root'))
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Parser {
* @param {type} 约定的类型
* @param {value} 明确规定的字面值,比如 ] , =
*/
expect: (type: string, value?: any) => never | void;
expect: (type: string, value?: any) => never | boolean;
/**
* 暂存指针,在某些情况下单一的nextToken已经不满足预判情况,例如 typeof(add) || add(1, 2) || 1 + add;
*/
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
"test"
],
"scripts": {
"start": "webpack-dev-server --config script/webpack.dev.js --hot --inline --open-page '?dev'",
"start": "cross-env NODE_ENV=development webpack-dev-server --config script/webpack.dev.js --hot --inline",
"https": "HTTPS=true npm start",
"build:umd": "webpack --config script/webpack.umd.js --hide-modules --progress --colors",
"build:typings": "tsc -p tsconfig.d.json",
"build:docs": "webpack --config script/webpack.doc.js --hide-modules --progress --colors",
"build:rollup": "rollup -c",
"build": "rm -rf lib/ && npm run test && npm run build:docs && npm run build:typings &&npm run build:rollup",
"build": "rm -rf lib/ && npm run test &&npm run build:docs && npm run build:typings && npm run build:rollup",
"release": "standard-version --release-as",
"publish:major": "npm run build && npm run release -- major && npm publish",
"publish:minor": "npm run build && npm run release -- minor && npm publish",
Expand Down Expand Up @@ -58,8 +58,9 @@
"@typescript-eslint/parser": "^2.0.0",
"babel-cli": "^6.26.0",
"babel-loader": "^8.0.0",
"cross-env": "^7.0.2",
"css-loader": "^1.0.0",
"dev-dashboard": "^2.0.6",
"dev-dashboard": "^2.0.7",
"eslint": "^6.8.0",
"eslint-config-airbnb": "^18.0.0",
"eslint-config-prettier": "^4.2.0",
Expand Down
2 changes: 2 additions & 0 deletions script/webpack.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const devBaseConfig = {
port: 8400,
host: '0.0.0.0',
stats: 'minimal',
open: true,
openPage: '?dev',
historyApiFallback: true
},

Expand Down
7 changes: 6 additions & 1 deletion src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@ export default class Parser {
* @param {type} 约定的类型
* @param {value} 明确规定的字面值,比如 ] , =
*/
expect = (type: string, value?): never | void => {
expect = (type: string, value?): never | boolean => {
if (!this.accept(type, value)) {
const { line, column } = this.tokens.getCoordinates()
this.printLog(`> line:${line} column:${column - 1} "${this.current.value}"\nThe next tag should be "${value}", But the reality is`, `${this.nextToken.type === TOKEN_END ? 'empty content' : `"${this.nextToken.value}"`}`
, console.error
)
throw new Error('Unexpected Tag');
} else {
return true
}
}

Expand Down Expand Up @@ -512,6 +514,9 @@ export default class Parser {
do {
this.parseExpression(instr)
} while (this.accept(TOKEN_SEMICOLON, ';'))
if(this.current.type !== TOKEN_SEMICOLON) {
throw new SyntaxError(`Function parse error: Function body each line must end with semicolon ';'`)
}
this.expect(TOKEN_CURLY, '}')
this.accept(TOKEN_SEMICOLON, ';')
exprInstr.push(new Instruction(INSTR_EXPRE, instr))
Expand Down
8 changes: 4 additions & 4 deletions src/token-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,13 @@ export default class TokenStream {
let line = 0;
let column = 0;
let index = -1;

do {
line++;
column = this.pos - index;
index = this.expression.substr(index + 1).indexOf('\n');
} while (index >= 0 && index < this.pos)

index += 1
index += this.expression.substr(index).indexOf('\n'); // 从每一行第一位开始寻找下一个换行符
} while (index >= 0 && index < this.pos && line < this.pos)
return {
line,
column
Expand Down
2 changes: 1 addition & 1 deletion src/utils/regExp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const supportOperator = Array.from(new Set(

export const whitespaceReg = /(\t|\n|\r|\s+)/;
export const booleanReg = /^(false|true)/;
export const commentReg = /^\/\*(.*)\*\//;
export const commentReg = /^\/\*(.*?)\*\//;
export const stringReg = /^\'(.*?)\'|^\"(.*?)\"/;
export const stringGreedyReg = /^\'(.*)\'|^\"(.*)\"/;
// export const regExpReg = /^ExecReg\((.*)\)/;
Expand Down
11 changes: 7 additions & 4 deletions test/function.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/* eslint-disable no-undef */
import Parser from '../src/index'

Expand Down Expand Up @@ -28,6 +27,13 @@ test('binary =', () => {
};
abs(3,4,8)`, value)).toEqual(5);

expect(() => parse(`
function abs(a,b,c) {
a = 5;
return(a) /* Must End Of Semicolon*/
};
abs(3,4,8)`, value)).toThrow(SyntaxError);

expect(parse(`
function abs(a,b,c) {
a = 5;
Expand Down Expand Up @@ -63,7 +69,4 @@ test('binary =', () => {
return(a+b+c);
};
abs(3,4,8)`, value)).toEqual(9);



}, 0)

0 comments on commit 9acba89

Please sign in to comment.