ps_parser.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * JavaScript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PostScriptParser = exports.PostScriptLexer = void 0;
  27. var _util = require("../shared/util.js");
  28. var _primitives = require("./primitives.js");
  29. var _core_utils = require("./core_utils.js");
  30. class PostScriptParser {
  31. constructor(lexer) {
  32. this.lexer = lexer;
  33. this.operators = [];
  34. this.token = null;
  35. this.prev = null;
  36. }
  37. nextToken() {
  38. this.prev = this.token;
  39. this.token = this.lexer.getToken();
  40. }
  41. accept(type) {
  42. if (this.token.type === type) {
  43. this.nextToken();
  44. return true;
  45. }
  46. return false;
  47. }
  48. expect(type) {
  49. if (this.accept(type)) {
  50. return true;
  51. }
  52. throw new _util.FormatError(`Unexpected symbol: found ${this.token.type} expected ${type}.`);
  53. }
  54. parse() {
  55. this.nextToken();
  56. this.expect(PostScriptTokenTypes.LBRACE);
  57. this.parseBlock();
  58. this.expect(PostScriptTokenTypes.RBRACE);
  59. return this.operators;
  60. }
  61. parseBlock() {
  62. while (true) {
  63. if (this.accept(PostScriptTokenTypes.NUMBER)) {
  64. this.operators.push(this.prev.value);
  65. } else if (this.accept(PostScriptTokenTypes.OPERATOR)) {
  66. this.operators.push(this.prev.value);
  67. } else if (this.accept(PostScriptTokenTypes.LBRACE)) {
  68. this.parseCondition();
  69. } else {
  70. return;
  71. }
  72. }
  73. }
  74. parseCondition() {
  75. const conditionLocation = this.operators.length;
  76. this.operators.push(null, null);
  77. this.parseBlock();
  78. this.expect(PostScriptTokenTypes.RBRACE);
  79. if (this.accept(PostScriptTokenTypes.IF)) {
  80. this.operators[conditionLocation] = this.operators.length;
  81. this.operators[conditionLocation + 1] = "jz";
  82. } else if (this.accept(PostScriptTokenTypes.LBRACE)) {
  83. const jumpLocation = this.operators.length;
  84. this.operators.push(null, null);
  85. const endOfTrue = this.operators.length;
  86. this.parseBlock();
  87. this.expect(PostScriptTokenTypes.RBRACE);
  88. this.expect(PostScriptTokenTypes.IFELSE);
  89. this.operators[jumpLocation] = this.operators.length;
  90. this.operators[jumpLocation + 1] = "j";
  91. this.operators[conditionLocation] = endOfTrue;
  92. this.operators[conditionLocation + 1] = "jz";
  93. } else {
  94. throw new _util.FormatError("PS Function: error parsing conditional.");
  95. }
  96. }
  97. }
  98. exports.PostScriptParser = PostScriptParser;
  99. const PostScriptTokenTypes = {
  100. LBRACE: 0,
  101. RBRACE: 1,
  102. NUMBER: 2,
  103. OPERATOR: 3,
  104. IF: 4,
  105. IFELSE: 5
  106. };
  107. class PostScriptToken {
  108. static get opCache() {
  109. return (0, _util.shadow)(this, "opCache", Object.create(null));
  110. }
  111. constructor(type, value) {
  112. this.type = type;
  113. this.value = value;
  114. }
  115. static getOperator(op) {
  116. const opValue = PostScriptToken.opCache[op];
  117. if (opValue) {
  118. return opValue;
  119. }
  120. return PostScriptToken.opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op);
  121. }
  122. static get LBRACE() {
  123. return (0, _util.shadow)(this, "LBRACE", new PostScriptToken(PostScriptTokenTypes.LBRACE, "{"));
  124. }
  125. static get RBRACE() {
  126. return (0, _util.shadow)(this, "RBRACE", new PostScriptToken(PostScriptTokenTypes.RBRACE, "}"));
  127. }
  128. static get IF() {
  129. return (0, _util.shadow)(this, "IF", new PostScriptToken(PostScriptTokenTypes.IF, "IF"));
  130. }
  131. static get IFELSE() {
  132. return (0, _util.shadow)(this, "IFELSE", new PostScriptToken(PostScriptTokenTypes.IFELSE, "IFELSE"));
  133. }
  134. }
  135. class PostScriptLexer {
  136. constructor(stream) {
  137. this.stream = stream;
  138. this.nextChar();
  139. this.strBuf = [];
  140. }
  141. nextChar() {
  142. return this.currentChar = this.stream.getByte();
  143. }
  144. getToken() {
  145. let comment = false;
  146. let ch = this.currentChar;
  147. while (true) {
  148. if (ch < 0) {
  149. return _primitives.EOF;
  150. }
  151. if (comment) {
  152. if (ch === 0x0a || ch === 0x0d) {
  153. comment = false;
  154. }
  155. } else if (ch === 0x25) {
  156. comment = true;
  157. } else if (!(0, _core_utils.isWhiteSpace)(ch)) {
  158. break;
  159. }
  160. ch = this.nextChar();
  161. }
  162. switch (ch | 0) {
  163. case 0x30:
  164. case 0x31:
  165. case 0x32:
  166. case 0x33:
  167. case 0x34:
  168. case 0x35:
  169. case 0x36:
  170. case 0x37:
  171. case 0x38:
  172. case 0x39:
  173. case 0x2b:
  174. case 0x2d:
  175. case 0x2e:
  176. return new PostScriptToken(PostScriptTokenTypes.NUMBER, this.getNumber());
  177. case 0x7b:
  178. this.nextChar();
  179. return PostScriptToken.LBRACE;
  180. case 0x7d:
  181. this.nextChar();
  182. return PostScriptToken.RBRACE;
  183. }
  184. const strBuf = this.strBuf;
  185. strBuf.length = 0;
  186. strBuf[0] = String.fromCharCode(ch);
  187. while ((ch = this.nextChar()) >= 0 && (ch >= 0x41 && ch <= 0x5a || ch >= 0x61 && ch <= 0x7a)) {
  188. strBuf.push(String.fromCharCode(ch));
  189. }
  190. const str = strBuf.join("");
  191. switch (str.toLowerCase()) {
  192. case "if":
  193. return PostScriptToken.IF;
  194. case "ifelse":
  195. return PostScriptToken.IFELSE;
  196. default:
  197. return PostScriptToken.getOperator(str);
  198. }
  199. }
  200. getNumber() {
  201. let ch = this.currentChar;
  202. const strBuf = this.strBuf;
  203. strBuf.length = 0;
  204. strBuf[0] = String.fromCharCode(ch);
  205. while ((ch = this.nextChar()) >= 0) {
  206. if (ch >= 0x30 && ch <= 0x39 || ch === 0x2d || ch === 0x2e) {
  207. strBuf.push(String.fromCharCode(ch));
  208. } else {
  209. break;
  210. }
  211. }
  212. const value = parseFloat(strBuf.join(""));
  213. if (isNaN(value)) {
  214. throw new _util.FormatError(`Invalid floating point number: ${value}`);
  215. }
  216. return value;
  217. }
  218. }
  219. exports.PostScriptLexer = PostScriptLexer;