2017-02-04 00:28:36 -08:00
|
|
|
#ifndef LIBLEX_EVAL_H
|
|
|
|
#define LIBLEX_EVAL_H
|
|
|
|
|
2017-07-20 21:46:39 -07:00
|
|
|
#include <stddef.h>
|
2017-02-04 00:28:36 -08:00
|
|
|
#include "pattern.h"
|
|
|
|
#include "pairmap.h"
|
|
|
|
#include "ll.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A pattern match.
|
|
|
|
* During lexing / evaluation, structs of this type
|
|
|
|
* are used to represent "matched" hypohteses.
|
|
|
|
*/
|
|
|
|
struct match_s {
|
2017-07-20 21:46:39 -07:00
|
|
|
size_t from;
|
|
|
|
size_t to;
|
2017-02-04 00:28:36 -08:00
|
|
|
int pattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used in matching a single word / lexeme / etc
|
|
|
|
* from some input.
|
|
|
|
*/
|
|
|
|
struct eval_s {
|
|
|
|
/**
|
|
|
|
* The input being evaluated.
|
|
|
|
*/
|
2017-07-20 22:09:42 -07:00
|
|
|
const char* string;
|
2017-02-04 00:28:36 -08:00
|
|
|
/**
|
|
|
|
* The current index in the input.
|
|
|
|
*/
|
2017-07-20 21:46:39 -07:00
|
|
|
size_t index;
|
2017-02-04 00:28:36 -08:00
|
|
|
/**
|
|
|
|
* The index where the valuation began.
|
|
|
|
*/
|
2017-07-20 21:46:39 -07:00
|
|
|
size_t begin;
|
2017-02-04 00:28:36 -08:00
|
|
|
/**
|
|
|
|
* The number of nodes / states matched in
|
|
|
|
* the last iteration.
|
|
|
|
*/
|
|
|
|
int matched;
|
|
|
|
/**
|
|
|
|
* Set A of the two sets of pattern nodes / states.
|
|
|
|
*/
|
|
|
|
ht set_a;
|
|
|
|
/**
|
|
|
|
* Set B of the two sets of pattern nodes / states.
|
|
|
|
*/
|
|
|
|
ht set_b;
|
|
|
|
/**
|
|
|
|
* The matches that were found so far.
|
|
|
|
*/
|
|
|
|
ll matches;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current set of states.
|
|
|
|
*/
|
|
|
|
ht* set_current;
|
|
|
|
/**
|
|
|
|
* The next set of states.
|
|
|
|
*/
|
|
|
|
ht* set_next;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configuration for evaluation.
|
|
|
|
* This contains a list of states that should be started with when
|
|
|
|
* the evaluation of a single token begins.
|
|
|
|
*/
|
|
|
|
struct eval_config_s {
|
|
|
|
/**
|
|
|
|
* The inital list of states to be checked.
|
|
|
|
*/
|
|
|
|
ll states;
|
|
|
|
};
|
|
|
|
|
2018-02-05 22:09:12 -08:00
|
|
|
/**
|
|
|
|
* Information about a state that has been
|
|
|
|
* added with eval_config_add.
|
|
|
|
*/
|
|
|
|
struct eval_state_s {
|
|
|
|
/**
|
|
|
|
* The string that was used to compile
|
|
|
|
* this state.
|
|
|
|
*/
|
|
|
|
const char* source;
|
|
|
|
/**
|
|
|
|
* The pattern that was generated with this state.
|
|
|
|
*/
|
|
|
|
pattern* pattern;
|
|
|
|
};
|
|
|
|
|
2017-02-04 00:28:36 -08:00
|
|
|
typedef struct match_s match;
|
|
|
|
typedef struct eval_s eval;
|
|
|
|
typedef struct eval_config_s eval_config;
|
2018-02-05 22:09:12 -08:00
|
|
|
typedef struct eval_state_s eval_state;
|
2017-02-04 00:28:36 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the evaluation configuration.
|
|
|
|
* @param config the configuration to initialize.
|
|
|
|
*/
|
|
|
|
void eval_config_init(eval_config* config);
|
|
|
|
/**
|
|
|
|
* Frees data allocated by an evaluation configuration.
|
|
|
|
* @param config the configuration file to free.
|
|
|
|
* @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLC if there was an allocaton failure.
|
|
|
|
*/
|
|
|
|
liblex_result eval_config_free(eval_config* config);
|
|
|
|
/**
|
|
|
|
* Adds a new pattern to the configuration, with a given pattern ID.
|
|
|
|
* @param config the configuration to add the pattern to.
|
|
|
|
* @param pattern the pattern to add
|
|
|
|
* @param pattern_id the id to associate with the pattern
|
|
|
|
* @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure.
|
|
|
|
*/
|
2017-07-20 22:09:42 -07:00
|
|
|
liblex_result eval_config_add(eval_config* config, const char* pattern, int pattern_id);
|
2018-02-05 22:42:02 -08:00
|
|
|
/**
|
|
|
|
* Removes a pattern from this configuration that has been previously
|
|
|
|
* registered.
|
|
|
|
* @param config the configuration to add the pattern to.
|
|
|
|
* @param pattern the pattern to remove.
|
|
|
|
* @param pattern_id the id associated with the pattern.
|
|
|
|
* @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure.
|
|
|
|
*/
|
|
|
|
liblex_result eval_config_remove(eval_config* config, const char* pattern, int pattern_id);
|
2017-02-04 00:28:36 -08:00
|
|
|
/**
|
|
|
|
* Evaluates / finds a single word.
|
|
|
|
* @param string the string to evaluate.
|
|
|
|
* @param index the index to start at.
|
|
|
|
* @param config the configuration to use
|
|
|
|
* @param match pointer to where to store the newly created match.
|
|
|
|
* @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure.
|
|
|
|
*/
|
2017-07-20 22:09:42 -07:00
|
|
|
liblex_result eval_word(const char* string, size_t index, eval_config* config, match* match);
|
2017-02-04 00:28:36 -08:00
|
|
|
/**
|
|
|
|
* Evaluates input starting at the index until it reaches the null terminator,
|
|
|
|
* adding the best matches to the linked list.
|
|
|
|
* @param string the string to evaluate.
|
|
|
|
* @param index the index to start at
|
|
|
|
* @param config the config to use
|
|
|
|
* @param matches the linked list to populate with matches
|
|
|
|
* @return LIBLEX_SUCCESS if all goes well, or LIBLEX_MALLOC if there was an allocation failure.
|
|
|
|
*/
|
2017-07-20 22:09:42 -07:00
|
|
|
liblex_result eval_all(const char* string, size_t index, eval_config* config, ll* matches);
|
2017-02-14 19:10:44 -08:00
|
|
|
/**
|
|
|
|
* Function intended to be passed to "foreach" calls in libds.
|
|
|
|
* Since eval_all creates a lot of matches and puts them all in a linked list,
|
|
|
|
* this function takes care of freeing the matches.
|
|
|
|
* @param data the match being freed
|
|
|
|
* @param args unused
|
|
|
|
* @return always 0.
|
|
|
|
*/
|
|
|
|
int eval_foreach_match_free(void *data, va_list args);
|
2017-02-04 00:28:36 -08:00
|
|
|
|
|
|
|
#endif
|