Skip to content

Commit 51ba4a2

Browse files
authored
Decouple parser from program (#1218)
1 parent c2949e2 commit 51ba4a2

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

Diff for: src/parser.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import {
1313
PATH_DELIMITER
1414
} from "./common";
1515

16-
import {
17-
Program
18-
} from "./program";
19-
2016
import {
2117
Tokenizer,
2218
Token,
@@ -28,7 +24,8 @@ import {
2824

2925
import {
3026
DiagnosticCode,
31-
DiagnosticEmitter
27+
DiagnosticEmitter,
28+
DiagnosticMessage
3229
} from "./diagnostics";
3330

3431
import {
@@ -95,8 +92,6 @@ import {
9592
/** Parser interface. */
9693
export class Parser extends DiagnosticEmitter {
9794

98-
/** Program being created. */
99-
program: Program;
10095
/** Source file names to be requested next. */
10196
backlog: string[] = new Array();
10297
/** Source file names already seen, that is processed or backlogged. */
@@ -109,11 +104,16 @@ export class Parser extends DiagnosticEmitter {
109104
currentSource: Source;
110105
/** Dependency map **/
111106
dependees: Map<string, Source> = new Map();
107+
/** An array of parsed sources. */
108+
sources: Source[];
112109

113110
/** Constructs a new parser. */
114-
constructor(program: Program) {
115-
super(program.diagnostics);
116-
this.program = program;
111+
constructor(
112+
diagnostics: DiagnosticMessage[] | null = null,
113+
sources: Source[] | null = null
114+
) {
115+
super(diagnostics);
116+
this.sources = sources ? sources : new Array<Source>();
117117
}
118118

119119
/** Parses a file and adds its definitions to the program. */
@@ -145,12 +145,12 @@ export class Parser extends DiagnosticEmitter {
145145
: SourceKind.LIBRARY
146146
: SourceKind.USER
147147
);
148-
var program = this.program;
149-
program.sources.push(source);
148+
149+
this.sources.push(source);
150150
this.currentSource = source;
151151

152152
// tokenize and parse
153-
var tn = new Tokenizer(source, program.diagnostics);
153+
var tn = new Tokenizer(source, this.diagnostics);
154154
tn.onComment = this.onComment;
155155
var statements = source.statements;
156156
while (!tn.skip(Token.ENDOFFILE)) {

Diff for: src/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ export class Program extends DiagnosticEmitter {
537537
var nativeFile = new File(this, nativeSource);
538538
this.nativeFile = nativeFile;
539539
this.filesByName.set(nativeFile.internalName, nativeFile);
540-
this.parser = new Parser(this);
540+
this.parser = new Parser(this.diagnostics, this.sources);
541541
this.resolver = new Resolver(this);
542542
}
543543

0 commit comments

Comments
 (0)