1 | //======================================================================== |
2 | // |
3 | // Lexer.h |
4 | // |
5 | // Copyright 1996-2003 Glyph & Cog, LLC |
6 | // |
7 | //======================================================================== |
8 | |
9 | //======================================================================== |
10 | // |
11 | // Modified under the Poppler project - http://poppler.freedesktop.org |
12 | // |
13 | // All changes made under the Poppler project to this file are licensed |
14 | // under GPL version 2 or later |
15 | // |
16 | // Copyright (C) 2006, 2007, 2010, 2013, 2017-2019 Albert Astals Cid <aacid@kde.org> |
17 | // Copyright (C) 2006 Krzysztof Kowalczyk <kkowalczyk@gmail.com> |
18 | // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com> |
19 | // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de> |
20 | // Copyright (C) 2019 Adam Reichold <adam.reichold@t-online.de> |
21 | // |
22 | // To see a description of the changes please see the Changelog file that |
23 | // came with your tarball or type make ChangeLog if you are building from git |
24 | // |
25 | //======================================================================== |
26 | |
27 | #ifndef LEXER_H |
28 | #define LEXER_H |
29 | |
30 | #include "Object.h" |
31 | #include "Stream.h" |
32 | |
33 | class XRef; |
34 | |
35 | #define tokBufSize 128 // size of token buffer |
36 | |
37 | //------------------------------------------------------------------------ |
38 | // Lexer |
39 | //------------------------------------------------------------------------ |
40 | |
41 | class POPPLER_PRIVATE_EXPORT Lexer |
42 | { |
43 | public: |
44 | // Construct a lexer for a single stream. Deletes the stream when |
45 | // lexer is deleted. |
46 | Lexer(XRef *xrefA, Stream *str); |
47 | |
48 | // Construct a lexer for a stream or array of streams (assumes obj |
49 | // is either a stream or array of streams). |
50 | Lexer(XRef *xrefA, Object *obj); |
51 | |
52 | // Destructor. |
53 | ~Lexer(); |
54 | |
55 | Lexer(const Lexer &) = delete; |
56 | Lexer &operator=(const Lexer &) = delete; |
57 | |
58 | // Get the next object from the input stream. |
59 | Object getObj(int objNum = -1); |
60 | Object getObj(const char *cmdA, int objNum); |
61 | template<typename T> |
62 | Object getObj(T) = delete; |
63 | |
64 | // Skip to the beginning of the next line in the input stream. |
65 | void skipToNextLine(); |
66 | |
67 | // Skip over one character. |
68 | void skipChar() { getChar(); } |
69 | |
70 | // Get stream. |
71 | Stream *getStream() { return curStr.isStream() ? curStr.getStream() : nullptr; } |
72 | |
73 | // Get current position in file. This is only used for error |
74 | // messages. |
75 | Goffset getPos() const { return curStr.isStream() ? curStr.getStream()->getPos() : -1; } |
76 | |
77 | // Set position in file. |
78 | void setPos(Goffset pos) |
79 | { |
80 | if (curStr.isStream()) { |
81 | curStr.getStream()->setPos(pos); |
82 | } |
83 | } |
84 | |
85 | // Returns true if <c> is a whitespace character. |
86 | static bool isSpace(int c); |
87 | |
88 | // often (e.g. ~30% on PDF Refernce 1.6 pdf file from Adobe site) getChar |
89 | // is called right after lookChar. In order to avoid expensive re-doing |
90 | // getChar() of underlying stream, we cache the last value found by |
91 | // lookChar() in lookCharLastValueCached. A special value |
92 | // LOOK_VALUE_NOT_CACHED that should never be part of stream indicates |
93 | // that no value was cached |
94 | static const int LOOK_VALUE_NOT_CACHED = -3; |
95 | int lookCharLastValueCached; |
96 | |
97 | XRef *getXRef() const { return xref; } |
98 | bool hasXRef() const { return xref != nullptr; } |
99 | |
100 | private: |
101 | int getChar(bool comesFromLook = false); |
102 | int lookChar(); |
103 | |
104 | Array *streams; // array of input streams |
105 | int strPtr; // index of current stream |
106 | Object curStr; // current stream |
107 | bool freeArray; // should lexer free the streams array? |
108 | char tokBuf[tokBufSize]; // temporary token buffer |
109 | |
110 | XRef *xref; |
111 | }; |
112 | |
113 | #endif |
114 | |