Something went wrong. Try again.
Shell parser/AST for POSIX/Bash/mksh/zsh, in TypeScript
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142import type { ParseError, ParseOptions, ParseResult } from "./ast";import { Parser } from "./parser";import { tokenize } from "./tokenizer";import { SourceMap } from "./tokenizer/cursor";
export function parse(source: string, options: ParseOptions = {}): ParseResult { if (options.recoverErrors) { return parseRecovering(source, options); } const tokens = tokenize(source, options); const parser = new Parser(tokens, options); const ast = parser.parseProgram(); parser.assertEof(); return { ast };}
function parseRecovering(source: string, options: ParseOptions): ParseResult { const errors: ParseError[] = []; let tokens: ReturnType<typeof tokenize>; try { tokens = tokenize(source, options, errors); } catch (e) { errors.push({ message: e instanceof Error ? e.message : String(e), pos: { offset: 0, line: 1, col: 1 }, }); const map = new SourceMap(source); return { ast: { type: "Program", body: [], pos: { offset: 0, line: 1, col: 1 }, end: map.posAt(source.length), }, errors, }; } const parser = new Parser(tokens, options); const result = parser.parseProgramRecovering(errors); return errors.length > 0 ? { ast: result, errors } : { ast: result };}