File tree 4 files changed +52
-24
lines changed
4 files changed +52
-24
lines changed Original file line number Diff line number Diff line change 3
3
4
4
#include " xml_parser.h"
5
5
6
- int yyxmllex ();
7
- extern char *yyxmltext ;
6
+ int yyxmllex (void * );
7
+ char *yyxmlget_text ( void *) ;
8
8
9
- int yyxmlerror (const std::string &error)
9
+ int yyxmlerror (xml_parsert &xml_parser, void *scanner, const std::string &error)
10
10
{
11
- xml_parser.parse_error (error, yyxmltext );
11
+ xml_parser.parse_error (error, yyxmlget_text (scanner) );
12
12
return 0 ;
13
13
}
14
14
@@ -26,6 +26,10 @@ int yyxmlerror(const std::string &error)
26
26
#endif
27
27
%}
28
28
29
+ %parse-param {xml_parsert &xml_parser}
30
+ %parse-param {void *scanner}
31
+ %lex-param {void *scanner}
32
+
29
33
%union {char *s;}
30
34
31
35
%token STARTXMLDECL
Original file line number Diff line number Diff line change 3
3
%option noinput
4
4
%option nounistd
5
5
%option never-interactive
6
+ %option noyywrap
7
+ %option reentrant
8
+ %option extra-type="xml_parsert *"
6
9
7
10
%{
8
11
19
22
#include " xml_parser.h"
20
23
#include " xml_y.tab.h"
21
24
22
- #define PARSER xml_parser
25
+ #define PARSER (*yyextra)
23
26
24
27
// static int keep; /* To store start condition */
25
28
@@ -87,9 +90,5 @@ string \"([^"&]|{esc})*\"|\'([^'&]|{esc})*\'
87
90
<DTD >. {/* skip */ }
88
91
<DTD >\] {close } {BEGIN (INITIAL); /* skip */ }
89
92
90
- . { yyxmlerror (" unexpected character" ); }
93
+ . { yyxmlerror (*yyextra, yyscanner, " unexpected character" ); }
91
94
{nl } {/* skip, must be an extra one at EOF */ ;}
92
-
93
- %%
94
-
95
- int yywrap () { return 1 ; }
Original file line number Diff line number Diff line change 10
10
11
11
#include < fstream>
12
12
13
- xml_parsert xml_parser;
13
+ int xml_parsert::instance_count = 0 ;
14
+
15
+ int yyxmllex_init_extra (xml_parsert *, void **);
16
+ int yyxmllex_destroy (void *);
17
+ int yyxmlparse (xml_parsert &, void *);
18
+
19
+ bool xml_parsert::parse ()
20
+ {
21
+ void *scanner;
22
+ yyxmllex_init_extra (this , &scanner);
23
+ bool parse_fail = yyxmlparse (*this , scanner) != 0 ;
24
+ yyxmllex_destroy (scanner);
25
+ return parse_fail;
26
+ }
14
27
15
28
// 'do it all' function
16
29
bool parse_xml (
@@ -19,12 +32,12 @@ bool parse_xml(
19
32
message_handlert &message_handler,
20
33
xmlt &dest)
21
34
{
22
- xml_parser.clear ();
35
+ xml_parsert xml_parser{message_handler};
36
+
23
37
xml_parser.set_file (filename);
24
38
xml_parser.in =∈
25
- xml_parser.log .set_message_handler (message_handler);
26
39
27
- bool result= yyxmlparse ()!= 0 ;
40
+ bool result = xml_parser. parse () ;
28
41
29
42
// save result
30
43
xml_parser.parse_tree .element .swap (dest);
Original file line number Diff line number Diff line change 14
14
15
15
#include " xml_parse_tree.h"
16
16
17
- int yyxmlparse ();
18
-
19
17
class xml_parsert :public parsert
20
18
{
21
19
public:
20
+ explicit xml_parsert (message_handlert &message_handler)
21
+ : parsert(message_handler)
22
+ {
23
+ // Simplistic check that we don't attempt to do reentrant parsing as the
24
+ // Bison-generated parser has global state.
25
+ PRECONDITION (++instance_count == 1 );
26
+ clear ();
27
+ }
28
+
29
+ xml_parsert (const xml_parsert &) = delete ;
30
+
31
+ ~xml_parsert () override
32
+ {
33
+ --instance_count;
34
+ }
35
+
22
36
xml_parse_treet parse_tree;
23
37
24
38
std::list<xmlt *> stack;
@@ -28,29 +42,27 @@ class xml_parsert:public parsert
28
42
return *stack.back ();
29
43
}
30
44
31
- virtual bool parse ()
32
- {
33
- return yyxmlparse ()!=0 ;
34
- }
45
+ bool parse () override ;
35
46
36
47
void new_level ()
37
48
{
38
49
current ().elements .push_back (xmlt ());
39
50
stack.push_back (¤t ().elements .back ());
40
51
}
41
52
42
- virtual void clear ()
53
+ void clear () override
43
54
{
44
55
parse_tree.clear ();
45
56
// set up stack
46
57
stack.clear ();
47
58
stack.push_back (&parse_tree.element );
48
59
}
49
- };
50
60
51
- extern xml_parsert xml_parser;
61
+ protected:
62
+ static int instance_count;
63
+ };
52
64
53
- int yyxmlerror (const std::string &error );
65
+ int yyxmlerror (xml_parsert &, void *, const std::string &);
54
66
55
67
// 'do it all' functions
56
68
bool parse_xml (
You can’t perform that action at this time.
0 commit comments