• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Amber Authors.
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 #ifndef SRC_VKSCRIPT_SECTION_PARSER_H_
16 #define SRC_VKSCRIPT_SECTION_PARSER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "amber/result.h"
23 #include "amber/shader_info.h"
24 
25 namespace amber {
26 namespace vkscript {
27 
28 enum class NodeType : uint8_t {
29   kComment = 0,
30   kShader,
31   kIndices,
32   kVertexData,
33   kRequire,
34   kTest,
35 };
36 
37 /// Parses the VkScript into the general sections. This includes things like
38 /// the [test], [indices], [vertex data], etc.
39 class SectionParser {
40  public:
41   /// Structure describing a single section of the VkScript document.
42   struct Section {
43     NodeType section_type;
44     ShaderType shader_type;  // Only valid when section_type == kShader
45     ShaderFormat format;
46     size_t starting_line_number;
47     std::string contents;
48   };
49 
50   static bool HasShader(const NodeType type);
51 
52   SectionParser();
53   ~SectionParser();
54 
55   Result Parse(const std::string& data);
Sections()56   const std::vector<Section>& Sections() const { return sections_; }
57 
SplitSectionsForTesting(const std::string & data)58   Result SplitSectionsForTesting(const std::string& data) {
59     return SplitSections(data);
60   }
61 
NameToNodeTypeForTesting(const std::string & name,NodeType * section_type,ShaderType * shader_type,ShaderFormat * fmt)62   Result NameToNodeTypeForTesting(const std::string& name,
63                                   NodeType* section_type,
64                                   ShaderType* shader_type,
65                                   ShaderFormat* fmt) const {
66     return NameToNodeType(name, section_type, shader_type, fmt);
67   }
68 
69  private:
70   Result SplitSections(const std::string& data);
71   void AddSection(NodeType section_type,
72                   ShaderType shader_type,
73                   ShaderFormat fmt,
74                   size_t starting_line_number,
75                   const std::string& contents);
76   Result NameToNodeType(const std::string& name,
77                         NodeType* section_type,
78                         ShaderType* shader_type,
79                         ShaderFormat* fmt) const;
80 
81   std::vector<Section> sections_;
82 };
83 
84 }  // namespace vkscript
85 }  // namespace amber
86 
87 #endif  // SRC_VKSCRIPT_SECTION_PARSER_H_
88