parser stuff
This commit is contained in:
parent
48e95bdef5
commit
c92fce8ccb
@ -22,7 +22,8 @@ export type StmtKind =
|
|||||||
export type ItemStmt = { item: Item };
|
export type ItemStmt = { item: Item };
|
||||||
|
|
||||||
export type LetStmt = {
|
export type LetStmt = {
|
||||||
subject: Pat;
|
pat: Pat;
|
||||||
|
ty: Ty;
|
||||||
expr?: Expr;
|
expr?: Expr;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -56,11 +57,18 @@ export type ItemKind =
|
|||||||
| { tag: "use" } & UseItem
|
| { tag: "use" } & UseItem
|
||||||
| { tag: "type_alias" } & TypeAliasItem;
|
| { tag: "type_alias" } & TypeAliasItem;
|
||||||
|
|
||||||
export type ModBlockItem = { ident: Ident; stmts: Stmt[] };
|
export type ModBlockItem = { stmts: Stmt[] };
|
||||||
export type ModFileItem = { ident: Ident; filePath: string };
|
export type ModFileItem = { filePath: string };
|
||||||
export type EnumItem = { variants: Variant[] };
|
export type EnumItem = { variants: Variant[] };
|
||||||
export type StructItem = { data: VariantData };
|
export type StructItem = { data: VariantData };
|
||||||
export type FnItem = { _: 0 };
|
|
||||||
|
export type FnItem = {
|
||||||
|
generics?: Generics;
|
||||||
|
params: Param[];
|
||||||
|
returnTy?: Ty;
|
||||||
|
body: Block;
|
||||||
|
};
|
||||||
|
|
||||||
export type UseItem = { _: 0 };
|
export type UseItem = { _: 0 };
|
||||||
export type TypeAliasItem = { ty: Ty };
|
export type TypeAliasItem = { ty: Ty };
|
||||||
|
|
||||||
@ -92,6 +100,21 @@ export type FieldDef = {
|
|||||||
span: Span;
|
span: Span;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Param = {
|
||||||
|
pat: Pat;
|
||||||
|
ty: Ty;
|
||||||
|
span: Span;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Generics = {
|
||||||
|
params: GenericParam[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GenericParam = {
|
||||||
|
ident: Ident;
|
||||||
|
span: Span;
|
||||||
|
};
|
||||||
|
|
||||||
export type Expr = {
|
export type Expr = {
|
||||||
kind: ExprKind;
|
kind: ExprKind;
|
||||||
span: Span;
|
span: Span;
|
||||||
@ -129,7 +152,7 @@ export type StringExpr = { value: string };
|
|||||||
export type GroupExpr = { expr: Expr };
|
export type GroupExpr = { expr: Expr };
|
||||||
export type ArrayExpr = { exprs: Expr[] };
|
export type ArrayExpr = { exprs: Expr[] };
|
||||||
export type RepeatExpr = { expr: Expr; length: Expr };
|
export type RepeatExpr = { expr: Expr; length: Expr };
|
||||||
export type StructExpr = { path?: Path; field: ExprField[] };
|
export type StructExpr = { path?: Path; fields: ExprField[] };
|
||||||
export type RefExpr = { expr: Expr; refType: RefType; mut: boolean };
|
export type RefExpr = { expr: Expr; refType: RefType; mut: boolean };
|
||||||
export type DerefExpr = { expr: Expr };
|
export type DerefExpr = { expr: Expr };
|
||||||
export type ElemExpr = { expr: Expr; elem: number };
|
export type ElemExpr = { expr: Expr; elem: number };
|
||||||
@ -137,8 +160,8 @@ export type FieldExpr = { expr: Expr; ident: Ident };
|
|||||||
export type IndexExpr = { expr: Expr; index: Expr };
|
export type IndexExpr = { expr: Expr; index: Expr };
|
||||||
export type CallExpr = { expr: Expr; args: Expr[] };
|
export type CallExpr = { expr: Expr; args: Expr[] };
|
||||||
export type UnaryExpr = { unaryType: UnaryType; expr: Expr };
|
export type UnaryExpr = { unaryType: UnaryType; expr: Expr };
|
||||||
export type BinaryExpr = { unaryType: UnaryType; left: Expr; right: Expr };
|
export type BinaryExpr = { binaryType: BinaryType; left: Expr; right: Expr };
|
||||||
export type IfExpr = { cond: Expr; truthy: Block; falsy?: Block };
|
export type IfExpr = { cond: Expr; truthy: Block; falsy?: Expr };
|
||||||
export type LoopExpr = { body: Block };
|
export type LoopExpr = { body: Block };
|
||||||
export type WhileExpr = { cond: Expr; body: Block };
|
export type WhileExpr = { cond: Expr; body: Block };
|
||||||
export type ForExpr = { pat: Pat; expr: Expr; body: Block };
|
export type ForExpr = { pat: Pat; expr: Expr; body: Block };
|
||||||
|
@ -12,6 +12,10 @@ export type Pos = {
|
|||||||
col: number;
|
col: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const Span = {
|
||||||
|
fromto: ({ begin }: Span, { end }: Span): Span => ({ begin, end }),
|
||||||
|
} as const;
|
||||||
|
|
||||||
export type Report = {
|
export type Report = {
|
||||||
severity: "fatal" | "error" | "warning" | "info";
|
severity: "fatal" | "error" | "warning" | "info";
|
||||||
origin?: string;
|
origin?: string;
|
||||||
@ -31,43 +35,82 @@ function severityColor(severity: "fatal" | "error" | "warning" | "info") {
|
|||||||
return "\x1b[1m\x1b[33m";
|
return "\x1b[1m\x1b[33m";
|
||||||
case "info":
|
case "info":
|
||||||
return "\x1b[1m\x1b[34m";
|
return "\x1b[1m\x1b[34m";
|
||||||
}
|
}
|
||||||
exhausted(severity)
|
exhausted(severity);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prettyPrintReport(ctx: Ctx, rep: Report) {
|
export function prettyPrintReport(ctx: Ctx, rep: Report) {
|
||||||
const { severity, msg } = rep;
|
const { severity, msg } = rep;
|
||||||
const origin = rep.origin ? `\x1b[1m${rep.origin}:\x1b[0m ` : "";
|
const origin = rep.origin ? `\x1b[1m${rep.origin}:\x1b[0m ` : "";
|
||||||
console.error(`${origin}${severityColor(severity)}${severity}:\x1b[0m \x1b[37m${msg}\x1b[0m`);
|
console.error(
|
||||||
|
`${origin}${
|
||||||
|
severityColor(severity)
|
||||||
|
}${severity}:\x1b[0m \x1b[37m${msg}\x1b[0m`,
|
||||||
|
);
|
||||||
if (rep.file && (rep.span || rep.pos)) {
|
if (rep.file && (rep.span || rep.pos)) {
|
||||||
const errorLineOffset = 2
|
const errorLineOffset = 2;
|
||||||
const { absPath: path } = ctx.fileInfo(rep.file);
|
const { absPath: path } = ctx.fileInfo(rep.file);
|
||||||
const { line, col } = rep.span?.begin ?? rep.pos!;
|
const { line, col } = rep.span?.begin ?? rep.pos!;
|
||||||
console.error(` --> ./${path}:${line}:${col}`);
|
console.error(` --> ./${path}:${line}:${col}`);
|
||||||
if (rep.span) {
|
if (rep.span) {
|
||||||
const spanLines = ctx.fileSpanText(rep.file, rep.span).split("\n");
|
const spanLines = ctx.fileSpanText(rep.file, rep.span).split("\n");
|
||||||
spanLines.pop()
|
spanLines.pop();
|
||||||
if (spanLines.length == 1) {
|
if (spanLines.length == 1) {
|
||||||
console.error(`${rep.span.begin.line.toString().padStart(4, ' ')}| ${spanLines[0]}`);
|
console.error(
|
||||||
console.error(` | ${severityColor(severity)}${" ".repeat(rep.span.begin.col)}${"~".repeat(rep.span.end.col-rep.span.begin.col)}\x1b[0m`)
|
`${rep.span.begin.line.toString().padStart(4, " ")}| ${
|
||||||
return
|
spanLines[0]
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
console.error(
|
||||||
|
` | ${severityColor(severity)}${
|
||||||
|
" ".repeat(rep.span.begin.col)
|
||||||
|
}${
|
||||||
|
"~".repeat(rep.span.end.col - rep.span.begin.col)
|
||||||
|
}\x1b[0m`,
|
||||||
|
);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
for (let i = 0; i < spanLines.length; i++) {
|
for (let i = 0; i < spanLines.length; i++) {
|
||||||
console.error(`${(rep.span.begin.line+i).toString().padStart(4, ' ')}| ${spanLines[i]}`);
|
console.error(
|
||||||
|
`${
|
||||||
|
(rep.span.begin.line + i).toString().padStart(4, " ")
|
||||||
|
}| ${spanLines[i]}`,
|
||||||
|
);
|
||||||
if (i == 0) {
|
if (i == 0) {
|
||||||
console.error(` | ${" ".repeat(rep.span.begin.col-1)}${severityColor(severity)}${"~".repeat(spanLines[i].length-(rep.span.begin.col-1))}\x1b[0m`)
|
console.error(
|
||||||
}
|
` | ${" ".repeat(rep.span.begin.col - 1)}${
|
||||||
else if (i == spanLines.length-1) {
|
severityColor(severity)
|
||||||
console.error(` | ${severityColor(severity)}${"~".repeat(rep.span.end.col)}\x1b[0m`)
|
}${
|
||||||
}
|
"~".repeat(
|
||||||
else {
|
spanLines[i].length - (rep.span.begin.col - 1),
|
||||||
console.error(` | ${severityColor(severity)}${"~".repeat(spanLines[i].length)}\x1b[0m`)
|
)
|
||||||
|
}\x1b[0m`,
|
||||||
|
);
|
||||||
|
} else if (i == spanLines.length - 1) {
|
||||||
|
console.error(
|
||||||
|
` | ${severityColor(severity)}${
|
||||||
|
"~".repeat(rep.span.end.col)
|
||||||
|
}\x1b[0m`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.error(
|
||||||
|
` | ${severityColor(severity)}${
|
||||||
|
"~".repeat(spanLines[i].length)
|
||||||
|
}\x1b[0m`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else if (rep.pos) {
|
||||||
else if (rep.pos) {
|
console.error(
|
||||||
console.error(`${rep.pos.line.toString().padStart(4, ' ')}| ${ctx.filePosLineText(rep.file, rep.pos)}`);
|
`${rep.pos.line.toString().padStart(4, " ")}| ${
|
||||||
console.error(` | ${severityColor(severity)}${" ".repeat(rep.pos.col)}^\x1b[0m`)
|
ctx.filePosLineText(rep.file, rep.pos)
|
||||||
|
}`,
|
||||||
|
);
|
||||||
|
console.error(
|
||||||
|
` | ${severityColor(severity)}${
|
||||||
|
" ".repeat(rep.pos.col)
|
||||||
|
}^\x1b[0m`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -1,16 +1,14 @@
|
|||||||
import { Span } from "../diagnostics.ts";
|
import { Span } from "../diagnostics.ts";
|
||||||
|
|
||||||
export type Token = TokenData & {
|
export type Token = {
|
||||||
|
type: string;
|
||||||
span: Span;
|
span: Span;
|
||||||
length: number;
|
length: number;
|
||||||
|
identValue?: string;
|
||||||
|
intValue?: number;
|
||||||
|
stringValue?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TokenData =
|
|
||||||
| { type: "ident"; identValue: string }
|
|
||||||
| { type: "int"; intValue: number }
|
|
||||||
| { type: "string"; stringValue: string }
|
|
||||||
| { type: string };
|
|
||||||
|
|
||||||
export interface TokenIter {
|
export interface TokenIter {
|
||||||
next(): Token | null;
|
next(): Token | null;
|
||||||
}
|
}
|
||||||
@ -29,4 +27,3 @@ export class SigFilter implements TokenIter {
|
|||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@ export type Err<E> = { ok: false; val: E };
|
|||||||
export const Ok = <V>(val: V): Ok<V> => ({ ok: true, val });
|
export const Ok = <V>(val: V): Ok<V> => ({ ok: true, val });
|
||||||
export const Err = <E>(val: E): Err<E> => ({ ok: false, val });
|
export const Err = <E>(val: E): Err<E> => ({ ok: false, val });
|
||||||
|
|
||||||
|
export const Res = { Ok, Err } as const;
|
||||||
|
|
||||||
export type ControlFlow<
|
export type ControlFlow<
|
||||||
R = undefined,
|
R = undefined,
|
||||||
V = undefined,
|
V = undefined,
|
||||||
|
Loading…
Reference in New Issue
Block a user