| 1 |
/// @file |
| 2 |
/// @ingroup common_utils |
| 3 |
/************************************************************************* |
| 4 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* https://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: Details at https://graphviz.org |
| 11 |
*************************************************************************/ |
| 12 |
|
| 13 |
%require "3.0" |
| 14 |
|
| 15 |
/* By default, Bison emits a parser using symbols prefixed with "yy". Graphviz |
| 16 |
* contains multiple Bison-generated parsers, so we alter this prefix to avoid |
| 17 |
* symbol clashes. |
| 18 |
*/ |
| 19 |
%define api.prefix {html} |
| 20 |
|
| 21 |
/* Generate a reentrant parser with no global state */ |
| 22 |
%define api.pure full |
| 23 |
%param { htmlscan_t *scanner } |
| 24 |
|
| 25 |
|
| 26 |
%code requires { |
| 27 |
#include <common/htmllex.h> |
| 28 |
#include <common/htmltable.h> |
| 29 |
#include <common/textspan.h> |
| 30 |
#include <gvc/gvcext.h> |
| 31 |
#include <util/agxbuf.h> |
| 32 |
#include <util/list.h> |
| 33 |
#include <util/strview.h> |
| 34 |
} |
| 35 |
|
| 36 |
%code provides { |
| 37 |
|
| 38 |
static inline void free_ti(textspan_t item) { |
| 39 |
free(item.str); |
| 40 |
} |
| 41 |
|
| 42 |
static inline void free_hi(htextspan_t item) { |
| 43 |
for (size_t i = 0; i < item.nitems; i++) { |
| 44 |
free(item.items[i].str); |
| 45 |
} |
| 46 |
free(item.items); |
| 47 |
} |
| 48 |
|
| 49 |
struct htmlparserstate_s { |
| 50 |
htmllabel_t* lbl; /* Generated label */ |
| 51 |
htmltbl_t* tblstack; /* Stack of tables maintained during parsing */ |
| 52 |
LIST(textspan_t) fitemList; |
| 53 |
LIST(htextspan_t) fspanList; |
| 54 |
agxbuf* str; /* Buffer for text */ |
| 55 |
LIST(textfont_t *) fontstack; |
| 56 |
GVC_t* gvc; |
| 57 |
}; |
| 58 |
|
| 59 |
typedef struct { |
| 60 |
#ifdef HAVE_EXPAT |
| 61 |
struct XML_ParserStruct *parser; |
| 62 |
#endif |
| 63 |
char* ptr; // input source |
| 64 |
int tok; // token type |
| 65 |
agxbuf* xb; // buffer to gather T_string data |
| 66 |
agxbuf lb; // buffer for translating lexical data |
| 67 |
int warn; // set if warning given |
| 68 |
int error; // set if error given |
| 69 |
char inCell; // set if in TD to allow T_string |
| 70 |
char mode; // for handling artificial <HTML>..</HTML> |
| 71 |
strview_t currtok; // for error reporting |
| 72 |
strview_t prevtok; // for error reporting |
| 73 |
GVC_t *gvc; // current GraphViz context |
| 74 |
HTMLSTYPE *htmllval; // generated by htmlparse.y |
| 75 |
} htmllexstate_t; |
| 76 |
|
| 77 |
|
| 78 |
struct htmlscan_s { |
| 79 |
htmllexstate_t lexer; |
| 80 |
htmlparserstate_t parser; |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
%{ |
| 85 |
|
| 86 |
#include <common/render.h> |
| 87 |
#include <common/htmltable.h> |
| 88 |
#include <common/htmllex.h> |
| 89 |
#include <stdbool.h> |
| 90 |
#include <util/alloc.h> |
| 91 |
|
| 92 |
/// Clean up cell if error in parsing. |
| 93 |
static void cleanCell(htmlcell_t *cp); |
| 94 |
|
| 95 |
/// Clean up table if error in parsing. |
| 96 |
static void cleanTbl(htmltbl_t *tp) { |
| 97 |
rows_t *rows = &tp->rows; |
| 98 |
for (size_t r = 0; r < LIST_SIZE(rows); ++r) { |
| 99 |
row_t *rp = LIST_GET(rows, r); |
| 100 |
for (size_t c = 0; c < LIST_SIZE(&rp->rp); ++c) { |
| 101 |
cleanCell(LIST_GET(&rp->rp, c)); |
| 102 |
} |
| 103 |
} |
| 104 |
LIST_FREE(rows); |
| 105 |
free_html_data(&tp->data); |
| 106 |
free(tp); |
| 107 |
} |
| 108 |
|
| 109 |
/// Clean up cell if error in parsing. |
| 110 |
static void |
| 111 |
cleanCell (htmlcell_t* cp) |
| 112 |
{ |
| 113 |
if (cp->child.kind == HTML_TBL) cleanTbl (cp->child.u.tbl); |
| 114 |
else if (cp->child.kind == HTML_TEXT) free_html_text (cp->child.u.txt); |
| 115 |
free_html_data (&cp->data); |
| 116 |
free (cp); |
| 117 |
} |
| 118 |
|
| 119 |
/// Append a new text span to the list. |
| 120 |
static void |
| 121 |
appendFItemList (htmlparserstate_t *html_state, agxbuf *ag); |
| 122 |
|
| 123 |
static void |
| 124 |
appendFLineList (htmlparserstate_t *html_state, int v); |
| 125 |
|
| 126 |
static htmltxt_t* |
| 127 |
mkText(htmlparserstate_t *html_state); |
| 128 |
|
| 129 |
static row_t *lastRow(htmlparserstate_t *html_state); |
| 130 |
|
| 131 |
/// Add new cell row to current table. |
| 132 |
static void addRow(htmlparserstate_t *html_state); |
| 133 |
|
| 134 |
/// Set cell body and type and attach to row |
| 135 |
static void setCell(htmlparserstate_t *html_state, htmlcell_t *cp, void *obj, label_type_t kind); |
| 136 |
|
| 137 |
/// Create label, given body and type. |
| 138 |
static htmllabel_t *mkLabel(void *obj, label_type_t kind) { |
| 139 |
htmllabel_t* lp = gv_alloc(sizeof(htmllabel_t)); |
| 140 |
|
| 141 |
lp->kind = kind; |
| 142 |
if (kind == HTML_TEXT) |
| 143 |
lp->u.txt = obj; |
| 144 |
else |
| 145 |
lp->u.tbl = obj; |
| 146 |
return lp; |
| 147 |
} |
| 148 |
|
| 149 |
/* Called on error. Frees resources allocated during parsing. |
| 150 |
* This includes a label, plus a walk down the stack of |
| 151 |
* tables. Note that `cleanTbl` frees the contained cells. |
| 152 |
*/ |
| 153 |
static void cleanup (htmlparserstate_t *html_state); |
| 154 |
|
| 155 |
/// Return true if s contains a non-space character. |
| 156 |
static bool nonSpace(const char *s) { |
| 157 |
char c; |
| 158 |
|
| 159 |
while ((c = *s++)) { |
| 160 |
if (c != ' ') return true; |
| 161 |
} |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
/// Fonts are allocated in the lexer. |
| 166 |
static void |
| 167 |
pushFont (htmlparserstate_t *html_state, textfont_t *fp); |
| 168 |
|
| 169 |
static void |
| 170 |
popFont (htmlparserstate_t *html_state); |
| 171 |
|
| 172 |
%} |
| 173 |
|
| 174 |
%union { |
| 175 |
int i; |
| 176 |
htmltxt_t* txt; |
| 177 |
htmlcell_t* cell; |
| 178 |
htmltbl_t* tbl; |
| 179 |
textfont_t* font; |
| 180 |
htmlimg_t* img; |
| 181 |
row_t *p; |
| 182 |
} |
| 183 |
|
| 184 |
%token T_end_br T_end_img T_row T_end_row T_html T_end_html |
| 185 |
%token T_end_table T_end_cell T_end_font T_string T_error |
| 186 |
%token T_n_italic T_n_bold T_n_underline T_n_overline T_n_sup T_n_sub T_n_s |
| 187 |
%token T_HR T_hr T_end_hr |
| 188 |
%token T_VR T_vr T_end_vr |
| 189 |
%token <i> T_BR T_br |
| 190 |
%token <img> T_IMG T_img |
| 191 |
%token <tbl> T_table |
| 192 |
%token <cell> T_cell |
| 193 |
%token <font> T_font T_italic T_bold T_underline T_overline T_sup T_sub T_s |
| 194 |
|
| 195 |
%type <txt> fonttext |
| 196 |
%type <cell> cell cells |
| 197 |
%type <i> br |
| 198 |
%type <tbl> table fonttable |
| 199 |
%type <img> image |
| 200 |
%type <p> row rows |
| 201 |
|
| 202 |
%start html |
| 203 |
|
| 204 |
%% |
| 205 |
|
| 206 |
html : T_html fonttext T_end_html { scanner->parser.lbl = mkLabel($2,HTML_TEXT); } |
| 207 |
| T_html fonttable T_end_html { scanner->parser.lbl = mkLabel($2,HTML_TBL); } |
| 208 |
| error { cleanup(&scanner->parser); YYABORT; } |
| 209 |
; |
| 210 |
|
| 211 |
fonttext : text { $$ = mkText(&scanner->parser); } |
| 212 |
; |
| 213 |
|
| 214 |
text : text textitem |
| 215 |
| textitem |
| 216 |
; |
| 217 |
|
| 218 |
textitem : string { appendFItemList(&scanner->parser,scanner->parser.str);} |
| 219 |
| br {appendFLineList(&scanner->parser,$1);} |
| 220 |
| font text n_font |
| 221 |
| italic text n_italic |
| 222 |
| underline text n_underline |
| 223 |
| overline text n_overline |
| 224 |
| bold text n_bold |
| 225 |
| sup text n_sup |
| 226 |
| sub text n_sub |
| 227 |
| strike text n_strike |
| 228 |
; |
| 229 |
|
| 230 |
font : T_font { pushFont (&scanner->parser,$1); } |
| 231 |
; |
| 232 |
|
| 233 |
n_font : T_end_font { popFont (&scanner->parser); } |
| 234 |
; |
| 235 |
|
| 236 |
italic : T_italic {pushFont(&scanner->parser,$1);} |
| 237 |
; |
| 238 |
|
| 239 |
n_italic : T_n_italic {popFont(&scanner->parser);} |
| 240 |
; |
| 241 |
|
| 242 |
bold : T_bold {pushFont(&scanner->parser,$1);} |
| 243 |
; |
| 244 |
|
| 245 |
n_bold : T_n_bold {popFont(&scanner->parser);} |
| 246 |
; |
| 247 |
|
| 248 |
strike : T_s {pushFont(&scanner->parser,$1);} |
| 249 |
; |
| 250 |
|
| 251 |
n_strike : T_n_s {popFont(&scanner->parser);} |
| 252 |
; |
| 253 |
|
| 254 |
underline : T_underline {pushFont(&scanner->parser,$1);} |
| 255 |
; |
| 256 |
|
| 257 |
n_underline : T_n_underline {popFont(&scanner->parser);} |
| 258 |
; |
| 259 |
|
| 260 |
overline : T_overline {pushFont(&scanner->parser,$1);} |
| 261 |
; |
| 262 |
|
| 263 |
n_overline : T_n_overline {popFont(&scanner->parser);} |
| 264 |
; |
| 265 |
|
| 266 |
sup : T_sup {pushFont(&scanner->parser,$1);} |
| 267 |
; |
| 268 |
|
| 269 |
n_sup : T_n_sup {popFont(&scanner->parser);} |
| 270 |
; |
| 271 |
|
| 272 |
sub : T_sub {pushFont(&scanner->parser,$1);} |
| 273 |
; |
| 274 |
|
| 275 |
n_sub : T_n_sub {popFont(&scanner->parser);} |
| 276 |
; |
| 277 |
|
| 278 |
br : T_br T_end_br { $$ = $1; } |
| 279 |
| T_BR { $$ = $1; } |
| 280 |
; |
| 281 |
|
| 282 |
string : T_string |
| 283 |
| string T_string |
| 284 |
; |
| 285 |
|
| 286 |
table : opt_space T_table { |
| 287 |
if (nonSpace(agxbuse(scanner->parser.str))) { |
| 288 |
htmlerror (scanner,"Syntax error: non-space string used before <TABLE>"); |
| 289 |
cleanup(&scanner->parser); YYABORT; |
| 290 |
} |
| 291 |
$2->prev = scanner->parser.tblstack; |
| 292 |
$2->rows = (rows_t){.dtor = free_ritem}; |
| 293 |
scanner->parser.tblstack = $2; |
| 294 |
$2->font = *LIST_BACK(&scanner->parser.fontstack); |
| 295 |
$<tbl>$ = $2; |
| 296 |
} |
| 297 |
rows T_end_table opt_space { |
| 298 |
if (nonSpace(agxbuse(scanner->parser.str))) { |
| 299 |
htmlerror (scanner,"Syntax error: non-space string used after </TABLE>"); |
| 300 |
cleanup(&scanner->parser); YYABORT; |
| 301 |
} |
| 302 |
$$ = scanner->parser.tblstack; |
| 303 |
scanner->parser.tblstack = scanner->parser.tblstack->prev; |
| 304 |
} |
| 305 |
; |
| 306 |
|
| 307 |
fonttable : table { $$ = $1; } |
| 308 |
| font table n_font { $$=$2; } |
| 309 |
| italic table n_italic { $$=$2; } |
| 310 |
| underline table n_underline { $$=$2; } |
| 311 |
| overline table n_overline { $$=$2; } |
| 312 |
| bold table n_bold { $$=$2; } |
| 313 |
; |
| 314 |
|
| 315 |
opt_space : string |
| 316 |
| /* empty*/ |
| 317 |
; |
| 318 |
|
| 319 |
rows : row { $$ = $1; } |
| 320 |
| rows row { $$ = $2; } |
| 321 |
| rows HR row { $1->ruled = true; $$ = $3; } |
| 322 |
; |
| 323 |
|
| 324 |
row : T_row { addRow (&scanner->parser); } cells T_end_row { $$ = lastRow(&scanner->parser); } |
| 325 |
; |
| 326 |
|
| 327 |
cells : cell { $$ = $1; } |
| 328 |
| cells cell { $$ = $2; } |
| 329 |
| cells VR cell { $1->vruled = true; $$ = $3; } |
| 330 |
; |
| 331 |
|
| 332 |
cell : T_cell fonttable { setCell(&scanner->parser,$1,$2,HTML_TBL); } T_end_cell { $$ = $1; } |
| 333 |
| T_cell fonttext { setCell(&scanner->parser,$1,$2,HTML_TEXT); } T_end_cell { $$ = $1; } |
| 334 |
| T_cell image { setCell(&scanner->parser,$1,$2,HTML_IMAGE); } T_end_cell { $$ = $1; } |
| 335 |
| T_cell { setCell(&scanner->parser,$1,mkText(&scanner->parser),HTML_TEXT); } T_end_cell { $$ = $1; } |
| 336 |
; |
| 337 |
|
| 338 |
image : T_img T_end_img { $$ = $1; } |
| 339 |
| T_IMG { $$ = $1; } |
| 340 |
; |
| 341 |
|
| 342 |
HR : T_hr T_end_hr |
| 343 |
| T_HR |
| 344 |
; |
| 345 |
|
| 346 |
VR : T_vr T_end_vr |
| 347 |
| T_VR |
| 348 |
; |
| 349 |
|
| 350 |
|
| 351 |
%% |
| 352 |
|
| 353 |
static void |
| 354 |
appendFItemList (htmlparserstate_t *html_state, agxbuf *ag) |
| 355 |
{ |
| 356 |
const textspan_t ti = {.str = agxbdisown(ag), |
| 357 |
.font = *LIST_BACK(&html_state->fontstack)}; |
| 358 |
LIST_APPEND(&html_state->fitemList, ti); |
| 359 |
} |
| 360 |
|
| 361 |
static void |
| 362 |
appendFLineList (htmlparserstate_t *html_state, int v) |
| 363 |
{ |
| 364 |
htextspan_t lp = {0}; |
| 365 |
|
| 366 |
size_t cnt = LIST_SIZE(&html_state->fitemList); |
| 367 |
lp.just = v; |
| 368 |
if (cnt) { |
| 369 |
lp.nitems = cnt; |
| 370 |
lp.items = gv_calloc(cnt, sizeof(textspan_t)); |
| 371 |
|
| 372 |
for (size_t i = 0; i < LIST_SIZE(&html_state->fitemList); ++i) { |
| 373 |
// move this text span into the new list |
| 374 |
textspan_t *ti = LIST_AT(&html_state->fitemList, i); |
| 375 |
lp.items[i] = *ti; |
| 376 |
*ti = (textspan_t){0}; |
| 377 |
} |
| 378 |
} |
| 379 |
else { |
| 380 |
lp.items = gv_alloc(sizeof(textspan_t)); |
| 381 |
lp.nitems = 1; |
| 382 |
lp.items[0].str = gv_strdup(""); |
| 383 |
lp.items[0].font = *LIST_BACK(&html_state->fontstack); |
| 384 |
} |
| 385 |
|
| 386 |
LIST_CLEAR(&html_state->fitemList); |
| 387 |
|
| 388 |
LIST_APPEND(&html_state->fspanList, lp); |
| 389 |
} |
| 390 |
|
| 391 |
static htmltxt_t* |
| 392 |
mkText(htmlparserstate_t *html_state) |
| 393 |
{ |
| 394 |
htmltxt_t *hft = gv_alloc(sizeof(htmltxt_t)); |
| 395 |
|
| 396 |
if (!LIST_IS_EMPTY(&html_state->fitemList)) |
| 397 |
appendFLineList (html_state, UNSET_ALIGN); |
| 398 |
|
| 399 |
size_t cnt = LIST_SIZE(&html_state->fspanList); |
| 400 |
hft->nspans = cnt; |
| 401 |
|
| 402 |
hft->spans = gv_calloc(cnt, sizeof(htextspan_t)); |
| 403 |
for (size_t i = 0; i < LIST_SIZE(&html_state->fspanList); ++i) { |
| 404 |
// move this HTML text span into the new list |
| 405 |
htextspan_t *hi = LIST_AT(&html_state->fspanList, i); |
| 406 |
hft->spans[i] = *hi; |
| 407 |
*hi = (htextspan_t){0}; |
| 408 |
} |
| 409 |
|
| 410 |
LIST_CLEAR(&html_state->fspanList); |
| 411 |
|
| 412 |
return hft; |
| 413 |
} |
| 414 |
|
| 415 |
static row_t *lastRow(htmlparserstate_t *html_state) { |
| 416 |
htmltbl_t* tbl = html_state->tblstack; |
| 417 |
row_t *sp = *LIST_BACK(&tbl->rows); |
| 418 |
return sp; |
| 419 |
} |
| 420 |
|
| 421 |
static void addRow(htmlparserstate_t *html_state) { |
| 422 |
htmltbl_t* tbl = html_state->tblstack; |
| 423 |
row_t *sp = gv_alloc(sizeof(row_t)); |
| 424 |
if (tbl->hrule) |
| 425 |
sp->ruled = true; |
| 426 |
LIST_APPEND(&tbl->rows, sp); |
| 427 |
} |
| 428 |
|
| 429 |
static void setCell(htmlparserstate_t *html_state, htmlcell_t *cp, void *obj, label_type_t kind) { |
| 430 |
htmltbl_t* tbl = html_state->tblstack; |
| 431 |
row_t *rp = *LIST_BACK(&tbl->rows); |
| 432 |
LIST_APPEND(&rp->rp, cp); |
| 433 |
cp->child.kind = kind; |
| 434 |
if (tbl->vrule) { |
| 435 |
cp->vruled = true; |
| 436 |
cp->hruled = false; |
| 437 |
} |
| 438 |
|
| 439 |
if(kind == HTML_TEXT) |
| 440 |
cp->child.u.txt = obj; |
| 441 |
else if (kind == HTML_IMAGE) |
| 442 |
cp->child.u.img = obj; |
| 443 |
else |
| 444 |
cp->child.u.tbl = obj; |
| 445 |
} |
| 446 |
|
| 447 |
static void cleanup (htmlparserstate_t *html_state) |
| 448 |
{ |
| 449 |
htmltbl_t* tp = html_state->tblstack; |
| 450 |
htmltbl_t* next; |
| 451 |
|
| 452 |
if (html_state->lbl) { |
| 453 |
free_html_label (html_state->lbl,1); |
| 454 |
html_state->lbl = NULL; |
| 455 |
} |
| 456 |
while (tp) { |
| 457 |
next = tp->prev; |
| 458 |
cleanTbl (tp); |
| 459 |
tp = next; |
| 460 |
} |
| 461 |
|
| 462 |
LIST_CLEAR(&html_state->fitemList); |
| 463 |
LIST_CLEAR(&html_state->fspanList); |
| 464 |
|
| 465 |
LIST_FREE(&html_state->fontstack); |
| 466 |
} |
| 467 |
|
| 468 |
static void |
| 469 |
pushFont (htmlparserstate_t *html_state, textfont_t *fp) |
| 470 |
{ |
| 471 |
textfont_t* curfont = *LIST_BACK(&html_state->fontstack); |
| 472 |
textfont_t f = *fp; |
| 473 |
|
| 474 |
if (curfont) { |
| 475 |
if (!f.color && curfont->color) |
| 476 |
f.color = curfont->color; |
| 477 |
if ((f.size < 0.0) && (curfont->size >= 0.0)) |
| 478 |
f.size = curfont->size; |
| 479 |
if (!f.name && curfont->name) |
| 480 |
f.name = curfont->name; |
| 481 |
if (curfont->flags) |
| 482 |
f.flags |= curfont->flags; |
| 483 |
} |
| 484 |
|
| 485 |
textfont_t *const ft = dtinsert(html_state->gvc->textfont_dt, &f); |
| 486 |
LIST_PUSH_BACK(&html_state->fontstack, ft); |
| 487 |
} |
| 488 |
|
| 489 |
static void |
| 490 |
popFont (htmlparserstate_t *html_state) |
| 491 |
{ |
| 492 |
LIST_DROP_BACK(&html_state->fontstack); |
| 493 |
} |
| 494 |
|
| 495 |
/* Return parsed label or NULL if failure. |
| 496 |
* Set warn to 0 on success; 1 for warning message; 2 if no expat; 3 for error |
| 497 |
* message. |
| 498 |
*/ |
| 499 |
htmllabel_t* |
| 500 |
parseHTML (char* txt, int* warn, htmlenv_t *env) |
| 501 |
{ |
| 502 |
agxbuf str = {0}; |
| 503 |
htmllabel_t* l = NULL; |
| 504 |
htmlscan_t scanner = {0}; |
| 505 |
|
| 506 |
LIST_PUSH_BACK(&scanner.parser.fontstack, NULL); |
| 507 |
scanner.parser.fitemList.dtor = free_ti; |
| 508 |
scanner.parser.fspanList.dtor = free_hi; |
| 509 |
scanner.parser.gvc = GD_gvc(env->g); |
| 510 |
scanner.parser.str = &str; |
| 511 |
|
| 512 |
if (initHTMLlexer (&scanner, txt, &str, env)) {/* failed: no libexpat - give up */ |
| 513 |
*warn = 2; |
| 514 |
} |
| 515 |
else { |
| 516 |
htmlparse(&scanner); |
| 517 |
*warn = clearHTMLlexer (&scanner); |
| 518 |
l = scanner.parser.lbl; |
| 519 |
} |
| 520 |
|
| 521 |
LIST_FREE(&scanner.parser.fitemList); |
| 522 |
LIST_FREE(&scanner.parser.fspanList); |
| 523 |
|
| 524 |
LIST_FREE(&scanner.parser.fontstack); |
| 525 |
|
| 526 |
agxbfree (&str); |
| 527 |
|
| 528 |
return l; |
| 529 |
} |