Add missing index.ts
This commit is contained in:
parent
19af942889
commit
088bdc94c2
1 changed files with 27 additions and 0 deletions
27
src/index.ts
Normal file
27
src/index.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { SourceFile, TextFile } from "./cst";
|
||||
import { Diagnostics, UnexpectedCharDiagnostic, UnexpectedTokenDiagnostic } from "./diagnostics";
|
||||
import { ParseError, Parser } from "./parser";
|
||||
import { Punctuator, ScanError, Scanner } from "./scanner";
|
||||
|
||||
export function parseSourceFile(file: TextFile, diagnostics: Diagnostics): SourceFile | null {
|
||||
const scanner = new Scanner(file.text, 0, diagnostics, file);
|
||||
const punctuated = new Punctuator(scanner);
|
||||
const parser = new Parser(file, punctuated);
|
||||
let sourceFile;
|
||||
try {
|
||||
sourceFile = parser.parseSourceFile();
|
||||
} catch (error) {
|
||||
if (error instanceof ParseError) {
|
||||
diagnostics.add(new UnexpectedTokenDiagnostic(error.file, error.actual, error.expected));
|
||||
return null;
|
||||
}
|
||||
if (error instanceof ScanError) {
|
||||
diagnostics.add(new UnexpectedCharDiagnostic(error.file, error.position, error.actual));
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
sourceFile.setParents();
|
||||
return sourceFile;
|
||||
}
|
||||
|
Loading…
Reference in a new issue