RigsofRods
Soft-body Physics Simulation
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("flexbodies") \
163  E_KEYWORD_INLINE("flexbody_camera_mode") \
164  E_KEYWORD_BLOCK("flexbodywheels") \
165  E_KEYWORD_INLINE_UNSEPARATED("forset") \
166  E_KEYWORD_BLOCK("forwardcommands") \
167  E_KEYWORD_BLOCK("fusedrag") \
168  E_KEYWORD_BLOCK("globals") \
169  E_KEYWORD_INLINE("guid") \
170  E_KEYWORD_BLOCK("guisettings") \
171  E_KEYWORD_BLOCK("help") \
172  E_KEYWORD_BLOCK("hideInChooser") \
173  E_KEYWORD_BLOCK("hookgroup") \
174  E_KEYWORD_BLOCK("hooks") \
175  E_KEYWORD_BLOCK("hydros") \
176  E_KEYWORD_BLOCK("importcommands") \
177  E_KEYWORD_BLOCK("interaxles") \
178  E_KEYWORD_BLOCK("lockgroups") \
179  E_KEYWORD_BLOCK("lockgroup_default_nolock") \
180  E_KEYWORD_BLOCK("managedmaterials") \
181  E_KEYWORD_BLOCK("materialflarebindings") \
182  E_KEYWORD_BLOCK("meshwheels") \
183  E_KEYWORD_BLOCK("meshwheels2") \
184  E_KEYWORD_BLOCK("minimass") \
185  E_KEYWORD_BLOCK("nodecollision") \
186  E_KEYWORD_BLOCK("nodes") \
187  E_KEYWORD_BLOCK("nodes2") \
188  E_KEYWORD_BLOCK("particles") \
189  E_KEYWORD_BLOCK("pistonprops") \
190  E_KEYWORD_INLINE("prop_camera_mode") \
191  E_KEYWORD_BLOCK("props") \
192  E_KEYWORD_BLOCK("railgroups") \
193  E_KEYWORD_BLOCK("rescuer") \
194  E_KEYWORD_BLOCK("rigidifiers") \
195  E_KEYWORD_BLOCK("rollon") \
196  E_KEYWORD_BLOCK("ropables") \
197  E_KEYWORD_BLOCK("ropes") \
198  E_KEYWORD_BLOCK("rotators") \
199  E_KEYWORD_BLOCK("rotators2") \
200  E_KEYWORD_BLOCK("screwprops") \
201  E_KEYWORD_BLOCK("scripts") \
202  E_KEYWORD_INLINE("section") \
203  E_KEYWORD_INLINE("sectionconfig") \
204  E_KEYWORD_INLINE("set_beam_defaults") \
205  E_KEYWORD_INLINE("set_beam_defaults_scale") \
206  E_KEYWORD_INLINE("set_collision_range") \
207  E_KEYWORD_INLINE("set_default_minimass") \
208  E_KEYWORD_INLINE("set_inertia_defaults") \
209  E_KEYWORD_INLINE("set_managedmaterials_options") \
210  E_KEYWORD_INLINE("set_node_defaults") \
211  E_KEYWORD_BLOCK("set_shadows") \
212  E_KEYWORD_INLINE("set_skeleton_settings") \
213  E_KEYWORD_BLOCK("shocks") \
214  E_KEYWORD_BLOCK("shocks2") \
215  E_KEYWORD_BLOCK("shocks3") \
216  E_KEYWORD_BLOCK("slidenode_connect_instantly") \
217  E_KEYWORD_BLOCK("slidenodes") \
218  E_KEYWORD_INLINE("SlopeBrake") \
219  E_KEYWORD_BLOCK("soundsources") \
220  E_KEYWORD_BLOCK("soundsources2") \
221  E_KEYWORD_INLINE("speedlimiter") \
222  E_KEYWORD_BLOCK("submesh") \
223  E_KEYWORD_INLINE("submesh_groundmodel") \
224  E_KEYWORD_BLOCK("texcoords") \
225  E_KEYWORD_BLOCK("ties") \
226  E_KEYWORD_BLOCK("torquecurve") \
227  E_KEYWORD_INLINE("TractionControl") \
228  E_KEYWORD_BLOCK("transfercase") \
229  E_KEYWORD_BLOCK("triggers") \
230  E_KEYWORD_BLOCK("turbojets") \
231  E_KEYWORD_BLOCK("turboprops") \
232  E_KEYWORD_BLOCK("turboprops2") \
233  E_KEYWORD_BLOCK("videocamera") \
234  E_KEYWORD_BLOCK("wheeldetachers") \
235  E_KEYWORD_BLOCK("wheels") \
236  E_KEYWORD_BLOCK("wheels2") \
237  E_KEYWORD_BLOCK("wings")
238 
239 DEFINE_REGEX_IGNORECASE( IDENTIFY_KEYWORD_IGNORE_CASE, IDENTIFY_KEYWORD_REGEX_STRING )
240 
241 #define E_2xCAPTURE_TRAILING_COMMENT \
242  E_OPTIONAL_SPACE \
243  E_CAPTURE_OPTIONAL( \
244  E_CAPTURE( \
245  ";" \
246  E_OR \
247  E_SLASH E_SLASH \
248  ) \
249  ".*" \
250  ) \
251  "$"
252 
253 // -------------------------------------------------------------------------- //
254 // Regexes for parsing sections //
255 // -------------------------------------------------------------------------- //
256 
257 DEFINE_REGEX( PARSE_ANIMATORS_NUMBERED_KEYWORD,
259  E_CAPTURE( "throttle|rpm|aerotorq|aeropit|aerostatus" )
260  E_CAPTURE( "[[:digit:]]" )
262  );
263 
264 DEFINE_REGEX( SECTION_AXLES_PROPERTY,
266  E_CAPTURE_OPTIONAL( // #1
268  "w(1|2)" // #2 wheel number
269  "\\("
270  E_CAPTURE( E_NODE_ID ) // #3 Node 1
272  E_CAPTURE( E_NODE_ID ) // #4 Node 2
273  "\\)"
275  )
276  E_CAPTURE_OPTIONAL( // #5
278  "d\\("
279  E_CAPTURE( "[olsv]*" ) // #6 Differential modes
280  "\\)"
282  )
284  );
285 
286 
287 
288 // -------------------------------------------------------------------------- //
289 // Cleanup //
290 // -------------------------------------------------------------------------- //
291 
292 
293 #undef E_SLASH
294 #undef E_TRAILING_WHITESPACE
295 #undef E_LEADING_WHITESPACE
296 #undef E_DELIMITER_SPACE
297 #undef E_OPTIONAL_SPACE
298 #undef E_OR
299 #undef E_NODE_ID
300 #undef E_CAPTURE
301 #undef E_CAPTURE_OPTIONAL
302 #undef DEFINE_REGEX
303 #undef E_2xCAPTURE_TRAILING_COMMENT
304 
305 } // namespace Regexes
306 
307 } // 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: RigDef_File.cpp:32
E_2xCAPTURE_TRAILING_COMMENT
#define E_2xCAPTURE_TRAILING_COMMENT
Definition: RigDef_Regexes.h:241
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