Skip to content

Commit a81896e

Browse files
authored
Reorganize & refactor diagnostic's utils. Add more const enums (#2438)
1 parent 256a6b0 commit a81896e

12 files changed

+110
-96
lines changed

src/ast.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ import {
2222
} from "./common";
2323

2424
import {
25-
Token,
2625
Range
26+
} from "./diagnostics";
27+
28+
import {
29+
Token
2730
} from "./tokenizer";
2831

2932
import {
@@ -41,7 +44,7 @@ import {
4144
} from "./types";
4245

4346
/** Indicates the kind of a node. */
44-
export enum NodeKind {
47+
export const enum NodeKind {
4548

4649
SOURCE,
4750

@@ -931,7 +934,7 @@ export class TypeParameterNode extends Node {
931934
}
932935

933936
/** Represents the kind of a parameter. */
934-
export enum ParameterKind {
937+
export const enum ParameterKind {
935938
/** No specific flags. */
936939
DEFAULT,
937940
/** Is an optional parameter. */
@@ -1083,7 +1086,7 @@ export class DecoratorNode extends Node {
10831086
}
10841087

10851088
/** Comment kinds. */
1086-
export enum CommentKind {
1089+
export const enum CommentKind {
10871090
/** Line comment. */
10881091
LINE,
10891092
/** Triple-slash line comment. */
@@ -1126,7 +1129,7 @@ export class IdentifierExpression extends Expression {
11261129
}
11271130

11281131
/** Indicates the kind of a literal. */
1129-
export enum LiteralKind {
1132+
export const enum LiteralKind {
11301133
FLOAT,
11311134
INTEGER,
11321135
STRING,
@@ -1161,7 +1164,7 @@ export class ArrayLiteralExpression extends LiteralExpression {
11611164
}
11621165

11631166
/** Indicates the kind of an assertion. */
1164-
export enum AssertionKind {
1167+
export const enum AssertionKind {
11651168
/** A prefix assertion, i.e. `<T>expr`. */
11661169
PREFIX,
11671170
/** An as assertion, i.e. `expr as T`. */
@@ -1602,7 +1605,7 @@ export class CompiledExpression extends Expression {
16021605
export abstract class Statement extends Node { }
16031606

16041607
/** Indicates the specific kind of a source. */
1605-
export enum SourceKind {
1608+
export const enum SourceKind {
16061609
/** User-provided file. */
16071610
USER = 0,
16081611
/** User-provided entry file. */

src/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
/** Indicates traits of a {@link Node} or {@link Element}. */
7-
export enum CommonFlags {
7+
export const enum CommonFlags {
88
/** No flags set. */
99
NONE = 0,
1010

src/compiler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "./builtins";
1616

1717
import {
18+
Range,
1819
DiagnosticCode,
1920
DiagnosticEmitter
2021
} from "./diagnostics";
@@ -106,7 +107,6 @@ import {
106107

107108
import {
108109
Token,
109-
Range,
110110
operatorTokenToString
111111
} from "./tokenizer";
112112

src/diagnostics.ts

+44-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
* @license Apache-2.0
44
*/
55

6-
import {
7-
Range
8-
} from "./tokenizer";
9-
106
import {
117
Source
128
} from "./ast";
@@ -35,7 +31,7 @@ export {
3531
} from "./diagnosticMessages.generated";
3632

3733
/** Indicates the category of a {@link DiagnosticMessage}. */
38-
export enum DiagnosticCategory {
34+
export const enum DiagnosticCategory {
3935
/** Overly pedantic message. */
4036
PEDANTIC,
4137
/** Informatory message. */
@@ -46,6 +42,48 @@ export enum DiagnosticCategory {
4642
ERROR
4743
}
4844

45+
export class Range {
46+
47+
source!: Source;
48+
debugInfoRef: usize = 0;
49+
50+
constructor(public start: i32, public end: i32) {}
51+
52+
static join(a: Range, b: Range): Range {
53+
if (a.source != b.source) throw new Error("source mismatch");
54+
let range = new Range(
55+
a.start < b.start ? a.start : b.start,
56+
a.end > b.end ? a.end : b.end
57+
);
58+
range.source = a.source;
59+
return range;
60+
}
61+
62+
equals(other: Range): bool {
63+
return (
64+
this.source == other.source &&
65+
this.start == other.start &&
66+
this.end == other.end
67+
);
68+
}
69+
70+
get atStart(): Range {
71+
let range = new Range(this.start, this.start);
72+
range.source = this.source;
73+
return range;
74+
}
75+
76+
get atEnd(): Range {
77+
let range = new Range(this.end, this.end);
78+
range.source = this.source;
79+
return range;
80+
}
81+
82+
toString(): string {
83+
return this.source.text.substring(this.start, this.end);
84+
}
85+
}
86+
4987
/** Returns the string representation of the specified diagnostic category. */
5088
export function diagnosticCategoryToString(category: DiagnosticCategory): string {
5189
switch (category) {
@@ -239,7 +277,7 @@ function formatDiagnosticContext(range: Range): string {
239277
lineNumber,
240278
" │ ",
241279
text.substring(start, end).replaceAll("\t", " "),
242-
"\n ",
280+
"\n ",
243281
lineSpace,
244282
" │ "
245283
];

src/flow.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export const enum FlowFlags {
162162
}
163163

164164
/** Flags indicating the current state of a local. */
165-
export enum LocalFlags {
165+
export const enum LocalFlags {
166166
/** No specific conditions. */
167167
NONE = 0,
168168

@@ -177,7 +177,7 @@ export enum LocalFlags {
177177
}
178178

179179
/** Flags indicating the current state of a field. */
180-
export enum FieldFlags {
180+
export const enum FieldFlags {
181181
NONE = 0,
182182
INITIALIZED = 1 << 0
183183
}

src/index-wasm.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@
1414
* When compiling to WebAssembly `glue/wasm/index.ts` must be included.
1515
*/
1616

17-
import { Target, Runtime, Feature } from "./common";
18-
import { Compiler, Options } from "./compiler";
19-
import { TSDBuilder, JSBuilder } from "./bindings";
20-
import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics";
17+
import {
18+
Target,
19+
Runtime,
20+
Feature
21+
} from "./common";
22+
23+
import {
24+
Compiler,
25+
Options
26+
} from "./compiler";
27+
28+
import {
29+
TSDBuilder,
30+
JSBuilder
31+
} from "./bindings";
32+
33+
import {
34+
Range,
35+
DiagnosticMessage,
36+
DiagnosticCategory,
37+
formatDiagnosticMessage
38+
} from "./diagnostics";
39+
2140
import { Module } from "./module";
2241
import { Program } from "./program";
23-
import { Range } from "./tokenizer";
2442
import { Source } from "./ast";
2543

2644
// Options

src/module.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export namespace HeapTypeRef {
8585
}
8686

8787
/** Binaryen feature constants. */
88-
export enum FeatureFlags {
88+
export const enum FeatureFlags {
8989
MVP = 0 /* _BinaryenFeatureMVP */,
9090
Atomics = 1 /* _BinaryenFeatureAtomics */,
9191
MutableGlobals = 2 /* _BinaryenFeatureMutableGlobals */,
@@ -107,7 +107,7 @@ export enum FeatureFlags {
107107
}
108108

109109
/** Binaryen expression id constants. */
110-
export enum ExpressionId {
110+
export const enum ExpressionId {
111111
Invalid = 0 /* _BinaryenInvalidId */,
112112
Block = 1 /* _BinaryenBlockId */,
113113
If = 2 /* _BinaryenIfId */,
@@ -181,7 +181,7 @@ export enum ExpressionId {
181181
}
182182

183183
/** Binaryen external kind constants. */
184-
export enum ExternalKind {
184+
export const enum ExternalKind {
185185
Function = 0 /* _BinaryenExternalFunction */,
186186
Table = 1 /* _BinaryenExternalTable */,
187187
Memory = 2 /* _BinaryenExternalMemory */,
@@ -190,7 +190,7 @@ export enum ExternalKind {
190190
}
191191

192192
/** Binaryen unary operation constants. */
193-
export enum UnaryOp {
193+
export const enum UnaryOp {
194194
/** i32.clz */
195195
ClzI32 = 0 /* _BinaryenClzInt32 */,
196196
/** i64.clz */
@@ -466,7 +466,7 @@ export enum UnaryOp {
466466
}
467467

468468
/** Binaryen binary operation constants. */
469-
export enum BinaryOp {
469+
export const enum BinaryOp {
470470
/** i32.add */
471471
AddI32 = 0 /* _BinaryenAddInt32 */,
472472
/** i32.sub */
@@ -920,7 +920,7 @@ export enum BinaryOp {
920920
}
921921

922922
/** Binaryen atomic read-modify-write operation constants. */
923-
export enum AtomicRMWOp {
923+
export const enum AtomicRMWOp {
924924
/** 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 */
925925
Add = 0 /* _BinaryenAtomicRMWAdd */,
926926
/** 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 */
@@ -936,7 +936,7 @@ export enum AtomicRMWOp {
936936
}
937937

938938
/** Binaryen SIMD extract operation constants. */
939-
export enum SIMDExtractOp {
939+
export const enum SIMDExtractOp {
940940
/** i8x16.extract_lane_s */
941941
ExtractLaneI8x16 = 0 /* _BinaryenExtractLaneSVecI8x16 */,
942942
/** i8x16.extract_lane_u */
@@ -956,7 +956,7 @@ export enum SIMDExtractOp {
956956
}
957957

958958
/** Binaryen SIMD replace operation constants. */
959-
export enum SIMDReplaceOp {
959+
export const enum SIMDReplaceOp {
960960
/** i8x16.replace_lane */
961961
ReplaceLaneI8x16 = 0 /* _BinaryenReplaceLaneVecI8x16 */,
962962
/** i16x8.replace_lane */
@@ -972,7 +972,7 @@ export enum SIMDReplaceOp {
972972
}
973973

974974
/** Binaryen SIMD shift operation constants. */
975-
export enum SIMDShiftOp {
975+
export const enum SIMDShiftOp {
976976
/** i8x16.shl */
977977
ShlI8x16 = 0 /* _BinaryenShlVecI8x16 */,
978978
/** i8x16.shr_s */
@@ -1000,7 +1000,7 @@ export enum SIMDShiftOp {
10001000
}
10011001

10021002
/** Binaryen SIMD load operation constants. */
1003-
export enum SIMDLoadOp {
1003+
export const enum SIMDLoadOp {
10041004
/** v128.load8_splat */
10051005
Load8Splat = 0 /* _BinaryenLoad8SplatVec128 */,
10061006
/** v128.load16_splat */
@@ -1028,7 +1028,7 @@ export enum SIMDLoadOp {
10281028
}
10291029

10301030
/** Binaryen SIMD load/store lane operation constants. */
1031-
export enum SIMDLoadStoreLaneOp {
1031+
export const enum SIMDLoadStoreLaneOp {
10321032
/** v128.load8_lane */
10331033
Load8Lane = 0 /* _BinaryenLoad8LaneVec128 */,
10341034
/** v128.load16_lane */
@@ -1048,13 +1048,13 @@ export enum SIMDLoadStoreLaneOp {
10481048
}
10491049

10501050
/** Binaryen SIMD ternary operation constants. */
1051-
export enum SIMDTernaryOp {
1051+
export const enum SIMDTernaryOp {
10521052
/** v128.bitselect */
10531053
Bitselect = 0 /* _BinaryenBitselectVec128 */
10541054
}
10551055

10561056
/** Binaryen RefIs operation constants. */
1057-
export enum RefIsOp {
1057+
export const enum RefIsOp {
10581058
/** ref.is_null */
10591059
RefIsNull = 0 /* _BinaryenRefIsNull */,
10601060
/** ref.is_func */
@@ -1066,7 +1066,7 @@ export enum RefIsOp {
10661066
}
10671067

10681068
/** Binaryen RefAs operation constants. */
1069-
export enum RefAsOp {
1069+
export const enum RefAsOp {
10701070
/** ref.as_non_null */
10711071
RefAsNonNull = 0 /* _BinaryenRefAsNonNull */,
10721072
/** ref.as_func */
@@ -1078,7 +1078,7 @@ export enum RefAsOp {
10781078
}
10791079

10801080
/** Binaryen BrOn operation constants. */
1081-
export enum BrOnOp {
1081+
export const enum BrOnOp {
10821082
/** br_on_null */
10831083
BrOnNull = 0 /* TODO_BinaryenBrOnNull */,
10841084
/** br_on_cast */
@@ -1092,7 +1092,7 @@ export enum BrOnOp {
10921092
}
10931093

10941094
/** Binaryen expression runner flags. */
1095-
export enum ExpressionRunnerFlags {
1095+
export const enum ExpressionRunnerFlags {
10961096
Default = 0 /* _ExpressionRunnerFlagsDefault */,
10971097
PreserveSideeffects = 1 /* _ExpressionRunnerFlagsPreserveSideeffects */,
10981098
TraverseCalls = 2 /* _ExpressionRunnerFlagsTraverseCalls */
@@ -3127,7 +3127,7 @@ export class SwitchBuilder {
31273127
}
31283128
}
31293129

3130-
export enum SideEffects {
3130+
export const enum SideEffects {
31313131
None = 0 /* _BinaryenSideEffectNone */,
31323132
Branches = 1 /* _BinaryenSideEffectBranches */,
31333133
Calls = 2 /* _BinaryenSideEffectCalls */,

0 commit comments

Comments
 (0)