Skip to content

Reorganize & refactor diagnostic's utils. Add more const enums #2438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import {
} from "./common";

import {
Token,
Range
} from "./diagnostics";

import {
Token
} from "./tokenizer";

import {
Expand All @@ -41,7 +44,7 @@ import {
} from "./types";

/** Indicates the kind of a node. */
export enum NodeKind {
export const enum NodeKind {

SOURCE,

Expand Down Expand Up @@ -931,7 +934,7 @@ export class TypeParameterNode extends Node {
}

/** Represents the kind of a parameter. */
export enum ParameterKind {
export const enum ParameterKind {
/** No specific flags. */
DEFAULT,
/** Is an optional parameter. */
Expand Down Expand Up @@ -1083,7 +1086,7 @@ export class DecoratorNode extends Node {
}

/** Comment kinds. */
export enum CommentKind {
export const enum CommentKind {
/** Line comment. */
LINE,
/** Triple-slash line comment. */
Expand Down Expand Up @@ -1126,7 +1129,7 @@ export class IdentifierExpression extends Expression {
}

/** Indicates the kind of a literal. */
export enum LiteralKind {
export const enum LiteralKind {
FLOAT,
INTEGER,
STRING,
Expand Down Expand Up @@ -1161,7 +1164,7 @@ export class ArrayLiteralExpression extends LiteralExpression {
}

/** Indicates the kind of an assertion. */
export enum AssertionKind {
export const enum AssertionKind {
/** A prefix assertion, i.e. `<T>expr`. */
PREFIX,
/** An as assertion, i.e. `expr as T`. */
Expand Down Expand Up @@ -1602,7 +1605,7 @@ export class CompiledExpression extends Expression {
export abstract class Statement extends Node { }

/** Indicates the specific kind of a source. */
export enum SourceKind {
export const enum SourceKind {
/** User-provided file. */
USER = 0,
/** User-provided entry file. */
Expand Down
2 changes: 1 addition & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/** Indicates traits of a {@link Node} or {@link Element}. */
export enum CommonFlags {
export const enum CommonFlags {
/** No flags set. */
NONE = 0,

Expand Down
2 changes: 1 addition & 1 deletion src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "./builtins";

import {
Range,
DiagnosticCode,
DiagnosticEmitter
} from "./diagnostics";
Expand Down Expand Up @@ -106,7 +107,6 @@ import {

import {
Token,
Range,
operatorTokenToString
} from "./tokenizer";

Expand Down
50 changes: 44 additions & 6 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
* @license Apache-2.0
*/

import {
Range
} from "./tokenizer";

import {
Source
} from "./ast";
Expand Down Expand Up @@ -35,7 +31,7 @@ export {
} from "./diagnosticMessages.generated";

/** Indicates the category of a {@link DiagnosticMessage}. */
export enum DiagnosticCategory {
export const enum DiagnosticCategory {
/** Overly pedantic message. */
PEDANTIC,
/** Informatory message. */
Expand All @@ -46,6 +42,48 @@ export enum DiagnosticCategory {
ERROR
}

export class Range {

source!: Source;
debugInfoRef: usize = 0;

constructor(public start: i32, public end: i32) {}

static join(a: Range, b: Range): Range {
if (a.source != b.source) throw new Error("source mismatch");
let range = new Range(
a.start < b.start ? a.start : b.start,
a.end > b.end ? a.end : b.end
);
range.source = a.source;
return range;
}

equals(other: Range): bool {
return (
this.source == other.source &&
this.start == other.start &&
this.end == other.end
);
}

get atStart(): Range {
let range = new Range(this.start, this.start);
range.source = this.source;
return range;
}

get atEnd(): Range {
let range = new Range(this.end, this.end);
range.source = this.source;
return range;
}

toString(): string {
return this.source.text.substring(this.start, this.end);
}
}

/** Returns the string representation of the specified diagnostic category. */
export function diagnosticCategoryToString(category: DiagnosticCategory): string {
switch (category) {
Expand Down Expand Up @@ -239,7 +277,7 @@ function formatDiagnosticContext(range: Range): string {
lineNumber,
" │ ",
text.substring(start, end).replaceAll("\t", " "),
"\n ",
"\n ",
lineSpace,
" │ "
];
Expand Down
4 changes: 2 additions & 2 deletions src/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const enum FlowFlags {
}

/** Flags indicating the current state of a local. */
export enum LocalFlags {
export const enum LocalFlags {
/** No specific conditions. */
NONE = 0,

Expand All @@ -177,7 +177,7 @@ export enum LocalFlags {
}

/** Flags indicating the current state of a field. */
export enum FieldFlags {
export const enum FieldFlags {
NONE = 0,
INITIALIZED = 1 << 0
}
Expand Down
28 changes: 23 additions & 5 deletions src/index-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,31 @@
* When compiling to WebAssembly `glue/wasm/index.ts` must be included.
*/

import { Target, Runtime, Feature } from "./common";
import { Compiler, Options } from "./compiler";
import { TSDBuilder, JSBuilder } from "./bindings";
import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics";
import {
Target,
Runtime,
Feature
} from "./common";

import {
Compiler,
Options
} from "./compiler";

import {
TSDBuilder,
JSBuilder
} from "./bindings";

import {
Range,
DiagnosticMessage,
DiagnosticCategory,
formatDiagnosticMessage
} from "./diagnostics";

import { Module } from "./module";
import { Program } from "./program";
import { Range } from "./tokenizer";
import { Source } from "./ast";

// Options
Expand Down
34 changes: 17 additions & 17 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export namespace HeapTypeRef {
}

/** Binaryen feature constants. */
export enum FeatureFlags {
export const enum FeatureFlags {
MVP = 0 /* _BinaryenFeatureMVP */,
Atomics = 1 /* _BinaryenFeatureAtomics */,
MutableGlobals = 2 /* _BinaryenFeatureMutableGlobals */,
Expand All @@ -107,7 +107,7 @@ export enum FeatureFlags {
}

/** Binaryen expression id constants. */
export enum ExpressionId {
export const enum ExpressionId {
Invalid = 0 /* _BinaryenInvalidId */,
Block = 1 /* _BinaryenBlockId */,
If = 2 /* _BinaryenIfId */,
Expand Down Expand Up @@ -181,7 +181,7 @@ export enum ExpressionId {
}

/** Binaryen external kind constants. */
export enum ExternalKind {
export const enum ExternalKind {
Function = 0 /* _BinaryenExternalFunction */,
Table = 1 /* _BinaryenExternalTable */,
Memory = 2 /* _BinaryenExternalMemory */,
Expand All @@ -190,7 +190,7 @@ export enum ExternalKind {
}

/** Binaryen unary operation constants. */
export enum UnaryOp {
export const enum UnaryOp {
/** i32.clz */
ClzI32 = 0 /* _BinaryenClzInt32 */,
/** i64.clz */
Expand Down Expand Up @@ -466,7 +466,7 @@ export enum UnaryOp {
}

/** Binaryen binary operation constants. */
export enum BinaryOp {
export const enum BinaryOp {
/** i32.add */
AddI32 = 0 /* _BinaryenAddInt32 */,
/** i32.sub */
Expand Down Expand Up @@ -920,7 +920,7 @@ export enum BinaryOp {
}

/** Binaryen atomic read-modify-write operation constants. */
export enum AtomicRMWOp {
export const enum AtomicRMWOp {
/** i32.atomic.rmw.add, i32.atomic.rmw8.add_u, i32.atomic.rmw16.add_u, i64.atomic.rmw.add, i64.atomic.rmw8.add_u, i64.atomic.rmw16.add_u, i64.atomic.rmw32.add_u */
Add = 0 /* _BinaryenAtomicRMWAdd */,
/** i32.atomic.rmw.sub, i32.atomic.rmw8.sub_u, i32.atomic.rmw16.sub_u, i64.atomic.rmw.sub, i64.atomic.rmw8.sub_u, i64.atomic.rmw16.sub_u, i64.atomic.rmw32.sub_u */
Expand All @@ -936,7 +936,7 @@ export enum AtomicRMWOp {
}

/** Binaryen SIMD extract operation constants. */
export enum SIMDExtractOp {
export const enum SIMDExtractOp {
/** i8x16.extract_lane_s */
ExtractLaneI8x16 = 0 /* _BinaryenExtractLaneSVecI8x16 */,
/** i8x16.extract_lane_u */
Expand All @@ -956,7 +956,7 @@ export enum SIMDExtractOp {
}

/** Binaryen SIMD replace operation constants. */
export enum SIMDReplaceOp {
export const enum SIMDReplaceOp {
/** i8x16.replace_lane */
ReplaceLaneI8x16 = 0 /* _BinaryenReplaceLaneVecI8x16 */,
/** i16x8.replace_lane */
Expand All @@ -972,7 +972,7 @@ export enum SIMDReplaceOp {
}

/** Binaryen SIMD shift operation constants. */
export enum SIMDShiftOp {
export const enum SIMDShiftOp {
/** i8x16.shl */
ShlI8x16 = 0 /* _BinaryenShlVecI8x16 */,
/** i8x16.shr_s */
Expand Down Expand Up @@ -1000,7 +1000,7 @@ export enum SIMDShiftOp {
}

/** Binaryen SIMD load operation constants. */
export enum SIMDLoadOp {
export const enum SIMDLoadOp {
/** v128.load8_splat */
Load8Splat = 0 /* _BinaryenLoad8SplatVec128 */,
/** v128.load16_splat */
Expand Down Expand Up @@ -1028,7 +1028,7 @@ export enum SIMDLoadOp {
}

/** Binaryen SIMD load/store lane operation constants. */
export enum SIMDLoadStoreLaneOp {
export const enum SIMDLoadStoreLaneOp {
/** v128.load8_lane */
Load8Lane = 0 /* _BinaryenLoad8LaneVec128 */,
/** v128.load16_lane */
Expand All @@ -1048,13 +1048,13 @@ export enum SIMDLoadStoreLaneOp {
}

/** Binaryen SIMD ternary operation constants. */
export enum SIMDTernaryOp {
export const enum SIMDTernaryOp {
/** v128.bitselect */
Bitselect = 0 /* _BinaryenBitselectVec128 */
}

/** Binaryen RefIs operation constants. */
export enum RefIsOp {
export const enum RefIsOp {
/** ref.is_null */
RefIsNull = 0 /* _BinaryenRefIsNull */,
/** ref.is_func */
Expand All @@ -1066,7 +1066,7 @@ export enum RefIsOp {
}

/** Binaryen RefAs operation constants. */
export enum RefAsOp {
export const enum RefAsOp {
/** ref.as_non_null */
RefAsNonNull = 0 /* _BinaryenRefAsNonNull */,
/** ref.as_func */
Expand All @@ -1078,7 +1078,7 @@ export enum RefAsOp {
}

/** Binaryen BrOn operation constants. */
export enum BrOnOp {
export const enum BrOnOp {
/** br_on_null */
BrOnNull = 0 /* TODO_BinaryenBrOnNull */,
/** br_on_cast */
Expand All @@ -1092,7 +1092,7 @@ export enum BrOnOp {
}

/** Binaryen expression runner flags. */
export enum ExpressionRunnerFlags {
export const enum ExpressionRunnerFlags {
Default = 0 /* _ExpressionRunnerFlagsDefault */,
PreserveSideeffects = 1 /* _ExpressionRunnerFlagsPreserveSideeffects */,
TraverseCalls = 2 /* _ExpressionRunnerFlagsTraverseCalls */
Expand Down Expand Up @@ -3127,7 +3127,7 @@ export class SwitchBuilder {
}
}

export enum SideEffects {
export const enum SideEffects {
None = 0 /* _BinaryenSideEffectNone */,
Branches = 1 /* _BinaryenSideEffectBranches */,
Calls = 2 /* _BinaryenSideEffectCalls */,
Expand Down
Loading