• 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 class Token;
31 
32 namespace amberscript {
33 
34 /// Parser for the `AmberScript` format.
35 class Parser : public amber::Parser {
36  public:
37   Parser();
38   explicit Parser(Delegate* delegate);
39   ~Parser() override;
40 
41   // amber::Parser
42   Result Parse(const std::string& data) override;
43 
44  private:
45   std::string make_error(const std::string& err);
46   Result ToShaderType(const std::string& str, ShaderType* type);
47   Result ToBufferType(const std::string& str, BufferType* type);
48   Result ToShaderFormat(const std::string& str, ShaderFormat* fmt);
49   Result ToPipelineType(const std::string& str, PipelineType* type);
50   Result ValidateEndOfStatement(const std::string& name);
51 
52   Result ParseStruct();
53   Result ParseBuffer();
54   Result ParseImage();
55   Result ParseBufferInitializer(Buffer*);
56   Result ParseBufferInitializerSize(Buffer*);
57   Result ParseBufferInitializerFill(Buffer*, uint32_t);
58   Result ParseBufferInitializerSeries(Buffer*, uint32_t);
59   Result ParseBufferInitializerData(Buffer*);
60   Result ParseBufferInitializerFile(Buffer*);
61   Result ParseShaderBlock();
62   Result ParsePipelineBlock();
63   Result ParsePipelineAttach(Pipeline*);
64   Result ParsePipelineShaderOptimizations(Pipeline*);
65   Result ParsePipelineShaderCompileOptions(Pipeline*);
66   Result ParsePipelineSubgroup(Pipeline* pipeline);
67   Result ParsePipelinePatchControlPoints(Pipeline* pipeline);
68   Result ParsePipelineFramebufferSize(Pipeline*);
69   Result ParsePipelineViewport(Pipeline*);
70   Result ParsePipelineBind(Pipeline*);
71   Result ParsePipelineVertexData(Pipeline*);
72   Result ParsePipelineIndexData(Pipeline*);
73   Result ParsePipelineSet(Pipeline*);
74   Result ParsePipelinePolygonMode(Pipeline*);
75   Result ParsePipelineDepth(Pipeline* pipeline);
76   Result ParsePipelineStencil(Pipeline* pipeline);
77   Result ParsePipelineBlend(Pipeline* pipeline);
78   Result ParseRun();
79   Result ParseDebug();
80   Result ParseDebugThread(debug::Events*, Pipeline* pipeline);
81   Result ParseDebugThreadBody(debug::Thread* thread);
82   Result ParseClear();
83   Result ParseClearColor();
84   Result ParseClearDepth();
85   Result ParseClearStencil();
86   Result ParseExpect();
87   Result ParseCopy();
88   Result ParseDeviceFeature();
89   Result ParseDeviceExtension();
90   Result ParseInstanceExtension();
91   Result ParseRepeat();
92   Result ParseSet();
93   bool IsRepeatable(const std::string& name) const;
94   Result ParseRepeatableCommand(const std::string& name);
95   Result ParseDerivePipelineBlock();
96   Result ParsePipelineBody(const std::string& cmd_name,
97                            std::unique_ptr<Pipeline> pipeline);
98   Result ParseShaderSpecialization(Pipeline* pipeline);
99   Result ParseSampler();
100   Result ParseTolerances(std::vector<Probe::Tolerance>* tolerances);
101 
102   /// Parses a set of values out of the token stream. |name| is the name of the
103   /// current command we're parsing for error purposes. The |type| is the type
104   /// of data we expect for the current buffer. |values| will be appended to
105   /// with the parsed values.
106   Result ParseValues(const std::string& name,
107                      Format* fmt,
108                      std::vector<Value>* values);
109 
110   Result ParseVirtualFile();
111 
112   std::unique_ptr<Tokenizer> tokenizer_;
113   std::vector<std::unique_ptr<Command>> command_list_;
114 };
115 
116 }  // namespace amberscript
117 }  // namespace amber
118 
119 #endif  // SRC_AMBERSCRIPT_PARSER_H_
120