-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify parser to create Node objects
- Loading branch information
1 parent
caa6c66
commit eb8b956
Showing
4 changed files
with
54 additions
and
112 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
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
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
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 |
---|---|---|
@@ -1,46 +1,34 @@ | ||
import { parse, tokenize, ParseError } from "../src" | ||
import { AST } from "../src/parse" | ||
import { CommandNode, parse } from "../src/parse" | ||
import { Token } from "../src/tokenize" | ||
|
||
describe("parse", () => { | ||
it("should return command offsets", () => { | ||
expect(parse("").commandOffsets).toStrictEqual([]) | ||
expect(parse("ls").commandOffsets).toStrictEqual([0, 1]) | ||
expect(parse("ls -al").commandOffsets).toStrictEqual([0, 2]) | ||
expect(parse("ls -al;").commandOffsets).toStrictEqual([0, 2]) | ||
expect(parse("ls -al;pwd").commandOffsets).toStrictEqual([0, 2, 3, 4]) | ||
expect(parse("ls -al; pwd").commandOffsets).toStrictEqual([0, 2, 3, 4]) | ||
it("should support no commands", () => { | ||
expect(parse("")).toEqual([]) | ||
expect(parse(";")).toEqual([]) | ||
expect(parse(" ; ; ")).toEqual([]) | ||
}) | ||
|
||
it("should return commands", () => { | ||
expect(parse("ls").commands).toEqual([["ls"]]) | ||
expect(parse("ls -l -a").commands).toEqual([["ls", "-l", "-a"]]) | ||
expect(parse("ls -l -a; pwd").commands).toEqual([["ls", "-l", "-a"], ["pwd"]]) | ||
it("should support single command", () => { | ||
expect(parse("ls")).toEqual([ | ||
new CommandNode(new Token(0, "ls"), []), | ||
]) | ||
expect(parse("ls -al")).toEqual([ | ||
new CommandNode(new Token(0, "ls"), [new Token(3, "-al")]), | ||
]) | ||
expect(parse("ls -al;")).toEqual([ | ||
new CommandNode(new Token(0, "ls"), [new Token(3, "-al")]), | ||
]) | ||
}) | ||
|
||
it("should raise on invalid index bounds", () => { | ||
const ast = parse("ls -al") | ||
expect(() => ast.command(-1)).toThrow(RangeError) | ||
expect(() => ast.command(2)).toThrow(RangeError) | ||
}) | ||
}) | ||
|
||
describe("AST validate", () => { | ||
const tokenizedSource = tokenize("ls -al; pwd") | ||
|
||
it("should raise if odd number of offsets", () => { | ||
expect(() => new AST(tokenizedSource, [0])).toThrow(ParseError) | ||
}) | ||
|
||
it("should raise if offset end not greater than start", () => { | ||
expect(() => new AST(tokenizedSource, [0, 0])).toThrow(ParseError) | ||
}) | ||
|
||
it("should raise if tokens overlap", () => { | ||
expect(() => new AST(tokenizedSource, [0, 2, 1, 3])).toThrow(ParseError) | ||
}) | ||
|
||
it("should raise if offsets out of bounds", () => { | ||
expect(() => new AST(tokenizedSource, [3, 5])).toThrow(ParseError) | ||
expect(() => new AST(tokenizedSource, [-1, 1])).toThrow(ParseError) | ||
it("should support multiple commands", () => { | ||
expect(parse("ls -al;pwd")).toEqual([ | ||
new CommandNode(new Token(0, "ls"), [new Token(3, "-al")]), | ||
new CommandNode(new Token(7, "pwd"), []), | ||
]) | ||
expect(parse("echo abc;pwd;ls -al")).toEqual([ | ||
new CommandNode(new Token(0, "echo"), [new Token(5, "abc")]), | ||
new CommandNode(new Token(9, "pwd"), []), | ||
new CommandNode(new Token(13, "ls"), [new Token(16, "-al")]), | ||
]) | ||
}) | ||
}) |