ps_parser.js 6.7 KB

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