Merge pull request #10219 from Snuffleupagus/ps-parser-class

Convert `src/core/ps_parser.js` to use ES6 classes
This commit is contained in:
Tim van der Meij 2018-11-07 23:15:01 +01:00 committed by GitHub
commit 744a693554
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,43 +13,47 @@
* limitations under the License. * limitations under the License.
*/ */
import { FormatError, isSpace } from '../shared/util'; import { FormatError, isSpace, shadow } from '../shared/util';
import { EOF } from './primitives'; import { EOF } from './primitives';
var PostScriptParser = (function PostScriptParserClosure() { class PostScriptParser {
function PostScriptParser(lexer) { constructor(lexer) {
this.lexer = lexer; this.lexer = lexer;
this.operators = []; this.operators = [];
this.token = null; this.token = null;
this.prev = null; this.prev = null;
} }
PostScriptParser.prototype = {
nextToken: function PostScriptParser_nextToken() { nextToken() {
this.prev = this.token; this.prev = this.token;
this.token = this.lexer.getToken(); this.token = this.lexer.getToken();
}, }
accept: function PostScriptParser_accept(type) {
accept(type) {
if (this.token.type === type) { if (this.token.type === type) {
this.nextToken(); this.nextToken();
return true; return true;
} }
return false; return false;
}, }
expect: function PostScriptParser_expect(type) {
expect(type) {
if (this.accept(type)) { if (this.accept(type)) {
return true; return true;
} }
throw new FormatError( throw new FormatError(
`Unexpected symbol: found ${this.token.type} expected ${type}.`); `Unexpected symbol: found ${this.token.type} expected ${type}.`);
}, }
parse: function PostScriptParser_parse() {
parse() {
this.nextToken(); this.nextToken();
this.expect(PostScriptTokenTypes.LBRACE); this.expect(PostScriptTokenTypes.LBRACE);
this.parseBlock(); this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE); this.expect(PostScriptTokenTypes.RBRACE);
return this.operators; return this.operators;
}, }
parseBlock: function PostScriptParser_parseBlock() {
parseBlock() {
while (true) { while (true) {
if (this.accept(PostScriptTokenTypes.NUMBER)) { if (this.accept(PostScriptTokenTypes.NUMBER)) {
this.operators.push(this.prev.value); this.operators.push(this.prev.value);
@ -61,28 +65,28 @@ var PostScriptParser = (function PostScriptParserClosure() {
return; return;
} }
} }
}, }
parseCondition: function PostScriptParser_parseCondition() {
parseCondition() {
// Add two place holders that will be updated later // Add two place holders that will be updated later
var conditionLocation = this.operators.length; const conditionLocation = this.operators.length;
this.operators.push(null, null); this.operators.push(null, null);
this.parseBlock(); this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE); this.expect(PostScriptTokenTypes.RBRACE);
if (this.accept(PostScriptTokenTypes.IF)) { if (this.accept(PostScriptTokenTypes.IF)) {
// The true block is right after the 'if' so it just falls through on // The true block is right after the 'if' so it just falls through on true
// true else it jumps and skips the true block. // else it jumps and skips the true block.
this.operators[conditionLocation] = this.operators.length; this.operators[conditionLocation] = this.operators.length;
this.operators[conditionLocation + 1] = 'jz'; this.operators[conditionLocation + 1] = 'jz';
} else if (this.accept(PostScriptTokenTypes.LBRACE)) { } else if (this.accept(PostScriptTokenTypes.LBRACE)) {
var jumpLocation = this.operators.length; const jumpLocation = this.operators.length;
this.operators.push(null, null); this.operators.push(null, null);
var endOfTrue = this.operators.length; const endOfTrue = this.operators.length;
this.parseBlock(); this.parseBlock();
this.expect(PostScriptTokenTypes.RBRACE); this.expect(PostScriptTokenTypes.RBRACE);
this.expect(PostScriptTokenTypes.IFELSE); this.expect(PostScriptTokenTypes.IFELSE);
// The jump is added at the end of the true block to skip the false // The jump is added at the end of the true block to skip the false block.
// block.
this.operators[jumpLocation] = this.operators.length; this.operators[jumpLocation] = this.operators.length;
this.operators[jumpLocation + 1] = 'j'; this.operators[jumpLocation + 1] = 'j';
@ -91,12 +95,10 @@ var PostScriptParser = (function PostScriptParserClosure() {
} else { } else {
throw new FormatError('PS Function: error parsing conditional.'); throw new FormatError('PS Function: error parsing conditional.');
} }
}, }
}; }
return PostScriptParser;
})();
var PostScriptTokenTypes = { const PostScriptTokenTypes = {
LBRACE: 0, LBRACE: 0,
RBRACE: 1, RBRACE: 1,
NUMBER: 2, NUMBER: 2,
@ -105,46 +107,62 @@ var PostScriptTokenTypes = {
IFELSE: 5, IFELSE: 5,
}; };
var PostScriptToken = (function PostScriptTokenClosure() { const PostScriptToken = (function PostScriptTokenClosure() {
function PostScriptToken(type, value) { const opCache = Object.create(null);
class PostScriptToken {
constructor(type, value) {
this.type = type; this.type = type;
this.value = value; this.value = value;
} }
var opCache = Object.create(null); static getOperator(op) {
const opValue = opCache[op];
PostScriptToken.getOperator = function PostScriptToken_getOperator(op) {
var opValue = opCache[op];
if (opValue) { if (opValue) {
return opValue; return opValue;
} }
return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op); return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR,
}; op);
}
PostScriptToken.LBRACE = new PostScriptToken(PostScriptTokenTypes.LBRACE, static get LBRACE() {
'{'); return shadow(this, 'LBRACE',
PostScriptToken.RBRACE = new PostScriptToken(PostScriptTokenTypes.RBRACE, new PostScriptToken(PostScriptTokenTypes.LBRACE, '{'));
'}'); }
PostScriptToken.IF = new PostScriptToken(PostScriptTokenTypes.IF, 'IF');
PostScriptToken.IFELSE = new PostScriptToken(PostScriptTokenTypes.IFELSE, static get RBRACE() {
'IFELSE'); return shadow(this, 'RBRACE',
new PostScriptToken(PostScriptTokenTypes.RBRACE, '}'));
}
static get IF() {
return shadow(this, 'IF',
new PostScriptToken(PostScriptTokenTypes.IF, 'IF'));
}
static get IFELSE() {
return shadow(this, 'IFELSE',
new PostScriptToken(PostScriptTokenTypes.IFELSE, 'IFELSE'));
}
}
return PostScriptToken; return PostScriptToken;
})(); })();
var PostScriptLexer = (function PostScriptLexerClosure() { class PostScriptLexer {
function PostScriptLexer(stream) { constructor(stream) {
this.stream = stream; this.stream = stream;
this.nextChar(); this.nextChar();
this.strBuf = []; this.strBuf = [];
} }
PostScriptLexer.prototype = {
nextChar: function PostScriptLexer_nextChar() { nextChar() {
return (this.currentChar = this.stream.getByte()); return (this.currentChar = this.stream.getByte());
}, }
getToken: function PostScriptLexer_getToken() {
var comment = false; getToken() {
var ch = this.currentChar; let comment = false;
let ch = this.currentChar;
// skip comments // skip comments
while (true) { while (true) {
@ -177,7 +195,7 @@ var PostScriptLexer = (function PostScriptLexerClosure() {
return PostScriptToken.RBRACE; return PostScriptToken.RBRACE;
} }
// operator // operator
var strBuf = this.strBuf; const strBuf = this.strBuf;
strBuf.length = 0; strBuf.length = 0;
strBuf[0] = String.fromCharCode(ch); strBuf[0] = String.fromCharCode(ch);
@ -185,7 +203,7 @@ var PostScriptLexer = (function PostScriptLexerClosure() {
((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) { ((ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A))) {
strBuf.push(String.fromCharCode(ch)); strBuf.push(String.fromCharCode(ch));
} }
var str = strBuf.join(''); const str = strBuf.join('');
switch (str.toLowerCase()) { switch (str.toLowerCase()) {
case 'if': case 'if':
return PostScriptToken.IF; return PostScriptToken.IF;
@ -194,10 +212,11 @@ var PostScriptLexer = (function PostScriptLexerClosure() {
default: default:
return PostScriptToken.getOperator(str); return PostScriptToken.getOperator(str);
} }
}, }
getNumber: function PostScriptLexer_getNumber() {
var ch = this.currentChar; getNumber() {
var strBuf = this.strBuf; let ch = this.currentChar;
const strBuf = this.strBuf;
strBuf.length = 0; strBuf.length = 0;
strBuf[0] = String.fromCharCode(ch); strBuf[0] = String.fromCharCode(ch);
@ -209,15 +228,13 @@ var PostScriptLexer = (function PostScriptLexerClosure() {
break; break;
} }
} }
var value = parseFloat(strBuf.join('')); const value = parseFloat(strBuf.join(''));
if (isNaN(value)) { if (isNaN(value)) {
throw new FormatError(`Invalid floating point number: ${value}`); throw new FormatError(`Invalid floating point number: ${value}`);
} }
return value; return value;
}, }
}; }
return PostScriptLexer;
})();
export { export {
PostScriptLexer, PostScriptLexer,