add find-closing-brace util function

This commit is contained in:
Kurt Hutten IrevDev
2022-11-17 16:06:38 +11:00
parent 7b0a102e98
commit e89241de92
2 changed files with 73 additions and 1 deletions

View File

@ -1,6 +1,24 @@
import { abstractSyntaxTree } from "./abstractSyntaxTree";
import { abstractSyntaxTree, findClosingBrace } from "./abstractSyntaxTree";
import { lexer } from "./tokeniser";
describe("findClosingBrace", () => {
test("finds the closing brace", () => {
const basic = "( hey )"
expect(findClosingBrace(lexer(basic), 0)).toBe(4)
const handlesNonZeroIndex = "(indexForBracketToRightOfThisIsTwo(shouldBeFour)AndNotThisSix)"
expect(findClosingBrace(lexer(handlesNonZeroIndex), 2)).toBe(4)
expect(findClosingBrace(lexer(handlesNonZeroIndex), 0)).toBe(6)
const handlesNested = "{a{b{c(}d]}eathou athoeu tah u} thatOneToTheLeftIsLast }"
expect(findClosingBrace(lexer(handlesNested), 0)).toBe(18)
// throws when not started on a brace
expect(() => findClosingBrace(lexer(handlesNested), 1)).toThrow()
})
})
describe("testing AST", () => {
test("test 5 + 6", () => {
const tokens = lexer("5 +6");