File: biblex.l

package info (click to toggle)
python-bibtex 1.2.2-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 376 kB
  • ctags: 616
  • sloc: ansic: 2,832; yacc: 372; python: 225; lex: 175; makefile: 53
file content (175 lines) | stat: -rw-r--r-- 3,997 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
%{

/*
 This file is part of pybliographer
 
 Copyright (C) 1998-1999 Frederic GOBRY
 Email : gobry@idiap.ch
 	   
 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.
   
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details. 
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 $Id: biblex.l,v 1.1.2.2 2003/09/02 14:35:33 fredgo Exp $
*/

/*  #include "parsername.h" */

#include <string.h>
#include "bibtex.h"
#include "bibparse.h"

static BibtexSource * current_source;

gboolean bibtex_parser_is_content;

extern void bibtex_next_line (void);

#define YY_USER_ACTION  current_source->offset += bibtex_parser_leng;

 
%}

DIGIT    [0-9]+
NAME     [^ \\\t{}\"@,=%#\n\r~]+
BODY 	 [^{}\\\" \n\t\r~]+

%option noyywrap
%x comment entry

%%
		if (YY_START == INITIAL) { BEGIN(comment); }

<comment>^[ \t]*@     	BEGIN(entry); return ('@'); /* Match begin of entry */

<comment>\n		bibtex_next_line (); /* Increment line number */
			
<comment>.


<comment,entry><<EOF>>  {  
    /* Indicate EOF */
    return (end_of_file); 
}

<entry>\\([a-zA-Z]+|[^a-zA-Z]) {
    /* Gestion du caractere \ */

    bibtex_parser_lval.text = g_strdup (bibtex_parser_text); 
    bibtex_tmp_string (bibtex_parser_lval.text);

    return (L_COMMAND); 
}

<entry>{BODY} {
    if (bibtex_parser_is_content) {
	/* Word in the text */
	bibtex_parser_lval.text = bibtex_tmp_string (g_strdup (bibtex_parser_text));

	return L_BODY;
    }

    /* Don't parse it if we are not inside a text field */
    current_source->offset -= bibtex_parser_leng;
    REJECT;
}


<entry>[ \t\n\r~]+ 	{
    /* Spaces handling */
    char * tmp = bibtex_parser_text;
    
    while (* tmp) {
	if (* tmp == '\n') bibtex_next_line ();
	tmp ++;
    }

    if (bibtex_parser_is_content) {
	/* Is it an unbreakable space ? */
	if (strcmp (bibtex_parser_text, "~") == 0) {
	    return L_UBSPACE;
	}
	return L_SPACE;
    }
}


<entry>{DIGIT}	 { 
    /* Lecture d'un nombre */

    bibtex_parser_lval.text = g_strdup (bibtex_parser_text); 
    bibtex_tmp_string (bibtex_parser_lval.text); 

    return (L_DIGIT); 
}


<entry>{NAME} { 
    /* Lecture d'un nom simple */

    bibtex_parser_lval.text = g_strdup (bibtex_parser_text); 
    bibtex_tmp_string (bibtex_parser_lval.text); 

    return (L_NAME); 
}

<entry>. 	{
    return bibtex_parser_text [0];
}
%%

/* Start the parser on the specified source */
void bibtex_parser_initialize (BibtexSource * source) {
    g_return_if_fail (source != NULL);
    
    /* Destroy old buffer */
    if (source->buffer) {
	bibtex_parser__delete_buffer ((YY_BUFFER_STATE) source->buffer);
    }

    switch (source->type) {
    case BIBTEX_SOURCE_FILE:
	source->buffer = (gpointer) 
	    bibtex_parser__create_buffer (source->source.file, 1024);
	break;
	
    case BIBTEX_SOURCE_STRING:
	source->buffer = (gpointer) 
	    bibtex_parser__scan_string (source->source.string);
	break;

    default:
	g_warning ("scanning nothing !");
	source->buffer = NULL;
    }
}

/* Continue parsing on the next entry */
void bibtex_parser_continue (BibtexSource * source) { 
    g_return_if_fail (source != NULL);
    
    current_source = source;
    
    bibtex_parser__switch_to_buffer ((YY_BUFFER_STATE) source->buffer);
    BEGIN (INITIAL); 
}

/* Parsing is over */
void bibtex_parser_finish (BibtexSource * source) {
    g_return_if_fail (source != NULL);
    
    if (source->buffer) {
	bibtex_parser__delete_buffer ((YY_BUFFER_STATE) source->buffer);
	source->buffer = NULL;
    }
}