ps_parser.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. const PostScriptToken = function PostScriptTokenClosure() {
  108. const opCache = Object.create(null);
  109. class PostScriptToken {
  110. constructor(type, value) {
  111. this.type = type;
  112. this.value = value;
  113. }
  114. static getOperator(op) {
  115. const opValue = opCache[op];
  116. if (opValue) {
  117. return opValue;
  118. }
  119. return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op);
  120. }
  121. static get LBRACE() {
  122. return (0, _util.shadow)(this, "LBRACE", new PostScriptToken(PostScriptTokenTypes.LBRACE, "{"));
  123. }
  124. static get RBRACE() {
  125. return (0, _util.shadow)(this, "RBRACE", new PostScriptToken(PostScriptTokenTypes.RBRACE, "}"));
  126. }
  127. static get IF() {
  128. return (0, _util.shadow)(this, "IF", new PostScriptToken(PostScriptTokenTypes.IF, "IF"));
  129. }
  130. static get IFELSE() {
  131. return (0, _util.shadow)(this, "IFELSE", new PostScriptToken(PostScriptTokenTypes.IFELSE, "IFELSE"));
  132. }
  133. }
  134. return PostScriptToken;
  135. }();
  136. class PostScriptLexer {
  137. constructor(stream) {
  138. this.stream = stream;
  139. this.nextChar();
  140. this.strBuf = [];
  141. }
  142. nextChar() {
  143. return this.currentChar = this.stream.getByte();
  144. }
  145. getToken() {
  146. let comment = false;
  147. let ch = this.currentChar;
  148. while (true) {
  149. if (ch < 0) {
  150. return _primitives.EOF;
  151. }
  152. if (comment) {
  153. if (ch === 0x0a || ch === 0x0d) {
  154. comment = false;
  155. }
  156. } else if (ch === 0x25) {
  157. comment = true;
  158. } else if (!(0, _core_utils.isWhiteSpace)(ch)) {
  159. break;
  160. }
  161. ch = this.nextChar();
  162. }
  163. switch (ch | 0) {
  164. case 0x30:
  165. case 0x31:
  166. case 0x32:
  167. case 0x33:
  168. case 0x34:
  169. case 0x35:
  170. case 0x36:
  171. case 0x37:
  172. case 0x38:
  173. case 0x39:
  174. case 0x2b:
  175. case 0x2d:
  176. case 0x2e:
  177. return new PostScriptToken(PostScriptTokenTypes.NUMBER, this.getNumber());
  178. case 0x7b:
  179. this.nextChar();
  180. return PostScriptToken.LBRACE;
  181. case 0x7d:
  182. this.nextChar();
  183. return PostScriptToken.RBRACE;
  184. }
  185. const strBuf = this.strBuf;
  186. strBuf.length = 0;
  187. strBuf[0] = String.fromCharCode(ch);
  188. while ((ch = this.nextChar()) >= 0 && (ch >= 0x41 && ch <= 0x5a || ch >= 0x61 && ch <= 0x7a)) {
  189. strBuf.push(String.fromCharCode(ch));
  190. }
  191. const str = strBuf.join("");
  192. switch (str.toLowerCase()) {
  193. case "if":
  194. return PostScriptToken.IF;
  195. case "ifelse":
  196. return PostScriptToken.IFELSE;
  197. default:
  198. return PostScriptToken.getOperator(str);
  199. }
  200. }
  201. getNumber() {
  202. let ch = this.currentChar;
  203. const strBuf = this.strBuf;
  204. strBuf.length = 0;
  205. strBuf[0] = String.fromCharCode(ch);
  206. while ((ch = this.nextChar()) >= 0) {
  207. if (ch >= 0x30 && ch <= 0x39 || ch === 0x2d || ch === 0x2e) {
  208. strBuf.push(String.fromCharCode(ch));
  209. } else {
  210. break;
  211. }
  212. }
  213. const value = parseFloat(strBuf.join(""));
  214. if (isNaN(value)) {
  215. throw new _util.FormatError(`Invalid floating point number: ${value}`);
  216. }
  217. return value;
  218. }
  219. }
  220. exports.PostScriptLexer = PostScriptLexer;