Skip to content

Commit

Permalink
refactor(parse): ♻️
Browse files Browse the repository at this point in the history
  • Loading branch information
vivaxy committed Dec 15, 2019
1 parent e09e409 commit 0dc583e
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 27 deletions.
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/parse/nested-tags-000/actual.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
2 changes: 1 addition & 1 deletion lib/__tests__/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('parse', async (t) => {
};

const onlyCaseName = '';
const todoTestCaseNames = [];
const todoTestCaseNames = ['nested-tags-000'];
await runFixtures(
path.join(__dirname, 'fixtures', 'parse'),
runTest,
Expand Down
11 changes: 4 additions & 7 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module.exports = function parse(input) {
node.tagName = tagName;
},
[tokenize.TYPES.TAG_CLOSE](closing, selfClosing) {
if (attributeName) {
node.attrs[attributeName] = true;
attributeName = '';
}
if (closing && selfClosing) {
throwError('Unexpected closing with selfClosing. e.g. </tag />');
}
Expand Down Expand Up @@ -66,13 +70,6 @@ module.exports = function parse(input) {

function traverse(type, ...args) {
const action = stateMachine[type];
if (!action) {
Object.keys(tokenize.TYPES).forEach(function(key) {
if (tokenize.TYPES[key] === type) {
console.log(key);
}
});
}
action(...args);
}

Expand Down
71 changes: 52 additions & 19 deletions lib/parse/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ function tokenize(input, traverse) {
EXCLAMATION: '!',
SLASH: '/',
MINUS: '-',
BACK_SLASH: '\\',
SPACE: ' ',
TABLE: '\t',
EQUAL: '=',
QUESTION: '?',
SINGLE_QUOTE: "'",
DOUBLE_QUOTE: '"',
LEFT_BRACE: '{',
RIGHT_BRACE: '}',
LINE_FEED: '\n',
CARRIAGE_RETURN: '\r',
};
const ACTIONS = {
SPACE: 100,
Expand All @@ -37,23 +48,7 @@ function tokenize(input, traverse) {
BRACE_RIGHT: 109,
CHAR: 110,
MINUS: 111,
};

const CHAR_TO_ACTIONS = {
' ': ACTIONS.SPACE,
'\t': ACTIONS.SPACE,
'\n': ACTIONS.SPACE,
'\r': ACTIONS.SPACE,
[CHAR.LT]: ACTIONS.LT,
[CHAR.GT]: ACTIONS.GT,
'"': ACTIONS.QUOTE,
"'": ACTIONS.QUOTE,
'=': ACTIONS.EQUAL,
[CHAR.SLASH]: ACTIONS.SLASH,
[CHAR.EXCLAMATION]: ACTIONS.EXCLAMATION,
'?': ACTIONS.QUESTION,
'{': ACTIONS.BRACE_LEFT,
'}': ACTIONS.BRACE_RIGHT,
BACK_SLASH: 112,
};
const STATES = {
TEXT: 200,
Expand All @@ -63,6 +58,24 @@ function tokenize(input, traverse) {
ATTRIBUTE_VALUE: 204,
COMMENT: 205,
};
const CHAR_TO_ACTIONS = {
[CHAR.SPACE]: ACTIONS.SPACE,
[CHAR.TABLE]: ACTIONS.SPACE,
[CHAR.LINE_FEED]: ACTIONS.SPACE,
[CHAR.CARRIAGE_RETURN]: ACTIONS.SPACE,
[CHAR.LT]: ACTIONS.LT,
[CHAR.GT]: ACTIONS.GT,
[CHAR.DOUBLE_QUOTE]: ACTIONS.QUOTE,
[CHAR.SINGLE_QUOTE]: ACTIONS.QUOTE,
[CHAR.EQUAL]: ACTIONS.EQUAL,
[CHAR.SLASH]: ACTIONS.SLASH,
[CHAR.EXCLAMATION]: ACTIONS.EXCLAMATION,
[CHAR.QUESTION]: ACTIONS.QUESTION,
[CHAR.LEFT_BRACE]: ACTIONS.BRACE_LEFT,
[CHAR.RIGHT_BRACE]: ACTIONS.BRACE_RIGHT,
[CHAR.MINUS]: ACTIONS.MINUS,
[CHAR.BACK_SLASH]: ACTIONS.BACK_SLASH,
};

let state = STATES.TEXT;
let text = '';
Expand Down Expand Up @@ -107,12 +120,14 @@ function tokenize(input, traverse) {
}
if (text) {
traverse(TYPES.TEXT, text);
text = '';
}
state = STATES.TAG_OPEN;
},
[ACTIONS.CHAR](char) {
addText(char);
if (i === input.length - 1) {
// the end
traverse(TYPES.TEXT, text);
text = '';
}
Expand Down Expand Up @@ -140,13 +155,19 @@ function tokenize(input, traverse) {
[ACTIONS.EXCLAMATION](char) {
if (input[i + 1] === CHAR.MINUS && input[i + 2] === CHAR.MINUS) {
state = STATES.COMMENT;
// Side effect!
i += 2;
return;
}
createUnexpected('TAG_OPEN', 'EXCLAMATION')(char);
},
},
[STATES.TAG_NAME]: {
[ACTIONS.SPACE]() {
if (!text) {
// `< div`
return;
}
// end of a tagName
traverse(TYPES.TAG_OPEN, text, closing);
text = '';
Expand All @@ -164,8 +185,6 @@ function tokenize(input, traverse) {
[ACTIONS.SLASH](char) {
if (input[i + 1] === CHAR.GT) {
selfClosing = true;
traverse(TYPES.TAG_OPEN, text, closing);
text = '';
return;
}
createUnexpected('TAG_NAME', 'SLASH', char);
Expand Down Expand Up @@ -217,6 +236,10 @@ function tokenize(input, traverse) {
addText(char);
return;
}
if (!text) {
// `<div id= ` or `<div id `
return;
}
traverse(TYPES.ATTRIBUTE_VALUE, text);
text = '';
state = STATES.ATTRIBUTE_NAME;
Expand Down Expand Up @@ -248,12 +271,22 @@ function tokenize(input, traverse) {
selfClosing = false;
state = STATES.TEXT;
},
[ACTIONS.BACK_SLASH]() {
if (quote && input[i + 1] === quote) {
// in quote back slash means to escape next char
// Side effect!
text += quote;
i++;
}
},
[ACTIONS.CHAR]: addText,
},
[STATES.COMMENT]: {
[ACTIONS.CHAR]: addText,
[ACTIONS.MINUS](char) {
if (input[i + 1] === CHAR.MINUS && input[i + 2] === CHAR.GT) {
// Side effect!
i += 2;
traverse(TYPES.COMMENT, text);
text = '';
state = STATES.TEXT;
Expand Down
69 changes: 69 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@vivaxy/wxml",
"version": "0.2.3",
"description": "wxml parser and serializer",
"keywords": [
"wxml",
"vivaxy",
"wxml-parser",
"wxml-serializer",
"wxml-traverser",
"wxml-stringifier",
"parse-wxml",
"serialize-wxml",
"traverse-wxml",
"stringify-wxml",
"weixin",
"mp",
"wxmp",
"miniprogram",
"parse",
"traverse",
"serialize",
"parser",
"traverser",
"serializer"
],
"main": "index.js",
"scripts": {
"test": "ava",
"coverage": "nyc npm run test && nyc report --reporter=html && nyc report --reporter=lcov > coverage.lcov",
"release:beta": "npm run coverage && standard-version --prerelease beta && npm publish --tag beta && git push --follow-tags",
"release": "npm run coverage && standard-version && npm publish && git push --follow-tags"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vivaxy/WXML.git"
},
"author": "vivaxy <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/vivaxy/WXML/issues"
},
"homepage": "https://github.com/vivaxy/WXML#readme",
"devDependencies": {
"ava": "^2.2.0",
"fs-extra": "^8.1.0",
"husky": "^3.0.3",
"lint-staged": "^9.2.1",
"nyc": "^14.1.1",
"prettier": "^1.18.2",
"standard-version": "^7.0.0"
},
"lint-staged": {
"**/**.{js,json,md}": [
"prettier --write",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"ava": {
"helpers": [
"**/helpers/**/*"
]
}
}

0 comments on commit 0dc583e

Please sign in to comment.