-
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
jindy
committed
Jun 18, 2022
1 parent
de2bd59
commit 6893188
Showing
4 changed files
with
85 additions
and
9 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,6 @@ | ||
export const enum NodeTypes { | ||
// interpolation | ||
INTERPOLATION, | ||
// simple_expression | ||
SIMPLE_EXPRESSION, | ||
} |
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,60 @@ | ||
import { NodeTypes } from "./ast" | ||
|
||
export function baseParse(content: string) { | ||
const context = createParseContext(content) | ||
return createRoot(parseChildren(context)) | ||
} | ||
|
||
function parseChildren(context) { | ||
const nodes: any = [] | ||
let node | ||
if (context.source.startsWith("{{")) { | ||
node = parseInterpolation(context) | ||
} | ||
nodes.push(node) | ||
return nodes | ||
} | ||
|
||
function parseInterpolation(context) { | ||
const openDelimiter = "{{" | ||
const closeDelimiter = "}}" | ||
|
||
const closeIndex = context.source.indexOf( | ||
closeDelimiter, | ||
openDelimiter.length | ||
) | ||
// context.source = context.source.slice(openDelimiter.length) | ||
advanceBy(context, openDelimiter.length) | ||
|
||
const rawContentLength = closeIndex - openDelimiter.length | ||
const rawContent = context.source.slice(0, rawContentLength) | ||
const content = rawContent.trim() | ||
|
||
// context.source = context.source.slice( | ||
// rawContentLength + closeDelimiter.length | ||
// ) | ||
advanceBy(context, rawContentLength + closeDelimiter.length) | ||
return { | ||
type: NodeTypes.INTERPOLATION, | ||
content: { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: content, | ||
}, | ||
} | ||
} | ||
|
||
function advanceBy(context, length) { | ||
context.source = context.source.slice(length) | ||
} | ||
|
||
function createRoot(children) { | ||
return { | ||
children, | ||
} | ||
} | ||
|
||
function createParseContext(content) { | ||
return { | ||
source: content, | ||
} | ||
} |
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,19 @@ | ||
import { NodeTypes } from "../src/ast" | ||
import { baseParse } from "../src/parse" | ||
|
||
describe("Parse", () => { | ||
describe("interpolation", () => { | ||
test("simple interpolation", () => { | ||
// root | ||
const ast: any = baseParse("{{massage}}") | ||
|
||
expect(ast.children[0]).toStrictEqual({ | ||
type: NodeTypes.INTERPOLATION, | ||
content: { | ||
type: NodeTypes.SIMPLE_EXPRESSION, | ||
content: "massage", | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.