mirror of
				https://git.sfja.dk/Mikkel/slige.git
				synced 2025-11-04 06:58:16 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			88 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			88 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import * as ast from "./ast/mod.ts";
 | 
						|
import { prettyPrintReport, printStackTrace, Report } from "./diagnostics.ts";
 | 
						|
 | 
						|
export class Ctx {
 | 
						|
    private fileIds = new Ids();
 | 
						|
    private files = new Map<Id<File>, FileInfo>();
 | 
						|
 | 
						|
    private reports: Report[] = [];
 | 
						|
 | 
						|
    public fileHasChildWithIdent(file: File, childIdent: string): boolean {
 | 
						|
        return this.files.get(id(file))!
 | 
						|
            .subFiles.has(childIdent);
 | 
						|
    }
 | 
						|
 | 
						|
    public addFile(
 | 
						|
        ident: string,
 | 
						|
        absPath: string,
 | 
						|
        relPath: string,
 | 
						|
        superFile: File | undefined,
 | 
						|
        text: string,
 | 
						|
    ): File {
 | 
						|
        const file = this.fileIds.nextThenStep();
 | 
						|
        this.files.set(id(file), {
 | 
						|
            ident,
 | 
						|
            absPath,
 | 
						|
            relPath,
 | 
						|
            superFile,
 | 
						|
            subFiles: new Map(),
 | 
						|
            text,
 | 
						|
        });
 | 
						|
        if (superFile) {
 | 
						|
            this.files.get(id(superFile))!
 | 
						|
                .subFiles.set(ident, file);
 | 
						|
        }
 | 
						|
        return file;
 | 
						|
    }
 | 
						|
 | 
						|
    public addFileAst(file: File, ast: ast.File) {
 | 
						|
        this.files.get(id(file))!.ast = ast;
 | 
						|
    }
 | 
						|
 | 
						|
    public fileInfo(file: File): FileInfo {
 | 
						|
        return this.files.get(id(file))!;
 | 
						|
    }
 | 
						|
 | 
						|
    public report(rep: Report) {
 | 
						|
        this.reports.push(rep);
 | 
						|
        this.reportImmediately(rep);
 | 
						|
    }
 | 
						|
 | 
						|
    public enableReportImmediately = false;
 | 
						|
    public enableStacktrace = false;
 | 
						|
    private reportImmediately(rep: Report) {
 | 
						|
        if (this.enableReportImmediately) {
 | 
						|
            prettyPrintReport(this, rep);
 | 
						|
            if (this.enableStacktrace) {
 | 
						|
                printStackTrace();
 | 
						|
            }
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
export type File = IdBase;
 | 
						|
 | 
						|
export type FileInfo = {
 | 
						|
    ident: string;
 | 
						|
    absPath: string;
 | 
						|
    relPath: string;
 | 
						|
    superFile?: File;
 | 
						|
    subFiles: Map<string, File>;
 | 
						|
    text: string;
 | 
						|
    ast?: ast.File;
 | 
						|
};
 | 
						|
 | 
						|
export type IdBase = { id: number };
 | 
						|
 | 
						|
export type Id<IdType extends IdBase> = IdType["id"];
 | 
						|
export const id = <IdType extends IdBase>(id: IdType): Id<IdType> => id.id;
 | 
						|
 | 
						|
export class Ids<IdType extends IdBase> {
 | 
						|
    private next = 0;
 | 
						|
    public nextThenStep(): IdType {
 | 
						|
        const id = this.next;
 | 
						|
        this.next += 1;
 | 
						|
        return { id } as IdType;
 | 
						|
    }
 | 
						|
}
 |