1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
22 //------------------------------------------------------------------------
24 // A '1' in this array means the character is white space. A '1' or
25 // '2' means the character ends a name or command.
26 static char specialChars[256] = {
27 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, // 0x
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1x
29 1, 0, 0, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2, // 2x
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, // 3x
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4x
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 5x
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6x
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, // 7x
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 8x
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 9x
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ax
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // bx
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // cx
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // dx
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // ex
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // fx
45 //------------------------------------------------------------------------
47 //------------------------------------------------------------------------
49 Lexer::Lexer(XRef *xref, Stream *str) {
52 curStr.initStream(str);
53 streams = new Array(xref);
54 streams->add(curStr.copy(&obj));
60 Lexer::Lexer(XRef *xref, Object *obj) {
63 if (obj->isStream()) {
64 streams = new Array(xref);
66 streams->add(obj->copy(&obj2));
68 streams = obj->getArray();
72 if (streams->getLength() > 0) {
73 streams->get(strPtr, &curStr);
79 if (!curStr.isNone()) {
88 int Lexer::getChar() {
92 while (!curStr.isNone() && (c = curStr.streamGetChar()) == EOF) {
96 if (strPtr < streams->getLength()) {
97 streams->get(strPtr, &curStr);
104 int Lexer::lookChar() {
105 if (curStr.isNone()) {
108 return curStr.streamLookChar();
111 Object *Lexer::getObj(Object *obj) {
114 GBool comment, neg, done;
121 // skip whitespace and comments
124 if ((c = getChar()) == EOF) {
125 return obj->initEOF();
128 if (c == '\r' || c == '\n')
130 } else if (c == '%') {
132 } else if (specialChars[c] != 1) {
137 // start reading token
141 case '0': case '1': case '2': case '3': case '4':
142 case '5': case '6': case '7': case '8': case '9':
148 } else if (c == '.') {
157 xi = xi * 10 + (c - '0');
158 } else if (c == '.') {
178 xf = xf + scale * (c - '0');
195 switch (c = getChar()) {
199 // This breaks some PDF files, e.g., ones from Photoshop.
203 error(getPos(), "Unterminated string");
213 if (--numParen == 0) {
221 switch (c = getChar()) {
242 case '0': case '1': case '2': case '3':
243 case '4': case '5': case '6': case '7':
246 if (c >= '0' && c <= '7') {
248 c2 = (c2 << 3) + (c - '0');
250 if (c >= '0' && c <= '7') {
252 c2 = (c2 << 3) + (c - '0');
265 error(getPos(), "Unterminated string");
280 if (n == tokBufSize) {
282 s = new GString(tokBuf, tokBufSize);
284 s->append(tokBuf, tokBufSize);
293 s = new GString(tokBuf, n);
295 s->append(tokBuf, n);
303 while ((c = lookChar()) != EOF && !specialChars[c]) {
307 if (c2 >= '0' && c2 <= '9') {
309 } else if (c2 >= 'A' && c2 <= 'F') {
311 } else if (c2 >= 'a' && c2 <= 'f') {
319 if (c2 >= '0' && c2 <= '9') {
321 } else if (c2 >= 'A' && c2 <= 'F') {
323 } else if (c2 >= 'a' && c2 <= 'f') {
326 error(getPos(), "Illegal digit in hex char in name");
330 if (++n == tokBufSize) {
331 error(getPos(), "Name token too long");
337 obj->initName(tokBuf);
345 obj->initCmd(tokBuf);
348 // hex string or dict punctuation
355 tokBuf[0] = tokBuf[1] = '<';
357 obj->initCmd(tokBuf);
369 } else if (c == EOF) {
370 error(getPos(), "Unterminated hex string");
372 } else if (specialChars[c] != 1) {
374 if (c >= '0' && c <= '9')
376 else if (c >= 'A' && c <= 'F')
378 else if (c >= 'a' && c <= 'f')
381 error(getPos(), "Illegal character <%02x> in hex string", c);
383 if (n == tokBufSize) {
385 s = new GString(tokBuf, tokBufSize);
387 s->append(tokBuf, tokBufSize);
399 s = new GString(tokBuf, n);
401 s->append(tokBuf, n);
403 s->append((char)(c2 << 4));
413 tokBuf[0] = tokBuf[1] = '>';
415 obj->initCmd(tokBuf);
417 error(getPos(), "Illegal character '>'");
426 error(getPos(), "Illegal character '%c'", c);
435 while ((c = lookChar()) != EOF && !specialChars[c]) {
437 if (++n == tokBufSize) {
438 error(getPos(), "Command token too long");
444 if (tokBuf[0] == 't' && !strcmp(tokBuf, "true")) {
445 obj->initBool(gTrue);
446 } else if (tokBuf[0] == 'f' && !strcmp(tokBuf, "false")) {
447 obj->initBool(gFalse);
448 } else if (tokBuf[0] == 'n' && !strcmp(tokBuf, "null")) {
451 obj->initCmd(tokBuf);
459 void Lexer::skipToNextLine() {
464 if (c == EOF || c == '\n') {
468 if ((c = lookChar()) == '\n') {