2
0

ps_parser.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  27. var _util = require('../shared/util');
  28. var _primitives = require('./primitives');
  29. var PostScriptParser = function PostScriptParserClosure() {
  30. function PostScriptParser(lexer) {
  31. this.lexer = lexer;
  32. this.operators = [];
  33. this.token = null;
  34. this.prev = null;
  35. }
  36. PostScriptParser.prototype = {
  37. nextToken: function PostScriptParser_nextToken() {
  38. this.prev = this.token;
  39. this.token = this.lexer.getToken();
  40. },
  41. accept: function PostScriptParser_accept(type) {
  42. if (this.token.type === type) {
  43. this.nextToken();
  44. return true;
  45. }
  46. return false;
  47. },
  48. expect: function PostScriptParser_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: function PostScriptParser_parse() {
  55. this.nextToken();
  56. this.expect(PostScriptTokenTypes.LBRACE);
  57. this.parseBlock();
  58. this.expect(PostScriptTokenTypes.RBRACE);
  59. return this.operators;
  60. },
  61. parseBlock: function PostScriptParser_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: function PostScriptParser_parseCondition() {
  75. var 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. var jumpLocation = this.operators.length;
  84. this.operators.push(null, null);
  85. var 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. return PostScriptParser;
  99. }();
  100. var PostScriptTokenTypes = {
  101. LBRACE: 0,
  102. RBRACE: 1,
  103. NUMBER: 2,
  104. OPERATOR: 3,
  105. IF: 4,
  106. IFELSE: 5
  107. };
  108. var PostScriptToken = function PostScriptTokenClosure() {
  109. function PostScriptToken(type, value) {
  110. this.type = type;
  111. this.value = value;
  112. }
  113. var opCache = Object.create(null);
  114. PostScriptToken.getOperator = function PostScriptToken_getOperator(op) {
  115. var opValue = opCache[op];
  116. if (opValue) {
  117. return opValue;
  118. }
  119. return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op);
  120. };
  121. PostScriptToken.LBRACE = new PostScriptToken(PostScriptTokenTypes.LBRACE, '{');
  122. PostScriptToken.RBRACE = new PostScriptToken(PostScriptTokenTypes.RBRACE, '}');
  123. PostScriptToken.IF = new PostScriptToken(PostScriptTokenTypes.IF, 'IF');
  124. PostScriptToken.IFELSE = new PostScriptToken(PostScriptTokenTypes.IFELSE, 'IFELSE');
  125. return PostScriptToken;
  126. }();
  127. var PostScriptLexer = function PostScriptLexerClosure() {
  128. function PostScriptLexer(stream) {
  129. this.stream = stream;
  130. this.nextChar();
  131. this.strBuf = [];
  132. }
  133. PostScriptLexer.prototype = {
  134. nextChar: function PostScriptLexer_nextChar() {
  135. return this.currentChar = this.stream.getByte();
  136. },
  137. getToken: function PostScriptLexer_getToken() {
  138. var comment = false;
  139. var ch = this.currentChar;
  140. while (true) {
  141. if (ch < 0) {
  142. return _primitives.EOF;
  143. }
  144. if (comment) {
  145. if (ch === 0x0A || ch === 0x0D) {
  146. comment = false;
  147. }
  148. } else if (ch === 0x25) {
  149. comment = true;
  150. } else if (!(0, _util.isSpace)(ch)) {
  151. break;
  152. }
  153. ch = this.nextChar();
  154. }
  155. switch (ch | 0) {
  156. case 0x30:
  157. case 0x31:
  158. case 0x32:
  159. case 0x33:
  160. case 0x34:
  161. case 0x35:
  162. case 0x36:
  163. case 0x37:
  164. case 0x38:
  165. case 0x39:
  166. case 0x2B:
  167. case 0x2D:
  168. case 0x2E:
  169. return new PostScriptToken(PostScriptTokenTypes.NUMBER, this.getNumber());
  170. case 0x7B:
  171. this.nextChar();
  172. return PostScriptToken.LBRACE;
  173. case 0x7D:
  174. this.nextChar();
  175. return PostScriptToken.RBRACE;
  176. }
  177. var strBuf = this.strBuf;
  178. strBuf.length = 0;
  179. strBuf[0] = String.fromCharCode(ch);
  180. while ((ch = this.nextChar()) >= 0 && (ch >= 0x41 && ch <= 0x5A || ch >= 0x61 && ch <= 0x7A)) {
  181. strBuf.push(String.fromCharCode(ch));
  182. }
  183. var str = strBuf.join('');
  184. switch (str.toLowerCase()) {
  185. case 'if':
  186. return PostScriptToken.IF;
  187. case 'ifelse':
  188. return PostScriptToken.IFELSE;
  189. default:
  190. return PostScriptToken.getOperator(str);
  191. }
  192. },
  193. getNumber: function PostScriptLexer_getNumber() {
  194. var ch = this.currentChar;
  195. var strBuf = this.strBuf;
  196. strBuf.length = 0;
  197. strBuf[0] = String.fromCharCode(ch);
  198. while ((ch = this.nextChar()) >= 0) {
  199. if (ch >= 0x30 && ch <= 0x39 || ch === 0x2D || ch === 0x2E) {
  200. strBuf.push(String.fromCharCode(ch));
  201. } else {
  202. break;
  203. }
  204. }
  205. var value = parseFloat(strBuf.join(''));
  206. if (isNaN(value)) {
  207. throw new _util.FormatError('Invalid floating point number: ' + value);
  208. }
  209. return value;
  210. }
  211. };
  212. return PostScriptLexer;
  213. }();
  214. exports.PostScriptLexer = PostScriptLexer;
  215. exports.PostScriptParser = PostScriptParser;