2016-03-24 01:05:28 +00:00
|
|
|
/*
|
|
|
|
* FSM table generator:
|
|
|
|
* Generate FSM tables as ".c" files from FSM functions.
|
|
|
|
*
|
|
|
|
* 23.03.2016 (dd.mm.yyyy)
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-08-29 01:03:26 +00:00
|
|
|
#include <stdint.h>
|
2016-03-24 14:01:53 +00:00
|
|
|
|
2017-04-15 21:47:23 +00:00
|
|
|
typedef int32_t bool32;
|
|
|
|
|
2016-08-30 21:23:34 +00:00
|
|
|
#define Assert(n) do{ if (!(n)) { *(int*)0 = 0xA11E; } }while(0)
|
2016-03-24 14:01:53 +00:00
|
|
|
#define ArrayCount(a) (sizeof(a)/sizeof(*a))
|
2016-03-24 01:05:28 +00:00
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
#define LEXER_TABLE_FILE "4cpp/4cpp_lexer_tables.c"
|
|
|
|
|
|
|
|
#include "../4cpp/4cpp_lexer_types.h"
|
2017-01-07 02:59:55 +00:00
|
|
|
#include "../4ed_mem_ansi.c"
|
2016-03-24 01:05:28 +00:00
|
|
|
|
2016-08-30 21:23:34 +00:00
|
|
|
typedef struct Whitespace_FSM{
|
2016-08-30 19:30:41 +00:00
|
|
|
unsigned char pp_state;
|
|
|
|
unsigned char white_done;
|
2016-08-30 21:23:34 +00:00
|
|
|
} Whitespace_FSM;
|
2016-05-18 23:22:58 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
Whitespace_FSM
|
|
|
|
whitespace_skip_fsm(Whitespace_FSM wfsm, char c){
|
|
|
|
if (wfsm.pp_state != LSPP_default){
|
|
|
|
if (c == '\n') wfsm.pp_state = LSPP_default;
|
|
|
|
}
|
|
|
|
if (!(c == ' ' || c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v')){
|
|
|
|
wfsm.white_done = 1;
|
|
|
|
}
|
|
|
|
return(wfsm);
|
|
|
|
}
|
|
|
|
|
2016-09-06 21:22:35 +00:00
|
|
|
Cpp_Lex_FSM
|
|
|
|
int_fsm(Cpp_Lex_FSM fsm, char c){
|
2016-03-24 01:05:28 +00:00
|
|
|
switch (fsm.int_state){
|
|
|
|
case LSINT_default:
|
|
|
|
switch (c){
|
|
|
|
case 'u': case 'U': fsm.int_state = LSINT_u; break;
|
|
|
|
case 'l': fsm.int_state = LSINT_l; break;
|
|
|
|
case 'L': fsm.int_state = LSINT_L; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_u:
|
|
|
|
switch (c){
|
|
|
|
case 'l': fsm.int_state = LSINT_ul; break;
|
|
|
|
case 'L': fsm.int_state = LSINT_uL; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_l:
|
|
|
|
switch (c){
|
|
|
|
case 'l': fsm.int_state = LSINT_ll; break;
|
|
|
|
case 'U': case 'u': fsm.int_state = LSINT_extra; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_L:
|
|
|
|
switch (c){
|
|
|
|
case 'L': fsm.int_state = LSINT_ll; break;
|
|
|
|
case 'U': case 'u': fsm.int_state = LSINT_extra; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_ul:
|
|
|
|
switch (c){
|
|
|
|
case 'l': fsm.int_state = LSINT_extra; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_uL:
|
|
|
|
switch (c){
|
|
|
|
case 'L': fsm.int_state = LSINT_extra; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_ll:
|
|
|
|
switch (c){
|
|
|
|
case 'u': case 'U': fsm.int_state = LSINT_extra; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-03-24 01:05:28 +00:00
|
|
|
case LSINT_extra:
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return(fsm);
|
|
|
|
}
|
|
|
|
|
2016-09-06 21:22:35 +00:00
|
|
|
Cpp_Lex_FSM
|
2017-04-15 21:47:23 +00:00
|
|
|
main_fsm(Cpp_Lex_FSM fsm, uint8_t pp_state, uint8_t c, bool32 ignore_string_delims){
|
2016-08-30 19:30:41 +00:00
|
|
|
if (c == 0){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
else{
|
2016-03-24 01:05:28 +00:00
|
|
|
switch (pp_state){
|
2016-08-30 19:30:41 +00:00
|
|
|
case LSPP_error:
|
|
|
|
fsm.state = LS_error_message;
|
|
|
|
if (c == '\n') fsm.emit_token = 1;
|
2016-03-24 01:05:28 +00:00
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
default:
|
|
|
|
switch (fsm.state){
|
|
|
|
case LS_default:
|
2017-03-27 22:36:42 +00:00
|
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c >= 128){
|
2016-08-30 19:30:41 +00:00
|
|
|
fsm.state = LS_identifier;
|
|
|
|
}
|
|
|
|
else if (c >= '1' && c <= '9'){
|
|
|
|
fsm.state = LS_number;
|
|
|
|
}
|
|
|
|
else if (c == '0'){
|
|
|
|
fsm.state = LS_number0;
|
|
|
|
}
|
2016-08-30 21:23:34 +00:00
|
|
|
else{
|
|
|
|
switch (c){
|
2017-04-15 21:47:23 +00:00
|
|
|
case '\'':
|
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_identifier;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.state = LS_char;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_identifier;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.state = LS_string;
|
|
|
|
}
|
|
|
|
}break;
|
2016-08-30 21:23:34 +00:00
|
|
|
|
|
|
|
case '/': fsm.state = LS_comment_pre; break;
|
|
|
|
|
|
|
|
case '.': fsm.state = LS_dot; break;
|
|
|
|
|
|
|
|
case '<':
|
2017-04-15 21:47:23 +00:00
|
|
|
if (pp_state == LSPP_include && !ignore_string_delims){
|
2016-08-30 21:23:34 +00:00
|
|
|
fsm.state = LS_string;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.state = LS_less;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>': fsm.state = LS_more; break;
|
|
|
|
|
|
|
|
case '-': fsm.state = LS_minus; break;
|
|
|
|
|
|
|
|
case '&': fsm.state = LS_and; break;
|
|
|
|
case '|': fsm.state = LS_or; break;
|
|
|
|
|
|
|
|
case '+': fsm.state = LS_plus; break;
|
|
|
|
|
|
|
|
case ':': fsm.state = LS_colon; break;
|
|
|
|
|
|
|
|
case '*': fsm.state = LS_star; break;
|
|
|
|
|
|
|
|
case '%': fsm.state = LS_modulo; break;
|
|
|
|
case '^': fsm.state = LS_caret; break;
|
|
|
|
|
|
|
|
case '=': fsm.state = LS_eq; break;
|
|
|
|
case '!': fsm.state = LS_bang; break;
|
|
|
|
|
|
|
|
case '#':
|
|
|
|
if (pp_state == LSPP_default){
|
|
|
|
fsm.state = LS_pp;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.state = LS_pound;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
#define OperCase(op,type) case op: fsm.emit_token = 1; break;
|
2016-08-30 21:23:34 +00:00
|
|
|
OperCase('{', CPP_TOKEN_BRACE_OPEN);
|
|
|
|
OperCase('}', CPP_TOKEN_BRACE_CLOSE);
|
|
|
|
|
|
|
|
OperCase('[', CPP_TOKEN_BRACKET_OPEN);
|
|
|
|
OperCase(']', CPP_TOKEN_BRACKET_CLOSE);
|
|
|
|
|
|
|
|
OperCase('(', CPP_TOKEN_PARENTHESE_OPEN);
|
|
|
|
OperCase(')', CPP_TOKEN_PARENTHESE_CLOSE);
|
|
|
|
|
|
|
|
OperCase('~', CPP_TOKEN_TILDE);
|
|
|
|
OperCase(',', CPP_TOKEN_COMMA);
|
|
|
|
OperCase(';', CPP_TOKEN_SEMICOLON);
|
|
|
|
OperCase('?', CPP_TOKEN_TERNARY_QMARK);
|
|
|
|
|
|
|
|
OperCase('@', CPP_TOKEN_JUNK);
|
|
|
|
OperCase('\\', CPP_TOKEN_JUNK);
|
2016-08-30 19:30:41 +00:00
|
|
|
#undef OperCase
|
2016-08-30 21:23:34 +00:00
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_identifier:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
2017-04-15 21:47:23 +00:00
|
|
|
int is_ident = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c >= 128 || (ignore_string_delims && (c == '\'' || c == '"'));
|
|
|
|
|
2017-03-27 22:36:42 +00:00
|
|
|
if (!is_ident){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_pound:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_pp:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
|
|
|
if (c == ' ' || c == '\r' || c == '\v' || c == '\f'){
|
|
|
|
// NOTE(allen): do nothing
|
|
|
|
}
|
2017-04-15 21:47:23 +00:00
|
|
|
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 128 || (ignore_string_delims && (c == '\'' || c == '"'))){
|
2017-03-27 22:36:42 +00:00
|
|
|
fsm.state = LS_ppdef;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_ppdef:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
2017-04-15 21:47:23 +00:00
|
|
|
int is_ident = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c >= 128 || (ignore_string_delims && (c == '\'' || c == '"'));
|
2017-03-27 22:36:42 +00:00
|
|
|
if (!is_ident){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2017-03-27 22:36:42 +00:00
|
|
|
case LS_char: case LS_char_multiline:
|
2017-04-15 21:47:23 +00:00
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_string;
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch(c){
|
|
|
|
case '\n': case '\'': fsm.emit_token = 1; break;
|
|
|
|
case '\\': fsm.state = LS_char_slashed; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_char_slashed:
|
2017-04-15 21:47:23 +00:00
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_string;
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (c){
|
|
|
|
case '\r': case '\f': case '\v': break;
|
|
|
|
case '\n': fsm.state = LS_char_multiline; break;
|
|
|
|
default: fsm.state = LS_char; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_string:
|
|
|
|
case LS_string_multiline:
|
2017-04-15 21:47:23 +00:00
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_string;
|
2016-08-30 21:23:34 +00:00
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
2017-04-15 21:47:23 +00:00
|
|
|
else{
|
|
|
|
switch(c){
|
|
|
|
case '\n': case '"': fsm.emit_token = 1; break;
|
|
|
|
case '>':
|
|
|
|
if (pp_state == LSPP_include){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\': fsm.state = LS_string_slashed; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_string_slashed:
|
2017-04-15 21:47:23 +00:00
|
|
|
{
|
|
|
|
if (ignore_string_delims){
|
|
|
|
fsm.state = LS_string;
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (c){
|
|
|
|
case '\r': case '\f': case '\v': break;
|
|
|
|
case '\n': fsm.state = LS_string_multiline; break;
|
|
|
|
default: fsm.state = LS_string; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_number:
|
|
|
|
if (c >= '0' && c <= '9'){
|
|
|
|
fsm.state = LS_number;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (c){
|
|
|
|
case '.': fsm.state = LS_float; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_number0:
|
|
|
|
if (c >= '0' && c <= '9'){
|
|
|
|
fsm.state = LS_number;
|
|
|
|
}
|
|
|
|
else if (c == 'x'){
|
|
|
|
fsm.state = LS_hex;
|
|
|
|
}
|
|
|
|
else if (c == '.'){
|
|
|
|
fsm.state = LS_float;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_float:
|
|
|
|
if (!(c >= '0' && c <= '9')){
|
|
|
|
switch (c){
|
|
|
|
case 'e': fsm.state = LS_crazy_float0; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_crazy_float0:
|
|
|
|
{
|
|
|
|
if ((c >= '0' && c <= '9') || c == '-'){
|
|
|
|
fsm.state = LS_crazy_float1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_crazy_float1:
|
|
|
|
{
|
|
|
|
if (!(c >= '0' && c <= '9')){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_hex:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
|
|
|
int is_hex = c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F' || c >= 128;
|
|
|
|
if (!is_hex){
|
|
|
|
fsm.emit_token = 1;
|
|
|
|
}
|
|
|
|
}break;
|
2016-08-30 19:30:41 +00:00
|
|
|
|
|
|
|
case LS_dot:
|
2017-03-27 22:36:42 +00:00
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9'){
|
|
|
|
fsm.state = LS_float;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
switch (c){
|
|
|
|
case '.': fsm.state = LS_ellipsis; break;
|
|
|
|
case '*': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_ellipsis: fsm.emit_token = 1; break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_less:
|
|
|
|
switch (c){
|
|
|
|
case '<': fsm.state = LS_less_less; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_less_less:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_more:
|
|
|
|
switch (c){
|
|
|
|
case '>': fsm.state = LS_more_more; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-07-18 23:57:08 +00:00
|
|
|
|
2016-08-30 19:30:41 +00:00
|
|
|
case LS_more_more:
|
2016-03-24 01:05:28 +00:00
|
|
|
switch (c){
|
2016-08-30 19:30:41 +00:00
|
|
|
case '=': fsm.emit_token = 1; break;
|
2016-03-24 01:05:28 +00:00
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_comment_pre:
|
2016-03-24 01:05:28 +00:00
|
|
|
switch (c){
|
2016-08-30 19:30:41 +00:00
|
|
|
case '/': fsm.state = LS_comment; break;
|
|
|
|
case '*': fsm.state = LS_comment_block; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
2016-03-24 01:05:28 +00:00
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_comment:
|
|
|
|
switch (c){
|
|
|
|
case '\\': fsm.state = LS_comment_slashed; break;
|
|
|
|
case '\n': fsm.emit_token = 1; break;
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_comment_slashed:
|
|
|
|
switch (c){
|
|
|
|
case '\r': case '\f': case '\v': break;
|
|
|
|
default: fsm.state = LS_comment; break;
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_comment_block:
|
|
|
|
switch (c){
|
|
|
|
case '*': fsm.state = LS_comment_block_ending; break;
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
2016-08-30 19:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_comment_block_ending:
|
2016-03-24 01:05:28 +00:00
|
|
|
switch (c){
|
2016-08-30 19:30:41 +00:00
|
|
|
case '*': fsm.state = LS_comment_block_ending; break;
|
|
|
|
case '/': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.state = LS_comment_block; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_minus:
|
|
|
|
switch (c){
|
|
|
|
case '>': fsm.state = LS_arrow; break;
|
|
|
|
case '-': fsm.emit_token = 1; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_arrow:
|
|
|
|
switch (c){
|
|
|
|
case '*': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_and:
|
|
|
|
switch (c){
|
|
|
|
case '&': fsm.emit_token = 1; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_or:
|
|
|
|
switch (c){
|
|
|
|
case '|': fsm.emit_token = 1; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_plus:
|
|
|
|
switch (c){
|
|
|
|
case '+': fsm.emit_token = 1; break;
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_colon:
|
|
|
|
switch (c){
|
|
|
|
case ':': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_star:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_modulo:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_caret:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_eq:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LS_bang:
|
|
|
|
switch (c){
|
|
|
|
case '=': fsm.emit_token = 1; break;
|
|
|
|
default: fsm.emit_token = 1; break;
|
|
|
|
}
|
|
|
|
break;
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return(fsm);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 14:01:53 +00:00
|
|
|
begin_table(FILE *file, char *type, char *group_name, char *table_name){
|
2016-08-29 01:03:26 +00:00
|
|
|
fprintf(file, "%s %s_%s[] = {\n", type, group_name, table_name);
|
2016-03-24 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 01:05:28 +00:00
|
|
|
begin_table(FILE *file, char *type, char *table_name){
|
2016-08-29 01:03:26 +00:00
|
|
|
fprintf(file, "%s %s[] = {\n", type, table_name);
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 14:01:53 +00:00
|
|
|
begin_ptr_table(FILE *file, char *type, char *table_name){
|
2016-08-29 01:03:26 +00:00
|
|
|
fprintf(file, "%s * %s[] = {\n", type, table_name);
|
2016-03-24 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
|
|
|
do_table_item(FILE *file, uint16_t item){
|
|
|
|
fprintf(file, "%2d,", (int32_t)item);
|
2016-03-24 14:01:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item_direct(FILE *file, char *item, char *tail){
|
|
|
|
fprintf(file, "%s%s,", item, tail);
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 01:05:28 +00:00
|
|
|
end_row(FILE *file){
|
|
|
|
fprintf(file, "\n");
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 01:05:28 +00:00
|
|
|
end_table(FILE *file){
|
|
|
|
fprintf(file, "};\n\n");
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
typedef struct FSM_Tables{
|
|
|
|
uint8_t *full_transition_table;
|
|
|
|
uint8_t *marks;
|
|
|
|
uint8_t *eq_class;
|
|
|
|
uint8_t *eq_class_rep;
|
|
|
|
uint8_t *reduced_transition_table;
|
|
|
|
|
|
|
|
uint8_t eq_class_counter;
|
|
|
|
uint16_t state_count;
|
|
|
|
} FSM_Tables;
|
|
|
|
|
|
|
|
static void
|
|
|
|
allocate_full_tables(FSM_Tables *table, uint8_t state_count){
|
|
|
|
table->full_transition_table = (uint8_t*)malloc(state_count * 256);
|
|
|
|
table->marks = (uint8_t*)malloc(state_count * 256);
|
|
|
|
table->eq_class = (uint8_t*)malloc(state_count * 256);
|
|
|
|
table->eq_class_rep = (uint8_t*)malloc(state_count * 256);
|
2016-03-31 04:05:47 +00:00
|
|
|
table->state_count = state_count;
|
2016-08-30 21:23:34 +00:00
|
|
|
block_zero(table->marks, 256);
|
2016-03-31 04:05:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
|
|
|
do_table_reduction(FSM_Tables *table, uint16_t state_count){
|
2016-03-26 08:06:41 +00:00
|
|
|
{
|
|
|
|
table->eq_class_counter = 0;
|
2016-08-29 01:03:26 +00:00
|
|
|
uint8_t *c_line = table->full_transition_table;
|
|
|
|
for (uint16_t c = 0; c < 256; ++c){
|
2016-03-26 08:06:41 +00:00
|
|
|
if (table->marks[c] == 0){
|
|
|
|
table->eq_class[c] = table->eq_class_counter;
|
2016-08-29 01:03:26 +00:00
|
|
|
table->eq_class_rep[table->eq_class_counter] = (uint8_t)c;
|
|
|
|
uint8_t *c2_line = c_line + state_count;
|
|
|
|
for (uint16_t c2 = c + 1; c2 < 256; ++c2){
|
2016-08-30 21:23:34 +00:00
|
|
|
if (block_compare(c_line, c2_line, state_count) == 0){
|
2016-03-26 08:06:41 +00:00
|
|
|
table->marks[c2] = 1;
|
|
|
|
table->eq_class[c2] = table->eq_class_counter;
|
|
|
|
}
|
|
|
|
c2_line += state_count;
|
|
|
|
}
|
|
|
|
++table->eq_class_counter;
|
|
|
|
}
|
|
|
|
c_line += state_count;
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 01:03:26 +00:00
|
|
|
|
|
|
|
table->reduced_transition_table = (uint8_t*)malloc(state_count * table->eq_class_counter);
|
2016-03-26 08:06:41 +00:00
|
|
|
{
|
2016-08-29 01:03:26 +00:00
|
|
|
uint8_t *r_line = table->reduced_transition_table;
|
|
|
|
for (uint16_t eq = 0; eq < table->eq_class_counter; ++eq){
|
|
|
|
uint8_t *u_line = table->full_transition_table + state_count * table->eq_class_rep[eq];
|
2016-08-30 21:23:34 +00:00
|
|
|
block_copy(r_line, u_line, state_count);
|
2016-03-26 08:06:41 +00:00
|
|
|
r_line += state_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static FSM_Tables
|
2016-03-24 14:01:53 +00:00
|
|
|
generate_whitespace_skip_table(){
|
2016-08-29 01:03:26 +00:00
|
|
|
uint8_t state_count = LSPP_count;
|
2016-03-24 14:01:53 +00:00
|
|
|
FSM_Tables table;
|
2016-03-31 04:05:47 +00:00
|
|
|
allocate_full_tables(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
|
|
|
int32_t i = 0;
|
2016-03-24 14:01:53 +00:00
|
|
|
Whitespace_FSM wfsm = {0};
|
|
|
|
Whitespace_FSM new_wfsm;
|
2016-08-29 01:03:26 +00:00
|
|
|
for (uint16_t c = 0; c < 256; ++c){
|
|
|
|
for (uint8_t state = 0; state < state_count; ++state){
|
2016-03-24 14:01:53 +00:00
|
|
|
wfsm.pp_state = state;
|
|
|
|
wfsm.white_done = 0;
|
2016-08-29 01:03:26 +00:00
|
|
|
new_wfsm = whitespace_skip_fsm(wfsm, (uint8_t)c);
|
2016-03-24 14:01:53 +00:00
|
|
|
table.full_transition_table[i++] = new_wfsm.pp_state + state_count*new_wfsm.white_done;
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-26 08:06:41 +00:00
|
|
|
do_table_reduction(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-26 08:06:41 +00:00
|
|
|
return(table);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static FSM_Tables
|
2016-03-26 08:06:41 +00:00
|
|
|
generate_int_table(){
|
2016-08-29 01:03:26 +00:00
|
|
|
uint8_t state_count = LSINT_count;
|
2016-03-26 08:06:41 +00:00
|
|
|
FSM_Tables table;
|
2016-03-31 04:05:47 +00:00
|
|
|
allocate_full_tables(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
|
|
|
int32_t i = 0;
|
2016-09-06 21:22:35 +00:00
|
|
|
Cpp_Lex_FSM fsm = {0};
|
|
|
|
Cpp_Lex_FSM new_fsm = {0};
|
2016-08-29 01:03:26 +00:00
|
|
|
for (uint16_t c = 0; c < 256; ++c){
|
|
|
|
for (uint8_t state = 0; state < state_count; ++state){
|
2016-03-26 08:06:41 +00:00
|
|
|
fsm.int_state = state;
|
|
|
|
fsm.emit_token = 0;
|
2016-08-29 01:03:26 +00:00
|
|
|
new_fsm = int_fsm(fsm, (uint8_t)c);
|
2016-03-26 08:06:41 +00:00
|
|
|
table.full_transition_table[i++] = new_fsm.int_state + state_count*new_fsm.emit_token;
|
2016-03-24 14:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-26 08:06:41 +00:00
|
|
|
do_table_reduction(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-24 14:01:53 +00:00
|
|
|
return(table);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static FSM_Tables
|
2017-04-15 21:47:23 +00:00
|
|
|
generate_fsm_table(uint8_t pp_state, bool32 ignore_string_delims){
|
2016-08-29 01:03:26 +00:00
|
|
|
uint8_t state_count = LS_count;
|
2016-03-24 14:01:53 +00:00
|
|
|
FSM_Tables table;
|
2016-03-31 04:05:47 +00:00
|
|
|
allocate_full_tables(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
|
|
|
int32_t i = 0;
|
2016-09-06 21:22:35 +00:00
|
|
|
Cpp_Lex_FSM fsm = {0};
|
|
|
|
Cpp_Lex_FSM new_fsm = {0};
|
2016-08-29 01:03:26 +00:00
|
|
|
for (uint16_t c = 0; c < 256; ++c){
|
|
|
|
for (uint8_t state = 0; state < state_count; ++state){
|
2016-03-24 01:05:28 +00:00
|
|
|
fsm.state = state;
|
|
|
|
fsm.emit_token = 0;
|
2017-04-15 21:47:23 +00:00
|
|
|
new_fsm = main_fsm(fsm, pp_state, (uint8_t)c, ignore_string_delims);
|
2016-03-24 14:01:53 +00:00
|
|
|
table.full_transition_table[i++] = new_fsm.state + state_count*new_fsm.emit_token;
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-26 08:06:41 +00:00
|
|
|
do_table_reduction(&table, state_count);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
2016-03-24 14:01:53 +00:00
|
|
|
return(table);
|
|
|
|
}
|
2016-03-24 01:05:28 +00:00
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-03-24 14:01:53 +00:00
|
|
|
render_fsm_table(FILE *file, FSM_Tables tables, char *group_name){
|
2016-08-29 01:03:26 +00:00
|
|
|
begin_table(file, "uint16_t", group_name, "eq_classes");
|
|
|
|
for (uint16_t c = 0; c < 256; ++c){
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item(file, tables.eq_class[c]*tables.state_count);
|
2016-03-24 01:05:28 +00:00
|
|
|
}
|
|
|
|
end_row(file);
|
|
|
|
end_table(file);
|
2016-08-29 01:03:26 +00:00
|
|
|
|
|
|
|
fprintf(file, "const int32_t num_%s_eq_classes = %d;\n\n", group_name, tables.eq_class_counter);
|
|
|
|
|
|
|
|
int32_t i = 0;
|
|
|
|
begin_table(file, "uint8_t", group_name, "table");
|
|
|
|
for (uint16_t c = 0; c < tables.eq_class_counter; ++c){
|
|
|
|
for (uint8_t state = 0; state < tables.state_count; ++state){
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item(file, tables.reduced_transition_table[i++]);
|
|
|
|
}
|
|
|
|
end_row(file);
|
|
|
|
}
|
|
|
|
end_table(file);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
|
|
|
render_variable(FILE *file, char *type, char *variable, uint32_t x){
|
2016-03-31 04:05:47 +00:00
|
|
|
fprintf(file, "%s %s = %d;\n\n", type, variable, x);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static void
|
2016-04-08 00:21:24 +00:00
|
|
|
render_comment(FILE *file, char *comment){
|
|
|
|
fprintf(file, "/*\n%s*/\n", comment);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
typedef struct PP_Names{
|
|
|
|
uint8_t pp_state;
|
2016-03-24 14:01:53 +00:00
|
|
|
char *name;
|
2017-04-15 21:47:23 +00:00
|
|
|
bool32 ignore_string_delims;
|
2016-08-29 01:03:26 +00:00
|
|
|
} PP_Names;
|
2016-03-24 14:01:53 +00:00
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
static PP_Names pp_names[] = {
|
2017-04-15 21:47:23 +00:00
|
|
|
{LSPP_default, "main_fsm", false},
|
|
|
|
{LSPP_include, "pp_include_fsm", false},
|
|
|
|
{LSPP_macro_identifier, "pp_macro_fsm", false},
|
|
|
|
{LSPP_identifier, "pp_identifier_fsm", false},
|
|
|
|
{LSPP_body_if, "pp_body_if_fsm", false},
|
|
|
|
{LSPP_body, "pp_body_fsm", false},
|
|
|
|
{LSPP_number, "pp_number_fsm", false},
|
|
|
|
{LSPP_error, "pp_error_fsm", false},
|
|
|
|
{LSPP_junk, "pp_junk_fsm", false},
|
|
|
|
{LSPP_default, "no_string_fsm", true},
|
2016-03-24 14:01:53 +00:00
|
|
|
};
|
|
|
|
|
2016-03-31 04:05:47 +00:00
|
|
|
int
|
|
|
|
main(){
|
2017-04-15 21:47:23 +00:00
|
|
|
FILE *file = fopen(LEXER_TABLE_FILE, "wb");
|
2016-03-24 01:05:28 +00:00
|
|
|
|
2016-03-24 14:01:53 +00:00
|
|
|
FSM_Tables wtables = generate_whitespace_skip_table();
|
|
|
|
render_fsm_table(file, wtables, "whitespace_fsm");
|
2016-03-24 01:05:28 +00:00
|
|
|
|
2016-03-26 08:06:41 +00:00
|
|
|
FSM_Tables itables = generate_int_table();
|
|
|
|
render_fsm_table(file, itables, "int_fsm");
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
begin_table(file, "uint8_t", "multiline_state_table");
|
2016-08-30 19:30:41 +00:00
|
|
|
for (uint8_t state = 0; state < LS_count*2; ++state){
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item(file, (state == LS_string_multiline || state == LS_char_multiline));
|
|
|
|
}
|
|
|
|
end_row(file);
|
|
|
|
end_table(file);
|
2016-04-08 00:21:24 +00:00
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
for (int32_t i = 0; i < ArrayCount(pp_names); ++i){
|
2017-04-15 21:47:23 +00:00
|
|
|
FSM_Tables tables = generate_fsm_table(pp_names[i].pp_state, pp_names[i].ignore_string_delims);
|
2016-03-24 14:01:53 +00:00
|
|
|
render_fsm_table(file, tables, pp_names[i].name);
|
|
|
|
}
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
begin_ptr_table(file, "uint16_t", "get_eq_classes");
|
|
|
|
for (int32_t i = 0; i < ArrayCount(pp_names); ++i){
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item_direct(file, pp_names[i].name, "_eq_classes");
|
2016-03-24 01:05:28 +00:00
|
|
|
end_row(file);
|
2016-03-24 14:01:53 +00:00
|
|
|
}
|
2016-03-24 01:05:28 +00:00
|
|
|
end_table(file);
|
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
begin_ptr_table(file, "uint8_t", "get_table");
|
|
|
|
for (int32_t i = 0; i < ArrayCount(pp_names); ++i){
|
2016-03-24 14:01:53 +00:00
|
|
|
do_table_item_direct(file, pp_names[i].name, "_table");
|
2016-03-24 01:05:28 +00:00
|
|
|
end_row(file);
|
|
|
|
}
|
|
|
|
end_table(file);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|
|
|
|
|