1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #include "tensorflow/contrib/lite/testing/message.h" 16 17 #include <stack> 18 19 #include "tensorflow/contrib/lite/testing/tokenize.h" 20 21 namespace tflite { 22 namespace testing { 23 24 // A token processor that builds messages and forward calls to the current 25 // message object. Place a new message at the top of the stack when it start 26 // and remove it when it is finished. 27 class MessageStack : public TokenProcessor { 28 public: 29 // Start a new MessageStack with the given first_node, which will be used to 30 // process freestanding fields and submessages. 31 explicit MessageStack(Message* first_node) { 32 nodes_.push(first_node); 33 valid_ = true; 34 } 35 36 void ConsumeToken(std::string* token) override { 37 if (!valid_) return; 38 Message* current_node = nodes_.top(); 39 if (*token == "{") { 40 // This is the beginning of a new message, names after the previous token. 41 if (previous_token_.empty()) { 42 valid_ = false; 43 return; 44 } 45 nodes_.push(current_node ? current_node->AddChild(previous_token_) 46 : nullptr); 47 previous_token_.clear(); 48 } else if (*token == "}") { 49 // A message is being completed. There should be no previous token. Note 50 // that the top-level message never closes, so we should always have at 51 // least one entry in the stack. 52 if (nodes_.size() == 1 || !previous_token_.empty()) { 53 valid_ = false; 54 return; 55 } 56 if (current_node) { 57 current_node->Finish(); 58 } 59 nodes_.pop(); 60 } else if (*token == ":") { 61 // We reached the end of the 'key' portion of a field. Store the token 62 // until we have the 'value' portion. 63 if (previous_token_.empty()) { 64 valid_ = false; 65 return; 66 } 67 } else { 68 if (previous_token_.empty()) { 69 previous_token_.swap(*token); 70 } else { 71 // This is the 'value' portion of a field. The previous token is the 72 // 'key'. 73 if (current_node) { 74 current_node->SetField(previous_token_, *token); 75 } 76 previous_token_.clear(); 77 } 78 } 79 } 80 81 bool valid() const { return valid_; } 82 83 private: 84 std::stack<Message*> nodes_; 85 std::string previous_token_; 86 bool valid_; 87 }; 88 89 bool Message::Read(std::istream* input, Message* message) { 90 MessageStack stack(message); 91 Tokenize(input, &stack); 92 return stack.valid(); 93 } 94 95 } // namespace testing 96 } // namespace tflite 97