terom@13: #ifndef LIB_LEXER_H terom@13: #define LIB_LEXER_H terom@13: terom@13: /* terom@13: * Simple FSM lexing terom@13: * terom@13: * The lexer is implemented as a Finite State Machine, consisting for a number of states, which then contain a set of terom@13: * transitions, which move the lexer from state to state based on each char of input at a time. terom@13: * terom@13: * Whenever the state changes, the token callback is triggered with the collected token data. terom@13: */ terom@13: terom@13: /* terom@14: * Transition flags terom@14: */ terom@14: enum lex_transition_flags { terom@14: LEX_TRANS_DEFAULT = 0x01, terom@14: LEX_TRANS_FINAL = 0x02, terom@14: }; terom@14: terom@14: /* terom@13: * A transition from one state to another. terom@13: */ terom@13: struct lex_transition { terom@13: // applies to chars [left, right] terom@13: char left, right; terom@13: terom@14: // flags from lex_transition_flags terom@14: char flags; terom@14: terom@13: // next state to enter terom@13: int next_state; terom@13: }; terom@13: terom@13: /* terom@14: * State flags terom@14: */ terom@14: enum lex_state_flags { terom@14: LEX_STATE_END = 0x01; terom@14: }; terom@14: terom@14: /* terom@13: * A state terom@13: */ terom@13: struct lex_state { terom@13: // the state name (for debugging) terom@13: const char *name; terom@13: terom@14: // flags from lex_state_flags terom@14: char flags; terom@14: terom@13: // list of transitions for this state, terminated by a transition with next_state=0 terom@13: struct lex_transition *trans_list; terom@13: }; terom@13: terom@13: /* terom@13: * Lex machine terom@13: */ terom@13: struct lex { terom@13: // number of states terom@13: size_t state_count; terom@13: terom@13: // array of lex_states, indexable by the state id. terom@13: struct lex_state *state_list; terom@13: terom@13: /* terom@13: * Core token handler. Everytime a full token is lexed (i.e. the state changes), this will be called. terom@13: * `this_token` represents the full token that was parsed, and `token_data` is the token's value. `next_token` terom@13: * is the state that terminated this token, and `prev_token` was the token before this one. terom@13: * terom@13: * `token_data` is a buffer allocated by the lexer that the actual input data is copied into. Thence, it can be terom@13: * modified, as its contents will be replaced by the next token. Hence, if you need to keep hold of it, copy it. terom@13: * terom@13: * Return zero to have lexing continue, nonzero to stop lexing. terom@13: */ terom@14: int (*token_fn) (int this_token, char *token_data, int next_token, int prev_token, void *arg); terom@13: terom@13: /* terom@13: * Called on every char handled by the lexer. `this_token` is the state of the token that the char belongs to. terom@13: * terom@13: * Return zero to have lexing continue, nonzero to stop lexing. terom@13: */ terom@14: int (*char_fn) (int this_token, char token_char, void *arg); terom@13: terom@13: /* terom@13: * Called when the end of input has been reached, `last_token` is the state that we terminated in. terom@13: * terom@13: * Return zero to indiciate that the input was valid, nonzero to indicate an error. terom@13: */ terom@14: int (*end_fn) (int last_token, void *arg); terom@13: }; terom@13: terom@13: /* terom@13: * Helper macros for building the state_list terom@13: */ terom@14: #define LEX_STATE(enum_val) { #enum_val, 0, terom@14: #define LEX_STATE_END(enum_val) { #enum_val, LEX_STATE_END, terom@13: terom@14: #define LEX_CHAR(c, to) { c, c, 0, to }, terom@14: #define LEX_RANGE(l, r, to) { l, r, 0, to }, terom@13: #define LEX_ALPHA(to) LEX_RANGE('a', 'z', to), LEX_RANGE('A', 'Z', to) terom@13: #define LEX_NUMBER(to) LEX_RANGE('0', '9', to) terom@13: #define LEX_ALNUM(to) LEX_ALPHA(to), LEX_NUMBER(to), LEX_CHAR('-', to), LEX_CHAR('_', to) terom@13: #define LEX_WHITESPACE(to) LEX_CHAR(' ', to), LEX_CHAR('\n', to), LEX_CHAR('\t', to) terom@13: terom@14: #define LEX_DEFAULT(to) { 0, 0, LEX_TRANS_DEFAULT, to } \ terom@14: } terom@14: #define LEX_END { 0, 0, 0, 0 } \ terom@14: } terom@13: terom@13: /* terom@13: * Lex it! terom@13: * terom@13: * Return zero to indiciate that the input was valid, nonzero otherwise. terom@13: */ terom@14: int lexer (const struct lex *lex, const char *input, void *arg); terom@13: terom@13: #endif /* LIB_LEXER_H */