Convert the Lexer
class in src/core/parser.js
to ES6 syntax
This commit is contained in:
parent
7d0ecee771
commit
8d4d7dbf58
@ -714,8 +714,40 @@ class Parser {
|
||||
}
|
||||
}
|
||||
|
||||
var Lexer = (function LexerClosure() {
|
||||
function Lexer(stream, knownCommands) {
|
||||
// A '1' in this array means the character is white space. A '1' or
|
||||
// '2' means the character ends a name or command.
|
||||
const specialChars = [
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
|
||||
1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, // 2x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, // 3x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 5x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 7x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ax
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // bx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // cx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // dx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ex
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
|
||||
];
|
||||
|
||||
function toHexDigit(ch) {
|
||||
if (ch >= 0x30 && ch <= 0x39) { // '0'-'9'
|
||||
return ch & 0x0F;
|
||||
}
|
||||
if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
|
||||
// 'A'-'F', 'a'-'f'
|
||||
return (ch & 0x0F) + 9;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
class Lexer {
|
||||
constructor(stream, knownCommands) {
|
||||
this.stream = stream;
|
||||
this.nextChar();
|
||||
|
||||
@ -738,50 +770,19 @@ var Lexer = (function LexerClosure() {
|
||||
this.beginInlineImagePos = -1;
|
||||
}
|
||||
|
||||
// A '1' in this array means the character is white space. A '1' or
|
||||
// '2' means the character ends a name or command.
|
||||
var specialChars = [
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
|
||||
1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, // 2x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, // 3x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 5x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 7x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ax
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // bx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // cx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // dx
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ex
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
|
||||
];
|
||||
|
||||
function toHexDigit(ch) {
|
||||
if (ch >= 0x30 && ch <= 0x39) { // '0'-'9'
|
||||
return ch & 0x0F;
|
||||
}
|
||||
if ((ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66)) {
|
||||
// 'A'-'F', 'a'-'f'
|
||||
return (ch & 0x0F) + 9;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Lexer.prototype = {
|
||||
nextChar: function Lexer_nextChar() {
|
||||
nextChar() {
|
||||
return (this.currentChar = this.stream.getByte());
|
||||
},
|
||||
peekChar: function Lexer_peekChar() {
|
||||
}
|
||||
|
||||
peekChar() {
|
||||
return this.stream.peekByte();
|
||||
},
|
||||
getNumber: function Lexer_getNumber() {
|
||||
var ch = this.currentChar;
|
||||
var eNotation = false;
|
||||
var divideBy = 0; // different from 0 if it's a floating point value
|
||||
var sign = 0;
|
||||
}
|
||||
|
||||
getNumber() {
|
||||
let ch = this.currentChar;
|
||||
let eNotation = false;
|
||||
let divideBy = 0; // Different from 0 if it's a floating point value.
|
||||
let sign = 0;
|
||||
|
||||
if (ch === 0x2D) { // '-'
|
||||
sign = -1;
|
||||
@ -817,17 +818,17 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
|
||||
sign = sign || 1;
|
||||
var baseValue = ch - 0x30; // '0'
|
||||
var powerValue = 0;
|
||||
var powerValueSign = 1;
|
||||
let baseValue = ch - 0x30; // '0'
|
||||
let powerValue = 0;
|
||||
let powerValueSign = 1;
|
||||
|
||||
while ((ch = this.nextChar()) >= 0) {
|
||||
if (0x30 <= ch && ch <= 0x39) { // '0' - '9'
|
||||
var currentDigit = ch - 0x30; // '0'
|
||||
if (eNotation) { // We are after an 'e' or 'E'
|
||||
const currentDigit = ch - 0x30; // '0'
|
||||
if (eNotation) { // We are after an 'e' or 'E'.
|
||||
powerValue = powerValue * 10 + currentDigit;
|
||||
} else {
|
||||
if (divideBy !== 0) { // We are after a point
|
||||
if (divideBy !== 0) { // We are after a point.
|
||||
divideBy *= 10;
|
||||
}
|
||||
baseValue = baseValue * 10 + currentDigit;
|
||||
@ -836,27 +837,27 @@ var Lexer = (function LexerClosure() {
|
||||
if (divideBy === 0) {
|
||||
divideBy = 1;
|
||||
} else {
|
||||
// A number can have only one '.'
|
||||
// A number can have only one dot.
|
||||
break;
|
||||
}
|
||||
} else if (ch === 0x2D) { // '-'
|
||||
// ignore minus signs in the middle of numbers to match
|
||||
// Adobe's behavior
|
||||
warn('Badly formatted number');
|
||||
// Ignore minus signs in the middle of numbers to match
|
||||
// Adobe's behavior.
|
||||
warn('Badly formatted number: minus sign in the middle');
|
||||
} else if (ch === 0x45 || ch === 0x65) { // 'E', 'e'
|
||||
// 'E' can be either a scientific notation or the beginning of a new
|
||||
// operator
|
||||
// operator.
|
||||
ch = this.peekChar();
|
||||
if (ch === 0x2B || ch === 0x2D) { // '+', '-'
|
||||
powerValueSign = (ch === 0x2D) ? -1 : 1;
|
||||
this.nextChar(); // Consume the sign character
|
||||
this.nextChar(); // Consume the sign character.
|
||||
} else if (ch < 0x30 || ch > 0x39) { // '0' - '9'
|
||||
// The 'E' must be the beginning of a new operator
|
||||
// The 'E' must be the beginning of a new operator.
|
||||
break;
|
||||
}
|
||||
eNotation = true;
|
||||
} else {
|
||||
// the last character doesn't belong to us
|
||||
// The last character doesn't belong to us.
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -868,16 +869,17 @@ var Lexer = (function LexerClosure() {
|
||||
baseValue *= Math.pow(10, powerValueSign * powerValue);
|
||||
}
|
||||
return sign * baseValue;
|
||||
},
|
||||
getString: function Lexer_getString() {
|
||||
var numParen = 1;
|
||||
var done = false;
|
||||
var strBuf = this.strBuf;
|
||||
}
|
||||
|
||||
getString() {
|
||||
let numParen = 1;
|
||||
let done = false;
|
||||
const strBuf = this.strBuf;
|
||||
strBuf.length = 0;
|
||||
|
||||
var ch = this.nextChar();
|
||||
let ch = this.nextChar();
|
||||
while (true) {
|
||||
var charBuffered = false;
|
||||
let charBuffered = false;
|
||||
switch (ch | 0) {
|
||||
case -1:
|
||||
warn('Unterminated string');
|
||||
@ -924,7 +926,7 @@ var Lexer = (function LexerClosure() {
|
||||
break;
|
||||
case 0x30: case 0x31: case 0x32: case 0x33: // '0'-'3'
|
||||
case 0x34: case 0x35: case 0x36: case 0x37: // '4'-'7'
|
||||
var x = ch & 0x0F;
|
||||
let x = ch & 0x0F;
|
||||
ch = this.nextChar();
|
||||
charBuffered = true;
|
||||
if (ch >= 0x30 && ch <= 0x37) { // '0'-'7'
|
||||
@ -961,11 +963,13 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
}
|
||||
return strBuf.join('');
|
||||
},
|
||||
getName: function Lexer_getName() {
|
||||
var ch, previousCh;
|
||||
var strBuf = this.strBuf;
|
||||
}
|
||||
|
||||
getName() {
|
||||
let ch, previousCh;
|
||||
const strBuf = this.strBuf;
|
||||
strBuf.length = 0;
|
||||
|
||||
while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
|
||||
if (ch === 0x23) { // '#'
|
||||
ch = this.nextChar();
|
||||
@ -975,14 +979,14 @@ var Lexer = (function LexerClosure() {
|
||||
strBuf.push('#');
|
||||
break;
|
||||
}
|
||||
var x = toHexDigit(ch);
|
||||
const x = toHexDigit(ch);
|
||||
if (x !== -1) {
|
||||
previousCh = ch;
|
||||
ch = this.nextChar();
|
||||
var x2 = toHexDigit(ch);
|
||||
const x2 = toHexDigit(ch);
|
||||
if (x2 === -1) {
|
||||
warn('Lexer_getName: Illegal digit (' +
|
||||
String.fromCharCode(ch) + ') in hexadecimal number.');
|
||||
warn(`Lexer_getName: Illegal digit (${String.fromCharCode(ch)}) ` +
|
||||
'in hexadecimal number.');
|
||||
strBuf.push('#', String.fromCharCode(previousCh));
|
||||
if (specialChars[ch]) {
|
||||
break;
|
||||
@ -999,17 +1003,18 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
}
|
||||
if (strBuf.length > 127) {
|
||||
warn('name token is longer than allowed by the spec: ' + strBuf.length);
|
||||
warn(`Name token is longer than allowed by the spec: ${strBuf.length}`);
|
||||
}
|
||||
return Name.get(strBuf.join(''));
|
||||
},
|
||||
getHexString: function Lexer_getHexString() {
|
||||
var strBuf = this.strBuf;
|
||||
}
|
||||
|
||||
getHexString() {
|
||||
const strBuf = this.strBuf;
|
||||
strBuf.length = 0;
|
||||
var ch = this.currentChar;
|
||||
var isFirstHex = true;
|
||||
var firstDigit;
|
||||
var secondDigit;
|
||||
let ch = this.currentChar;
|
||||
let isFirstHex = true;
|
||||
let firstDigit, secondDigit;
|
||||
|
||||
while (true) {
|
||||
if (ch < 0) {
|
||||
warn('Unterminated hex string');
|
||||
@ -1024,14 +1029,14 @@ var Lexer = (function LexerClosure() {
|
||||
if (isFirstHex) {
|
||||
firstDigit = toHexDigit(ch);
|
||||
if (firstDigit === -1) {
|
||||
warn('Ignoring invalid character "' + ch + '" in hex string');
|
||||
warn(`Ignoring invalid character "${ch}" in hex string`);
|
||||
ch = this.nextChar();
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
secondDigit = toHexDigit(ch);
|
||||
if (secondDigit === -1) {
|
||||
warn('Ignoring invalid character "' + ch + '" in hex string');
|
||||
warn(`Ignoring invalid character "${ch}" in hex string`);
|
||||
ch = this.nextChar();
|
||||
continue;
|
||||
}
|
||||
@ -1042,11 +1047,12 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
}
|
||||
return strBuf.join('');
|
||||
},
|
||||
getObj: function Lexer_getObj() {
|
||||
// skip whitespace and comments
|
||||
var comment = false;
|
||||
var ch = this.currentChar;
|
||||
}
|
||||
|
||||
getObj() {
|
||||
// Skip whitespace and comments.
|
||||
let comment = false;
|
||||
let ch = this.currentChar;
|
||||
while (true) {
|
||||
if (ch < 0) {
|
||||
return EOF;
|
||||
@ -1063,7 +1069,7 @@ var Lexer = (function LexerClosure() {
|
||||
ch = this.nextChar();
|
||||
}
|
||||
|
||||
// start reading token
|
||||
// Start reading a token.
|
||||
switch (ch | 0) {
|
||||
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: // '0'-'4'
|
||||
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: // '5'-'9'
|
||||
@ -1112,14 +1118,14 @@ var Lexer = (function LexerClosure() {
|
||||
throw new FormatError(`Illegal character: ${ch}`);
|
||||
}
|
||||
|
||||
// command
|
||||
var str = String.fromCharCode(ch);
|
||||
var knownCommands = this.knownCommands;
|
||||
var knownCommandFound = knownCommands && knownCommands[str] !== undefined;
|
||||
// Start reading a command.
|
||||
let str = String.fromCharCode(ch);
|
||||
const knownCommands = this.knownCommands;
|
||||
let knownCommandFound = knownCommands && knownCommands[str] !== undefined;
|
||||
while ((ch = this.nextChar()) >= 0 && !specialChars[ch]) {
|
||||
// stop if known command is found and next character does not make
|
||||
// the str a command
|
||||
var possibleCommand = str + String.fromCharCode(ch);
|
||||
// Stop if a known command is found and next character does not make
|
||||
// the string a command.
|
||||
const possibleCommand = str + String.fromCharCode(ch);
|
||||
if (knownCommandFound && knownCommands[possibleCommand] === undefined) {
|
||||
break;
|
||||
}
|
||||
@ -1146,9 +1152,10 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
|
||||
return Cmd.get(str);
|
||||
},
|
||||
skipToNextLine: function Lexer_skipToNextLine() {
|
||||
var ch = this.currentChar;
|
||||
}
|
||||
|
||||
skipToNextLine() {
|
||||
let ch = this.currentChar;
|
||||
while (ch >= 0) {
|
||||
if (ch === 0x0D) { // CR
|
||||
ch = this.nextChar();
|
||||
@ -1162,11 +1169,8 @@ var Lexer = (function LexerClosure() {
|
||||
}
|
||||
ch = this.nextChar();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
return Lexer;
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
var Linearization = {
|
||||
create: function LinearizationCreate(stream) {
|
||||
|
Loading…
Reference in New Issue
Block a user