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