miniRT
By Marcelo Magalhães and Ygor G. Sena, 2023.
parser.h
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* parser.h :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: yde-goes <yde-goes@student.42sp.org.br> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2023/05/10 12:11:15 by mdias-ma #+# #+# */
9 /* Updated: 2023/06/16 13:40:10 by yde-goes ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
14 #ifndef PARSER_H
15 
16 # include <fcntl.h>
17 # include <stdlib.h>
18 # include <string.h>
19 
20 # include "world.h"
21 # include "canvas.h"
22 
23 # define BUFFER 1024
24 
25 # define ERROR_UNEXPECTED_ELEMENT "Unexpected element"
26 # define ERROR_EXPECTED_NUMBER "Expected a number"
27 # define ERROR_EXPECTED_NEWLINE "Expected newline"
28 # define ERROR_EXPECTED_COMMA "Expected a comma"
29 
30 # define INVALID_NUMBER "Invalid number"
31 # define INVALID_VECTOR "Vector components must be between -1 and 1.\n"
32 # define INVALID_COLOR "Color values must be between 0 and 255.\n"
33 # define INVALID_LIGHT "Light ratio must be between 0 and 1.\n"
34 # define INVALID_FOV "Field of view must be between 0 and 180.\n"
35 # define INVALID_DIMENSION "Dimensions should be positive.\n"
36 # define INVALID_FILE "Invalid texture file"
37 # define NON_NORMALIZED "Vector should be normalized"
38 
39 # define ERROR_CAMERA "Only one camera allowed.\n"
40 # define ERROR_AMBIENT "Only one ambient light allowed.\n"
41 
42 typedef enum e_token
43 {
44  TOKEN_AMBIENT_LIGHT,
45  TOKEN_CAMERA,
46  TOKEN_LIGHT,
47  TOKEN_SPHERE,
48  TOKEN_PLANE,
49  TOKEN_CYLINDER,
50  TOKEN_CONE,
51  TOKEN_NEWLINE,
52  TOKEN_COMMENT,
53  TOKEN_ERROR,
54  TOKEN_COUNT
55 } t_token;
56 
75 typedef struct s_scanner
76 {
77  int line;
78  const char *start;
79  const char *consume;
80  const char *current;
81  const char *message;
82  const char *sync;
84 
87 
96 void init_scanner(t_scanner *scanner, const char *source);
97 
107 t_bool scan_integer(t_scanner *scanner);
108 
118 t_bool scan_double(t_scanner *scanner);
119 
128 t_bool scan_comma(t_scanner *scanner);
129 
138 t_bool scan_newline(t_scanner *scanner);
139 
142 
151 t_token parse_type(t_scanner *scanner);
152 
155 
164 void advance(t_scanner *scanner);
165 
174 void skip_whitespace(t_scanner *scanner);
175 
184 int is_sign(int c);
185 
197 void save_position(t_scanner *scanner);
198 
209 void sync_position(t_scanner *scanner);
210 
213 
224 t_bool parse(char *filename, t_scene *scene);
225 
241 t_bool check_extension(const char *filename, const char *extension);
242 
245 
258 t_bool parse_element(t_scanner *scanner, t_scene *scene, int totals[]);
259 
266 t_bool parse_comment(t_scanner *scanner);
267 
274 void add_object_to_world(t_shape *shape, t_world *world);
275 
282 void add_light_to_world(t_light *light, t_world *world);
283 
286 
294 t_bool parse_double(t_scanner *scanner, double *result);
295 
310 t_bool parse_light_ratio(t_scanner *scanner, double *light);
311 
328 t_bool parse_field_of_view(t_scanner *scanner, int *fov);
329 
345 t_bool parse_dimension(t_scanner *scanner, double *dimension);
346 
349 
366 t_bool parse_color(t_scanner *scanner, t_color *result);
367 
376 t_bool parse_position(t_scanner *scanner, t_point *position);
377 
394 t_bool parse_direction(t_scanner *scanner, t_vector *direction);
395 
404 t_bool parse_pattern(t_scanner *scanner, t_shape *shape);
405 
408 
416 t_bool parse_ambient_light(t_scanner *scanner, t_scene *scene);
417 
425 t_bool parse_camera(t_scanner *scanner, t_scene *scene);
426 
434 t_bool parse_light(t_scanner *scanner, t_scene *scene);
435 
438 
452 t_bool parse_sphere(t_scanner *scanner, t_scene *scene);
453 
467 t_bool parse_plane(t_scanner *scanner, t_scene *scene);
468 
483 t_bool parse_cylinder(t_scanner *scanner, t_scene *scene);
484 
499 t_bool parse_cone(t_scanner *scanner, t_scene *scene);
500 
503 
511 t_bool report_error(t_scanner *scanner);
512 
524 t_bool validate_range(double value, double start, double end);
525 
538 int get_error_column(t_scanner *scanner);
539 
551 void set_error_state(t_scanner *scanner, const char *error_message);
552 
555 
572 t_bool parse_texture(t_scanner *scanner, t_shape *shape);
573 
574 #endif // !PARSER_H
void save_position(t_scanner *scanner)
Saves the current position of the scanner for later restoration and synchronization with the parser.
Definition: scanner_utils.c:32
void sync_position(t_scanner *scanner)
Synchronizes the scanner with the parser by restoring its position to a previously saved position.
Definition: scanner_utils.c:38
t_bool parse_cone(t_scanner *scanner, t_scene *scene)
Convert a cone from the input and add it to the scene.
Definition: parse_shapes_2.c:69
t_bool parse_light(t_scanner *scanner, t_scene *scene)
Convert light point from input and add to scene.
Definition: parse_ambient.c:49
t_bool parse_dimension(t_scanner *scanner, double *dimension)
Convert dimension from input and store it in dimension.
Definition: parse_basic.c:47
void set_error_state(t_scanner *scanner, const char *error_message)
Determine the error message based on the state of the scanner.
Definition: parse_utils.c:36
t_bool parse(char *filename, t_scene *scene)
Parse.
Definition: parse.c:27
t_bool parse_color(t_scanner *scanner, t_color *result)
Parse grouped.
Definition: parse_grouped.c:18
t_bool parse_position(t_scanner *scanner, t_point *position)
Convert a position from the input and store it in position.
Definition: parse_grouped.c:29
t_bool scan_integer(t_scanner *scanner)
Identify if input character sequence forms an integer.
Definition: scanner.c:27
void add_light_to_world(t_light *light, t_world *world)
Add a light source to the world.
Definition: parse_element.c:60
t_bool check_extension(const char *filename, const char *extension)
Check if a filename has a specified extension.
Definition: parse.c:74
t_bool parse_element(t_scanner *scanner, t_scene *scene, int totals[])
Parse element.
Definition: parse_element.c:17
void skip_whitespace(t_scanner *scanner)
Skip all encountered whitespace characters.
Definition: scanner_utils.c:20
t_bool parse_camera(t_scanner *scanner, t_scene *scene)
Convert camera from input and add to scene.
Definition: parse_ambient.c:30
void add_object_to_world(t_shape *shape, t_world *world)
Add a shape to the world.
Definition: parse_element.c:53
t_bool parse_field_of_view(t_scanner *scanner, int *fov)
Convert field of view from input and store it in fov.
Definition: parse_basic.c:36
void advance(t_scanner *scanner)
Scanner utils.
Definition: scanner_utils.c:15
int get_error_column(t_scanner *scanner)
Get the column where an error occurred.
Definition: parse_utils.c:57
t_bool parse_pattern(t_scanner *scanner, t_shape *shape)
Parse a pattern from the input and assign it to a shape.
Definition: parse_pattern.c:22
int is_sign(int c)
Check if character is a sign.
Definition: scanner_utils.c:27
t_bool parse_direction(t_scanner *scanner, t_vector *direction)
Convert a direction from the input and store it in direction.
Definition: parse_grouped.c:41
t_bool scan_double(t_scanner *scanner)
Identify if input character sequence forms a double number.
Definition: scanner.c:42
t_bool scan_newline(t_scanner *scanner)
Verify presence of a newline in the input.
Definition: scanner.c:67
void init_scanner(t_scanner *scanner, const char *source)
Scanner.
Definition: scanner.c:15
t_token parse_type(t_scanner *scanner)
Parse types.
Definition: parse_types.c:18
struct s_scanner t_scanner
Represents a scanner.
t_bool parse_light_ratio(t_scanner *scanner, double *light)
Parse the light ratio from the input.
Definition: parse_basic.c:25
t_bool parse_ambient_light(t_scanner *scanner, t_scene *scene)
Parse ambient.
Definition: parse_ambient.c:17
t_bool parse_sphere(t_scanner *scanner, t_scene *scene)
Parse shapes.
Definition: parse_shapes_1.c:15
t_bool parse_texture(t_scanner *scanner, t_shape *shape)
Parse textures.
Definition: parse_textures.c:19
t_bool scan_comma(t_scanner *scanner)
Verify presence of a comma in the input.
Definition: scanner.c:55
t_bool parse_double(t_scanner *scanner, double *result)
Parse basic.
Definition: parse_basic.c:15
t_bool parse_cylinder(t_scanner *scanner, t_scene *scene)
Convert a cylinder from the input and add it to the scene.
Definition: parse_shapes_2.c:29
t_bool report_error(t_scanner *scanner)
Parse utils.
Definition: parse_utils.c:21
t_bool parse_plane(t_scanner *scanner, t_scene *scene)
Convert a plane from the input and add it to the scene.
Definition: parse_shapes_1.c:38
t_bool validate_range(double value, double start, double end)
Validate if a number is within a certain range.
Definition: parse_utils.c:50
t_bool parse_comment(t_scanner *scanner)
Parse comments.
Definition: parse_element.c:44
t_point position(t_ray ray, float distance)
The function position() finds the position of a given ray and a given time or distance....
Definition: rays.c:22
The struct s_color stores the color values of ray tracing. It contains a field for red,...
Definition: tuples.h:63
The struct s_light represents a light's source. This source has a position in space and an intensity ...
Definition: lights.h:44
Represents a scanner.
Definition: parser.h:76
The structure t_scene is how the user sees the ray tracing rendering. There is a camera from which th...
Definition: canvas.h:75
Tuple means a list of ordered things. The struct t_tuple stores coordinates for a left-handed coordin...
Definition: tuples.h:33
This struct contains everything related to the building of a ray tracing's world. The world has objec...
Definition: world.h:37
Represents a shape in a scene.