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 ParsePipelineFramebufferSize(Pipeline*); 68 Result ParsePipelineBind(Pipeline*); 69 Result ParsePipelineVertexData(Pipeline*); 70 Result ParsePipelineIndexData(Pipeline*); 71 Result ParsePipelineSet(Pipeline*); 72 Result ParsePipelinePolygonMode(Pipeline*); 73 Result ParsePipelineDepth(Pipeline* pipeline); 74 Result ParsePipelineStencil(Pipeline* pipeline); 75 Result ParseRun(); 76 Result ParseDebug(); 77 Result ParseDebugThread(debug::Events*, Pipeline* pipeline); 78 Result ParseDebugThreadBody(debug::Thread* thread); 79 Result ParseClear(); 80 Result ParseClearColor(); 81 Result ParseClearDepth(); 82 Result ParseClearStencil(); 83 Result ParseExpect(); 84 Result ParseCopy(); 85 Result ParseDeviceFeature(); 86 Result ParseDeviceExtension(); 87 Result ParseInstanceExtension(); 88 Result ParseRepeat(); 89 Result ParseSet(); 90 bool IsRepeatable(const std::string& name) const; 91 Result ParseRepeatableCommand(const std::string& name); 92 Result ParseDerivePipelineBlock(); 93 Result ParsePipelineBody(const std::string& cmd_name, 94 std::unique_ptr<Pipeline> pipeline); 95 Result ParseShaderSpecialization(Pipeline* pipeline); 96 Result ParseSampler(); 97 Result ParseTolerances(std::vector<Probe::Tolerance>* tolerances); 98 99 /// Parses a set of values out of the token stream. |name| is the name of the 100 /// current command we're parsing for error purposes. The |type| is the type 101 /// of data we expect for the current buffer. |values| will be appended to 102 /// with the parsed values. 103 Result ParseValues(const std::string& name, 104 Format* fmt, 105 std::vector<Value>* values); 106 107 Result ParseVirtualFile(); 108 109 std::unique_ptr<Tokenizer> tokenizer_; 110 std::vector<std::unique_ptr<Command>> command_list_; 111 }; 112 113 } // namespace amberscript 114 } // namespace amber 115 116 #endif // SRC_AMBERSCRIPT_PARSER_H_ 117