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