Skip to content

Decouple the parser from the program #1218

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 4 commits into from
Apr 13, 2020
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
26 changes: 13 additions & 13 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ import {
PATH_DELIMITER
} from "./common";

import {
Program
} from "./program";

import {
Tokenizer,
Token,
Expand All @@ -28,7 +24,8 @@ import {

import {
DiagnosticCode,
DiagnosticEmitter
DiagnosticEmitter,
DiagnosticMessage
} from "./diagnostics";

import {
Expand Down Expand Up @@ -95,8 +92,6 @@ import {
/** Parser interface. */
export class Parser extends DiagnosticEmitter {

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

/** Constructs a new parser. */
constructor(program: Program) {
super(program.diagnostics);
this.program = program;
constructor(
diagnostics: DiagnosticMessage[] | null = null,
sources: Source[] | null = null
) {
super(diagnostics);
this.sources = sources ? sources : new Array<Source>();
}

/** Parses a file and adds its definitions to the program. */
Expand Down Expand Up @@ -145,12 +145,12 @@ export class Parser extends DiagnosticEmitter {
: SourceKind.LIBRARY
: SourceKind.USER
);
var program = this.program;
program.sources.push(source);

this.sources.push(source);
this.currentSource = source;

// tokenize and parse
var tn = new Tokenizer(source, program.diagnostics);
var tn = new Tokenizer(source, this.diagnostics);
tn.onComment = this.onComment;
var statements = source.statements;
while (!tn.skip(Token.ENDOFFILE)) {
Expand Down
2 changes: 1 addition & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ export class Program extends DiagnosticEmitter {
var nativeFile = new File(this, nativeSource);
this.nativeFile = nativeFile;
this.filesByName.set(nativeFile.internalName, nativeFile);
this.parser = new Parser(this);
this.parser = new Parser(this.diagnostics, this.sources);
this.resolver = new Resolver(this);
}

Expand Down