PC_graph/lex.h
2024-12-30 19:08:36 +01:00

118 lines
2.4 KiB
C

#ifndef LEX_H
#define LEX_H
#include "errors.h"
#define LEX_DEBUG
/**
* @brief Typ tokenu
*/
enum token_type {
TOK_EOF,
TOK_ERROR,
TOK_NUMBER,
TOK_PLUS,
TOK_MINUS,
TOK_MULTIPLY,
TOK_DIVIDE,
TOK_POWER,
TOK_VARIABLE,
TOK_FUNCTION,
TOK_LEFT_PAREN,
TOK_RIGHT_PAREN,
TOK_COMMA
};
/**
* @brief Token
*
* Struktura popisující konkrétní token, který se vyskytnul při lexikální analýze funkce.
*/
struct token {
enum token_type type;
union token_val {
double num;
size_t fn_idx;
} val;
};
/**
* @brief Lexikální analyzátor (lexer)
*
* Struktura uchovávající stav lexikálního analyzátoru.
*/
struct lexer {
const char *start;
const char *prev_p;
const char *p;
struct token tok;
struct error_buffer eb;
const char *variable_name;
};
/**
* @brief Inicializuje lexikální analyzátor
*
* @param lex Lexer
* @param str Řetězec pro analýzu
* @param variable_name Název proměnné
*/
void lex_init(struct lexer *lex, const char *str, const char *variable_name);
/**
* @brief Načte další token
*
* @param lex Lexer
*/
void lex_next(struct lexer *lex);
/**
* @brief Vrátí adresu na aktuální token
*
* @param lex Lexer
* @return Adresa na token
*/
const struct token *lex_token(const struct lexer *lex);
/**
* @brief Vypíše informaci o aktuální pozici ve vstupním řetězci
*
* Tato funkce je volána, když při zpracování vstupní funkce nastane chyba.
*
* @param lex Lexer
* @param eb Zásobník, do kterého bude informace vypsána
*/
void lex_print_position(const struct lexer *lex, struct error_buffer *eb);
/**
* @brief Vrátí kód chyby, která nastala při lexikální analýze
*
* @param lex Lexer
* @return Chybový kód
*/
enum error_code lex_get_error(const struct lexer *lex);
/**
* @brief Vrátí textovou reprezentaci chyby, která nastala při lexikální analýze
*
* @param lex Lexer
* @return Řetězec s chybou
*/
const char *lex_get_error_text(const struct lexer *lex);
/**
* @brief Vrátí textovou reprezentaci tokenu
*
* Tato funkce zohledńuje pouze typ tokenu, hodnota (např. číselná u konstanty) nebude v řetězci obsažena.
*
* @param token Token
* @return Textová reprezentace
*/
const char *lex_token_str(enum token_type token);
#endif /* LEX_H */