From edc367259f736f18ad9426a68efc153d75178e4d Mon Sep 17 00:00:00 2001 From: Peter Verthez Date: Sat, 1 Dec 2001 19:25:19 +0000 Subject: [PATCH] Additional common code factored out. --- gedcom_1byte.lex | 53 +++++++------------------------------ gedcom_hilo.lex | 51 ++++++------------------------------ gedcom_lex_common.c | 64 ++++++++++++++++++++++++++++++++++++++++++--- gedcom_lohi.lex | 51 ++++++------------------------------ 4 files changed, 85 insertions(+), 134 deletions(-) diff --git a/gedcom_1byte.lex b/gedcom_1byte.lex index b7c1d16..4a4b3ec 100644 --- a/gedcom_1byte.lex +++ b/gedcom_1byte.lex @@ -11,12 +11,8 @@ /* $Name$ */ %{ -#include "gedcom.tab.h" -#include "gedcom.h" -#include "multilex.h" -#include "encoding.h" - -#define YY_NO_UNPUT +#undef IN_LEX /* include only a specific part of the following file */ +#include "gedcom_lex_common.c" %} %s NORMAL @@ -40,20 +36,10 @@ gen_delim {delim}|{tab} escape @#{any_char}+@ pointer @{alphanum}{non_at}+@ -%{ -static int current_level=-1; -static int level_diff=MAXGEDCLEVEL; - -#ifdef LEXER_TEST -YYSTYPE gedcom_lval; -int line_no = 1; -#endif - -%} - %% %{ +#define IN_LEX /* include only a specific part of the following file */ #include "gedcom_lex_common.c" ACTION_BEFORE_REGEXPS @@ -220,34 +206,13 @@ int yywrap() } #ifdef LEXER_TEST +int gedcom_lex() +{ + return gedcom_1byte_lex(); +} + int main() { - int tok, res; - init_encodings(); - set_encoding_width(ONE_BYTE); - res = open_conv_to_internal("ASCII"); - if (!res) { - gedcom_error("Unable to open conversion context: %s", - strerror(errno)); - return 1; - } - tok = gedcom_1byte_lex(); - while (tok) { - switch(tok) { - case BADTOKEN: printf("BADTOKEN "); break; - case OPEN: printf("OPEN(%d) ", gedcom_lval.level); break; - case CLOSE: printf("CLOSE "); break; - case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break; - case DELIM: printf("DELIM "); break; - case ANYCHAR: printf("%s ", gedcom_lval.string); break; - case POINTER: printf("POINTER(%s) ", gedcom_lval.pointer); break; - case USERTAG: printf("USERTAG(%s) ", gedcom_lval.tag); break; - default: printf("TAG(%s) ", gedcom_lval.tag); break; - } - tok = gedcom_1byte_lex(); - } - printf("\n"); - close_conv_to_internal(); - return 0; + return test_loop(ONE_BYTE, "ASCII"); } #endif diff --git a/gedcom_hilo.lex b/gedcom_hilo.lex index 2a05b31..418802b 100644 --- a/gedcom_hilo.lex +++ b/gedcom_hilo.lex @@ -14,12 +14,8 @@ /* i.e. this is utf-16-be */ %{ -#include "gedcom.tab.h" -#include "gedcom.h" -#include "multilex.h" -#include "encoding.h" - -#define YY_NO_UNPUT +#undef IN_LEX /* include only a specific part of the following file */ +#include "gedcom_lex_common.c" %} %s NORMAL @@ -43,19 +39,10 @@ gen_delim {delim}|{tab} escape \x00@\x00#{any_char}+\x00@ pointer \x00@{alphanum}{non_at}+\x00@ -%{ -static int current_level=-1; -static int level_diff=MAXGEDCLEVEL; - -#ifdef LEXER_TEST -YYSTYPE gedcom_lval; -int line_no = 1; -#endif -%} - %% %{ +#define IN_LEX /* include only a specific part of the following file */ #include "gedcom_lex_common.c" ACTION_BEFORE_REGEXPS @@ -222,35 +209,13 @@ int yywrap() } #ifdef LEXER_TEST +int gedcom_lex() +{ + return gedcom_hilo_lex(); +} int main() { - int tok, res; - init_encodings(); - set_encoding_width(TWO_BYTE_HILO); - res = open_conv_to_internal("UNICODE"); - if (!res) { - gedcom_error("Unable to open conversion context: %s", - strerror(errno)); - return 1; - } - tok = gedcom_hilo_lex(); - while (tok) { - switch(tok) { - case BADTOKEN: printf("BADTOKEN "); break; - case OPEN: printf("OPEN(%d) ", gedcom_lval.level); break; - case CLOSE: printf("CLOSE "); break; - case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break; - case DELIM: printf("DELIM "); break; - case ANYCHAR: printf("%s ", gedcom_lval.string); break; - case POINTER: printf("POINTER(%s) ", gedcom_lval.pointer); break; - case USERTAG: printf("USERTAG(%s) ", gedcom_lval.tag); break; - default: printf("TAG(%s) ", gedcom_lval.tag); break; - } - tok = gedcom_hilo_lex(); - } - printf("\n"); - close_conv_to_internal(); - return 0; + return test_loop(TWO_BYTE_HILO, "UNICODE"); } #endif diff --git a/gedcom_lex_common.c b/gedcom_lex_common.c index 6d29822..41f2da1 100644 --- a/gedcom_lex_common.c +++ b/gedcom_lex_common.c @@ -10,14 +10,68 @@ /* $Id$ */ /* $Name$ */ +#ifndef IN_LEX + +#include "gedcom.tab.h" +#include "gedcom.h" +#include "multilex.h" +#include "encoding.h" + +#define YY_NO_UNPUT + +static int current_level=-1; +static int level_diff=MAXGEDCLEVEL; + +#ifdef LEXER_TEST +YYSTYPE gedcom_lval; +int line_no = 1; + +int gedcom_lex(); + +int test_loop(ENCODING enc, char* code) +{ + int tok, res; + init_encodings(); + set_encoding_width(enc); + res = open_conv_to_internal(code); + if (!res) { + gedcom_error("Unable to open conversion context: %s", + strerror(errno)); + return 1; + } + tok = gedcom_lex(); + while (tok) { + switch(tok) { + case BADTOKEN: printf("BADTOKEN "); break; + case OPEN: printf("OPEN(%d) ", gedcom_lval.level); break; + case CLOSE: printf("CLOSE "); break; + case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break; + case DELIM: printf("DELIM "); break; + case ANYCHAR: printf("%s ", gedcom_lval.string); break; + case POINTER: printf("POINTER(%s) ", gedcom_lval.pointer); break; + case USERTAG: printf("USERTAG(%s) ", gedcom_lval.tag); break; + default: printf("TAG(%s) ", gedcom_lval.tag); break; + } + tok = gedcom_lex(); + } + printf("\n"); + close_conv_to_internal(); + return 0; +} + +#endif /* of #ifdef LEXER_TEST */ + +#else /* of #ifndef IN_LEX */ + char string_buf[MAXGEDCLINELEN+1]; #define TO_INTERNAL(str) to_internal(str, yyleng) -#define MKTAGACTION(the_tag) \ - { gedcom_lval.tag = TO_INTERNAL(yytext); \ - BEGIN(NORMAL); \ - return TAG_##the_tag; } +#define MKTAGACTION(the_tag) \ + { gedcom_lval.tag = TO_INTERNAL(yytext); \ + BEGIN(NORMAL); \ + return TAG_##the_tag; \ + } /* The GEDCOM level number is converted into a sequence of opening @@ -182,3 +236,5 @@ char string_buf[MAXGEDCLINELEN+1]; yytext, yytext[0]); \ return BADTOKEN; \ } + +#endif /* IN_LEX */ diff --git a/gedcom_lohi.lex b/gedcom_lohi.lex index e91d4f0..41d97ad 100644 --- a/gedcom_lohi.lex +++ b/gedcom_lohi.lex @@ -14,12 +14,8 @@ /* i.e. this is utf-16-le */ %{ -#include "gedcom.tab.h" -#include "gedcom.h" -#include "multilex.h" -#include "encoding.h" - -#define YY_NO_UNPUT +#undef IN_LEX /* include only a specific part of the following file */ +#include "gedcom_lex_common.c" %} %s NORMAL @@ -43,19 +39,10 @@ gen_delim {delim}|{tab} escape @\x00#\x00{any_char}+@\x00 pointer @\x00{alphanum}{non_at}+@\x00 -%{ -static int current_level=-1; -static int level_diff=MAXGEDCLEVEL; - -#ifdef LEXER_TEST -YYSTYPE gedcom_lval; -int line_no = 1; -#endif -%} - %% %{ +#define IN_LEX /* include only a specific part of the following file */ #include "gedcom_lex_common.c" ACTION_BEFORE_REGEXPS @@ -222,35 +209,13 @@ int yywrap() } #ifdef LEXER_TEST +int gedcom_lex() +{ + return gedcom_lohi_lex(); +} int main() { - int tok, res; - init_encodings(); - set_encoding_width(TWO_BYTE_LOHI); - res = open_conv_to_internal("UNICODE"); - if (!res) { - gedcom_error("Unable to open conversion context: %s", - strerror(errno)); - return 1; - } - tok = gedcom_lohi_lex(); - while (tok) { - switch(tok) { - case BADTOKEN: printf("BADTOKEN "); break; - case OPEN: printf("OPEN(%d) ", gedcom_lval.level); break; - case CLOSE: printf("CLOSE "); break; - case ESCAPE: printf("ESCAPE(%s) ", gedcom_lval.string); break; - case DELIM: printf("DELIM "); break; - case ANYCHAR: printf("%s ", gedcom_lval.string); break; - case POINTER: printf("POINTER(%s) ", gedcom_lval.pointer); break; - case USERTAG: printf("USERTAG(%s) ", gedcom_lval.tag); break; - default: printf("TAG(%s) ", gedcom_lval.tag); break; - } - tok = gedcom_lohi_lex(); - } - printf("\n"); - close_conv_to_internal(); - return 0; + return test_loop(TWO_BYTE_LOHI, "UNICODE"); } #endif -- 2.30.2