You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

247 lines
6.7 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXTokenizer.cpp
  34. * @brief Implementation of the FBX broadphase lexer
  35. */
  36. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. // tab width for logging columns
  39. #define ASSIMP_FBX_TAB_WIDTH 4
  40. #include "ParsingUtils.h"
  41. #include "FBXTokenizer.h"
  42. #include "FBXUtil.h"
  43. namespace Assimp {
  44. namespace FBX {
  45. // ------------------------------------------------------------------------------------------------
  46. Token::Token(const char* sbegin, const char* send, TokenType type, unsigned int line, unsigned int column)
  47. : sbegin(sbegin)
  48. , send(send)
  49. , type(type)
  50. , line(line)
  51. , column(column)
  52. #ifdef DEBUG
  53. , contents(sbegin, static_cast<size_t>(send-sbegin))
  54. #endif
  55. {
  56. ai_assert(sbegin);
  57. ai_assert(send);
  58. // tokens must be of non-zero length
  59. ai_assert(static_cast<size_t>(send-sbegin) > 0);
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. Token::~Token()
  63. {
  64. }
  65. namespace {
  66. // ------------------------------------------------------------------------------------------------
  67. // signal tokenization error, this is always unrecoverable. Throws DeadlyImportError.
  68. void TokenizeError(const std::string& message, unsigned int line, unsigned int column)
  69. {
  70. throw DeadlyImportError(Util::AddLineAndColumn("FBX-Tokenize",message,line,column));
  71. }
  72. // process a potential data token up to 'cur', adding it to 'output_tokens'.
  73. // ------------------------------------------------------------------------------------------------
  74. void ProcessDataToken( TokenList& output_tokens, const char*& start, const char*& end,
  75. unsigned int line,
  76. unsigned int column,
  77. TokenType type = TokenType_DATA,
  78. bool must_have_token = false)
  79. {
  80. if (start && end) {
  81. // sanity check:
  82. // tokens should have no whitespace outside quoted text and [start,end] should
  83. // properly delimit the valid range.
  84. bool in_double_quotes = false;
  85. for (const char* c = start; c != end + 1; ++c) {
  86. if (*c == '\"') {
  87. in_double_quotes = !in_double_quotes;
  88. }
  89. if (!in_double_quotes && IsSpaceOrNewLine(*c)) {
  90. TokenizeError("unexpected whitespace in token", line, column);
  91. }
  92. }
  93. if (in_double_quotes) {
  94. TokenizeError("non-terminated double quotes", line, column);
  95. }
  96. output_tokens.push_back(new_Token(start,end + 1,type,line,column));
  97. }
  98. else if (must_have_token) {
  99. TokenizeError("unexpected character, expected data token", line, column);
  100. }
  101. start = end = NULL;
  102. }
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. void Tokenize(TokenList& output_tokens, const char* input)
  106. {
  107. ai_assert(input);
  108. // line and column numbers numbers are one-based
  109. unsigned int line = 1;
  110. unsigned int column = 1;
  111. bool comment = false;
  112. bool in_double_quotes = false;
  113. bool pending_data_token = false;
  114. const char* token_begin = NULL, *token_end = NULL;
  115. for (const char* cur = input;*cur;column += (*cur == '\t' ? ASSIMP_FBX_TAB_WIDTH : 1), ++cur) {
  116. const char c = *cur;
  117. if (IsLineEnd(c)) {
  118. comment = false;
  119. column = 0;
  120. ++line;
  121. }
  122. if(comment) {
  123. continue;
  124. }
  125. if(in_double_quotes) {
  126. if (c == '\"') {
  127. in_double_quotes = false;
  128. token_end = cur;
  129. ProcessDataToken(output_tokens,token_begin,token_end,line,column);
  130. pending_data_token = false;
  131. }
  132. continue;
  133. }
  134. switch(c)
  135. {
  136. case '\"':
  137. if (token_begin) {
  138. TokenizeError("unexpected double-quote", line, column);
  139. }
  140. token_begin = cur;
  141. in_double_quotes = true;
  142. continue;
  143. case ';':
  144. ProcessDataToken(output_tokens,token_begin,token_end,line,column);
  145. comment = true;
  146. continue;
  147. case '{':
  148. ProcessDataToken(output_tokens,token_begin,token_end, line, column);
  149. output_tokens.push_back(new_Token(cur,cur+1,TokenType_OPEN_BRACKET,line,column));
  150. continue;
  151. case '}':
  152. ProcessDataToken(output_tokens,token_begin,token_end,line,column);
  153. output_tokens.push_back(new_Token(cur,cur+1,TokenType_CLOSE_BRACKET,line,column));
  154. continue;
  155. case ',':
  156. if (pending_data_token) {
  157. ProcessDataToken(output_tokens,token_begin,token_end,line,column,TokenType_DATA,true);
  158. }
  159. output_tokens.push_back(new_Token(cur,cur+1,TokenType_COMMA,line,column));
  160. continue;
  161. case ':':
  162. if (pending_data_token) {
  163. ProcessDataToken(output_tokens,token_begin,token_end,line,column,TokenType_KEY,true);
  164. }
  165. else {
  166. TokenizeError("unexpected colon", line, column);
  167. }
  168. continue;
  169. }
  170. if (IsSpaceOrNewLine(c)) {
  171. if (token_begin) {
  172. // peek ahead and check if the next token is a colon in which
  173. // case this counts as KEY token.
  174. TokenType type = TokenType_DATA;
  175. for (const char* peek = cur; *peek && IsSpaceOrNewLine(*peek); ++peek) {
  176. if (*peek == ':') {
  177. type = TokenType_KEY;
  178. cur = peek;
  179. break;
  180. }
  181. }
  182. ProcessDataToken(output_tokens,token_begin,token_end,line,column,type);
  183. }
  184. pending_data_token = false;
  185. }
  186. else {
  187. token_end = cur;
  188. if (!token_begin) {
  189. token_begin = cur;
  190. }
  191. pending_data_token = true;
  192. }
  193. }
  194. }
  195. } // !FBX
  196. } // !Assimp
  197. #endif