Skip to content
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

Change the signature of the Parser constructor to take a parameter object #10926

Merged
merged 1 commit into from
Jun 23, 2019
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
5 changes: 4 additions & 1 deletion src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,10 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
this.opMap = getOPMap();
// TODO(mduan): pass array of knownCommands rather than this.opMap
// dictionary
this.parser = new Parser(new Lexer(stream, this.opMap), false, xref);
this.parser = new Parser({
lexer: new Lexer(stream, this.opMap),
xref,
});
this.stateManager = stateManager;
this.nonProcessedArgs = [];
this._numInvalidPathOPS = 0;
Expand Down
27 changes: 21 additions & 6 deletions src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,8 +1477,12 @@ var XRef = (function XRefClosure() {
let trailerDict;
for (i = 0, ii = trailers.length; i < ii; ++i) {
stream.pos = trailers[i];
var parser = new Parser(new Lexer(stream), /* allowStreams = */ true,
/* xref = */ this, /* recoveryMode = */ true);
const parser = new Parser({
lexer: new Lexer(stream),
xref: this,
allowStreams: true,
recoveryMode: true,
});
var obj = parser.getObj();
if (!isCmd(obj, 'trailer')) {
continue;
Expand Down Expand Up @@ -1536,7 +1540,11 @@ var XRef = (function XRefClosure() {

stream.pos = startXRef + stream.start;

var parser = new Parser(new Lexer(stream), true, this);
const parser = new Parser({
lexer: new Lexer(stream),
xref: this,
allowStreams: true,
});
var obj = parser.getObj();
var dict;

Expand Down Expand Up @@ -1662,7 +1670,11 @@ var XRef = (function XRefClosure() {
}
var stream = this.stream.makeSubStream(xrefEntry.offset +
this.stream.start);
var parser = new Parser(new Lexer(stream), true, this);
const parser = new Parser({
lexer: new Lexer(stream),
xref: this,
allowStreams: true,
});
var obj1 = parser.getObj();
var obj2 = parser.getObj();
var obj3 = parser.getObj();
Expand Down Expand Up @@ -1709,8 +1721,11 @@ var XRef = (function XRefClosure() {
throw new FormatError(
'invalid first and n parameters for ObjStm stream');
}
var parser = new Parser(new Lexer(stream), false, this);
parser.allowStreams = true;
const parser = new Parser({
lexer: new Lexer(stream),
xref: this,
allowStreams: true,
});
var i, entries = [], num, nums = [];
// read the object numbers to populate cache
for (i = 0; i < n; ++i) {
Expand Down
11 changes: 7 additions & 4 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ function computeAdler32(bytes) {
}

class Parser {
constructor(lexer, allowStreams, xref, recoveryMode = false) {
constructor({ lexer, xref, allowStreams = false, recoveryMode = false, }) {
this.lexer = lexer;
this.allowStreams = allowStreams;
this.xref = xref;
this.allowStreams = allowStreams;
this.recoveryMode = recoveryMode;

this.imageCache = Object.create(null);
Expand Down Expand Up @@ -748,7 +748,7 @@ function toHexDigit(ch) {
}

class Lexer {
constructor(stream, knownCommands) {
constructor(stream, knownCommands = null) {
this.stream = stream;
this.nextChar();

Expand Down Expand Up @@ -1202,7 +1202,10 @@ class Linearization {
throw new Error('Hint array in the linearization dictionary is invalid.');
}

const parser = new Parser(new Lexer(stream), false, null);
const parser = new Parser({
lexer: new Lexer(stream),
xref: null,
});
const obj1 = parser.getObj();
const obj2 = parser.getObj();
const obj3 = parser.getObj();
Expand Down
13 changes: 9 additions & 4 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,10 @@ describe('annotation', function() {
'/URI (http://www.example.com/\\303\\274\\303\\266\\303\\244)\n' +
'>>\n'
);
const lexer = new Lexer(actionStream);
const parser = new Parser(lexer);
const parser = new Parser({
lexer: new Lexer(actionStream),
xref: null,
});
const actionDict = parser.getObj();

const annotationDict = new Dict();
Expand Down Expand Up @@ -1412,8 +1414,11 @@ describe('annotation', function() {
'Test attachment' +
'endstream\n'
);
const lexer = new Lexer(fileStream);
const parser = new Parser(lexer, /* allowStreams = */ true);
const parser = new Parser({
lexer: new Lexer(fileStream),
xref: null,
allowStreams: true,
});

const fileStreamRef = Ref.get(18, 0);
const fileStreamDict = parser.getObj();
Expand Down
18 changes: 12 additions & 6 deletions test/unit/parser_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ describe('parser', function() {
const string = 'q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 ' +
'/F /A85 ID abc123~> EI Q';
const input = new StringStream(string);
const lexer = new Lexer(input);
const parser = new Parser(lexer, /* allowStreams = */ true,
/* xref = */ null);
const parser = new Parser({
lexer: new Lexer(input),
xref: null,
allowStreams: true,
});

parser.inlineStreamSkipEI(input);
expect(input.pos).toEqual(string.indexOf('Q'));
expect(input.peekByte()).toEqual(0x51); // 'Q'
Expand All @@ -39,9 +42,12 @@ describe('parser', function() {
const string = 'q 1 0 0 1 0 0 cm BI /W 10 /H 10 /BPC 1 ' +
'/F /A85 ID abc123~> Q';
const input = new StringStream(string);
const lexer = new Lexer(input);
const parser = new Parser(lexer, /* allowStreams = */ true,
/* xref = */ null);
const parser = new Parser({
lexer: new Lexer(input),
xref: null,
allowStreams: true,
});

parser.inlineStreamSkipEI(input);
expect(input.pos).toEqual(string.length);
expect(input.peekByte()).toEqual(-1);
Expand Down