1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2017, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14 copyright notice, this list of conditions and the
15 following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the
19 following disclaimer in the documentation and/or other
20 materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23 contributors may be used to endorse or promote products
24 derived from this software without specific prior
25 written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41
42/** @file FBXTokenizer.h
43 * @brief FBX lexer
44 */
45#ifndef INCLUDED_AI_FBX_TOKENIZER_H
46#define INCLUDED_AI_FBX_TOKENIZER_H
47
48#include "FBXCompileConfig.h"
49#include <assimp/ai_assert.h>
50#include <vector>
51#include <string>
52
53namespace Assimp {
54namespace FBX {
55
56/** Rough classification for text FBX tokens used for constructing the
57 * basic scope hierarchy. */
58enum TokenType
59{
60 // {
61 TokenType_OPEN_BRACKET = 0,
62
63 // }
64 TokenType_CLOSE_BRACKET,
65
66 // '"blablubb"', '2', '*14' - very general token class,
67 // further processing happens at a later stage.
68 TokenType_DATA,
69
70 //
71 TokenType_BINARY_DATA,
72
73 // ,
74 TokenType_COMMA,
75
76 // blubb:
77 TokenType_KEY
78};
79
80
81/** Represents a single token in a FBX file. Tokens are
82 * classified by the #TokenType enumerated types.
83 *
84 * Offers iterator protocol. Tokens are immutable. */
85class Token
86{
87private:
88 static const unsigned int BINARY_MARKER = static_cast<unsigned int>(-1);
89
90public:
91 /** construct a textual token */
92 Token(const char* sbegin, const char* send, TokenType type, unsigned int line, unsigned int column);
93
94 /** construct a binary token */
95 Token(const char* sbegin, const char* send, TokenType type, unsigned int offset);
96
97 ~Token();
98
99public:
100 std::string StringContents() const {
101 return std::string(begin(),end());
102 }
103
104 bool IsBinary() const {
105 return column == BINARY_MARKER;
106 }
107
108 const char* begin() const {
109 return sbegin;
110 }
111
112 const char* end() const {
113 return send;
114 }
115
116 TokenType Type() const {
117 return type;
118 }
119
120 unsigned int Offset() const {
121 ai_assert(IsBinary());
122 return offset;
123 }
124
125 unsigned int Line() const {
126 ai_assert(!IsBinary());
127 return line;
128 }
129
130 unsigned int Column() const {
131 ai_assert(!IsBinary());
132 return column;
133 }
134
135private:
136
137#ifdef DEBUG
138 // full string copy for the sole purpose that it nicely appears
139 // in msvc's debugger window.
140 const std::string contents;
141#endif
142
143
144 const char* const sbegin;
145 const char* const send;
146 const TokenType type;
147
148 union {
149 const unsigned int line;
150 unsigned int offset;
151 };
152 const unsigned int column;
153};
154
155// XXX should use C++11's unique_ptr - but assimp's need to keep working with 03
156typedef const Token* TokenPtr;
157typedef std::vector< TokenPtr > TokenList;
158
159#define new_Token new Token
160
161
162/** Main FBX tokenizer function. Transform input buffer into a list of preprocessed tokens.
163 *
164 * Skips over comments and generates line and column numbers.
165 *
166 * @param output_tokens Receives a list of all tokens in the input data.
167 * @param input_buffer Textual input buffer to be processed, 0-terminated.
168 * @throw DeadlyImportError if something goes wrong */
169void Tokenize(TokenList& output_tokens, const char* input);
170
171
172/** Tokenizer function for binary FBX files.
173 *
174 * Emits a token list suitable for direct parsing.
175 *
176 * @param output_tokens Receives a list of all tokens in the input data.
177 * @param input_buffer Binary input buffer to be processed.
178 * @param length Length of input buffer, in bytes. There is no 0-terminal.
179 * @throw DeadlyImportError if something goes wrong */
180void TokenizeBinary(TokenList& output_tokens, const char* input, unsigned int length);
181
182
183} // ! FBX
184} // ! Assimp
185
186#endif // ! INCLUDED_AI_FBX_PARSER_H
187