-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
160 lines (130 loc) · 4.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Low-level C-like compiler API.
* @module index
*//***/
import {
Compiler,
Options,
Target
} from "./compiler";
import {
Decompiler
} from "./decompiler";
import {
IDLBuilder,
TSDBuilder
} from "./definitions";
import {
DiagnosticMessage,
DiagnosticCategory,
formatDiagnosticMessage
} from "./diagnostics";
import {
Module
} from "./module";
import {
Parser
} from "./parser";
import {
Program,
LIBRARY_PREFIX
} from "./program";
/** Parses a source file. If `parser` has been omitted a new one is created. */
export function parseFile(text: string, path: string, isEntry: bool = false,
parser: Parser | null = null
): Parser {
if (!parser) parser = new Parser();
parser.parseFile(text, path, isEntry);
return parser;
}
/** Obtains the next required file's path. Returns `null` once complete. */
export function nextFile(parser: Parser): string | null {
return parser.nextFile();
}
/** Obtains the next diagnostic message. Returns `null` once complete. */
export function nextDiagnostic(parser: Parser): DiagnosticMessage | null {
var program = parser.program;
return program.diagnosticsOffset < program.diagnostics.length
? program.diagnostics[program.diagnosticsOffset++]
: null;
}
/** Formats a diagnostic message to a string. */
export { formatDiagnosticMessage as formatDiagnostic };
/** Tests whether a diagnostic is informatory. */
export function isInfo(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.INFO;
}
/** Tests whether a diagnostic is a warning. */
export function isWarning(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.WARNING;
}
/** Tests whether a diagnostic is an error. */
export function isError(message: DiagnosticMessage): bool {
return message.category == DiagnosticCategory.ERROR;
}
/** Creates a new set of compiler options. */
export function createOptions(): Options {
return new Options();
}
/** Sets the `target` option. */
export function setTarget(options: Options, target: Target): void {
options.target = target;
}
/** Sets the `noTreeShaking` option. */
export function setNoTreeShaking(options: Options, noTreeShaking: bool): void {
options.noTreeShaking = noTreeShaking;
}
/** Sets the `noAssert` option. */
export function setNoAssert(options: Options, noAssert: bool): void {
options.noAssert = noAssert;
}
/** Sets the `noMemory` option. */
export function setNoMemory(options: Options, noMemory: bool): void {
options.noMemory = noMemory;
}
/** Sets the `importMemory` option. */
export function setImportMemory(options: Options, importMemory: bool): void {
options.importMemory = importMemory;
}
/** Sets the `importTable` option. */
export function setImportTable(options: Options, importTable: bool): void {
options.importTable = importTable;
}
/** Sets the `sourceMap` option. */
export function setSourceMap(options: Options, sourceMap: bool): void {
options.sourceMap = sourceMap;
}
/** Sets the `memoryBase` option. */
export function setMemoryBase(options: Options, memoryBase: u32): void {
options.memoryBase = memoryBase;
}
/** Sets a 'globalAliases' value. */
export function setGlobalAlias(options: Options, name: string, alias: string): void {
var globalAliases = options.globalAliases;
if (!globalAliases) options.globalAliases = globalAliases = new Map();
globalAliases.set(name, alias);
}
/** Finishes parsing. */
export function finishParsing(parser: Parser): Program {
return parser.finish();
}
/** Compiles the sources computed by the parser to a module. */
export function compileProgram(program: Program, options: Options | null = null): Module {
return new Compiler(program, options).compile();
}
/** Decompiles a module to its (low level) source. */
export function decompileModule(module: Module): string {
var decompiler = new Decompiler();
decompiler.decompile(module);
return decompiler.finish();
}
/** Builds WebIDL definitions for the specified program. */
export function buildIDL(program: Program): string {
return IDLBuilder.build(program);
}
/** Builds TypeScript definitions for the specified program. */
export function buildTSD(program: Program): string {
return TSDBuilder.build(program);
}
/** Prefix indicating a library file. */
export { LIBRARY_PREFIX };