遵循如何將 ab[cd][e].f[g[h[ij]]] 清楚地表示為物件樹?,您將如何撰寫一個演算法來從運算式生成 JS AST a.b[c.d][e].f[g[h[i.j]]]?我正在嘗試撰寫一個決議器來從這個運算式生成某種物件結構(理想情況下比 JS AST 更直觀MemberExpression,因此是另一個問題)。我想看看演算法是如何構建 JavaScriptMemberExpression樹的。
目前我有這種演算法來生成某種樹(但目前似乎不正確):
const patterns = [
[/^[a-z][a-z0-9]*(?:-[a-z0-9] )*/, 'name'],
[/^\[/, 'open'],
[/^\]/, 'close'],
[/^\./, 'stem']
]
console.log(parsePath('a.b[c.d][e].f[g[h[i.j]]]'))
function parsePath(str) {
let node
let nest = []
let result = nest
let stack = [nest]
while (str.length) {
nest = stack[stack.length - 1]
p:
for (let pattern of patterns) {
let match = str.match(pattern[0])
if (match) {
if (pattern[1] === 'name') {
node = {
form: `term`,
name: match[0],
link: []
}
nest.push(node)
} else if (pattern[1] === 'stem') {
stack.push(node.link)
} else if (pattern[1] === 'open') {
node = {
form: 'read',
link: []
}
nest.push(node)
stack.push(node.link)
} else if (pattern[1] === 'close') {
stack.pop()
}
str = str.substr(match[0].length)
break p
}
}
}
return result[0]
}
期望的結果是這樣的(或者如果您愿意創建一個更好、更直觀的資料結構):
{
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "a"
},
"property": {
"type": "Identifier",
"name": "b"
},
"computed": false
},
"property": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "c"
},
"property": {
"type": "Identifier",
"name": "d"
},
"computed": false
},
"computed": true
},
"property": {
"type": "Identifier",
"name": "e"
},
"computed": true
},
"property": {
"type": "Identifier",
"name": "f"
},
"computed": false
},
"property": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "g"
},
"property": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "h"
},
"property": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "i"
},
"property": {
"type": "Identifier",
"name": "j"
},
"computed": false
},
"computed": true
},
"computed": true
},
"computed": true
}
我掙扎的原因(部分)是我不喜歡這種MemberExpression樹狀結構,它是落后的感覺并且不是很直觀。因此,如果您可以構建一個更簡單、更直接的資料結構,那將是理想的(這是另一個問題),但如果不能,那么僅構建一個演算法就可以讓我繼續前進。
就我個人而言,我更愿意嘗試生成這種結構,因為我覺得它更直觀:
{
type: 'site',
site: [
{
type: 'term',
term: 'a'
},
{
type: 'term',
term: 'b'
},
{
type: 'sink',
sink: [
{
type: 'term',
term: 'c'
},
{
type: 'term',
term: 'd'
}
]
},
{
type: 'sink',
sink: [
{
type: 'term',
term: 'e'
}
]
},
{
type: 'term',
term: 'f'
},
{
type: 'sink',
sink: [
{
type: 'term',
term: 'g'
},
{
type: 'sink',
sink: [
{
type: 'term',
term: 'h'
},
{
type: 'sink',
sink: [
{
type: 'term',
term: 'i'
},
{
type: 'term',
term: 'j'
}
]
}
]
}
]
}
]
}
但其中任何一個都適合我(或兩者)。
If we go with the second one, my next problem will be how to convert that data structure into the MemberExpression tree/data structure :) But I'll try and do that myself first. So it's probably better to construct the MemberExpression in this question, then I can work off that.
uj5u.com熱心網友回復:
將字串分成第一級的物件和屬性組,例如
[ "a", "b", "[c.d]", "[e]", "f", "[g[h[i.j]]]" ]獲取物件
- 將最后一項作為財產。
- 檢查屬性是否以括號開頭,然后設定
computed為true并從周圍的括號中剝離該屬性。 - 回傳一個物件
type: "MemberExpression"object與物件 (2.)property與呼叫主函式的結果getAST(1.)
function getAST(string) {
function getObject(parts) {
if (parts.length === 1) return { type: "Identifier", name: parts[0] };
let property = parts.pop(),
computed = false;
if (property.startsWith('[')) {
computed = true;
property = property.slice(1, -1);
}
return {
type: "MemberExpression",
object: getObject(parts),
property: getAST(property),
computed
};
}
let i = 0,
dot,
bracket,
parts = [];
while (i < string.length) {
dot = string.indexOf('.', i);
bracket = string.indexOf('[', i);
if (dot !== -1 && (bracket === -1 || dot < bracket)) {
const temp = string.slice(i, dot);
if (temp) parts.push(temp);
i = dot 1;
continue;
}
if (bracket !== -1 && (dot === -1 || bracket < dot)) {
const temp = string.slice(i, bracket);
if (temp) parts.push(temp);
i = bracket;
let open = 1,
j = i;
while ( j < string.length) {
if (string[j] === '[') open ;
if (string[j] === ']') open--;
if (!open) break;
}
j ;
parts.push(string.slice(i, j));
i = j;
continue;
}
parts.push(string.slice(i));
break;
}
return getObject(parts);
}
console.log(getAST('a.b[c.d][e].f[g[h[i.j]]]'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/364325.html
標籤:javascript algorithm data-structures path
上一篇:c#函式接受整數n并回傳可以除以數字1..n的最小數
下一篇:查找整數排序串列更改的索引
