• 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_AMBERSCRIPT_PARSER_H_
16 #define SRC_AMBERSCRIPT_PARSER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "amber/result.h"
24 #include "src/parser.h"
25 #include "src/script.h"
26 
27 namespace amber {
28 
29 class Tokenizer;
30 
31 namespace amberscript {
32 
33 /// Parser for the `AmberScript` format.
34 class Parser : public amber::Parser {
35  public:
36   Parser();
37   ~Parser() override;
38 
39   // amber::Parser
40   Result Parse(const std::string& data) override;
41 
42  private:
43   std::string make_error(const std::string& err);
44   Result ToShaderType(const std::string& str, ShaderType* type);
45   Result ToBufferType(const std::string& str, BufferType* type);
46   Result ToShaderFormat(const std::string& str, ShaderFormat* fmt);
47   Result ToPipelineType(const std::string& str, PipelineType* type);
48   Result ValidateEndOfStatement(const std::string& name);
49 
50   Result ParseStruct();
51   Result ParseBuffer();
52   Result ParseBufferInitializer(Buffer*);
53   Result ParseBufferInitializerSize(Buffer*);
54   Result ParseBufferInitializerFill(Buffer*, uint32_t);
55   Result ParseBufferInitializerSeries(Buffer*, uint32_t);
56   Result ParseBufferInitializerData(Buffer*);
57   Result ParseShaderBlock();
58   Result ParsePipelineBlock();
59   Result ParsePipelineAttach(Pipeline*);
60   Result ParsePipelineShaderOptimizations(Pipeline*);
61   Result ParsePipelineShaderCompileOptions(Pipeline*);
62   Result ParsePipelineFramebufferSize(Pipeline*);
63   Result ParsePipelineBind(Pipeline*);
64   Result ParsePipelineVertexData(Pipeline*);
65   Result ParsePipelineIndexData(Pipeline*);
66   Result ParsePipelineSet(Pipeline*);
67   Result ParseRun();
68   Result ParseClear();
69   Result ParseClearColor();
70   Result ParseExpect();
71   Result ParseCopy();
72   Result ParseDeviceFeature();
73   Result ParseDeviceExtension();
74   Result ParseInstanceExtension();
75   Result ParseRepeat();
76   Result ParseSet();
77   bool IsRepeatable(const std::string& name) const;
78   Result ParseRepeatableCommand(const std::string& name);
79   Result ParseDerivePipelineBlock();
80   Result ParsePipelineBody(const std::string& cmd_name,
81                            std::unique_ptr<Pipeline> pipeline);
82   Result ParseShaderSpecialization(Pipeline* pipeline);
83 
84   /// Parses a set of values out of the token stream. |name| is the name of the
85   /// current command we're parsing for error purposes. The |type| is the type
86   /// of data we expect for the current buffer. |values| will be appended to
87   /// with the parsed values.
88   Result ParseValues(const std::string& name,
89                      Format* fmt,
90                      std::vector<Value>* values);
91 
92   std::unique_ptr<Tokenizer> tokenizer_;
93   std::vector<std::unique_ptr<Command>> command_list_;
94 };
95 
96 }  // namespace amberscript
97 }  // namespace amber
98 
99 #endif  // SRC_AMBERSCRIPT_PARSER_H_
100