| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2025 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #ifndef H_GEOGRAM_MESH_MESH_CSG_COMPILER_H | ||
| 41 | #define H_GEOGRAM_MESH_MESH_CSG_COMPILER_H | ||
| 42 | |||
| 43 | #include <geogram/basic/common.h> | ||
| 44 | #include <geogram/mesh/mesh_CSG_builder.h> | ||
| 45 | |||
| 46 | #include <filesystem> | ||
| 47 | #include <map> | ||
| 48 | |||
| 49 | namespace GEO { | ||
| 50 | |||
| 51 | class ProgressTask; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * \brief Creates meshes from OpenSCAD .csg files. | ||
| 55 | * \details Understands a subset of OpenSCAD .csg format. | ||
| 56 | */ | ||
| 57 | class GEOGRAM_API CSGCompiler { | ||
| 58 | public: | ||
| 59 | |||
| 60 | typedef GEOCSG::Value Value; | ||
| 61 | typedef GEOCSG::ArgList ArgList; | ||
| 62 | |||
| 63 | CSGCompiler(); | ||
| 64 | |||
| 65 | std::shared_ptr<Mesh> compile_file( | ||
| 66 | const std::filesystem::path& input_filename | ||
| 67 | ); | ||
| 68 | std::shared_ptr<Mesh> compile_string(const std::string& source); | ||
| 69 | |||
| 70 | /** | ||
| 71 | * \brief Displays (lots of) additional information | ||
| 72 | * \param[in] x whether additional information should be displayed. | ||
| 73 | * Default is off | ||
| 74 | */ | ||
| 75 | void set_verbose(bool x) { | ||
| 76 | builder_->set_verbose(x); | ||
| 77 | } | ||
| 78 | |||
| 79 | /** | ||
| 80 | * \brief Displays (even more) additional information | ||
| 81 | * \param[in] x whether additional information should be displayed. | ||
| 82 | * Default is off | ||
| 83 | */ | ||
| 84 | void set_detailed_verbose(bool x) { | ||
| 85 | builder_->set_detailed_verbose(x); | ||
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * \brief Gets the CSGbuilder | ||
| 90 | * \return a reference to the CSGBuilder | ||
| 91 | */ | ||
| 92 | CSGBuilder& builder() { | ||
| 93 | return *builder_; | ||
| 94 | } | ||
| 95 | |||
| 96 | /** | ||
| 97 | * \brief Gets the CSGbuilder | ||
| 98 | * \return a const reference to the CSGBuilder | ||
| 99 | */ | ||
| 100 | const CSGBuilder& builder() const { | ||
| 101 | return *builder_; | ||
| 102 | } | ||
| 103 | |||
| 104 | protected: | ||
| 105 | |||
| 106 | /***** Parser *******************************************/ | ||
| 107 | |||
| 108 | void parse_instruction_or_object(); | ||
| 109 | void parse_object(); | ||
| 110 | void parse_instruction(); | ||
| 111 | ArgList parse_arg_list(); | ||
| 112 | Value parse_value(); | ||
| 113 | Value parse_array(); | ||
| 114 | bool is_object(const std::string& id) const; | ||
| 115 | bool is_instruction(const std::string& id) const; | ||
| 116 | |||
| 117 | /** | ||
| 118 | * \brief Checks if a token corresponds to an instruction or | ||
| 119 | * object modifier | ||
| 120 | * \details A modifier is one of '%','#','!','*', where '%' and '*' | ||
| 121 | * discard the subtree, '#' does not change anything and '!' replaces | ||
| 122 | * the result with the subtree (re-root). | ||
| 123 | * Note: in OpenSCAD, '%' and '#' display the subtree as a transparent | ||
| 124 | * object. | ||
| 125 | */ | ||
| 126 | bool is_modifier(int toktype) const; | ||
| 127 | |||
| 128 | /***** Parser internals ********************************/ | ||
| 129 | |||
| 130 |
5/10✓ Branch 1 taken 18301 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 27308 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18281 times.
✓ Branch 8 taken 315 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 3 times.
✗ Branch 12 not taken.
|
88443 | struct Token { |
| 131 | Token(); | ||
| 132 | std::string to_string() const; | ||
| 133 | int type; | ||
| 134 | std::string str_val; | ||
| 135 | int int_val; | ||
| 136 | double double_val; | ||
| 137 | bool boolean_val; | ||
| 138 | }; | ||
| 139 | |||
| 140 | /** | ||
| 141 | * \brief Checks that the next token is a given character | ||
| 142 | * \details If the next token is something else than the given character, | ||
| 143 | * then parsing stops with an error message. | ||
| 144 | * \param[in] c the character | ||
| 145 | */ | ||
| 146 | void next_token_check(char c); | ||
| 147 | |||
| 148 | /** | ||
| 149 | * \brief Gets the next token. | ||
| 150 | * \details Parsing proceeds to the next token. | ||
| 151 | */ | ||
| 152 | Token next_token(); | ||
| 153 | |||
| 154 | /** | ||
| 155 | * \brief Gets the next token without any side effect. | ||
| 156 | * \details Parsing position remains at the same token. | ||
| 157 | */ | ||
| 158 | Token lookahead_token(); | ||
| 159 | |||
| 160 | /** | ||
| 161 | * \brief Function to actually get the next token from the stream. | ||
| 162 | * \details next_token() and lookahead_token() use a 1-token | ||
| 163 | * buffer to pretend that one can look at a token in advance | ||
| 164 | * without consuming it. | ||
| 165 | */ | ||
| 166 | Token next_token_internal(); | ||
| 167 | |||
| 168 | /** | ||
| 169 | * \brief Gets the total number of lines of the currently parsed source. | ||
| 170 | */ | ||
| 171 | int lines() const; | ||
| 172 | |||
| 173 | /** | ||
| 174 | * \brief Gets the currently parsed line source. | ||
| 175 | */ | ||
| 176 | int line() const; | ||
| 177 | |||
| 178 | /** | ||
| 179 | * \brief Throws an exception with an error message. | ||
| 180 | * \param[in] msg the error message to be displayed | ||
| 181 | */ | ||
| 182 | [[noreturn]] void syntax_error(const char* msg); | ||
| 183 | |||
| 184 | /** | ||
| 185 | * \brief Throws an exception with an error message. | ||
| 186 | * \param[in] msg the error message to be displayed | ||
| 187 | * \param[in] tok the currently parsed token, will be | ||
| 188 | * appended to the error message | ||
| 189 | */ | ||
| 190 | [[noreturn]] void syntax_error(const char* msg, const Token& tok); | ||
| 191 | |||
| 192 | protected: | ||
| 193 | void compute_lines() const; | ||
| 194 | |||
| 195 | private: | ||
| 196 | std::filesystem::path filename_; | ||
| 197 | void* lex_; | ||
| 198 | Token lookahead_token_; | ||
| 199 | ProgressTask* progress_; | ||
| 200 | std::shared_ptr<CSGBuilder> builder_; | ||
| 201 | mutable index_t lines_; | ||
| 202 | mutable index_t line_; | ||
| 203 | mutable const char* line_ptr_; | ||
| 204 | }; | ||
| 205 | |||
| 206 | } | ||
| 207 | |||
| 208 | #endif | ||
| 209 |