-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardcoded_xml_to_json.flex
138 lines (112 loc) · 3.57 KB
/
hardcoded_xml_to_json.flex
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
%%
%byaccj
%{
private Parser yyparser;
public Yylex(java.io.Reader r, Parser yyparser) {
this(r);
this.yyparser = yyparser;
}
%}
%x OPENING_TAG CLOSING_TAG
NL = [\s\r\n\t]+
VERSION = "<?xml version=\'1.0\' encoding=\'UTF-8\'?>"
DOCTYPE = "<\!DOCTYPE book SYSTEM \"book.dtd\">"
STRING = (\"[a-zA-Z0-9\-_ .]+\")
TEXT = [a-zA-Z0-9\-_,.;: \r\n\t\a\b]+
START_TAG = [<]
END_TAG = "/>"
CLOSE_TAG = [>]
OPEN_CLOSE = "</"
ATT_ID = (" id=")
ATT_TITLE = (" title=")
ATT_CAPTION = (" caption=")
ATT_PATH = (" path=")
ATT_EDITION = (" edition=")
%%
<YYINITIAL,OPENING_TAG,CLOSING_TAG>{NL} {}
{VERSION} {System.out.println("Debug: version");
return Parser.VERSION;}
{DOCTYPE} {return Parser.DOCTYPE;}
{START_TAG} { System.out.println("Debug: <");
yybegin(OPENING_TAG);
return Parser.START_TAG;}
{TEXT} {
System.out.println("Debug: text");
yyparser.yylval = new ParserVal(yytext());
return Parser.TEXT;}
{OPEN_CLOSE} { System.out.println("Debug: </");
yybegin(CLOSING_TAG);
return Parser.OPEN_CLOSE;}
<OPENING_TAG>{
"book" {
System.out.println("Debug: book");
return Parser.BOOK;}
"dedication" {System.out.println("Debug: dedication");
return Parser.DEDICATION;}
"preface" { System.out.println("Debug: preface");
return Parser.PREFACE;}
"part" { System.out.println("Debug: part");
return Parser.PART;}
"toc" { System.out.println("Debug: toc");
return Parser.TOC;}
"lof" {System.out.println("Debug: lof");
return Parser.LOF;}
"lot" {System.out.println("Debug: lot");
return Parser.LOT;}
"item" {System.out.println("Debug: item");
return Parser.ITEM;}
"chapter" {System.out.println("Debug: chapter");
return Parser.CHAPTER;}
"section" {System.out.println("Debug: section");
return Parser.SECTION;}
"figure" {System.out.println("Debug: figure");
return Parser.FIGURE;}
"table" {System.out.println("Debug: table");
return Parser.TABLE;}
"row" {System.out.println("Debug: row");
return Parser.ROW;}
"cell" {System.out.println("Debug: cell");
return Parser.CELL;}
"authornotes" {System.out.println("Debug: authornotes");
return Parser.AUTHORNOTES;}
"note" {System.out.println("Debug: note");
return Parser.NOTE;}
{END_TAG} { System.out.println("Debug: />");
yybegin(YYINITIAL);
return Parser.END_TAG;}
{CLOSE_TAG} { System.out.println("Debug: >");
yybegin(YYINITIAL);
return Parser.CLOSE_TAG;}
{ATT_EDITION} {return Parser.ATT_EDITION;}
{ATT_ID} {return Parser.ATT_ID;}
{ATT_TITLE} {return Parser.ATT_TITLE;}
{ATT_CAPTION} {return Parser.ATT_CAPTION;}
{ATT_PATH} {return Parser.ATT_PATH;}
{STRING} {System.out.println("Debug: string");
yyparser.yylval = new ParserVal(yytext());
return Parser.STRING;}
}
<CLOSING_TAG>{
"book" {return Parser.BOOK;}
"dedication" {return Parser.DEDICATION;}
"preface" {return Parser.PREFACE;}
"part" {return Parser.PART;}
"toc" {return Parser.TOC;}
"lof" {return Parser.LOF;}
"lot" {return Parser.LOT;}
"item" {return Parser.ITEM;}
"chapter" {return Parser.CHAPTER;}
"section" {return Parser.SECTION;}
"figure" {return Parser.FIGURE;}
"table" {return Parser.TABLE;}
"row" {return Parser.ROW;}
"cell" {return Parser.CELL;}
"authornotes" {return Parser.AUTHORNOTES;}
"note" {return Parser.NOTE;}
{CLOSE_TAG} {yybegin(YYINITIAL);
return Parser.CLOSE_TAG;}
}
/* whitespace */
[ \t]+ { }
/* error fallback */
[^] { System.out.println("Error: unexpected character '"+yytext()+"'"); return -1; }