把字符串形式的代码转换为 令牌(tokens)流,可以把令牌看作是一个扁平的语法片段数组 例如:
// 源
n * n;
// 词法分析结果
[
{ type: { ... }, value: "n", start: 0, end: 1, loc: { ... } },
{ type: { ... }, value: "*", start: 2, end: 3, loc: { ... } },
{ type: { ... }, value: "n", start: 4, end: 5, loc: { ... } },
]
每一个 type 有一组属性来描述该令牌:
const token = {
type: {
label: 'name',
keyword: undefined,
beforeExpr: false,
startsExpr: true,
rightAssociative: false,
isLoop: false,
isAssign: false,
prefix: false,
postfix: false,
binop: null,
updateContext: null,
},
};
// 源
function square(n) {
return n * n;
}
// ATS
const ast = {
type: 'FunctionDeclaration',
id: {
type: 'Identifier',
name: 'square',
},
params: [
{
type: 'Identifier',
name: 'n',
},
],
body: {
type: 'BlockStatement',
body: [
{
type: 'ReturnStatement',
argument: {
type: 'BinaryExpression',
operator: '*',
left: {
type: 'Identifier',
name: 'n',
},
right: {
type: 'Identifier',
name: 'n',
},
},
},
],
},
};