miniRT
By Marcelo Magalhães and Ygor G. Sena, 2023.
parser.h File Reference
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include "world.h"
#include "canvas.h"
Include dependency graph for parser.h:

Go to the source code of this file.

Data Structures

struct  s_scanner
 Represents a scanner. More...
 

Macros

#define BUFFER   1024
 
#define ERROR_UNEXPECTED_ELEMENT   "Unexpected element"
 
#define ERROR_EXPECTED_NUMBER   "Expected a number"
 
#define ERROR_EXPECTED_NEWLINE   "Expected newline"
 
#define ERROR_EXPECTED_COMMA   "Expected a comma"
 
#define INVALID_NUMBER   "Invalid number"
 
#define INVALID_VECTOR   "Vector components must be between -1 and 1.\n"
 
#define INVALID_COLOR   "Color values must be between 0 and 255.\n"
 
#define INVALID_LIGHT   "Light ratio must be between 0 and 1.\n"
 
#define INVALID_FOV   "Field of view must be between 0 and 180.\n"
 
#define INVALID_DIMENSION   "Dimensions should be positive.\n"
 
#define INVALID_FILE   "Invalid texture file"
 
#define NON_NORMALIZED   "Vector should be normalized"
 
#define ERROR_CAMERA   "Only one camera allowed.\n"
 
#define ERROR_AMBIENT   "Only one ambient light allowed.\n"
 

Typedefs

typedef enum e_token t_token
 
typedef struct s_scanner t_scanner
 Represents a scanner. More...
 

Enumerations

enum  e_token {
  TOKEN_AMBIENT_LIGHT , TOKEN_CAMERA , TOKEN_LIGHT , TOKEN_SPHERE ,
  TOKEN_PLANE , TOKEN_CYLINDER , TOKEN_CONE , TOKEN_NEWLINE ,
  TOKEN_COMMENT , TOKEN_ERROR , TOKEN_COUNT
}
 

Functions

void init_scanner (t_scanner *scanner, const char *source)
 Scanner. More...
 
t_bool scan_integer (t_scanner *scanner)
 Identify if input character sequence forms an integer. More...
 
t_bool scan_double (t_scanner *scanner)
 Identify if input character sequence forms a double number. More...
 
t_bool scan_comma (t_scanner *scanner)
 Verify presence of a comma in the input. More...
 
t_bool scan_newline (t_scanner *scanner)
 Verify presence of a newline in the input. More...
 
t_token parse_type (t_scanner *scanner)
 Parse types. More...
 
void advance (t_scanner *scanner)
 Scanner utils. More...
 
void skip_whitespace (t_scanner *scanner)
 Skip all encountered whitespace characters. More...
 
int is_sign (int c)
 Check if character is a sign. More...
 
void save_position (t_scanner *scanner)
 Saves the current position of the scanner for later restoration and synchronization with the parser. More...
 
void sync_position (t_scanner *scanner)
 Synchronizes the scanner with the parser by restoring its position to a previously saved position. More...
 
t_bool parse (char *filename, t_scene *scene)
 Parse. More...
 
t_bool check_extension (const char *filename, const char *extension)
 Check if a filename has a specified extension. More...
 
t_bool parse_element (t_scanner *scanner, t_scene *scene, int totals[])
 Parse element. More...
 
t_bool parse_comment (t_scanner *scanner)
 Parse comments. More...
 
void add_object_to_world (t_shape *shape, t_world *world)
 Add a shape to the world. More...
 
void add_light_to_world (t_light *light, t_world *world)
 Add a light source to the world. More...
 
t_bool parse_double (t_scanner *scanner, double *result)
 Parse basic. More...
 
t_bool parse_light_ratio (t_scanner *scanner, double *light)
 Parse the light ratio from the input. More...
 
t_bool parse_field_of_view (t_scanner *scanner, int *fov)
 Convert field of view from input and store it in fov. More...
 
t_bool parse_dimension (t_scanner *scanner, double *dimension)
 Convert dimension from input and store it in dimension. More...
 
t_bool parse_color (t_scanner *scanner, t_color *result)
 Parse grouped. More...
 
t_bool parse_position (t_scanner *scanner, t_point *position)
 Convert a position from the input and store it in position. More...
 
t_bool parse_direction (t_scanner *scanner, t_vector *direction)
 Convert a direction from the input and store it in direction. More...
 
t_bool parse_pattern (t_scanner *scanner, t_shape *shape)
 Parse a pattern from the input and assign it to a shape. More...
 
t_bool parse_ambient_light (t_scanner *scanner, t_scene *scene)
 Parse ambient. More...
 
t_bool parse_camera (t_scanner *scanner, t_scene *scene)
 Convert camera from input and add to scene. More...
 
t_bool parse_light (t_scanner *scanner, t_scene *scene)
 Convert light point from input and add to scene. More...
 
t_bool parse_sphere (t_scanner *scanner, t_scene *scene)
 Parse shapes. More...
 
t_bool parse_plane (t_scanner *scanner, t_scene *scene)
 Convert a plane from the input and add it to the scene. More...
 
t_bool parse_cylinder (t_scanner *scanner, t_scene *scene)
 Convert a cylinder from the input and add it to the scene. More...
 
t_bool parse_cone (t_scanner *scanner, t_scene *scene)
 Convert a cone from the input and add it to the scene. More...
 
t_bool report_error (t_scanner *scanner)
 Parse utils. More...
 
t_bool validate_range (double value, double start, double end)
 Validate if a number is within a certain range. More...
 
int get_error_column (t_scanner *scanner)
 Get the column where an error occurred. More...
 
void set_error_state (t_scanner *scanner, const char *error_message)
 Determine the error message based on the state of the scanner. More...
 
t_bool parse_texture (t_scanner *scanner, t_shape *shape)
 Parse textures. More...
 

Typedef Documentation

◆ t_scanner

typedef struct s_scanner t_scanner

Represents a scanner.

The scanner structure is used for scanning and tokenizing input text.

The scanning process is performed using two pointers, consume and current, which need to be kept separate. While current reads from the input and checks if the character sequence is correct, consume remains pointing to the beginning of the portion being verified. This eliminates the need to split the input into pieces and allows for precise identification of the section of input that needs to be converted.

Parameters
lineStores the line number being read.
startA pointer to the first character of the line.
consumeA pointer passed to the number conversion functions.
currentA pointer used for reading character by character.
messageA pointer to the current error message, if any.

Function Documentation

◆ add_light_to_world()

void add_light_to_world ( t_light light,
t_world world 
)

Add a light source to the world.

Parameters
lightPointer to the light source to be added.
worldPointer to the world.

◆ add_object_to_world()

void add_object_to_world ( t_shape shape,
t_world world 
)

Add a shape to the world.

Parameters
shapePointer to the shape to be added.
worldPointer to the world.

◆ advance()

void advance ( t_scanner scanner)

Scanner utils.

Advance the current pointer to the next character.

This function advances the current pointer of the scanner to the next character in the input.

Parameters
scannerPointer to the scanner.

◆ check_extension()

t_bool check_extension ( const char *  filename,
const char *  extension 
)

Check if a filename has a specified extension.

This function checks whether a given filename has a specified extension. It compares the extension of the filename with the provided extension parameter. The extension should be passed without the leading dot (e.g., "png" instead of ".png"). If the filename has the same extension, the function returns true; otherwise, it returns false.

Parameters
filenameThe filename to check.
extensionThe desired extension to compare with (without the leading dot).
Returns
True if the filename has the specified extension, false otherwise.

◆ get_error_column()

int get_error_column ( t_scanner scanner)

Get the column where an error occurred.

This function returns the column number where an error occurred during scanning. It calculates the column number by subtracting the starting pointer of the scanner (scanner->start) from the current pointer (scanner->current). The result represents the offset in characters between the start of the input and the current position where the error occurred.

Parameters
scannerPointer to the scanner.
Returns
The column number where an error occurred.

◆ init_scanner()

void init_scanner ( t_scanner scanner,
const char *  source 
)

Scanner.

Initializes a scanner with the provided source.

This function initializes a scanner structure with the given source.

Parameters
scannerA pointer to the scanner structure to be initialized.
sourceThe source to be scanned and tokenized.

◆ is_sign()

int is_sign ( int  c)

Check if character is a sign.

Checks if given character represents a sign (positive or negative).

Parameters
cCharacter to be checked.
Returns
1 if sign, 0 otherwise.

◆ parse()

t_bool parse ( char *  filename,
t_scene scene 
)

Parse.

Open and read a file, assigning found elements to the scene.

This function opens and reads a file specified by the filename. It parses the content of the file, assigning each found element to the given scene.

Parameters
filenameName of the file to open and read.
scenePointer to the scene object.
Returns
True if successful, false otherwise.

◆ parse_ambient_light()

t_bool parse_ambient_light ( t_scanner scanner,
t_scene scene 
)

Parse ambient.

Convert ambient light from input and add to scene.

Parameters
scannerPointer to the scanner object.
scenePointer to the scene to add ambient light.
Returns
True if successful, false otherwise.

◆ parse_camera()

t_bool parse_camera ( t_scanner scanner,
t_scene scene 
)

Convert camera from input and add to scene.

Parameters
scannerPointer to the scanner object.
scenePointer to the scene to add camera.
Returns
True if successful, false otherwise.

◆ parse_color()

t_bool parse_color ( t_scanner scanner,
t_color result 
)

Parse grouped.

Convert a color from the input and store it in result.

This function converts a color from the input by analyzing the scanned sequence in the given scanner and stores it in the memory location pointed to by result. It verifies if each color channel (red, green, and blue) is between 0 and 255. If there is an error during conversion or if any channel value is out of range, the scanner is configured with the appropriate error message.

Parameters
scannerPointer to the scanner.
resultPointer to the memory location where the parsed color will be stored.
Returns
True if successful, false otherwise. If false, check scanner->message for error message.

◆ parse_comment()

t_bool parse_comment ( t_scanner scanner)

Parse comments.

Parameters
scannerPointer to the scanner.
Returns
True if comment parsing is successful, false otherwise.

◆ parse_cone()

t_bool parse_cone ( t_scanner scanner,
t_scene scene 
)

Convert a cone from the input and add it to the scene.

This function converts a cone from the input by analyzing the scanned sequence in the given scanner and adds it to the scene. It uses the provided scanner to parse the necessary properties of the cone such as its position, direction, diameter, height, color, pattern, and any additional attributes.

Parameters
scannerPointer to the scanner.
scenePointer to the scene where the cone will be added.
Returns
True if the cone is successfully parsed and added to the scene, false otherwise.

◆ parse_cylinder()

t_bool parse_cylinder ( t_scanner scanner,
t_scene scene 
)

Convert a cylinder from the input and add it to the scene.

This function converts a cylinder from the input by analyzing the scanned sequence in the given scanner and adds it to the scene. It uses the provided scanner to parse the necessary properties of the cylinder such as its position, direction, diameter, height, color, pattern, and any additional attributes.

Parameters
scannerPointer to the scanner.
scenePointer to the scene where the cylinder will be added.
Returns
True if the cylinder is successfully parsed and added to the scene, false otherwise.

◆ parse_dimension()

t_bool parse_dimension ( t_scanner scanner,
double *  dimension 
)

Convert dimension from input and store it in dimension.

This function converts the dimension from the input by analyzing the scanned sequence in the given scanner and stores it in the memory location pointed to by dimension. It verifies if the dimension is a positive number. If there is an error during conversion or if the value is not positive, the scanner is configured with the appropriate error message.

Parameters
scannerPointer to the scanner.
dimensionPointer to the memory location where the parsed dimension will be stored.
Returns
True if successful, false otherwise. If false, check scanner->message for error message.

◆ parse_direction()

t_bool parse_direction ( t_scanner scanner,
t_vector direction 
)

Convert a direction from the input and store it in direction.

This function converts a direction vector from the input by analyzing the scanned sequence in the given scanner and stores it in the memory location pointed to by direction. It verifies if each component of the vector is between -1 and 1. If there is an error during conversion or if any component value is out of range, the scanner is configured with the appropriate error message.

Parameters
scannerPointer to the scanner object.
directionPointer to the memory location where the parsed direction will be stored.
Returns
True if successful, false otherwise. If false, check scanner->message for error message.

◆ parse_double()

t_bool parse_double ( t_scanner scanner,
double *  result 
)

Parse basic.

Convert double from input and store in result.

Parameters
scannerPointer to the scanner.
resultPointer to store the parsed double.
Returns
True if successful, false otherwise.

◆ parse_element()

t_bool parse_element ( t_scanner scanner,
t_scene scene,
int  totals[] 
)

Parse element.

Parses and processes an element from the input.

This function parses and processes an element from the input by analyzing the scanned sequence in the given scanner. It also updates the totals array to keep track of the number of elements read from the input.

Parameters
scannerA pointer to the scanner structure.
sceneA pointer to the scene structure.
totalsAn array to store the count of elements read from the input.
Returns
A boolean value indicating whether the parse was successful.

◆ parse_field_of_view()

t_bool parse_field_of_view ( t_scanner scanner,
int *  fov 
)

Convert field of view from input and store it in fov.

This function converts the field of view from the input by analyzing the scanned sequence in the given scanner and stores it in the memory location pointed to by fov. It verifies if the field of view is within the range of 0 to 180 degrees. If there is an error during conversion or if the value is outside the valid range, the scanner is configured with the appropriate error message.

Parameters
scannerPointer to the scanner.
fovPointer to the memory location where the parsed field of view will be stored.
Returns
True if successful, false otherwise. If false, check scanner->message for error message.

◆ parse_light()

t_bool parse_light ( t_scanner scanner,
t_scene scene 
)

Convert light point from input and add to scene.

Parameters
scannerPointer to the scanner object.
scenePointer to the scene to add light point.
Returns
True if successful, false otherwise.

◆ parse_light_ratio()

t_bool parse_light_ratio ( t_scanner scanner,
double *  light 
)

Parse the light ratio from the input.

This function parses the light ratio from the input by analyzing the scanned sequence in the given scanner. It verifies if the number is within the range [0, 1]. If there is an error during parsing or if the number is outside the valid range, the scanner is configured with the appropriate error message.

Parameters
scannerPointer to the scanner of type t_scanner.
lightPointer to the memory location where the parsed light ratio will be stored.
Returns
True if successful, false otherwise. If false, check scanner->message for error message.

◆ parse_pattern()

t_bool parse_pattern ( t_scanner scanner,
t_shape shape 
)

Parse a pattern from the input and assign it to a shape.

Parameters
scannerPointer to the scanner object.
shapePointer to the shape to which the parsed pattern will be assigned.
Returns
True if successful, false otherwise.

◆ parse_plane()

t_bool parse_plane ( t_scanner scanner,
t_scene scene 
)

Convert a plane from the input and add it to the scene.

This function converts a plane from the input by analyzing the scanned sequence in the given scanner and adds it to the scene. It uses the provided scanner to parse the necessary properties of the plane such as its position, direction, color, pattern, and any additional attributes.

Parameters
scannerPointer to the scanner.
scenePointer to the scene where the plane will be added.
Returns
True if the plane is successfully parsed and added to the scene, false otherwise.

◆ parse_position()

t_bool parse_position ( t_scanner scanner,
t_point position 
)

Convert a position from the input and store it in position.

Parameters
scannerPointer to the scanner object.
positionPointer to the memory location where the parsed position will be stored.
Returns
True if successful, false otherwise.

◆ parse_sphere()

t_bool parse_sphere ( t_scanner scanner,
t_scene scene 
)

Parse shapes.

Convert a sphere from the input and add it to the scene.

This function converts a sphere from the input by analyzing the scanned sequence in the given scanner and adds it to the scene. It uses the provided scanner to parse the necessary properties of the sphere such as its position, radius, color, pattern, and any additional attributes.

Parameters
scannerPointer to the scanner.
scenePointer to the scene where the sphere will be added.
Returns
True if the sphere is successfully parsed and added to the scene, false otherwise.

◆ parse_texture()

t_bool parse_texture ( t_scanner scanner,
t_shape shape 
)

Parse textures.

Validate and load a texture file into a UV map and assign it to the shape.

This function validates the integrity of the texture file to be scanned, loads the file into a UV map, and assigns it to the specified shape. The texture file is loaded into a UV map, which represents the mapping of the texture coordinates onto the surface of the shape. The UV map allows for accurate and realistic texture mapping, enhancing the visual appearance of the shape.

Parameters
scannerPointer to the scanner.
shapePointer to the shape to which the texture will be assigned.
Returns
True if the texture file is successfully validated, loaded into the UV map, and assigned to the shape, false otherwise.

◆ parse_type()

t_token parse_type ( t_scanner scanner)

Parse types.

Identify token characterizing analysis type in a line.

Determines the token representing the analysis type in a line of the input.

Parameters
scannerPointer to scanner object.
Returns
Token representing the analysis type.

◆ report_error()

t_bool report_error ( t_scanner scanner)

Parse utils.

Display an error message based on the state of the scanner.

Parameters
scannerPointer to the scanner.
Returns
True if the error message is successfully displayed, false otherwise.

◆ save_position()

void save_position ( t_scanner scanner)

Saves the current position of the scanner for later restoration and synchronization with the parser.

This function takes a pointer to a scanner object and saves the current position of the scanner in the sync pointer of the scanner object. The saved position can be used later to restore the scanner to its current position and synchronize it with the parser.

Parameters
scannerA pointer to a scanner object.

◆ scan_comma()

t_bool scan_comma ( t_scanner scanner)

Verify presence of a comma in the input.

This function verifies if there is a comma character in the input scanner.

Parameters
scannerPointer to the scanner.
Returns
True if a comma is present, false otherwise.

◆ scan_double()

t_bool scan_double ( t_scanner scanner)

Identify if input character sequence forms a double number.

This function checks if the character sequence in the input scanner represents a valid double number.

Parameters
scannerPointer to the scanner.
Returns
True if the sequence is a valid double, false otherwise.

◆ scan_integer()

t_bool scan_integer ( t_scanner scanner)

Identify if input character sequence forms an integer.

This function determines whether the character sequence in the input forms a valid integer.

Parameters
scannerPointer to the scanner.
Returns
True if the sequence is a valid integer, false otherwise.

◆ scan_newline()

t_bool scan_newline ( t_scanner scanner)

Verify presence of a newline in the input.

This function verifies if there is a newline character in the given scanner.

Parameters
scannerPointer to the scanner.
Returns
True if a newline is present, false otherwise.

◆ set_error_state()

void set_error_state ( t_scanner scanner,
const char *  error_message 
)

Determine the error message based on the state of the scanner.

This function sets the error state of the scanner by assigning the provided error message to the scanner's message field. It also resets the current position in the scanner's input to the consume position, effectively rewinding the scanner to a specific location.

Parameters
scannerPointer to the scanner object.
error_messageThe error message to be assigned to the scanner.

◆ skip_whitespace()

void skip_whitespace ( t_scanner scanner)

Skip all encountered whitespace characters.

This function skips all whitespace characters encountered by advancing the current pointer of the scanner past them in the input.

Parameters
scannerPointer to the scanner.

◆ sync_position()

void sync_position ( t_scanner scanner)

Synchronizes the scanner with the parser by restoring its position to a previously saved position.

This function takes a pointer to a scanner object and restores the scanner to the saved position in the sync pointer of the scanner object, synchronizing it with the parser.

Parameters
scannerA pointer to a scanner object.

◆ validate_range()

t_bool validate_range ( double  value,
double  start,
double  end 
)

Validate if a number is within a certain range.

This function checks if the given value is within the specified range, inclusive.

Parameters
valueThe value to be validated.
startThe start of the range.
endThe end of the range.
Returns
True if the value is within the range, false otherwise.