forked from cuixiaorui/mini-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4743310
commit baaba51
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { NodeTypes } from "../src/ast"; | ||
import { baseParse } from "../src/parser"; | ||
|
||
describe("parser", () => { | ||
test("simple text", () => { | ||
const ast = baseParse("some text"); | ||
const text = ast.children[0]; | ||
|
||
expect(text).toStrictEqual({ | ||
type: NodeTypes.TEXT, | ||
content: "some text", | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const enum NodeTypes { | ||
TEXT, | ||
ROOT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { NodeTypes } from "./ast"; | ||
|
||
export function baseParse(content: string) { | ||
const context = createParserContext(content); | ||
return createRoot(parseChildren(context)); | ||
} | ||
|
||
function createParserContext(content) { | ||
console.log("创建 paserContext"); | ||
return { | ||
source: content, | ||
}; | ||
} | ||
|
||
function parseChildren(context) { | ||
console.log("开始解析 children"); | ||
const nodes = []; | ||
const node = parseText(context); | ||
|
||
nodes.push(node); | ||
|
||
return nodes; | ||
} | ||
|
||
function parseText(context): any { | ||
console.log("解析 text", context); | ||
const endIndex = context.source.length; | ||
const content = parseTextData(context, endIndex); | ||
|
||
return { | ||
type: NodeTypes.TEXT, | ||
content, | ||
}; | ||
} | ||
|
||
function parseTextData(context: any, length: number): any { | ||
console.log("解析 textData"); | ||
// 1. 直接返回 context.source | ||
// 从 length 切的话,是为了可以获取到 text 的值(需要用一个范围来确定) | ||
const rawText = context.source.slice(0, length); | ||
// 2. 移动光标 | ||
advanceBy(context, length); | ||
|
||
return rawText; | ||
} | ||
|
||
function advanceBy(context, numberOfCharacters) { | ||
console.log("推进代码", context, numberOfCharacters); | ||
context.source = context.source.slice(numberOfCharacters); | ||
} | ||
|
||
function createRoot(children) { | ||
return { | ||
type: NodeTypes.ROOT, | ||
children, | ||
}; | ||
} |