RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
RigDef_Regexes.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
51 #pragma once
52 
53 #include <regex>
54 
55 namespace RigDef
56 {
57 
58 namespace Regexes
59 {
60 
61 // -------------------------------------------------------------------------- //
62 // Repeatedly used bits of regular expressions //
63 // Using #define-s instead of const vars to utilize C-string concatenation //
64 // REMEMBER! Do not use captures () - they would mess up result order. Use | //
65 // REMEMBER! Expressions using | MUST be enclosed in E_CAPTURE() //
66 // -------------------------------------------------------------------------- //
67 
68 #define E_SLASH "\\/"
69 
70 #define E_OPTIONAL_SPACE "[[:blank:]]*"
71 
72 #define E_OR "|"
73 
74 #define E_TRAILING_WHITESPACE "[[:blank:]]*$"
75 
76 #define E_LEADING_WHITESPACE "^[[:blank:]]*"
77 
78 #define E_DELIMITER_SPACE "[[:blank:]]+"
79 
80 #define E_DELIMITER_CLASSIC "[[:blank:],:|]+" // Separators: space/comma/colon/pipe, see also `IsSeparator()` in RigDef_Parser.cpp. This is what original parser did.
81 
82 #define E_NODE_ID "[[:alnum:]_-]+"
83 
84 // --------------------------------------------------------------------------
85 // Macros
86 // --------------------------------------------------------------------------
87 
89 #define E_CAPTURE(_REGEXP_) \
90  "(" _REGEXP_ ")"
91 
93 #define E_CAPTURE_OPTIONAL(_REGEXP_) \
94  "(" _REGEXP_ ")?"
95 
97 #define E_KEYWORD_BLOCK(_NAME_) \
98  "(^" _NAME_ "[[:blank:]]*$)?"
99 
101 #define E_KEYWORD_INLINE(_NAME_) \
102  "(^" _NAME_ E_DELIMITER_CLASSIC ".*$)?"
103 
105 #define E_KEYWORD_INLINE_UNSEPARATED(_NAME_) \
106  "(^" _NAME_ ".*$)?"
107 
109 #define DEFINE_REGEX(_NAME_,_REGEXP_) \
110  const std::regex _NAME_ = std::regex( _REGEXP_, std::regex::ECMAScript);
111 
112 #define DEFINE_REGEX_IGNORECASE(_NAME_,_REGEXP_) \
113  const std::regex _NAME_ = std::regex( _REGEXP_, std::regex::ECMAScript | std::regex::icase);
114 
115 // -------------------------------------------------------------------------- //
116 // Utility regexes //
117 // -------------------------------------------------------------------------- //
118 
119 // IMPORTANT! If you add a value here, you must also modify File::Keywords enum, it relies on positions in this regex
120 #define IDENTIFY_KEYWORD_REGEX_STRING \
121  E_KEYWORD_INLINE("add_animation") /* Position 1 */ \
122  E_KEYWORD_BLOCK("airbrakes") /* Position 2 */ \
123  E_KEYWORD_BLOCK("animators") /* Position 3 etc... */ \
124  E_KEYWORD_INLINE("AntiLockBrakes") \
125  E_KEYWORD_BLOCK("assetpacks") \
126  E_KEYWORD_INLINE("author") \
127  E_KEYWORD_BLOCK("axles") \
128  E_KEYWORD_BLOCK("backmesh") \
129  E_KEYWORD_BLOCK("beams") \
130  E_KEYWORD_BLOCK("brakes") \
131  E_KEYWORD_BLOCK("cab") \
132  E_KEYWORD_BLOCK("camerarail") \
133  E_KEYWORD_BLOCK("cameras") \
134  E_KEYWORD_BLOCK("cinecam") \
135  E_KEYWORD_BLOCK("collisionboxes") \
136  E_KEYWORD_BLOCK("commands") \
137  E_KEYWORD_BLOCK("commands2") \
138  E_KEYWORD_BLOCK("comment") \
139  E_KEYWORD_BLOCK("contacters") \
140  E_KEYWORD_INLINE("cruisecontrol") \
141  E_KEYWORD_INLINE("default_skin") \
142  E_KEYWORD_BLOCK("description") \
143  E_KEYWORD_INLINE("detacher_group") \
144  E_KEYWORD_BLOCK("disabledefaultsounds") \
145  E_KEYWORD_BLOCK("enable_advanced_deformation") \
146  E_KEYWORD_BLOCK("end") \
147  E_KEYWORD_BLOCK("end_comment") \
148  E_KEYWORD_BLOCK("end_description") \
149  E_KEYWORD_BLOCK("end_section") \
150  E_KEYWORD_BLOCK("engine") \
151  E_KEYWORD_BLOCK("engoption") \
152  E_KEYWORD_BLOCK("engturbo") \
153  E_KEYWORD_BLOCK("envmap") \
154  E_KEYWORD_BLOCK("exhausts") \
155  E_KEYWORD_INLINE("extcamera") \
156  E_KEYWORD_INLINE("fileformatversion") \
157  E_KEYWORD_INLINE("fileinfo") \
158  E_KEYWORD_BLOCK("fixes") \
159  E_KEYWORD_BLOCK("flares") \
160  E_KEYWORD_BLOCK("flares2") \
161  E_KEYWORD_BLOCK("flares3") \
162  E_KEYWORD_BLOCK("flaregroups_no_import") \
163  E_KEYWORD_BLOCK("flexbodies") \
164  E_KEYWORD_INLINE("flexbody_camera_mode") \
165  E_KEYWORD_BLOCK("flexbodywheels") \
166  E_KEYWORD_INLINE_UNSEPARATED("forset") \
167  E_KEYWORD_BLOCK("forwardcommands") \
168  E_KEYWORD_BLOCK("fusedrag") \
169  E_KEYWORD_BLOCK("globals") \
170  E_KEYWORD_INLINE("guid") \
171  E_KEYWORD_BLOCK("guisettings") \
172  E_KEYWORD_BLOCK("help") \
173  E_KEYWORD_BLOCK("hideInChooser") \
174  E_KEYWORD_BLOCK("hookgroup") \
175  E_KEYWORD_BLOCK("hooks") \
176  E_KEYWORD_BLOCK("hydros") \
177  E_KEYWORD_BLOCK("importcommands") \
178  E_KEYWORD_BLOCK("interaxles") \
179  E_KEYWORD_BLOCK("lockgroups") \
180  E_KEYWORD_BLOCK("lockgroup_default_nolock") \
181  E_KEYWORD_BLOCK("managedmaterials") \
182  E_KEYWORD_BLOCK("materialflarebindings") \
183  E_KEYWORD_BLOCK("meshwheels") \
184  E_KEYWORD_BLOCK("meshwheels2") \
185  E_KEYWORD_BLOCK("minimass") \
186  E_KEYWORD_BLOCK("nodecollision") \
187  E_KEYWORD_BLOCK("nodes") \
188  E_KEYWORD_BLOCK("nodes2") \
189  E_KEYWORD_BLOCK("particles") \
190  E_KEYWORD_BLOCK("pistonprops") \
191  E_KEYWORD_INLINE("prop_camera_mode") \
192  E_KEYWORD_BLOCK("props") \
193  E_KEYWORD_BLOCK("railgroups") \
194  E_KEYWORD_BLOCK("rescuer") \
195  E_KEYWORD_BLOCK("rigidifiers") \
196  E_KEYWORD_BLOCK("rollon") \
197  E_KEYWORD_BLOCK("ropables") \
198  E_KEYWORD_BLOCK("ropes") \
199  E_KEYWORD_BLOCK("rotators") \
200  E_KEYWORD_BLOCK("rotators2") \
201  E_KEYWORD_BLOCK("screwprops") \
202  E_KEYWORD_BLOCK("scripts") \
203  E_KEYWORD_INLINE("section") \
204  E_KEYWORD_INLINE("sectionconfig") \
205  E_KEYWORD_INLINE("set_beam_defaults") \
206  E_KEYWORD_INLINE("set_beam_defaults_scale") \
207  E_KEYWORD_INLINE("set_collision_range") \
208  E_KEYWORD_INLINE("set_default_minimass") \
209  E_KEYWORD_INLINE("set_inertia_defaults") \
210  E_KEYWORD_INLINE("set_managedmaterials_options") \
211  E_KEYWORD_INLINE("set_node_defaults") \
212  E_KEYWORD_BLOCK("set_shadows") \
213  E_KEYWORD_INLINE("set_skeleton_settings") \
214  E_KEYWORD_BLOCK("shocks") \
215  E_KEYWORD_BLOCK("shocks2") \
216  E_KEYWORD_BLOCK("shocks3") \
217  E_KEYWORD_BLOCK("slidenode_connect_instantly") \
218  E_KEYWORD_BLOCK("slidenodes") \
219  E_KEYWORD_INLINE("SlopeBrake") \
220  E_KEYWORD_BLOCK("soundsources") \
221  E_KEYWORD_BLOCK("soundsources2") \
222  E_KEYWORD_INLINE("speedlimiter") \
223  E_KEYWORD_BLOCK("submesh") \
224  E_KEYWORD_INLINE("submesh_groundmodel") \
225  E_KEYWORD_BLOCK("texcoords") \
226  E_KEYWORD_BLOCK("ties") \
227  E_KEYWORD_BLOCK("torquecurve") \
228  E_KEYWORD_INLINE("TractionControl") \
229  E_KEYWORD_BLOCK("transfercase") \
230  E_KEYWORD_BLOCK("triggers") \
231  E_KEYWORD_BLOCK("turbojets") \
232  E_KEYWORD_BLOCK("turboprops") \
233  E_KEYWORD_BLOCK("turboprops2") \
234  E_KEYWORD_BLOCK("videocamera") \
235  E_KEYWORD_BLOCK("wheeldetachers") \
236  E_KEYWORD_BLOCK("wheels") \
237  E_KEYWORD_BLOCK("wheels2") \
238  E_KEYWORD_BLOCK("wings")
239 
240 DEFINE_REGEX_IGNORECASE( IDENTIFY_KEYWORD_IGNORE_CASE, IDENTIFY_KEYWORD_REGEX_STRING )
241 
242 #define E_2xCAPTURE_TRAILING_COMMENT \
243  E_OPTIONAL_SPACE \
244  E_CAPTURE_OPTIONAL( \
245  E_CAPTURE( \
246  ";" \
247  E_OR \
248  E_SLASH E_SLASH \
249  ) \
250  ".*" \
251  ) \
252  "$"
253 
254 // -------------------------------------------------------------------------- //
255 // Regexes for parsing sections //
256 // -------------------------------------------------------------------------- //
257 
258 DEFINE_REGEX( PARSE_ANIMATORS_NUMBERED_KEYWORD,
260  E_CAPTURE( "throttle|rpm|aerotorq|aeropit|aerostatus" )
261  E_CAPTURE( "[[:digit:]]" )
263  );
264 
265 DEFINE_REGEX( SECTION_AXLES_PROPERTY,
267  E_CAPTURE_OPTIONAL( // #1
269  "w(1|2)" // #2 wheel number
270  "\\("
271  E_CAPTURE( E_NODE_ID ) // #3 Node 1
273  E_CAPTURE( E_NODE_ID ) // #4 Node 2
274  "\\)"
276  )
277  E_CAPTURE_OPTIONAL( // #5
279  "d\\("
280  E_CAPTURE( "[olsv]*" ) // #6 Differential modes
281  "\\)"
283  )
285  );
286 
287 
288 
289 // -------------------------------------------------------------------------- //
290 // Cleanup //
291 // -------------------------------------------------------------------------- //
292 
293 
294 #undef E_SLASH
295 #undef E_TRAILING_WHITESPACE
296 #undef E_LEADING_WHITESPACE
297 #undef E_DELIMITER_SPACE
298 #undef E_OPTIONAL_SPACE
299 #undef E_OR
300 #undef E_NODE_ID
301 #undef E_CAPTURE
302 #undef E_CAPTURE_OPTIONAL
303 #undef DEFINE_REGEX
304 #undef E_2xCAPTURE_TRAILING_COMMENT
305 
306 } // namespace Regexes
307 
308 } // namespace RigDef
E_CAPTURE
#define E_CAPTURE(_REGEXP_)
Encloses expression in 'capture' marks.
Definition: RigDef_Regexes.h:89
DEFINE_REGEX_IGNORECASE
#define DEFINE_REGEX_IGNORECASE(_NAME_, _REGEXP_)
Definition: RigDef_Regexes.h:112
RigDef::Regexes::DEFINE_REGEX
DEFINE_REGEX(PARSE_ANIMATORS_NUMBERED_KEYWORD, E_LEADING_WHITESPACE E_TRAILING_WHITESPACE)
E_DELIMITER_SPACE
#define E_DELIMITER_SPACE
Definition: RigDef_Regexes.h:78
IDENTIFY_KEYWORD_REGEX_STRING
#define IDENTIFY_KEYWORD_REGEX_STRING
Definition: RigDef_Regexes.h:120
E_OPTIONAL_SPACE
#define E_OPTIONAL_SPACE
Definition: RigDef_Regexes.h:70
E_NODE_ID
#define E_NODE_ID
Definition: RigDef_Regexes.h:82
RigDef
Definition: ForwardDeclarations.h:276
E_2xCAPTURE_TRAILING_COMMENT
#define E_2xCAPTURE_TRAILING_COMMENT
Definition: RigDef_Regexes.h:242
E_LEADING_WHITESPACE
#define E_LEADING_WHITESPACE
Definition: RigDef_Regexes.h:76
E_TRAILING_WHITESPACE
#define E_TRAILING_WHITESPACE
Definition: RigDef_Regexes.h:74
E_CAPTURE_OPTIONAL
#define E_CAPTURE_OPTIONAL(_REGEXP_)
Encloses expression in 'capture' marks with ? (zero or one time) mark.
Definition: RigDef_Regexes.h:93