/* This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * (C) 2001 by The Genes Development Team Original author: Peter Verthez (Peter.Verthez@advalvas.be) */ /* $Id$ */ /* $Name$ */ %{ #include "gedcom.tab.h" #include "gedcom.h" #include "multilex.h" #include "encoding.h" #define YY_NO_UNPUT %} %s NORMAL %s EXPECT_TAG alpha [A-Za-z_] digit [0-9] delim " " tab [\t] hash # literal_at @@ otherchar [\x21-\x22\x24-\x2F\x3A-\x3F\x5B-\x5E\x60\x7B-\x7E\x80-\xFE] terminator \x0D|\x0A|\x0D\x0A|\x0A\x0D any_char {alpha}|{digit}|{otherchar}|{delim}|{hash}|{literal_at} any_but_delim {alpha}|{digit}|{otherchar}|{hash}|{literal_at} non_at {alpha}|{digit}|{otherchar}|{delim}|{hash} alphanum {alpha}|{digit} 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 %} %% /* The GEDCOM level number is converted into a sequence of opening and closing brackets. Simply put, the following GEDCOM fragment: 0 HEAD 1 SOUR genes 2 VERS 1.6 2 NAME Genes 1 DATE 07 OCT 2001 ... 0 TRLR is converted into: { HEAD (initial) { SOUR genes (1 higher: no closing brackets) { VERS 1.6 (1 higher: no closing brackets) } { NAME Genes (same level: 1 closing bracket) } } { DATE 07 OCT 2001 (1 lower: 2 closing brackets) ... } { TRLR } or more clearly: { HEAD { SOUR genes { VERS 1.6 } { NAME Genes } } { DATE 07 OCT 2001 ... } { TRLR } But because this means that one token is converted into a series of tokens, there is some initial code following immediately here that returns "pending" tokens. */ %{ char string_buf[MAXGEDCLINELEN+1]; if (level_diff < 1) { level_diff++; return CLOSE; } else if (level_diff == 1) { level_diff++; return OPEN; } else { /* out of brackets... */ } #define TO_INTERNAL(str) to_internal(str, yyleng) #define MKTAGACTION(tag) \ { gedcom_lval.string = TO_INTERNAL(yytext); \ BEGIN(NORMAL); \ return TAG_##tag; } %} {gen_delim}* /* ignore leading whitespace (also tabs) */ 0{digit}+ { gedcom_error ("Level number with leading zero"); return BADTOKEN; } {digit}+ { int level = atoi(TO_INTERNAL(yytext)); if ((level < 0) || (level > MAXGEDCLEVEL)) { gedcom_error ("Level number out of range [0..%d]", MAXGEDCLEVEL); return BADTOKEN; } level_diff = level - current_level; BEGIN(EXPECT_TAG); current_level = level; if (level_diff < 1) { level_diff++; return CLOSE; } else if (level_diff == 1) { level_diff++; return OPEN; } else { /* should never happen (error to GEDCOM spec) */ gedcom_error ("GEDCOM level number is %d higher than " "previous", level_diff); return BADTOKEN; } } ABBR MKTAGACTION(ABBR) ADDR MKTAGACTION(ADDR) ADR1 MKTAGACTION(ADR1) ADR2 MKTAGACTION(ADR2) ADOP MKTAGACTION(ADOP) AFN MKTAGACTION(AFN) AGE MKTAGACTION(AGE) AGNC MKTAGACTION(AGNC) ALIA MKTAGACTION(ALIA) ANCE MKTAGACTION(ANCE) ANCI MKTAGACTION(ANCI) ANUL MKTAGACTION(ANUL) ASSO MKTAGACTION(ASSO) AUTH MKTAGACTION(AUTH) BAPL MKTAGACTION(BAPL) BAPM MKTAGACTION(BAPM) BARM MKTAGACTION(BARM) BASM MKTAGACTION(BASM) BIRT MKTAGACTION(BIRT) BLES MKTAGACTION(BLES) BLOB MKTAGACTION(BLOB) BURI MKTAGACTION(BURI) CALN MKTAGACTION(CALN) CAST MKTAGACTION(CAST) CAUS MKTAGACTION(CAUS) CENS MKTAGACTION(CENS) CHAN MKTAGACTION(CHAN) CHAR MKTAGACTION(CHAR) CHIL MKTAGACTION(CHIL) CHR MKTAGACTION(CHR) CHRA MKTAGACTION(CHRA) CITY MKTAGACTION(CITY) CONC MKTAGACTION(CONC) CONF MKTAGACTION(CONF) CONL MKTAGACTION(CONL) CONT MKTAGACTION(CONT) COPR MKTAGACTION(COPR) CORP MKTAGACTION(CORP) CREM MKTAGACTION(CREM) CTRY MKTAGACTION(CTRY) DATA MKTAGACTION(DATA) DATE MKTAGACTION(DATE) DEAT MKTAGACTION(DEAT) DESC MKTAGACTION(DESC) DESI MKTAGACTION(DESI) DEST MKTAGACTION(DEST) DIV MKTAGACTION(DIV) DIVF MKTAGACTION(DIVF) DSCR MKTAGACTION(DSCR) EDUC MKTAGACTION(EDUC) EMIG MKTAGACTION(EMIG) ENDL MKTAGACTION(ENDL) ENGA MKTAGACTION(ENGA) EVEN MKTAGACTION(EVEN) FAM MKTAGACTION(FAM) FAMC MKTAGACTION(FAMC) FAMF MKTAGACTION(FAMF) FAMS MKTAGACTION(FAMS) FCOM MKTAGACTION(FCOM) FILE MKTAGACTION(FILE) FORM MKTAGACTION(FORM) GEDC MKTAGACTION(GEDC) GIVN MKTAGACTION(GIVN) GRAD MKTAGACTION(GRAD) HEAD MKTAGACTION(HEAD) HUSB MKTAGACTION(HUSB) IDNO MKTAGACTION(IDNO) IMMI MKTAGACTION(IMMI) INDI MKTAGACTION(INDI) LANG MKTAGACTION(LANG) LEGA MKTAGACTION(LEGA) MARB MKTAGACTION(MARB) MARC MKTAGACTION(MARC) MARL MKTAGACTION(MARL) MARR MKTAGACTION(MARR) MARS MKTAGACTION(MARS) MEDI MKTAGACTION(MEDI) NAME MKTAGACTION(NAME) NATI MKTAGACTION(NATI) NATU MKTAGACTION(NATU) NCHI MKTAGACTION(NCHI) NICK MKTAGACTION(NICK) NMR MKTAGACTION(NMR) NOTE MKTAGACTION(NOTE) NPFX MKTAGACTION(NPFX) NSFX MKTAGACTION(NSFX) OBJE MKTAGACTION(OBJE) OCCU MKTAGACTION(OCCU) ORDI MKTAGACTION(ORDI) ORDN MKTAGACTION(ORDN) PAGE MKTAGACTION(PAGE) PEDI MKTAGACTION(PEDI) PHON MKTAGACTION(PHON) PLAC MKTAGACTION(PLAC) POST MKTAGACTION(POST) PROB MKTAGACTION(PROB) PROP MKTAGACTION(PROP) PUBL MKTAGACTION(PUBL) QUAY MKTAGACTION(QUAY) REFN MKTAGACTION(REFN) RELA MKTAGACTION(RELA) RELI MKTAGACTION(RELI) REPO MKTAGACTION(REPO) RESI MKTAGACTION(RESI) RESN MKTAGACTION(RESN) RETI MKTAGACTION(RETI) RFN MKTAGACTION(RFN) RIN MKTAGACTION(RIN) ROLE MKTAGACTION(ROLE) SEX MKTAGACTION(SEX) SLGC MKTAGACTION(SLGC) SLGS MKTAGACTION(SLGS) SOUR MKTAGACTION(SOUR) SPFX MKTAGACTION(SPFX) SSN MKTAGACTION(SSN) STAE MKTAGACTION(STAE) STAT MKTAGACTION(STAT) SUBM MKTAGACTION(SUBM) SUBN MKTAGACTION(SUBN) SURN MKTAGACTION(SURN) TEMP MKTAGACTION(TEMP) TEXT MKTAGACTION(TEXT) TIME MKTAGACTION(TIME) TITL MKTAGACTION(TITL) TRLR MKTAGACTION(TRLR) TYPE MKTAGACTION(TYPE) VERS MKTAGACTION(VERS) WIFE MKTAGACTION(WIFE) WILL MKTAGACTION(WILL) {alphanum}+ { if (strlen(yytext) > MAXGEDCTAGLEN) { gedcom_error("Tag '%s' too long, max %d chars"); return BADTOKEN; } strncpy(string_buf, yytext, MAXGEDCTAGLEN+1); gedcom_lval.string = TO_INTERNAL(string_buf); BEGIN(NORMAL); return USERTAG; } {delim} { gedcom_lval.string = TO_INTERNAL(yytext); return DELIM; } {any_but_delim} { gedcom_lval.string = TO_INTERNAL(yytext); /* Due to character conversions, it is possible that the current character will be combined with the next, and so now we don't have a character yet... This is only applicable to the 1byte case (e.g. ANSEL). */ if (strlen(gedcom_lval.string) > 0) return ANYCHAR; } {escape}/{non_at} { gedcom_lval.string = TO_INTERNAL(yytext); return ESCAPE; } {pointer} { gedcom_lval.string = TO_INTERNAL(yytext); return POINTER; } /* Due to the conversion of level numbers into brackets, the terminator is not important, so no token is returned here. Although not strictly according to the GEDCOM spec, we'll ignore whitespace just before the terminator. */ {gen_delim}*{terminator} { line_no++; BEGIN(INITIAL); } /* Eventually we have to return 1 closing bracket (for the trailer). We can detect whether we have sent the closing bracket using the level_diff (at eof, first it is 2, then we increment it ourselves) */ <> { if (level_diff == 2) { level_diff++; return CLOSE; } else { yyterminate(); } } . { gedcom_error("Unexpected character: '%s' (0x%02x)", yytext, yytext[0]); return BADTOKEN; } %% int yywrap() { return 1; } #ifdef LEXER_TEST 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 "); 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.string); break; case USERTAG: printf("USERTAG(%s) ", gedcom_lval.string); break; default: printf("TAG(%s) ", gedcom_lval.string); break; } tok = gedcom_1byte_lex(); } printf("\n"); close_conv_to_internal(); return 0; } #endif