| 1 |
// =========================================================================== |
| 2 |
// |
| 3 |
// PUBLIC DOMAIN NOTICE |
| 4 |
// National Center for Biotechnology Information (NCBI) |
| 5 |
// |
| 6 |
// This software/database is a "United States Government Work" under the |
| 7 |
// terms of the United States Copyright Act. It was written as part of |
| 8 |
// the author's official duties as a United States Government employee and |
| 9 |
// thus cannot be copyrighted. This software/database is freely available |
| 10 |
// to the public for use. The National Library of Medicine and the U.S. |
| 11 |
// Government do not place any restriction on its use or reproduction. |
| 12 |
// We would, however, appreciate having the NCBI and the author cited in |
| 13 |
// any work or product based on this material. |
| 14 |
// |
| 15 |
// Although all reasonable efforts have been taken to ensure the accuracy |
| 16 |
// and reliability of the software and data, the NLM and the U.S. |
| 17 |
// Government do not and cannot warrant the performance or results that |
| 18 |
// may be obtained by using this software or data. The NLM and the U.S. |
| 19 |
// Government disclaim all warranties, express or implied, including |
| 20 |
// warranties of performance, merchantability or fitness for any particular |
| 21 |
// purpose. |
| 22 |
// |
| 23 |
// =========================================================================== |
| 24 |
// |
| 25 |
// File Name: xparse.go |
| 26 |
// |
| 27 |
// Author: Jonathan Kans |
| 28 |
// |
| 29 |
// ========================================================================== |
| 30 |
|
| 31 |
package eutils |
| 32 |
|
| 33 |
import ( |
| 34 |
"fmt" |
| 35 |
"os" |
| 36 |
"strconv" |
| 37 |
"strings" |
| 38 |
"sync" |
| 39 |
) |
| 40 |
|
| 41 |
// SUPPORT CODE FOR xtract PARSING AND OBJECT PRINTING |
| 42 |
|
| 43 |
// TYPED CONSTANTS |
| 44 |
|
| 45 |
// LevelType is the integer type for exploration arguments |
| 46 |
type LevelType int |
| 47 |
|
| 48 |
// LevelType keys for exploration arguments |
| 49 |
const ( |
| 50 |
_ LevelType = iota |
| 51 |
UNIT |
| 52 |
SUBSETX |
| 53 |
SUBSET |
| 54 |
SECTIONX |
| 55 |
SECTION |
| 56 |
BLOCKX |
| 57 |
BLOCK |
| 58 |
BRANCHX |
| 59 |
BRANCH |
| 60 |
GROUPX |
| 61 |
GROUP |
| 62 |
DIVISIONX |
| 63 |
DIVISION |
| 64 |
PATHX |
| 65 |
PATH |
| 66 |
PATTERN |
| 67 |
) |
| 68 |
|
| 69 |
// IndentType is the integer type for XML formatting |
| 70 |
type IndentType int |
| 71 |
|
| 72 |
// IndentType keys for XML formatting |
| 73 |
const ( |
| 74 |
SINGULARITY IndentType = iota |
| 75 |
COMPACT |
| 76 |
FLUSH |
| 77 |
INDENT |
| 78 |
SUBTREE |
| 79 |
WRAPPED |
| 80 |
) |
| 81 |
|
| 82 |
// OpType is the integer type for operations |
| 83 |
type OpType int |
| 84 |
|
| 85 |
// OpType keys for operations |
| 86 |
const ( |
| 87 |
UNSET OpType = iota |
| 88 |
ELEMENT |
| 89 |
FIRST |
| 90 |
LAST |
| 91 |
EVEN |
| 92 |
ODD |
| 93 |
BACKWARD |
| 94 |
ENCODE |
| 95 |
DECODE |
| 96 |
UPPER |
| 97 |
LOWER |
| 98 |
CHAIN |
| 99 |
TITLE |
| 100 |
MIRROR |
| 101 |
ALPHA |
| 102 |
ALNUM |
| 103 |
BASIC |
| 104 |
PLAIN |
| 105 |
SIMPLE |
| 106 |
AUTHOR |
| 107 |
PROSE |
| 108 |
JOURNAL |
| 109 |
YEAR |
| 110 |
MONTH |
| 111 |
DATE |
| 112 |
PAGE |
| 113 |
AUTH |
| 114 |
INITIALS |
| 115 |
PROP |
| 116 |
TRIM |
| 117 |
WCT |
| 118 |
DOI |
| 119 |
TRANSLATE |
| 120 |
REPLACE |
| 121 |
TERMS |
| 122 |
WORDS |
| 123 |
PAIRS |
| 124 |
PAIRX |
| 125 |
SPLIT |
| 126 |
ORDER |
| 127 |
REVERSE |
| 128 |
LETTERS |
| 129 |
PENTAMERS |
| 130 |
CLAUSES |
| 131 |
INDEXER |
| 132 |
MESHCODE |
| 133 |
MATRIX |
| 134 |
CLASSIFY |
| 135 |
HISTOGRAM |
| 136 |
FREQUENCY |
| 137 |
ACCENTED |
| 138 |
TEST |
| 139 |
SCAN |
| 140 |
PFX |
| 141 |
SFX |
| 142 |
SEP |
| 143 |
TAB |
| 144 |
RET |
| 145 |
LBL |
| 146 |
TAG |
| 147 |
ATT |
| 148 |
ATR |
| 149 |
CLS |
| 150 |
SLF |
| 151 |
END |
| 152 |
CLR |
| 153 |
PFC |
| 154 |
DEQ |
| 155 |
PLG |
| 156 |
ELG |
| 157 |
FWD |
| 158 |
AWD |
| 159 |
WRP |
| 160 |
BKT |
| 161 |
ENC |
| 162 |
PKG |
| 163 |
RST |
| 164 |
DEF |
| 165 |
MSK |
| 166 |
REG |
| 167 |
EXP |
| 168 |
WITH |
| 169 |
GCODE |
| 170 |
FRAME0 |
| 171 |
FRAME1 |
| 172 |
COLOR |
| 173 |
POSITION |
| 174 |
SELECT |
| 175 |
IF |
| 176 |
UNLESS |
| 177 |
MATCH |
| 178 |
AVOID |
| 179 |
AND |
| 180 |
OR |
| 181 |
EQUALS |
| 182 |
CONTAINS |
| 183 |
MIMICS |
| 184 |
INCLUDES |
| 185 |
EXCLUDES |
| 186 |
ISWITHIN |
| 187 |
STARTSWITH |
| 188 |
ENDSWITH |
| 189 |
ISNOT |
| 190 |
ISBEFORE |
| 191 |
ISAFTER |
| 192 |
CONSISTSOF |
| 193 |
MATCHES |
| 194 |
RESEMBLES |
| 195 |
ISEQUALTO |
| 196 |
DIFFERSFROM |
| 197 |
GT |
| 198 |
GE |
| 199 |
LT |
| 200 |
LE |
| 201 |
EQ |
| 202 |
NE |
| 203 |
NUM |
| 204 |
LEN |
| 205 |
SUM |
| 206 |
ACC |
| 207 |
MIN |
| 208 |
MAX |
| 209 |
INC |
| 210 |
DEC |
| 211 |
SUB |
| 212 |
AVG |
| 213 |
DEV |
| 214 |
MED |
| 215 |
MUL |
| 216 |
DIV |
| 217 |
MOD |
| 218 |
GEO |
| 219 |
HRM |
| 220 |
RMS |
| 221 |
SQT |
| 222 |
LG2 |
| 223 |
LGE |
| 224 |
LOG |
| 225 |
BIN |
| 226 |
OCT |
| 227 |
HEX |
| 228 |
BIT |
| 229 |
PAD |
| 230 |
RAW |
| 231 |
ZEROBASED |
| 232 |
ONEBASED |
| 233 |
UCSCBASED |
| 234 |
NUCLEIC |
| 235 |
REVCOMP |
| 236 |
FASTA |
| 237 |
NCBI2NA |
| 238 |
NCBI4NA |
| 239 |
CDS2PROT |
| 240 |
PEPTS |
| 241 |
MOLWTX |
| 242 |
MOLWTM |
| 243 |
MOLWTF |
| 244 |
ACCESSION |
| 245 |
NUMERIC |
| 246 |
HGVS |
| 247 |
ELSE |
| 248 |
VARIABLE |
| 249 |
ACCUMULATOR |
| 250 |
CONVERTER |
| 251 |
VALUE |
| 252 |
QUESTION |
| 253 |
TILDE |
| 254 |
STAR |
| 255 |
DOT |
| 256 |
PRCNT |
| 257 |
DOLLAR |
| 258 |
ATSIGN |
| 259 |
COUNT |
| 260 |
LENGTH |
| 261 |
DEPTH |
| 262 |
INDEX |
| 263 |
INCR |
| 264 |
DECR |
| 265 |
UNRECOGNIZED |
| 266 |
) |
| 267 |
|
| 268 |
// ArgumentType is the integer type for argument classification |
| 269 |
type ArgumentType int |
| 270 |
|
| 271 |
// ArgumentType keys for argument classification |
| 272 |
const ( |
| 273 |
_ ArgumentType = iota |
| 274 |
EXPLORATION |
| 275 |
CONDITIONAL |
| 276 |
EXTRACTION |
| 277 |
CUSTOMIZATION |
| 278 |
) |
| 279 |
|
| 280 |
// RangeType is the integer type for element range choices |
| 281 |
type RangeType int |
| 282 |
|
| 283 |
// RangeType keys for element range choices |
| 284 |
const ( |
| 285 |
NORANGE RangeType = iota |
| 286 |
STRINGRANGE |
| 287 |
VARIABLERANGE |
| 288 |
INTEGERRANGE |
| 289 |
) |
| 290 |
|
| 291 |
// SeqEndType is used for -ucsc-based decisions |
| 292 |
type SeqEndType int |
| 293 |
|
| 294 |
// SeqEndType keys for -ucsc-based decisions |
| 295 |
const ( |
| 296 |
_ SeqEndType = iota |
| 297 |
ISSTART |
| 298 |
ISSTOP |
| 299 |
ISPOS |
| 300 |
) |
| 301 |
|
| 302 |
// SequenceType is used to record XML tag and position for -ucsc-based |
| 303 |
type SequenceType struct { |
| 304 |
Based int |
| 305 |
Which SeqEndType |
| 306 |
} |
| 307 |
|
| 308 |
// MUTEXES |
| 309 |
|
| 310 |
var hlock sync.Mutex |
| 311 |
|
| 312 |
var slock sync.RWMutex |
| 313 |
|
| 314 |
// ARGUMENT MAPS |
| 315 |
|
| 316 |
var argTypeIs = map[string]ArgumentType{ |
| 317 |
"-unit": EXPLORATION, |
| 318 |
"-Unit": EXPLORATION, |
| 319 |
"-subsetX": EXPLORATION, |
| 320 |
"-SubsetX": EXPLORATION, |
| 321 |
"-subset": EXPLORATION, |
| 322 |
"-Subset": EXPLORATION, |
| 323 |
"-sectionX": EXPLORATION, |
| 324 |
"-SectionX": EXPLORATION, |
| 325 |
"-section": EXPLORATION, |
| 326 |
"-Section": EXPLORATION, |
| 327 |
"-blockX": EXPLORATION, |
| 328 |
"-BlockX": EXPLORATION, |
| 329 |
"-block": EXPLORATION, |
| 330 |
"-Block": EXPLORATION, |
| 331 |
"-branchX": EXPLORATION, |
| 332 |
"-BranchX": EXPLORATION, |
| 333 |
"-branch": EXPLORATION, |
| 334 |
"-Branch": EXPLORATION, |
| 335 |
"-groupX": EXPLORATION, |
| 336 |
"-GroupX": EXPLORATION, |
| 337 |
"-group": EXPLORATION, |
| 338 |
"-Group": EXPLORATION, |
| 339 |
"-divisionX": EXPLORATION, |
| 340 |
"-DivisionX": EXPLORATION, |
| 341 |
"-division": EXPLORATION, |
| 342 |
"-Division": EXPLORATION, |
| 343 |
"-pathX": EXPLORATION, |
| 344 |
"-PathX": EXPLORATION, |
| 345 |
"-path": EXPLORATION, |
| 346 |
"-Path": EXPLORATION, |
| 347 |
"-pattern": EXPLORATION, |
| 348 |
"-Pattern": EXPLORATION, |
| 349 |
"-position": CONDITIONAL, |
| 350 |
"-select": CONDITIONAL, |
| 351 |
"-if": CONDITIONAL, |
| 352 |
"-unless": CONDITIONAL, |
| 353 |
"-match": CONDITIONAL, |
| 354 |
"-avoid": CONDITIONAL, |
| 355 |
"-and": CONDITIONAL, |
| 356 |
"-or": CONDITIONAL, |
| 357 |
"-equals": CONDITIONAL, |
| 358 |
"-contains": CONDITIONAL, |
| 359 |
"-mimics": CONDITIONAL, |
| 360 |
"-includes": CONDITIONAL, |
| 361 |
"-excludes": CONDITIONAL, |
| 362 |
"-is-within": CONDITIONAL, |
| 363 |
"-starts-with": CONDITIONAL, |
| 364 |
"-ends-with": CONDITIONAL, |
| 365 |
"-is-not": CONDITIONAL, |
| 366 |
"-is-before": CONDITIONAL, |
| 367 |
"-is-after": CONDITIONAL, |
| 368 |
"-consists-of": CONDITIONAL, |
| 369 |
"-matches": CONDITIONAL, |
| 370 |
"-resembles": CONDITIONAL, |
| 371 |
"-is-equal-to": CONDITIONAL, |
| 372 |
"-differs-from": CONDITIONAL, |
| 373 |
"-gt": CONDITIONAL, |
| 374 |
"-ge": CONDITIONAL, |
| 375 |
"-lt": CONDITIONAL, |
| 376 |
"-le": CONDITIONAL, |
| 377 |
"-eq": CONDITIONAL, |
| 378 |
"-ne": CONDITIONAL, |
| 379 |
"-element": EXTRACTION, |
| 380 |
"-first": EXTRACTION, |
| 381 |
"-last": EXTRACTION, |
| 382 |
"-even": EXTRACTION, |
| 383 |
"-odd": EXTRACTION, |
| 384 |
"-backward": EXTRACTION, |
| 385 |
"-encode": EXTRACTION, |
| 386 |
"-decode": EXTRACTION, |
| 387 |
"-upper": EXTRACTION, |
| 388 |
"-lower": EXTRACTION, |
| 389 |
"-chain": EXTRACTION, |
| 390 |
"-title": EXTRACTION, |
| 391 |
"-mirror": EXTRACTION, |
| 392 |
"-alpha": EXTRACTION, |
| 393 |
"-alnum": EXTRACTION, |
| 394 |
"-basic": EXTRACTION, |
| 395 |
"-plain": EXTRACTION, |
| 396 |
"-simple": EXTRACTION, |
| 397 |
"-author": EXTRACTION, |
| 398 |
"-prose": EXTRACTION, |
| 399 |
"-jour": EXTRACTION, |
| 400 |
"-journal": EXTRACTION, |
| 401 |
"-year": EXTRACTION, |
| 402 |
"-month": EXTRACTION, |
| 403 |
"-date": EXTRACTION, |
| 404 |
"-page": EXTRACTION, |
| 405 |
"-auth": EXTRACTION, |
| 406 |
"-initials": EXTRACTION, |
| 407 |
"-prop": EXTRACTION, |
| 408 |
"-trim": EXTRACTION, |
| 409 |
"-wct": EXTRACTION, |
| 410 |
"-doi": EXTRACTION, |
| 411 |
"-translate": EXTRACTION, |
| 412 |
"-replace": EXTRACTION, |
| 413 |
"-terms": EXTRACTION, |
| 414 |
"-words": EXTRACTION, |
| 415 |
"-pairs": EXTRACTION, |
| 416 |
"-pairx": EXTRACTION, |
| 417 |
"-split": EXTRACTION, |
| 418 |
"-order": EXTRACTION, |
| 419 |
"-reverse": EXTRACTION, |
| 420 |
"-letters": EXTRACTION, |
| 421 |
"-pentamers": EXTRACTION, |
| 422 |
"-clauses": EXTRACTION, |
| 423 |
"-indexer": EXTRACTION, |
| 424 |
"-meshcode": EXTRACTION, |
| 425 |
"-matrix": EXTRACTION, |
| 426 |
"-classify": EXTRACTION, |
| 427 |
"-histogram": EXTRACTION, |
| 428 |
"-frequency": EXTRACTION, |
| 429 |
"-accented": EXTRACTION, |
| 430 |
"-test": EXTRACTION, |
| 431 |
"-scan": EXTRACTION, |
| 432 |
"-num": EXTRACTION, |
| 433 |
"-len": EXTRACTION, |
| 434 |
"-sum": EXTRACTION, |
| 435 |
"-acc": EXTRACTION, |
| 436 |
"-min": EXTRACTION, |
| 437 |
"-max": EXTRACTION, |
| 438 |
"-inc": EXTRACTION, |
| 439 |
"-dec": EXTRACTION, |
| 440 |
"-sub": EXTRACTION, |
| 441 |
"-avg": EXTRACTION, |
| 442 |
"-dev": EXTRACTION, |
| 443 |
"-med": EXTRACTION, |
| 444 |
"-mul": EXTRACTION, |
| 445 |
"-div": EXTRACTION, |
| 446 |
"-mod": EXTRACTION, |
| 447 |
"-geo": EXTRACTION, |
| 448 |
"-hrm": EXTRACTION, |
| 449 |
"-rms": EXTRACTION, |
| 450 |
"-sqt": EXTRACTION, |
| 451 |
"-lg2": EXTRACTION, |
| 452 |
"-lge": EXTRACTION, |
| 453 |
"-log": EXTRACTION, |
| 454 |
"-bin": EXTRACTION, |
| 455 |
"-oct": EXTRACTION, |
| 456 |
"-hex": EXTRACTION, |
| 457 |
"-bit": EXTRACTION, |
| 458 |
"-pad": EXTRACTION, |
| 459 |
"-raw": EXTRACTION, |
| 460 |
"-0-based": EXTRACTION, |
| 461 |
"-zero-based": EXTRACTION, |
| 462 |
"-1-based": EXTRACTION, |
| 463 |
"-one-based": EXTRACTION, |
| 464 |
"-ucsc": EXTRACTION, |
| 465 |
"-ucsc-based": EXTRACTION, |
| 466 |
"-ucsc-coords": EXTRACTION, |
| 467 |
"-bed-based": EXTRACTION, |
| 468 |
"-bed-coords": EXTRACTION, |
| 469 |
"-nucleic": EXTRACTION, |
| 470 |
"-revcomp": EXTRACTION, |
| 471 |
"-fasta": EXTRACTION, |
| 472 |
"-ncbi2na": EXTRACTION, |
| 473 |
"-ncbi4na": EXTRACTION, |
| 474 |
"-cds2prot": EXTRACTION, |
| 475 |
"-pept": EXTRACTION, |
| 476 |
"-molwt": EXTRACTION, |
| 477 |
"-molwt-x": EXTRACTION, |
| 478 |
"-molwt-m": EXTRACTION, |
| 479 |
"-molwt-f": EXTRACTION, |
| 480 |
"-accession": EXTRACTION, |
| 481 |
"-numeric": EXTRACTION, |
| 482 |
"-hgvs": EXTRACTION, |
| 483 |
"-else": EXTRACTION, |
| 484 |
"-pfx": CUSTOMIZATION, |
| 485 |
"-sfx": CUSTOMIZATION, |
| 486 |
"-sep": CUSTOMIZATION, |
| 487 |
"-tab": CUSTOMIZATION, |
| 488 |
"-ret": CUSTOMIZATION, |
| 489 |
"-lbl": CUSTOMIZATION, |
| 490 |
"-tag": CUSTOMIZATION, |
| 491 |
"-att": CUSTOMIZATION, |
| 492 |
"-atr": CUSTOMIZATION, |
| 493 |
"-cls": CUSTOMIZATION, |
| 494 |
"-slf": CUSTOMIZATION, |
| 495 |
"-end": CUSTOMIZATION, |
| 496 |
"-clr": CUSTOMIZATION, |
| 497 |
"-pfc": CUSTOMIZATION, |
| 498 |
"-deq": CUSTOMIZATION, |
| 499 |
"-plg": CUSTOMIZATION, |
| 500 |
"-elg": CUSTOMIZATION, |
| 501 |
"-fwd": CUSTOMIZATION, |
| 502 |
"-awd": CUSTOMIZATION, |
| 503 |
"-wrp": CUSTOMIZATION, |
| 504 |
"-bkt": CUSTOMIZATION, |
| 505 |
"-enc": CUSTOMIZATION, |
| 506 |
"-pkg": CUSTOMIZATION, |
| 507 |
"-rst": CUSTOMIZATION, |
| 508 |
"-def": CUSTOMIZATION, |
| 509 |
"-mask": CUSTOMIZATION, |
| 510 |
"-reg": CUSTOMIZATION, |
| 511 |
"-exp": CUSTOMIZATION, |
| 512 |
"-with": CUSTOMIZATION, |
| 513 |
"-gcode": CUSTOMIZATION, |
| 514 |
"-frame": CUSTOMIZATION, |
| 515 |
"-frame0": CUSTOMIZATION, |
| 516 |
"-frame-0": CUSTOMIZATION, |
| 517 |
"-frame1": CUSTOMIZATION, |
| 518 |
"-frame-1": CUSTOMIZATION, |
| 519 |
"-color": CUSTOMIZATION, |
| 520 |
} |
| 521 |
|
| 522 |
var levTypeIs = map[string]LevelType{ |
| 523 |
"-unit": UNIT, |
| 524 |
"-subset": SUBSET, |
| 525 |
"-section": SECTION, |
| 526 |
"-block": BLOCK, |
| 527 |
"-branch": BRANCH, |
| 528 |
"-group": GROUP, |
| 529 |
"-division": DIVISION, |
| 530 |
"-path": PATH, |
| 531 |
"-pattern": PATTERN, |
| 532 |
"-record": PATTERN, |
| 533 |
} |
| 534 |
|
| 535 |
var opTypeIs = map[string]OpType{ |
| 536 |
"-element": ELEMENT, |
| 537 |
"-first": FIRST, |
| 538 |
"-last": LAST, |
| 539 |
"-even": EVEN, |
| 540 |
"-odd": ODD, |
| 541 |
"-backward": BACKWARD, |
| 542 |
"-encode": ENCODE, |
| 543 |
"-decode": DECODE, |
| 544 |
"-upper": UPPER, |
| 545 |
"-lower": LOWER, |
| 546 |
"-chain": CHAIN, |
| 547 |
"-title": TITLE, |
| 548 |
"-mirror": MIRROR, |
| 549 |
"-alpha": ALPHA, |
| 550 |
"-alnum": ALNUM, |
| 551 |
"-basic": BASIC, |
| 552 |
"-plain": PLAIN, |
| 553 |
"-simple": SIMPLE, |
| 554 |
"-author": AUTHOR, |
| 555 |
"-prose": PROSE, |
| 556 |
"-jour": JOURNAL, |
| 557 |
"-journal": JOURNAL, |
| 558 |
"-year": YEAR, |
| 559 |
"-month": MONTH, |
| 560 |
"-date": DATE, |
| 561 |
"-page": PAGE, |
| 562 |
"-auth": AUTH, |
| 563 |
"-initials": INITIALS, |
| 564 |
"-prop": PROP, |
| 565 |
"-trim": TRIM, |
| 566 |
"-wct": WCT, |
| 567 |
"-doi": DOI, |
| 568 |
"-translate": TRANSLATE, |
| 569 |
"-replace": REPLACE, |
| 570 |
"-terms": TERMS, |
| 571 |
"-words": WORDS, |
| 572 |
"-pairs": PAIRS, |
| 573 |
"-pairx": PAIRX, |
| 574 |
"-split": SPLIT, |
| 575 |
"-order": ORDER, |
| 576 |
"-reverse": REVERSE, |
| 577 |
"-letters": LETTERS, |
| 578 |
"-pentamers": PENTAMERS, |
| 579 |
"-clauses": CLAUSES, |
| 580 |
"-indexer": INDEXER, |
| 581 |
"-meshcode": MESHCODE, |
| 582 |
"-matrix": MATRIX, |
| 583 |
"-classify": CLASSIFY, |
| 584 |
"-histogram": HISTOGRAM, |
| 585 |
"-frequency": FREQUENCY, |
| 586 |
"-accented": ACCENTED, |
| 587 |
"-test": TEST, |
| 588 |
"-scan": SCAN, |
| 589 |
"-pfx": PFX, |
| 590 |
"-sfx": SFX, |
| 591 |
"-sep": SEP, |
| 592 |
"-tab": TAB, |
| 593 |
"-ret": RET, |
| 594 |
"-lbl": LBL, |
| 595 |
"-tag": TAG, |
| 596 |
"-att": ATT, |
| 597 |
"-atr": ATR, |
| 598 |
"-cls": CLS, |
| 599 |
"-slf": SLF, |
| 600 |
"-end": END, |
| 601 |
"-clr": CLR, |
| 602 |
"-pfc": PFC, |
| 603 |
"-deq": DEQ, |
| 604 |
"-plg": PLG, |
| 605 |
"-elg": ELG, |
| 606 |
"-fwd": FWD, |
| 607 |
"-awd": AWD, |
| 608 |
"-wrp": WRP, |
| 609 |
"-bkt": BKT, |
| 610 |
"-enc": ENC, |
| 611 |
"-pkg": PKG, |
| 612 |
"-rst": RST, |
| 613 |
"-def": DEF, |
| 614 |
"-mask": MSK, |
| 615 |
"-reg": REG, |
| 616 |
"-exp": EXP, |
| 617 |
"-with": WITH, |
| 618 |
"-gcode": GCODE, |
| 619 |
"-frame": FRAME1, |
| 620 |
"-frame0": FRAME0, |
| 621 |
"-frame-0": FRAME0, |
| 622 |
"-frame1": FRAME1, |
| 623 |
"-frame-1": FRAME1, |
| 624 |
"-color": COLOR, |
| 625 |
"-position": POSITION, |
| 626 |
"-select": SELECT, |
| 627 |
"-if": IF, |
| 628 |
"-unless": UNLESS, |
| 629 |
"-match": MATCH, |
| 630 |
"-avoid": AVOID, |
| 631 |
"-and": AND, |
| 632 |
"-or": OR, |
| 633 |
"-equals": EQUALS, |
| 634 |
"-contains": CONTAINS, |
| 635 |
"-mimics": MIMICS, |
| 636 |
"-includes": INCLUDES, |
| 637 |
"-excludes": EXCLUDES, |
| 638 |
"-is-within": ISWITHIN, |
| 639 |
"-starts-with": STARTSWITH, |
| 640 |
"-ends-with": ENDSWITH, |
| 641 |
"-is-not": ISNOT, |
| 642 |
"-is-before": ISBEFORE, |
| 643 |
"-is-after": ISAFTER, |
| 644 |
"-consists-of": CONSISTSOF, |
| 645 |
"-matches": MATCHES, |
| 646 |
"-resembles": RESEMBLES, |
| 647 |
"-is-equal-to": ISEQUALTO, |
| 648 |
"-differs-from": DIFFERSFROM, |
| 649 |
"-gt": GT, |
| 650 |
"-ge": GE, |
| 651 |
"-lt": LT, |
| 652 |
"-le": LE, |
| 653 |
"-eq": EQ, |
| 654 |
"-ne": NE, |
| 655 |
"-num": NUM, |
| 656 |
"-len": LEN, |
| 657 |
"-sum": SUM, |
| 658 |
"-acc": ACC, |
| 659 |
"-min": MIN, |
| 660 |
"-max": MAX, |
| 661 |
"-inc": INC, |
| 662 |
"-dec": DEC, |
| 663 |
"-sub": SUB, |
| 664 |
"-avg": AVG, |
| 665 |
"-dev": DEV, |
| 666 |
"-med": MED, |
| 667 |
"-mul": MUL, |
| 668 |
"-div": DIV, |
| 669 |
"-mod": MOD, |
| 670 |
"-geo": GEO, |
| 671 |
"-hrm": HRM, |
| 672 |
"-rms": RMS, |
| 673 |
"-sqt": SQT, |
| 674 |
"-lg2": LG2, |
| 675 |
"-lge": LGE, |
| 676 |
"-log": LOG, |
| 677 |
"-bin": BIN, |
| 678 |
"-oct": OCT, |
| 679 |
"-hex": HEX, |
| 680 |
"-bit": BIT, |
| 681 |
"-pad": PAD, |
| 682 |
"-raw": RAW, |
| 683 |
"-0-based": ZEROBASED, |
| 684 |
"-zero-based": ZEROBASED, |
| 685 |
"-1-based": ONEBASED, |
| 686 |
"-one-based": ONEBASED, |
| 687 |
"-ucsc": UCSCBASED, |
| 688 |
"-ucsc-based": UCSCBASED, |
| 689 |
"-ucsc-coords": UCSCBASED, |
| 690 |
"-bed-based": UCSCBASED, |
| 691 |
"-bed-coords": UCSCBASED, |
| 692 |
"-nucleic": NUCLEIC, |
| 693 |
"-revcomp": REVCOMP, |
| 694 |
"-fasta": FASTA, |
| 695 |
"-ncbi2na": NCBI2NA, |
| 696 |
"-ncbi4na": NCBI4NA, |
| 697 |
"-cds2prot": CDS2PROT, |
| 698 |
"-pept": PEPTS, |
| 699 |
"-molwt": MOLWTX, |
| 700 |
"-molwt-x": MOLWTX, |
| 701 |
"-molwt-m": MOLWTM, |
| 702 |
"-molwt-f": MOLWTF, |
| 703 |
"-accession": ACCESSION, |
| 704 |
"-numeric": NUMERIC, |
| 705 |
"-hgvs": HGVS, |
| 706 |
"-else": ELSE, |
| 707 |
} |
| 708 |
|
| 709 |
var sequenceTypeIs = map[string]SequenceType{ |
| 710 |
"INSDSeq:INSDInterval_from": {1, ISSTART}, |
| 711 |
"INSDSeq:INSDInterval_to": {1, ISSTOP}, |
| 712 |
"DocumentSummary:ChrStart": {0, ISSTART}, |
| 713 |
"DocumentSummary:ChrStop": {0, ISSTOP}, |
| 714 |
"DocumentSummary:Chr_start": {1, ISSTART}, |
| 715 |
"DocumentSummary:Chr_end": {1, ISSTOP}, |
| 716 |
"DocumentSummary:Chr_inner_start": {1, ISSTART}, |
| 717 |
"DocumentSummary:Chr_inner_end": {1, ISSTOP}, |
| 718 |
"DocumentSummary:Chr_outer_start": {1, ISSTART}, |
| 719 |
"DocumentSummary:Chr_outer_end": {1, ISSTOP}, |
| 720 |
"DocumentSummary:start": {1, ISSTART}, |
| 721 |
"DocumentSummary:stop": {1, ISSTOP}, |
| 722 |
"DocumentSummary:display_start": {1, ISSTART}, |
| 723 |
"DocumentSummary:display_stop": {1, ISSTOP}, |
| 724 |
"Entrezgene:Seq-interval_from": {0, ISSTART}, |
| 725 |
"Entrezgene:Seq-interval_to": {0, ISSTOP}, |
| 726 |
"GenomicInfoType:ChrStart": {0, ISSTART}, |
| 727 |
"GenomicInfoType:ChrStop": {0, ISSTOP}, |
| 728 |
"RS:position": {0, ISPOS}, |
| 729 |
"RS:@asnFrom": {0, ISSTART}, |
| 730 |
"RS:@asnTo": {0, ISSTOP}, |
| 731 |
"RS:@end": {0, ISSTOP}, |
| 732 |
"RS:@leftContigNeighborPos": {0, ISSTART}, |
| 733 |
"RS:@physMapInt": {0, ISPOS}, |
| 734 |
"RS:@protLoc": {0, ISPOS}, |
| 735 |
"RS:@rightContigNeighborPos": {0, ISSTOP}, |
| 736 |
"RS:@start": {0, ISSTART}, |
| 737 |
"RS:@structLoc": {0, ISPOS}, |
| 738 |
} |
| 739 |
|
| 740 |
var monthTable = map[string]int{ |
| 741 |
"jan": 1, |
| 742 |
"january": 1, |
| 743 |
"feb": 2, |
| 744 |
"february": 2, |
| 745 |
"mar": 3, |
| 746 |
"march": 3, |
| 747 |
"apr": 4, |
| 748 |
"april": 4, |
| 749 |
"may": 5, |
| 750 |
"jun": 6, |
| 751 |
"june": 6, |
| 752 |
"jul": 7, |
| 753 |
"july": 7, |
| 754 |
"aug": 8, |
| 755 |
"august": 8, |
| 756 |
"sep": 9, |
| 757 |
"september": 9, |
| 758 |
"oct": 10, |
| 759 |
"october": 10, |
| 760 |
"nov": 11, |
| 761 |
"november": 11, |
| 762 |
"dec": 12, |
| 763 |
"december": 12, |
| 764 |
} |
| 765 |
|
| 766 |
var propertyTable = map[string]string{ |
| 767 |
"AssociatedDataset": "Associated Dataset", |
| 768 |
"AssociatedPublication": "Associated Publication", |
| 769 |
"CommentIn": "Comment In", |
| 770 |
"CommentOn": "Comment On", |
| 771 |
"ErratumFor": "Erratum For", |
| 772 |
"ErratumIn": "Erratum In", |
| 773 |
"ExpressionOfConcernFor": "Expression Of Concern For", |
| 774 |
"ExpressionOfConcernIn": "Expression Of Concern In", |
| 775 |
"OriginalReportIn": "Original Report In", |
| 776 |
"ReprintIn": "Reprint In", |
| 777 |
"ReprintOf": "Reprint Of", |
| 778 |
"RepublishedFrom": "Republished From", |
| 779 |
"RepublishedIn": "Republished In", |
| 780 |
"RetractedandRepublishedFrom": "Retracted And Republished From", |
| 781 |
"RetractedandRepublishedIn": "Retracted And Republished In", |
| 782 |
"RetractionIn": "Retraction In", |
| 783 |
"RetractionOf": "Retraction Of", |
| 784 |
"SummaryForPatientsIn": "Summary For Patients In", |
| 785 |
"UpdateIn": "Update In", |
| 786 |
"UpdateOf": "Update Of", |
| 787 |
"aheadofprint": "Ahead Of Print", |
| 788 |
"epublish": "Electronically Published", |
| 789 |
"ppublish": "Published In Print", |
| 790 |
} |
| 791 |
|
| 792 |
// DATA OBJECTS |
| 793 |
|
| 794 |
// Step contains parameters for executing a single command step |
| 795 |
type Step struct { |
| 796 |
Value string |
| 797 |
Parent string |
| 798 |
Match string |
| 799 |
Attrib string |
| 800 |
StrL string |
| 801 |
StrR string |
| 802 |
IntL int |
| 803 |
IntR int |
| 804 |
Type OpType |
| 805 |
TypL RangeType |
| 806 |
TypR RangeType |
| 807 |
Norm bool |
| 808 |
Wild bool |
| 809 |
Unesc bool |
| 810 |
} |
| 811 |
|
| 812 |
// Operation breaks commands into sequential steps |
| 813 |
type Operation struct { |
| 814 |
Stages []*Step |
| 815 |
Value string |
| 816 |
Type OpType |
| 817 |
} |
| 818 |
|
| 819 |
// Block contains nested instructions for executing commands |
| 820 |
type Block struct { |
| 821 |
Conditions []*Operation |
| 822 |
Commands []*Operation |
| 823 |
Failure []*Operation |
| 824 |
Subtasks []*Block |
| 825 |
Path []string |
| 826 |
Working []string |
| 827 |
Parsed []string |
| 828 |
Visit string |
| 829 |
Parent string |
| 830 |
Match string |
| 831 |
Position string |
| 832 |
Foreword string |
| 833 |
Afterword string |
| 834 |
} |
| 835 |
|
| 836 |
// Limiter is used for collecting specific nodes (e.g., first and last) |
| 837 |
type Limiter struct { |
| 838 |
Obj *XMLNode |
| 839 |
Idx int |
| 840 |
Lvl int |
| 841 |
} |
| 842 |
|
| 843 |
// DebugBlock examines structure of parsed arguments (undocumented) |
| 844 |
/* |
| 845 |
func DebugBlock(blk *Block, depth int) { |
| 846 |
|
| 847 |
doIndent := func(indt int) { |
| 848 |
for i := 1; i < indt; i++ { |
| 849 |
fmt.Fprintf(os.Stderr, " ") |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
doIndent(depth) |
| 854 |
|
| 855 |
if blk.Visit != "" { |
| 856 |
doIndent(depth + 1) |
| 857 |
fmt.Fprintf(os.Stderr, "<Visit> %s </Visit>\n", blk.Visit) |
| 858 |
} |
| 859 |
if len(blk.Parsed) > 0 { |
| 860 |
doIndent(depth + 1) |
| 861 |
fmt.Fprintf(os.Stderr, "<Parsed>") |
| 862 |
for _, str := range blk.Parsed { |
| 863 |
fmt.Fprintf(os.Stderr, " %s", str) |
| 864 |
} |
| 865 |
fmt.Fprintf(os.Stderr, " </Parsed>\n") |
| 866 |
} |
| 867 |
|
| 868 |
if len(blk.Subtasks) > 0 { |
| 869 |
for _, sub := range blk.Subtasks { |
| 870 |
DebugBlock(sub, depth+1) |
| 871 |
} |
| 872 |
} |
| 873 |
} |
| 874 |
*/ |
| 875 |
|
| 876 |
// PARSE COMMAND-LINE ARGUMENTS |
| 877 |
|
| 878 |
// ParseArguments parses nested exploration instruction from command-line arguments |
| 879 |
func ParseArguments(cmdargs []string, pttrn string) *Block { |
| 880 |
|
| 881 |
// different names of exploration control arguments allow multiple levels of nested "for" loops in a linear command line |
| 882 |
// (capitalized versions for backward-compatibility with original Perl implementation handling of recursive definitions) |
| 883 |
var ( |
| 884 |
lcname = []string{ |
| 885 |
"", |
| 886 |
"-unit", |
| 887 |
"-subsetX", |
| 888 |
"-subset", |
| 889 |
"-sectionX", |
| 890 |
"-section", |
| 891 |
"-blockX", |
| 892 |
"-block", |
| 893 |
"-branchX", |
| 894 |
"-branch", |
| 895 |
"-groupX", |
| 896 |
"-group", |
| 897 |
"-divisionX", |
| 898 |
"-division", |
| 899 |
"-pathX", |
| 900 |
"-path", |
| 901 |
"-pattern", |
| 902 |
} |
| 903 |
|
| 904 |
ucname = []string{ |
| 905 |
"", |
| 906 |
"-Unit", |
| 907 |
"-SubsetX", |
| 908 |
"-Subset", |
| 909 |
"-SectionX", |
| 910 |
"-Section", |
| 911 |
"-BlockX", |
| 912 |
"-Block", |
| 913 |
"-BranchX", |
| 914 |
"-Branch", |
| 915 |
"-GroupX", |
| 916 |
"-Group", |
| 917 |
"-DivisionX", |
| 918 |
"-Division", |
| 919 |
"-PathX", |
| 920 |
"-Path", |
| 921 |
"-Pattern", |
| 922 |
} |
| 923 |
) |
| 924 |
|
| 925 |
parseFlag := func(str string, rest []string) (OpType, bool) { |
| 926 |
|
| 927 |
ch, ok := HasLeadingUnicodeDash(str) |
| 928 |
if ok { |
| 929 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, str) |
| 930 |
os.Exit(1) |
| 931 |
} |
| 932 |
|
| 933 |
op, ok := opTypeIs[str] |
| 934 |
if ok { |
| 935 |
if argTypeIs[str] == EXTRACTION { |
| 936 |
return op, true |
| 937 |
} |
| 938 |
return op, false |
| 939 |
} |
| 940 |
|
| 941 |
// check for user-defined variable - one to three dashes followed by all capital letters or digits |
| 942 |
|
| 943 |
if len(str) > 1 && str[0] == '-' && IsAllCapsOrDigits(str[1:]) { |
| 944 |
if len(rest) > 0 { |
| 945 |
next := rest[0] |
| 946 |
// if next argument is a command |
| 947 |
if strings.HasPrefix(next, "-") { |
| 948 |
// then no need for explicit --- prefix to indicate CONVERTER variable |
| 949 |
return CONVERTER, true |
| 950 |
} |
| 951 |
} |
| 952 |
return VARIABLE, true |
| 953 |
} |
| 954 |
|
| 955 |
if len(str) > 2 && strings.HasPrefix(str, "--") && IsAllCapsOrDigits(str[2:]) { |
| 956 |
return ACCUMULATOR, true |
| 957 |
} |
| 958 |
|
| 959 |
if len(str) > 3 && strings.HasPrefix(str, "---") && IsAllCapsOrDigits(str[3:]) { |
| 960 |
// original explicit triple dash prefix for CONVERTER variable development (undocumented) |
| 961 |
return CONVERTER, true |
| 962 |
} |
| 963 |
|
| 964 |
// any other argument starting with a dash is an unrecognized command |
| 965 |
|
| 966 |
if len(str) > 0 && str[0] == '-' { |
| 967 |
return UNRECOGNIZED, false |
| 968 |
} |
| 969 |
|
| 970 |
return UNSET, false |
| 971 |
} |
| 972 |
|
| 973 |
// parseCommands recursive definition |
| 974 |
var parseCommands func(parent *Block, startLevel LevelType) |
| 975 |
|
| 976 |
// parseCommands does initial parsing of exploration command structure |
| 977 |
parseCommands = func(parent *Block, startLevel LevelType) { |
| 978 |
|
| 979 |
// find next highest level exploration argument |
| 980 |
findNextLevel := func(args []string, level LevelType) (LevelType, string, string) { |
| 981 |
|
| 982 |
if len(args) > 1 { |
| 983 |
|
| 984 |
for { |
| 985 |
|
| 986 |
if level < UNIT { |
| 987 |
break |
| 988 |
} |
| 989 |
|
| 990 |
lctag := lcname[level] |
| 991 |
uctag := ucname[level] |
| 992 |
|
| 993 |
for _, txt := range args { |
| 994 |
if txt == lctag { |
| 995 |
return level, lctag, uctag |
| 996 |
} |
| 997 |
if txt == uctag { |
| 998 |
DisplayWarning("Upper-case '%s' exploration command is deprecated, use lower-case '%s' instead", uctag, lctag) |
| 999 |
return level, lctag, uctag |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
level-- |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
return 0, "", "" |
| 1008 |
} |
| 1009 |
|
| 1010 |
arguments := parent.Working |
| 1011 |
|
| 1012 |
level, lctag, uctag := findNextLevel(arguments, startLevel) |
| 1013 |
|
| 1014 |
if level < UNIT { |
| 1015 |
|
| 1016 |
// break recursion |
| 1017 |
return |
| 1018 |
} |
| 1019 |
|
| 1020 |
// group arguments at a given exploration level |
| 1021 |
subsetCommands := func(args []string) *Block { |
| 1022 |
|
| 1023 |
max := len(args) |
| 1024 |
|
| 1025 |
visit := "" |
| 1026 |
|
| 1027 |
// extract name of object to visit |
| 1028 |
if max > 1 { |
| 1029 |
visit = args[1] |
| 1030 |
args = args[2:] |
| 1031 |
max -= 2 |
| 1032 |
} |
| 1033 |
|
| 1034 |
partition := 0 |
| 1035 |
for cur, str := range args { |
| 1036 |
|
| 1037 |
// record point of next exploration command |
| 1038 |
partition = cur + 1 |
| 1039 |
|
| 1040 |
// skip if not a command |
| 1041 |
if len(str) < 1 || str[0] != '-' { |
| 1042 |
continue |
| 1043 |
} |
| 1044 |
|
| 1045 |
if argTypeIs[str] == EXPLORATION { |
| 1046 |
partition = cur |
| 1047 |
break |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
// convert slashes (e.g., parent/child construct) to periods (e.g., dotted exploration path) |
| 1052 |
if strings.Contains(visit, "/") { |
| 1053 |
if !strings.Contains(visit, ".") { |
| 1054 |
visit = strings.Replace(visit, "/", ".", -1) |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
// parse parent.child or dotted path construct |
| 1059 |
// colon indicates a namespace prefix in any or all of the components |
| 1060 |
prnt, rmdr := SplitInTwoRight(visit, ".") |
| 1061 |
match, rest := SplitInTwoLeft(rmdr, ".") |
| 1062 |
|
| 1063 |
if rest != "" { |
| 1064 |
|
| 1065 |
// exploration match on first component, then search remainder one level at a time with subsequent components |
| 1066 |
dirs := strings.Split(rmdr, ".") |
| 1067 |
|
| 1068 |
// signal with "path" position |
| 1069 |
return &Block{Visit: visit, Parent: "", Match: prnt, Path: dirs, Position: "path", Parsed: args[0:partition], Working: args[partition:]} |
| 1070 |
} |
| 1071 |
|
| 1072 |
// promote arguments parsed at this level |
| 1073 |
return &Block{Visit: visit, Parent: prnt, Match: match, Parsed: args[0:partition], Working: args[partition:]} |
| 1074 |
} |
| 1075 |
|
| 1076 |
cur := 0 |
| 1077 |
|
| 1078 |
// search for positions of current exploration command |
| 1079 |
|
| 1080 |
for idx, txt := range arguments { |
| 1081 |
if txt == lctag || txt == uctag { |
| 1082 |
if idx == 0 { |
| 1083 |
continue |
| 1084 |
} |
| 1085 |
|
| 1086 |
blk := subsetCommands(arguments[cur:idx]) |
| 1087 |
parseCommands(blk, level-1) |
| 1088 |
parent.Subtasks = append(parent.Subtasks, blk) |
| 1089 |
|
| 1090 |
cur = idx |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
if cur < len(arguments) { |
| 1095 |
blk := subsetCommands(arguments[cur:]) |
| 1096 |
parseCommands(blk, level-1) |
| 1097 |
parent.Subtasks = append(parent.Subtasks, blk) |
| 1098 |
} |
| 1099 |
|
| 1100 |
// clear execution arguments from parent after subsetting |
| 1101 |
parent.Working = nil |
| 1102 |
} |
| 1103 |
|
| 1104 |
// parse optional [min:max], [&VAR:&VAR], or [after|before] range specification |
| 1105 |
parseRange := func(item, rnge string) (typL RangeType, strL string, intL int, typR RangeType, strR string, intR int) { |
| 1106 |
|
| 1107 |
typL = NORANGE |
| 1108 |
typR = NORANGE |
| 1109 |
strL = "" |
| 1110 |
strR = "" |
| 1111 |
intL = 0 |
| 1112 |
intR = 0 |
| 1113 |
|
| 1114 |
if rnge == "" { |
| 1115 |
// no range specification, return default values |
| 1116 |
return |
| 1117 |
} |
| 1118 |
|
| 1119 |
// check if last character is right square bracket |
| 1120 |
if !strings.HasSuffix(rnge, "]") { |
| 1121 |
DisplayError("Unrecognized range %s", rnge) |
| 1122 |
os.Exit(1) |
| 1123 |
} |
| 1124 |
|
| 1125 |
rnge = strings.TrimSuffix(rnge, "]") |
| 1126 |
|
| 1127 |
if rnge == "" { |
| 1128 |
DisplayError("Empty range %s[]", item) |
| 1129 |
os.Exit(1) |
| 1130 |
} |
| 1131 |
|
| 1132 |
// check for caret [after^before] variant that allows vertical bar in contents |
| 1133 |
if strings.Contains(rnge, "^") { |
| 1134 |
|
| 1135 |
strL, strR = SplitInTwoLeft(rnge, "^") |
| 1136 |
// spacing matters, so do not call TrimSpace |
| 1137 |
|
| 1138 |
if strL == "" && strR == "" { |
| 1139 |
DisplayError("Empty range %s[^]", item) |
| 1140 |
os.Exit(1) |
| 1141 |
} |
| 1142 |
|
| 1143 |
typL = STRINGRANGE |
| 1144 |
typR = STRINGRANGE |
| 1145 |
|
| 1146 |
// return statement returns named variables |
| 1147 |
return |
| 1148 |
} |
| 1149 |
|
| 1150 |
// next check for original vertical bar [after|before] variant |
| 1151 |
if strings.Contains(rnge, "|") { |
| 1152 |
|
| 1153 |
strL, strR = SplitInTwoLeft(rnge, "|") |
| 1154 |
// spacing matters, so do not call TrimSpace |
| 1155 |
|
| 1156 |
if strL == "" && strR == "" { |
| 1157 |
DisplayError("Empty range %s[|]", item) |
| 1158 |
os.Exit(1) |
| 1159 |
} |
| 1160 |
|
| 1161 |
typL = STRINGRANGE |
| 1162 |
typR = STRINGRANGE |
| 1163 |
|
| 1164 |
// return statement returns named variables |
| 1165 |
return |
| 1166 |
} |
| 1167 |
|
| 1168 |
// otherwise must have colon and integers [from:to] within brackets |
| 1169 |
if !strings.Contains(rnge, ":") { |
| 1170 |
DisplayError("Colon missing in range %s[%s]", item, rnge) |
| 1171 |
os.Exit(1) |
| 1172 |
} |
| 1173 |
|
| 1174 |
// split at colon |
| 1175 |
lft, rgt := SplitInTwoLeft(rnge, ":") |
| 1176 |
|
| 1177 |
lft = strings.TrimSpace(lft) |
| 1178 |
rgt = strings.TrimSpace(rgt) |
| 1179 |
|
| 1180 |
if lft == "" && rgt == "" { |
| 1181 |
DisplayError("Empty range %s[:]", item) |
| 1182 |
os.Exit(1) |
| 1183 |
} |
| 1184 |
|
| 1185 |
// for variable, parse optional +/- offset suffix |
| 1186 |
parseOffset := func(str string) (string, int) { |
| 1187 |
|
| 1188 |
if str == "" || str[0] == ' ' { |
| 1189 |
DisplayError("Unrecognized variable '&%s'", str) |
| 1190 |
os.Exit(1) |
| 1191 |
} |
| 1192 |
|
| 1193 |
pls := "" |
| 1194 |
mns := "" |
| 1195 |
|
| 1196 |
ofs := 0 |
| 1197 |
|
| 1198 |
// check for &VAR+1 or &VAR-1 integer adjustment |
| 1199 |
str, pls = SplitInTwoLeft(str, "+") |
| 1200 |
str, mns = SplitInTwoLeft(str, "-") |
| 1201 |
|
| 1202 |
if pls != "" { |
| 1203 |
val, err := strconv.Atoi(pls) |
| 1204 |
if err != nil { |
| 1205 |
DisplayError("Unrecognized range adjustment &%s+%s", str, pls) |
| 1206 |
os.Exit(1) |
| 1207 |
} |
| 1208 |
ofs = val |
| 1209 |
} else if mns != "" { |
| 1210 |
val, err := strconv.Atoi(mns) |
| 1211 |
if err != nil { |
| 1212 |
DisplayError("Unrecognized range adjustment &%s-%s", str, mns) |
| 1213 |
os.Exit(1) |
| 1214 |
} |
| 1215 |
ofs = -val |
| 1216 |
} |
| 1217 |
|
| 1218 |
return str, ofs |
| 1219 |
} |
| 1220 |
|
| 1221 |
// parse integer position, 1-based coordinate must be greater than 0 |
| 1222 |
parseInteger := func(str string, mustBePositive bool) int { |
| 1223 |
if str == "" { |
| 1224 |
return 0 |
| 1225 |
} |
| 1226 |
|
| 1227 |
val, err := strconv.Atoi(str) |
| 1228 |
if err != nil { |
| 1229 |
DisplayError("Unrecognized range component %s[%s:]", item, str) |
| 1230 |
os.Exit(1) |
| 1231 |
} |
| 1232 |
if mustBePositive { |
| 1233 |
if val < 1 { |
| 1234 |
DisplayError("Range component %s[%s:] must be positive", item, str) |
| 1235 |
os.Exit(1) |
| 1236 |
} |
| 1237 |
} else { |
| 1238 |
if val == 0 { |
| 1239 |
DisplayError("Range component %s[%s:] must not be zero", item, str) |
| 1240 |
os.Exit(1) |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
return val |
| 1245 |
} |
| 1246 |
|
| 1247 |
if lft != "" { |
| 1248 |
if lft[0] == '&' { |
| 1249 |
lft = lft[1:] |
| 1250 |
strL, intL = parseOffset(lft) |
| 1251 |
typL = VARIABLERANGE |
| 1252 |
} else { |
| 1253 |
intL = parseInteger(lft, true) |
| 1254 |
typL = INTEGERRANGE |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
if rgt != "" { |
| 1259 |
if rgt[0] == '&' { |
| 1260 |
rgt = rgt[1:] |
| 1261 |
strR, intR = parseOffset(rgt) |
| 1262 |
typR = VARIABLERANGE |
| 1263 |
} else { |
| 1264 |
intR = parseInteger(rgt, false) |
| 1265 |
typR = INTEGERRANGE |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
// return statement required to return named variables |
| 1270 |
return |
| 1271 |
} |
| 1272 |
|
| 1273 |
parseConditionals := func(cmds *Block, arguments []string) []*Operation { |
| 1274 |
|
| 1275 |
max := len(arguments) |
| 1276 |
if max < 1 { |
| 1277 |
return nil |
| 1278 |
} |
| 1279 |
|
| 1280 |
// check for missing condition command |
| 1281 |
txt := arguments[0] |
| 1282 |
|
| 1283 |
ch, ok := HasLeadingUnicodeDash(txt) |
| 1284 |
if ok { |
| 1285 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, txt) |
| 1286 |
os.Exit(1) |
| 1287 |
} |
| 1288 |
|
| 1289 |
if txt != "-if" && txt != "-unless" && txt != "-select" && txt != "-match" && txt != "-avoid" && txt != "-position" { |
| 1290 |
DisplayError("Missing -if command before '%s'", txt) |
| 1291 |
os.Exit(1) |
| 1292 |
} |
| 1293 |
if txt == "-position" && max > 2 { |
| 1294 |
DisplayError("Cannot combine -position with -if or -unless commands") |
| 1295 |
os.Exit(1) |
| 1296 |
} |
| 1297 |
// check for missing argument after last condition |
| 1298 |
txt = arguments[max-1] |
| 1299 |
if len(txt) > 0 && txt[0] == '-' { |
| 1300 |
DisplayError("Item missing after %s command", txt) |
| 1301 |
os.Exit(1) |
| 1302 |
} |
| 1303 |
|
| 1304 |
cond := make([]*Operation, 0, max) |
| 1305 |
|
| 1306 |
// parse conditional clause into execution step |
| 1307 |
parseStep := func(op *Operation, elementColonValue bool) { |
| 1308 |
|
| 1309 |
if op == nil { |
| 1310 |
return |
| 1311 |
} |
| 1312 |
|
| 1313 |
str := op.Value |
| 1314 |
|
| 1315 |
status := ELEMENT |
| 1316 |
|
| 1317 |
// isolate and parse optional [min:max], [&VAR:&VAR], or [after|before] range specification |
| 1318 |
str, rnge := SplitInTwoLeft(str, "[") |
| 1319 |
|
| 1320 |
str = strings.TrimSpace(str) |
| 1321 |
rnge = strings.TrimSpace(rnge) |
| 1322 |
|
| 1323 |
if str == "" && rnge != "" { |
| 1324 |
// rnge should already end with right square bracket |
| 1325 |
DisplayError("Variable missing in range specification [%s", rnge) |
| 1326 |
os.Exit(1) |
| 1327 |
} |
| 1328 |
|
| 1329 |
typL, strL, intL, typR, strR, intR := parseRange(str, rnge) |
| 1330 |
|
| 1331 |
// check for pound, percent, or caret character at beginning of name |
| 1332 |
if len(str) > 1 { |
| 1333 |
switch str[0] { |
| 1334 |
case '&': |
| 1335 |
if IsAllCapsOrDigits(str[1:]) { |
| 1336 |
status = VARIABLE |
| 1337 |
str = str[1:] |
| 1338 |
} else if strings.Contains(str, ":") { |
| 1339 |
DisplayError("Unsupported construct '%s', use -if &VARIABLE -equals VALUE instead", str) |
| 1340 |
os.Exit(1) |
| 1341 |
} else { |
| 1342 |
DisplayError("Unrecognized variable '%s'", str) |
| 1343 |
os.Exit(1) |
| 1344 |
} |
| 1345 |
case '#': |
| 1346 |
status = COUNT |
| 1347 |
str = str[1:] |
| 1348 |
case '%': |
| 1349 |
status = LENGTH |
| 1350 |
str = str[1:] |
| 1351 |
case '^': |
| 1352 |
status = DEPTH |
| 1353 |
str = str[1:] |
| 1354 |
default: |
| 1355 |
} |
| 1356 |
} else if str == "+" { |
| 1357 |
status = INDEX |
| 1358 |
} |
| 1359 |
|
| 1360 |
// parse parent/element@attribute construct |
| 1361 |
// colon indicates a namespace prefix in any or all of the components |
| 1362 |
prnt, match := SplitInTwoRight(str, "/") |
| 1363 |
match, attrib := SplitInTwoLeft(match, "@") |
| 1364 |
val := "" |
| 1365 |
|
| 1366 |
// leading colon indicates namespace prefix wildcard |
| 1367 |
wildcard := false |
| 1368 |
if strings.HasPrefix(prnt, ":") || strings.HasPrefix(match, ":") || strings.HasPrefix(attrib, ":") { |
| 1369 |
wildcard = true |
| 1370 |
} |
| 1371 |
|
| 1372 |
if elementColonValue { |
| 1373 |
|
| 1374 |
// allow parent/element@attribute:value construct for deprecated -match and -avoid, and for subsequent -and and -or commands |
| 1375 |
match, val = SplitInTwoLeft(str, ":") |
| 1376 |
prnt, match = SplitInTwoRight(match, "/") |
| 1377 |
match, attrib = SplitInTwoLeft(match, "@") |
| 1378 |
} |
| 1379 |
|
| 1380 |
norm := true |
| 1381 |
if rnge != "" { |
| 1382 |
if typL != NORANGE || typR != NORANGE || strL != "" || strR != "" || intL != 0 || intR != 0 { |
| 1383 |
norm = false |
| 1384 |
} |
| 1385 |
} |
| 1386 |
|
| 1387 |
tsk := &Step{Type: status, Value: str, Parent: prnt, Match: match, Attrib: attrib, |
| 1388 |
TypL: typL, StrL: strL, IntL: intL, TypR: typR, StrR: strR, IntR: intR, |
| 1389 |
Norm: norm, Wild: wildcard} |
| 1390 |
|
| 1391 |
op.Stages = append(op.Stages, tsk) |
| 1392 |
|
| 1393 |
// transform old -match "element:value" to -match element -equals value |
| 1394 |
if val != "" { |
| 1395 |
tsk := &Step{Type: EQUALS, Value: val} |
| 1396 |
op.Stages = append(op.Stages, tsk) |
| 1397 |
} |
| 1398 |
} |
| 1399 |
|
| 1400 |
idx := 0 |
| 1401 |
|
| 1402 |
// conditionals should alternate between command and object/value |
| 1403 |
expectDash := true |
| 1404 |
last := "" |
| 1405 |
|
| 1406 |
var op *Operation |
| 1407 |
|
| 1408 |
// flag to allow element-colon-value for deprecated -match and -avoid commands, otherwise colon is for namespace prefixes |
| 1409 |
elementColonValue := false |
| 1410 |
|
| 1411 |
status := UNSET |
| 1412 |
|
| 1413 |
numIf := 0 |
| 1414 |
numUnless := 0 |
| 1415 |
lastCond := "" |
| 1416 |
|
| 1417 |
// parse command strings into operation structure |
| 1418 |
for idx < max { |
| 1419 |
str := arguments[idx] |
| 1420 |
idx++ |
| 1421 |
|
| 1422 |
// conditionals should alternate between command and object/value |
| 1423 |
if expectDash { |
| 1424 |
|
| 1425 |
ch, ok := HasLeadingUnicodeDash(str) |
| 1426 |
if ok { |
| 1427 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, str) |
| 1428 |
os.Exit(1) |
| 1429 |
} |
| 1430 |
|
| 1431 |
if len(str) < 1 || str[0] != '-' { |
| 1432 |
DisplayError("Unexpected '%s' argument after '%s'", str, last) |
| 1433 |
os.Exit(1) |
| 1434 |
} |
| 1435 |
expectDash = false |
| 1436 |
|
| 1437 |
} else { |
| 1438 |
|
| 1439 |
if len(str) > 0 && str[0] == '-' { |
| 1440 |
DisplayError("Unexpected '%s' command after '%s'", str, last) |
| 1441 |
os.Exit(1) |
| 1442 |
} |
| 1443 |
expectDash = true |
| 1444 |
} |
| 1445 |
last = str |
| 1446 |
|
| 1447 |
switch status { |
| 1448 |
case UNSET: |
| 1449 |
status, _ = parseFlag(str, arguments[idx:]) |
| 1450 |
case POSITION: |
| 1451 |
if cmds.Position != "" { |
| 1452 |
DisplayError("-position '%s' conflicts with existing '%s'", str, cmds.Position) |
| 1453 |
os.Exit(1) |
| 1454 |
} |
| 1455 |
cmds.Position = str |
| 1456 |
status = UNSET |
| 1457 |
case MATCH, AVOID: |
| 1458 |
elementColonValue = true |
| 1459 |
fallthrough |
| 1460 |
case IF: |
| 1461 |
numIf++ |
| 1462 |
if numIf > 1 || numUnless > 1 || numIf > 0 && numUnless > 0 { |
| 1463 |
DisplayError("Unexpected '-if %s' after '%s'", str, lastCond) |
| 1464 |
os.Exit(1) |
| 1465 |
} |
| 1466 |
lastCond = "-if " + str |
| 1467 |
op = &Operation{Type: status, Value: str} |
| 1468 |
cond = append(cond, op) |
| 1469 |
parseStep(op, elementColonValue) |
| 1470 |
status = UNSET |
| 1471 |
case UNLESS: |
| 1472 |
numUnless++ |
| 1473 |
if numIf > 1 || numUnless > 1 || numIf > 0 && numUnless > 0 { |
| 1474 |
DisplayError("Unexpected '-unless %s' after '%s'", str, lastCond) |
| 1475 |
os.Exit(1) |
| 1476 |
} |
| 1477 |
lastCond = "-unless " + str |
| 1478 |
op = &Operation{Type: status, Value: str} |
| 1479 |
cond = append(cond, op) |
| 1480 |
parseStep(op, elementColonValue) |
| 1481 |
status = UNSET |
| 1482 |
case SELECT, AND, OR: |
| 1483 |
op = &Operation{Type: status, Value: str} |
| 1484 |
cond = append(cond, op) |
| 1485 |
parseStep(op, elementColonValue) |
| 1486 |
status = UNSET |
| 1487 |
case EQUALS, CONTAINS, MIMICS, INCLUDES, EXCLUDES, ISWITHIN, STARTSWITH, ENDSWITH, ISNOT, ISBEFORE, ISAFTER, CONSISTSOF: |
| 1488 |
if op != nil { |
| 1489 |
if len(str) > 1 && str[0] == '\\' { |
| 1490 |
// first character may be backslash protecting dash (undocumented) |
| 1491 |
str = str[1:] |
| 1492 |
} |
| 1493 |
tsk := &Step{Type: status, Value: str} |
| 1494 |
op.Stages = append(op.Stages, tsk) |
| 1495 |
op = nil |
| 1496 |
} else { |
| 1497 |
DisplayError("Unexpected adjacent string match constraints") |
| 1498 |
os.Exit(1) |
| 1499 |
} |
| 1500 |
status = UNSET |
| 1501 |
case MATCHES: |
| 1502 |
if op != nil { |
| 1503 |
if len(str) > 1 && str[0] == '\\' { |
| 1504 |
// first character may be backslash protecting dash (undocumented) |
| 1505 |
str = str[1:] |
| 1506 |
} |
| 1507 |
str = RemoveCommaOrSemicolon(str) |
| 1508 |
tsk := &Step{Type: status, Value: str} |
| 1509 |
op.Stages = append(op.Stages, tsk) |
| 1510 |
op = nil |
| 1511 |
} else { |
| 1512 |
DisplayError("Unexpected adjacent string match constraints") |
| 1513 |
os.Exit(1) |
| 1514 |
} |
| 1515 |
status = UNSET |
| 1516 |
case RESEMBLES: |
| 1517 |
if op != nil { |
| 1518 |
if len(str) > 1 && str[0] == '\\' { |
| 1519 |
// first character may be backslash protecting dash (undocumented) |
| 1520 |
str = str[1:] |
| 1521 |
} |
| 1522 |
str = SortStringByWords(str) |
| 1523 |
tsk := &Step{Type: status, Value: str} |
| 1524 |
op.Stages = append(op.Stages, tsk) |
| 1525 |
op = nil |
| 1526 |
} else { |
| 1527 |
DisplayError("Unexpected adjacent string match constraints") |
| 1528 |
os.Exit(1) |
| 1529 |
} |
| 1530 |
status = UNSET |
| 1531 |
case ISEQUALTO, DIFFERSFROM: |
| 1532 |
if op != nil { |
| 1533 |
if len(str) < 1 { |
| 1534 |
DisplayError("Empty conditional argument") |
| 1535 |
os.Exit(1) |
| 1536 |
} |
| 1537 |
ch := str[0] |
| 1538 |
// uses element as second argument |
| 1539 |
orig := str |
| 1540 |
if ch == '#' || ch == '%' || ch == '^' { |
| 1541 |
// check for pound, percent, or caret character at beginning of element (undocumented) |
| 1542 |
str = str[1:] |
| 1543 |
if len(str) < 1 { |
| 1544 |
DisplayError("Unexpected conditional constraints") |
| 1545 |
os.Exit(1) |
| 1546 |
} |
| 1547 |
ch = str[0] |
| 1548 |
} |
| 1549 |
if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') { |
| 1550 |
prnt, match := SplitInTwoRight(str, "/") |
| 1551 |
match, attrib := SplitInTwoLeft(match, "@") |
| 1552 |
wildcard := false |
| 1553 |
if strings.HasPrefix(prnt, ":") || strings.HasPrefix(match, ":") || strings.HasPrefix(attrib, ":") { |
| 1554 |
wildcard = true |
| 1555 |
} |
| 1556 |
tsk := &Step{Type: status, Value: orig, Parent: prnt, Match: match, Attrib: attrib, Wild: wildcard} |
| 1557 |
op.Stages = append(op.Stages, tsk) |
| 1558 |
} else { |
| 1559 |
DisplayError("Unexpected conditional constraints") |
| 1560 |
os.Exit(1) |
| 1561 |
} |
| 1562 |
op = nil |
| 1563 |
} |
| 1564 |
status = UNSET |
| 1565 |
case GT, GE, LT, LE, EQ, NE: |
| 1566 |
if op != nil { |
| 1567 |
if len(str) > 1 && str[0] == '\\' { |
| 1568 |
// first character may be backslash protecting minus sign (undocumented) |
| 1569 |
str = str[1:] |
| 1570 |
} |
| 1571 |
if len(str) < 1 { |
| 1572 |
DisplayError("Empty numeric match constraints") |
| 1573 |
os.Exit(1) |
| 1574 |
} |
| 1575 |
ch := str[0] |
| 1576 |
if (ch >= '0' && ch <= '9') || ch == '-' || ch == '+' { |
| 1577 |
// literal numeric constant |
| 1578 |
tsk := &Step{Type: status, Value: str} |
| 1579 |
op.Stages = append(op.Stages, tsk) |
| 1580 |
} else { |
| 1581 |
// numeric test allows element as second argument |
| 1582 |
orig := str |
| 1583 |
if ch == '#' || ch == '%' || ch == '^' { |
| 1584 |
// check for pound, percent, or caret character at beginning of element (undocumented) |
| 1585 |
str = str[1:] |
| 1586 |
if len(str) < 1 { |
| 1587 |
DisplayError("Unexpected numeric match constraints") |
| 1588 |
os.Exit(1) |
| 1589 |
} |
| 1590 |
ch = str[0] |
| 1591 |
} |
| 1592 |
if (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '&' { |
| 1593 |
prnt, match := SplitInTwoRight(str, "/") |
| 1594 |
match, attrib := SplitInTwoLeft(match, "@") |
| 1595 |
wildcard := false |
| 1596 |
if strings.HasPrefix(prnt, ":") || strings.HasPrefix(match, ":") || strings.HasPrefix(attrib, ":") { |
| 1597 |
wildcard = true |
| 1598 |
} |
| 1599 |
tsk := &Step{Type: status, Value: orig, Parent: prnt, Match: match, Attrib: attrib, Wild: wildcard} |
| 1600 |
op.Stages = append(op.Stages, tsk) |
| 1601 |
} else { |
| 1602 |
DisplayError("Unexpected numeric match constraints") |
| 1603 |
os.Exit(1) |
| 1604 |
} |
| 1605 |
} |
| 1606 |
op = nil |
| 1607 |
} else { |
| 1608 |
DisplayError("Unexpected adjacent numeric match constraints") |
| 1609 |
os.Exit(1) |
| 1610 |
} |
| 1611 |
status = UNSET |
| 1612 |
case UNRECOGNIZED: |
| 1613 |
DisplayError("Unrecognized argument '%s'", str) |
| 1614 |
os.Exit(1) |
| 1615 |
default: |
| 1616 |
DisplayError("Unexpected argument '%s'", str) |
| 1617 |
os.Exit(1) |
| 1618 |
} |
| 1619 |
} |
| 1620 |
|
| 1621 |
return cond |
| 1622 |
} |
| 1623 |
|
| 1624 |
parseExtractions := func(cmds *Block, arguments []string) []*Operation { |
| 1625 |
|
| 1626 |
max := len(arguments) |
| 1627 |
if max < 1 { |
| 1628 |
return nil |
| 1629 |
} |
| 1630 |
|
| 1631 |
// check for missing -element (or -first, etc.) command |
| 1632 |
txt := arguments[0] |
| 1633 |
|
| 1634 |
ch, ok := HasLeadingUnicodeDash(txt) |
| 1635 |
if ok { |
| 1636 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, txt) |
| 1637 |
os.Exit(1) |
| 1638 |
} |
| 1639 |
|
| 1640 |
if len(txt) < 1 || txt[0] != '-' { |
| 1641 |
DisplayError("Missing -element command before '%s'", txt) |
| 1642 |
os.Exit(1) |
| 1643 |
} |
| 1644 |
// check for missing argument after last -element (or -first, etc.) command |
| 1645 |
txt = arguments[max-1] |
| 1646 |
if len(txt) > 0 && txt[0] == '-' { |
| 1647 |
if txt == "-rst" { |
| 1648 |
DisplayError("Unexpected position for %s command", txt) |
| 1649 |
os.Exit(1) |
| 1650 |
} else if txt == "-clr" { |
| 1651 |
// main loop runs out after trailing -clr, add another one so this one will be executed |
| 1652 |
arguments = append(arguments, "-clr") |
| 1653 |
max++ |
| 1654 |
} else if txt == "-cls" || txt == "-slf" { |
| 1655 |
// okay at end |
| 1656 |
} else if max < 2 || arguments[max-2] != "-lbl" { |
| 1657 |
DisplayError("Item missing after %s command", txt) |
| 1658 |
os.Exit(1) |
| 1659 |
} else if max < 3 || (arguments[max-3] != "-att" && arguments[max-3] != "-atr") { |
| 1660 |
DisplayError("Item missing after %s command", txt) |
| 1661 |
os.Exit(1) |
| 1662 |
} |
| 1663 |
} |
| 1664 |
|
| 1665 |
comm := make([]*Operation, 0, max) |
| 1666 |
|
| 1667 |
removeLeadingHyphens := func(str string) string { |
| 1668 |
|
| 1669 |
for strings.HasPrefix(str, "-") { |
| 1670 |
str = strings.TrimPrefix(str, "-") |
| 1671 |
} |
| 1672 |
|
| 1673 |
ch, ok := HasLeadingUnicodeDash(str) |
| 1674 |
if ok { |
| 1675 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, str) |
| 1676 |
os.Exit(1) |
| 1677 |
} |
| 1678 |
|
| 1679 |
return str |
| 1680 |
} |
| 1681 |
|
| 1682 |
// parse next argument |
| 1683 |
nextStatus := func(str string, rest []string) (OpType, bool) { |
| 1684 |
|
| 1685 |
status, isExtraction := parseFlag(str, rest) |
| 1686 |
|
| 1687 |
// no-argument flags must be handled here to prevent subsequent "No -element before" error |
| 1688 |
switch status { |
| 1689 |
case VARIABLE: |
| 1690 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1691 |
comm = append(comm, op) |
| 1692 |
status = VALUE |
| 1693 |
case ACCUMULATOR: |
| 1694 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1695 |
comm = append(comm, op) |
| 1696 |
status = VALUE |
| 1697 |
case CONVERTER: |
| 1698 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1699 |
comm = append(comm, op) |
| 1700 |
// CONVERTER sets next status to UNSET, not VALUE |
| 1701 |
status = UNSET |
| 1702 |
case CLR, RST: |
| 1703 |
op := &Operation{Type: status, Value: ""} |
| 1704 |
comm = append(comm, op) |
| 1705 |
status = UNSET |
| 1706 |
case ELEMENT: |
| 1707 |
case TAB, RET, PFX, SFX, SEP, LBL, TAG, ATT, ATR, END, PFC, DEQ, PLG, ELG, WRP, BKT, ENC, DEF, MSK, REG, EXP, WITH, GCODE, FRAME0, FRAME1, COLOR: |
| 1708 |
case CLS: |
| 1709 |
op := &Operation{Type: LBL, Value: ">"} |
| 1710 |
comm = append(comm, op) |
| 1711 |
status = UNSET |
| 1712 |
case SLF: |
| 1713 |
op := &Operation{Type: LBL, Value: " />"} |
| 1714 |
comm = append(comm, op) |
| 1715 |
status = UNSET |
| 1716 |
case FWD, AWD, PKG: |
| 1717 |
case UNSET: |
| 1718 |
DisplayError("No -element before '%s'", str) |
| 1719 |
os.Exit(1) |
| 1720 |
case UNRECOGNIZED: |
| 1721 |
DisplayError("Unrecognized argument '%s'", str) |
| 1722 |
os.Exit(1) |
| 1723 |
default: |
| 1724 |
if !isExtraction { |
| 1725 |
// not ELEMENT through HGVS |
| 1726 |
DisplayError("Misplaced %s command", str) |
| 1727 |
os.Exit(1) |
| 1728 |
} |
| 1729 |
} |
| 1730 |
|
| 1731 |
return status, isExtraction |
| 1732 |
} |
| 1733 |
|
| 1734 |
// parse extraction clause into individual steps |
| 1735 |
parseSteps := func(op *Operation, pttrn string) { |
| 1736 |
|
| 1737 |
if op == nil { |
| 1738 |
return |
| 1739 |
} |
| 1740 |
|
| 1741 |
stat := op.Type |
| 1742 |
str := op.Value |
| 1743 |
|
| 1744 |
// element names combined with commas are treated as a prefix-separator-suffix group |
| 1745 |
comma := strings.Split(str, ",") |
| 1746 |
|
| 1747 |
rnge := "" |
| 1748 |
for _, item := range comma { |
| 1749 |
status := stat |
| 1750 |
|
| 1751 |
// isolate and parse optional [min:max], [&VAR:&VAR], or [after|before] range specification |
| 1752 |
item, rnge = SplitInTwoLeft(item, "[") |
| 1753 |
|
| 1754 |
item = strings.TrimSpace(item) |
| 1755 |
rnge = strings.TrimSpace(rnge) |
| 1756 |
|
| 1757 |
if item == "" && rnge != "" { |
| 1758 |
// rnge should already end with right square bracket |
| 1759 |
DisplayError("Variable missing in range specification [%s", rnge) |
| 1760 |
os.Exit(1) |
| 1761 |
} |
| 1762 |
|
| 1763 |
typL, strL, intL, typR, strR, intR := parseRange(item, rnge) |
| 1764 |
|
| 1765 |
// check for special character at beginning of name |
| 1766 |
if len(item) > 1 { |
| 1767 |
switch item[0] { |
| 1768 |
case '&': |
| 1769 |
if IsAllCapsOrDigits(item[1:]) { |
| 1770 |
status = VARIABLE |
| 1771 |
item = item[1:] |
| 1772 |
} else { |
| 1773 |
DisplayError("Unrecognized variable '%s'", item) |
| 1774 |
os.Exit(1) |
| 1775 |
} |
| 1776 |
case '#': |
| 1777 |
status = COUNT |
| 1778 |
item = item[1:] |
| 1779 |
case '%': |
| 1780 |
status = LENGTH |
| 1781 |
item = item[1:] |
| 1782 |
case '^': |
| 1783 |
status = DEPTH |
| 1784 |
item = item[1:] |
| 1785 |
case '*': |
| 1786 |
for _, ch := range item { |
| 1787 |
if ch != '*' { |
| 1788 |
break |
| 1789 |
} |
| 1790 |
} |
| 1791 |
status = STAR |
| 1792 |
default: |
| 1793 |
} |
| 1794 |
} else { |
| 1795 |
switch item { |
| 1796 |
case "?": |
| 1797 |
status = QUESTION |
| 1798 |
case "~": |
| 1799 |
status = TILDE |
| 1800 |
case ".": |
| 1801 |
status = DOT |
| 1802 |
case "%": |
| 1803 |
status = PRCNT |
| 1804 |
case "*": |
| 1805 |
status = STAR |
| 1806 |
case "$": |
| 1807 |
status = DOLLAR |
| 1808 |
case "@": |
| 1809 |
status = ATSIGN |
| 1810 |
case "+": |
| 1811 |
status = INDEX |
| 1812 |
default: |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
// parse parent/element@attribute construct |
| 1817 |
// colon indicates a namespace prefix in any or all of the components |
| 1818 |
prnt, match := SplitInTwoRight(item, "/") |
| 1819 |
match, attrib := SplitInTwoLeft(match, "@") |
| 1820 |
|
| 1821 |
// leading colon indicates namespace prefix wildcard |
| 1822 |
wildcard := false |
| 1823 |
if strings.HasPrefix(prnt, ":") || strings.HasPrefix(match, ":") || strings.HasPrefix(attrib, ":") { |
| 1824 |
wildcard = true |
| 1825 |
} |
| 1826 |
|
| 1827 |
// sequence coordinate adjustments |
| 1828 |
switch status { |
| 1829 |
case ZEROBASED, ONEBASED, UCSCBASED: |
| 1830 |
seq := pttrn + ":" |
| 1831 |
if attrib != "" { |
| 1832 |
seq += "@" |
| 1833 |
seq += attrib |
| 1834 |
} else if match != "" { |
| 1835 |
seq += match |
| 1836 |
} |
| 1837 |
// confirm -0-based or -1-based arguments are known sequence position elements or attributes |
| 1838 |
slock.RLock() |
| 1839 |
seqtype, ok := sequenceTypeIs[seq] |
| 1840 |
slock.RUnlock() |
| 1841 |
if !ok { |
| 1842 |
DisplayError("Element '%s' is not suitable for sequence coordinate conversion", item) |
| 1843 |
os.Exit(1) |
| 1844 |
} |
| 1845 |
switch status { |
| 1846 |
case ZEROBASED: |
| 1847 |
status = ELEMENT |
| 1848 |
// if 1-based coordinates, decrement to get 0-based value |
| 1849 |
if seqtype.Based == 1 { |
| 1850 |
status = DECR |
| 1851 |
} |
| 1852 |
case ONEBASED: |
| 1853 |
status = ELEMENT |
| 1854 |
// if 0-based coordinates, increment to get 1-based value |
| 1855 |
if seqtype.Based == 0 { |
| 1856 |
status = INCR |
| 1857 |
} |
| 1858 |
case UCSCBASED: |
| 1859 |
status = ELEMENT |
| 1860 |
// half-open intervals, start is 0-based, stop is 1-based |
| 1861 |
if seqtype.Based == 0 && seqtype.Which == ISSTOP { |
| 1862 |
status = INCR |
| 1863 |
} else if seqtype.Based == 1 && seqtype.Which == ISSTART { |
| 1864 |
status = DECR |
| 1865 |
} |
| 1866 |
default: |
| 1867 |
status = ELEMENT |
| 1868 |
} |
| 1869 |
default: |
| 1870 |
} |
| 1871 |
|
| 1872 |
norm := true |
| 1873 |
if rnge != "" { |
| 1874 |
if typL != NORANGE || typR != NORANGE || strL != "" || strR != "" || intL != 0 || intR != 0 { |
| 1875 |
norm = false |
| 1876 |
} |
| 1877 |
} |
| 1878 |
|
| 1879 |
unescape := (status != INDEXER && status != RAW) |
| 1880 |
|
| 1881 |
tsk := &Step{Type: status, Value: item, Parent: prnt, Match: match, Attrib: attrib, |
| 1882 |
TypL: typL, StrL: strL, IntL: intL, TypR: typR, StrR: strR, IntR: intR, |
| 1883 |
Norm: norm, Wild: wildcard, Unesc: unescape} |
| 1884 |
|
| 1885 |
op.Stages = append(op.Stages, tsk) |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
idx := 0 |
| 1890 |
|
| 1891 |
status := UNSET |
| 1892 |
isExtraction := false |
| 1893 |
|
| 1894 |
// parse command strings into operation structure |
| 1895 |
for idx < max { |
| 1896 |
str := arguments[idx] |
| 1897 |
idx++ |
| 1898 |
|
| 1899 |
if argTypeIs[str] == CONDITIONAL { |
| 1900 |
DisplayError("Misplaced %s command", str) |
| 1901 |
os.Exit(1) |
| 1902 |
} |
| 1903 |
|
| 1904 |
switch status { |
| 1905 |
case UNSET: |
| 1906 |
status, isExtraction = nextStatus(str, arguments[idx:]) |
| 1907 |
case TAB, RET, PFX, SFX, SEP, LBL, CLS, SLF, PFC, DEQ, PLG, ELG, WRP, BKT, ENC, DEF, MSK, REG, EXP, WITH, GCODE, FRAME0, FRAME1, COLOR: |
| 1908 |
op := &Operation{Type: status, Value: ConvertSlash(str)} |
| 1909 |
comm = append(comm, op) |
| 1910 |
status = UNSET |
| 1911 |
case TAG: |
| 1912 |
// when starting to construct XML tag and attributes from components, first clear -tab and -sep values |
| 1913 |
op := &Operation{Type: TAB, Value: ""} |
| 1914 |
comm = append(comm, op) |
| 1915 |
op = &Operation{Type: SEP, Value: ""} |
| 1916 |
comm = append(comm, op) |
| 1917 |
// TAG variant of LBL sets wrp flag for automatic content reencoding |
| 1918 |
op = &Operation{Type: TAG, Value: "<" + ConvertSlash(str)} |
| 1919 |
comm = append(comm, op) |
| 1920 |
status = UNSET |
| 1921 |
case ATT: |
| 1922 |
if idx < max { |
| 1923 |
// -att takes key and literal string value |
| 1924 |
val := arguments[idx] |
| 1925 |
idx++ |
| 1926 |
if val != "" { |
| 1927 |
op := &Operation{Type: LBL, Value: " " + ConvertSlash(str) + "=" + "\"" + ConvertSlash(val) + "\""} |
| 1928 |
comm = append(comm, op) |
| 1929 |
} |
| 1930 |
} |
| 1931 |
status = UNSET |
| 1932 |
case ATR: |
| 1933 |
if idx < max { |
| 1934 |
// -atr takes key and object or &variable name |
| 1935 |
val := arguments[idx] |
| 1936 |
idx++ |
| 1937 |
if val != "" { |
| 1938 |
op := &Operation{Type: LBL, Value: " " + ConvertSlash(str) + "=" + "\""} |
| 1939 |
comm = append(comm, op) |
| 1940 |
op = &Operation{Type: ELEMENT, Value: val} |
| 1941 |
comm = append(comm, op) |
| 1942 |
parseSteps(op, pttrn) |
| 1943 |
op = &Operation{Type: LBL, Value: "\""} |
| 1944 |
comm = append(comm, op) |
| 1945 |
} |
| 1946 |
} |
| 1947 |
status = UNSET |
| 1948 |
case END: |
| 1949 |
op := &Operation{Type: LBL, Value: "</" + ConvertSlash(str) + ">"} |
| 1950 |
comm = append(comm, op) |
| 1951 |
status = UNSET |
| 1952 |
case FWD: |
| 1953 |
cmds.Foreword = ConvertSlash(str) |
| 1954 |
status = UNSET |
| 1955 |
case AWD: |
| 1956 |
cmds.Afterword = ConvertSlash(str) |
| 1957 |
status = UNSET |
| 1958 |
case PKG: |
| 1959 |
pkg := ConvertSlash(str) |
| 1960 |
cmds.Foreword = "" |
| 1961 |
cmds.Afterword = "" |
| 1962 |
if pkg != "" && pkg != "-" { |
| 1963 |
items := strings.Split(pkg, "/") |
| 1964 |
for i := range len(items) { |
| 1965 |
cmds.Foreword += "<" + items[i] + ">" |
| 1966 |
} |
| 1967 |
for i := len(items) - 1; i >= 0; i-- { |
| 1968 |
cmds.Afterword += "</" + items[i] + ">" |
| 1969 |
} |
| 1970 |
} |
| 1971 |
status = UNSET |
| 1972 |
case VARIABLE: |
| 1973 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1974 |
comm = append(comm, op) |
| 1975 |
status = VALUE |
| 1976 |
case ACCUMULATOR: |
| 1977 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1978 |
comm = append(comm, op) |
| 1979 |
status = VALUE |
| 1980 |
case CONVERTER: |
| 1981 |
op := &Operation{Type: status, Value: removeLeadingHyphens(str)} |
| 1982 |
comm = append(comm, op) |
| 1983 |
// CONVERTER sets next status to UNSET, not VALUE |
| 1984 |
status = UNSET |
| 1985 |
case VALUE: |
| 1986 |
op := &Operation{Type: status, Value: str} |
| 1987 |
comm = append(comm, op) |
| 1988 |
parseSteps(op, pttrn) |
| 1989 |
status = UNSET |
| 1990 |
case UNRECOGNIZED: |
| 1991 |
DisplayError("Unrecognized argument '%s'", str) |
| 1992 |
os.Exit(1) |
| 1993 |
default: |
| 1994 |
if isExtraction { |
| 1995 |
// ELEMENT through HGVS |
| 1996 |
for !strings.HasPrefix(str, "-") { |
| 1997 |
// create one operation per argument, even if under a single -element statement |
| 1998 |
op := &Operation{Type: status, Value: str} |
| 1999 |
comm = append(comm, op) |
| 2000 |
parseSteps(op, pttrn) |
| 2001 |
if idx >= max { |
| 2002 |
break |
| 2003 |
} |
| 2004 |
str = arguments[idx] |
| 2005 |
idx++ |
| 2006 |
} |
| 2007 |
status = UNSET |
| 2008 |
if idx < max { |
| 2009 |
status, isExtraction = nextStatus(str, arguments[idx:]) |
| 2010 |
} |
| 2011 |
} |
| 2012 |
} |
| 2013 |
} |
| 2014 |
|
| 2015 |
return comm |
| 2016 |
} |
| 2017 |
|
| 2018 |
// parseOperations recursive definition |
| 2019 |
var parseOperations func(parent *Block) |
| 2020 |
|
| 2021 |
// parseOperations converts parsed arguments to operations lists |
| 2022 |
parseOperations = func(parent *Block) { |
| 2023 |
|
| 2024 |
args := parent.Parsed |
| 2025 |
|
| 2026 |
partition := 0 |
| 2027 |
for cur, str := range args { |
| 2028 |
|
| 2029 |
// record junction between conditional and extraction commands |
| 2030 |
partition = cur + 1 |
| 2031 |
|
| 2032 |
// skip if not a command |
| 2033 |
if len(str) < 1 || str[0] != '-' { |
| 2034 |
continue |
| 2035 |
} |
| 2036 |
|
| 2037 |
if argTypeIs[str] != CONDITIONAL { |
| 2038 |
partition = cur |
| 2039 |
break |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
// split arguments into conditional tests and extraction or customization commands |
| 2044 |
conditionals := args[0:partition] |
| 2045 |
args = args[partition:] |
| 2046 |
|
| 2047 |
partition = 0 |
| 2048 |
foundElse := false |
| 2049 |
for cur, str := range args { |
| 2050 |
|
| 2051 |
// record junction at -else command |
| 2052 |
partition = cur + 1 |
| 2053 |
|
| 2054 |
// skip if not a command |
| 2055 |
if len(str) < 1 || str[0] != '-' { |
| 2056 |
continue |
| 2057 |
} |
| 2058 |
|
| 2059 |
if str == "-else" { |
| 2060 |
partition = cur |
| 2061 |
foundElse = true |
| 2062 |
break |
| 2063 |
} |
| 2064 |
} |
| 2065 |
|
| 2066 |
extractions := args[0:partition] |
| 2067 |
alternative := args[partition:] |
| 2068 |
|
| 2069 |
if len(alternative) > 0 && alternative[0] == "-else" { |
| 2070 |
alternative = alternative[1:] |
| 2071 |
} |
| 2072 |
|
| 2073 |
// validate argument structure and convert to operations lists |
| 2074 |
parent.Conditions = parseConditionals(parent, conditionals) |
| 2075 |
parent.Commands = parseExtractions(parent, extractions) |
| 2076 |
parent.Failure = parseExtractions(parent, alternative) |
| 2077 |
|
| 2078 |
// reality checks on placement of -else command |
| 2079 |
if foundElse { |
| 2080 |
if len(conditionals) < 1 { |
| 2081 |
DisplayError("Misplaced -else command") |
| 2082 |
os.Exit(1) |
| 2083 |
} |
| 2084 |
if len(alternative) < 1 { |
| 2085 |
DisplayError("Misplaced -else command") |
| 2086 |
os.Exit(1) |
| 2087 |
} |
| 2088 |
if len(parent.Subtasks) > 0 { |
| 2089 |
DisplayError("Misplaced -else command") |
| 2090 |
os.Exit(1) |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
for _, sub := range parent.Subtasks { |
| 2095 |
parseOperations(sub) |
| 2096 |
} |
| 2097 |
} |
| 2098 |
|
| 2099 |
// preprocess arguments to work around -pkg artifact |
| 2100 |
|
| 2101 |
var expargs []string |
| 2102 |
|
| 2103 |
lastLevel := "" |
| 2104 |
lastVisit := "" |
| 2105 |
isExplore := false |
| 2106 |
|
| 2107 |
// add second exploration level to "-block INSDFeature -pkg feat_intervals" |
| 2108 |
// resulting in "-block INSDFeature -blockX INSDFeature -pkg feat_intervals" |
| 2109 |
for _, txt := range cmdargs { |
| 2110 |
|
| 2111 |
if argTypeIs[txt] == EXPLORATION && txt != "-pattern" && txt != "-Pattern" { |
| 2112 |
|
| 2113 |
// extra exploration arguments ending in X are for internal use only |
| 2114 |
if strings.HasSuffix(txt, "X") { |
| 2115 |
DisplayError("Unrecognized argument '%s'", txt) |
| 2116 |
os.Exit(1) |
| 2117 |
} |
| 2118 |
|
| 2119 |
expargs = append(expargs, txt) |
| 2120 |
|
| 2121 |
lastLevel = txt |
| 2122 |
lastVisit = "" |
| 2123 |
isExplore = true |
| 2124 |
|
| 2125 |
continue |
| 2126 |
} |
| 2127 |
|
| 2128 |
if isExplore { |
| 2129 |
|
| 2130 |
expargs = append(expargs, txt) |
| 2131 |
|
| 2132 |
// get last component of parent/child or multi-level exploration path |
| 2133 |
if strings.Contains(txt, "/") && !strings.Contains(txt, ".") { |
| 2134 |
txt = strings.Replace(txt, "/", ".", -1) |
| 2135 |
} |
| 2136 |
lstidx := strings.LastIndex(txt, ".") |
| 2137 |
if lstidx >= 0 { |
| 2138 |
txt = txt[lstidx+1:] |
| 2139 |
} |
| 2140 |
|
| 2141 |
lastVisit = txt |
| 2142 |
isExplore = false |
| 2143 |
|
| 2144 |
continue |
| 2145 |
} |
| 2146 |
|
| 2147 |
// -pkg adds additional exploration level |
| 2148 |
if txt == "-pkg" && lastLevel != "" && lastVisit != "" && lastVisit != "*" { |
| 2149 |
expargs = append(expargs, lastLevel+"X") |
| 2150 |
expargs = append(expargs, lastVisit) |
| 2151 |
} |
| 2152 |
|
| 2153 |
expargs = append(expargs, txt) |
| 2154 |
} |
| 2155 |
|
| 2156 |
// ParseArguments |
| 2157 |
|
| 2158 |
head := &Block{} |
| 2159 |
|
| 2160 |
for _, txt := range expargs { |
| 2161 |
head.Working = append(head.Working, txt) |
| 2162 |
} |
| 2163 |
|
| 2164 |
// initial parsing of exploration command structure |
| 2165 |
parseCommands(head, PATTERN) |
| 2166 |
|
| 2167 |
if len(head.Subtasks) != 1 { |
| 2168 |
return nil |
| 2169 |
} |
| 2170 |
|
| 2171 |
// skip past empty placeholder |
| 2172 |
head = head.Subtasks[0] |
| 2173 |
|
| 2174 |
// convert command strings to array of operations for faster processing |
| 2175 |
parseOperations(head) |
| 2176 |
|
| 2177 |
// check for no -element or multiple -pattern commands |
| 2178 |
noElement := true |
| 2179 |
noClose := true |
| 2180 |
numPatterns := 0 |
| 2181 |
for _, txt := range expargs { |
| 2182 |
if argTypeIs[txt] == EXTRACTION { |
| 2183 |
noElement = false |
| 2184 |
} |
| 2185 |
if txt == "-pattern" || txt == "-Pattern" { |
| 2186 |
numPatterns++ |
| 2187 |
} else if txt == "-select" { |
| 2188 |
noElement = false |
| 2189 |
head.Position = "select" |
| 2190 |
} else if txt == "-cls" || txt == "-slf" { |
| 2191 |
noClose = false |
| 2192 |
} |
| 2193 |
} |
| 2194 |
|
| 2195 |
if numPatterns < 1 { |
| 2196 |
DisplayError("No -pattern in command-line arguments") |
| 2197 |
os.Exit(1) |
| 2198 |
} |
| 2199 |
|
| 2200 |
if numPatterns > 1 { |
| 2201 |
DisplayError("Only one -pattern command is permitted") |
| 2202 |
os.Exit(1) |
| 2203 |
} |
| 2204 |
|
| 2205 |
if noElement && noClose { |
| 2206 |
DisplayError("No -element statement in argument list") |
| 2207 |
os.Exit(1) |
| 2208 |
} |
| 2209 |
|
| 2210 |
return head |
| 2211 |
} |
| 2212 |
|
| 2213 |
// PrettyArguments indents xtract arguments by exploration level |
| 2214 |
func PrettyArguments(cmdargs []string) { |
| 2215 |
|
| 2216 |
levels := make(map[LevelType]bool) |
| 2217 |
|
| 2218 |
hasLeading := false |
| 2219 |
isFirst := true |
| 2220 |
|
| 2221 |
for _, str := range cmdargs { |
| 2222 |
|
| 2223 |
ch, ok := HasLeadingUnicodeDash(str) |
| 2224 |
if ok { |
| 2225 |
DisplayError("Unicode dash %d replaced expected ASCII hyphen in '%s'", ch, str) |
| 2226 |
os.Exit(1) |
| 2227 |
} |
| 2228 |
|
| 2229 |
lev, ok := levTypeIs[str] |
| 2230 |
if ok { |
| 2231 |
levels[lev] = true |
| 2232 |
} else if isFirst { |
| 2233 |
hasLeading = true |
| 2234 |
} |
| 2235 |
|
| 2236 |
isFirst = false |
| 2237 |
} |
| 2238 |
|
| 2239 |
indents := make(map[LevelType]string) |
| 2240 |
|
| 2241 |
lev := PATTERN |
| 2242 |
spaces := " " |
| 2243 |
indent := 0 |
| 2244 |
if hasLeading { |
| 2245 |
indent += 2 |
| 2246 |
} |
| 2247 |
|
| 2248 |
for { |
| 2249 |
if lev < UNIT { |
| 2250 |
break |
| 2251 |
} |
| 2252 |
|
| 2253 |
if levels[lev] { |
| 2254 |
indents[lev] = spaces[0:indent] |
| 2255 |
indent += 2 |
| 2256 |
} |
| 2257 |
|
| 2258 |
lev-- |
| 2259 |
} |
| 2260 |
|
| 2261 |
isFirst = true |
| 2262 |
quote := "\"" |
| 2263 |
apos := "'" |
| 2264 |
|
| 2265 |
needsQuotes := func(str string) bool { |
| 2266 |
|
| 2267 |
for _, ch := range str { |
| 2268 |
if ch == ' ' || |
| 2269 |
ch == '!' || |
| 2270 |
ch == '#' || |
| 2271 |
ch == '$' || |
| 2272 |
ch == '%' || |
| 2273 |
ch == '&' || |
| 2274 |
ch == '(' || |
| 2275 |
ch == ')' || |
| 2276 |
ch == '*' || |
| 2277 |
ch == '+' || |
| 2278 |
ch == ',' || |
| 2279 |
ch == '.' || |
| 2280 |
ch == ':' || |
| 2281 |
ch == ';' || |
| 2282 |
ch == '=' || |
| 2283 |
ch == '?' || |
| 2284 |
ch == '@' || |
| 2285 |
ch == '[' || |
| 2286 |
ch == '\\' || |
| 2287 |
ch == ']' || |
| 2288 |
ch == '^' || |
| 2289 |
ch == '|' || |
| 2290 |
ch == '~' { |
| 2291 |
return true |
| 2292 |
} |
| 2293 |
} |
| 2294 |
|
| 2295 |
return false |
| 2296 |
} |
| 2297 |
|
| 2298 |
fmt.Fprintf(os.Stdout, "\nxtract") |
| 2299 |
for _, str := range cmdargs { |
| 2300 |
|
| 2301 |
lev, ok := levTypeIs[str] |
| 2302 |
if ok { |
| 2303 |
if isFirst && lev == PATTERN { |
| 2304 |
fmt.Fprintf(os.Stdout, " %s", str) |
| 2305 |
} else { |
| 2306 |
spc := indents[lev] |
| 2307 |
fmt.Fprintf(os.Stdout, " \\\n%s%s", spc, str) |
| 2308 |
} |
| 2309 |
} else if strings.Index(str, quote) >= 0 { |
| 2310 |
fmt.Fprintf(os.Stdout, " %s", apos+str+apos) |
| 2311 |
} else if needsQuotes(str) || str == "" || str == "-" { |
| 2312 |
fmt.Fprintf(os.Stdout, " %s", quote+str+quote) |
| 2313 |
} else { |
| 2314 |
fmt.Fprintf(os.Stdout, " %s", str) |
| 2315 |
} |
| 2316 |
|
| 2317 |
isFirst = false |
| 2318 |
} |
| 2319 |
fmt.Fprintf(os.Stdout, "\n\n") |
| 2320 |
} |
| 2321 |
|
| 2322 |
// printXMLtree supports XML compression styles selected by -element "*" through "****" |
| 2323 |
func printXMLtree(node *XMLNode, style IndentType, mask string, printAttrs bool, proc func(string)) { |
| 2324 |
|
| 2325 |
if node == nil || proc == nil { |
| 2326 |
return |
| 2327 |
} |
| 2328 |
|
| 2329 |
// WRAPPED is SUBTREE plus each attribute on its own line |
| 2330 |
wrapped := false |
| 2331 |
if style == WRAPPED { |
| 2332 |
style = SUBTREE |
| 2333 |
wrapped = true |
| 2334 |
} |
| 2335 |
|
| 2336 |
// INDENT is offset by two spaces to allow for parent tag, SUBTREE is not offset |
| 2337 |
initial := 1 |
| 2338 |
if style == SUBTREE { |
| 2339 |
style = INDENT |
| 2340 |
initial = 0 |
| 2341 |
} |
| 2342 |
|
| 2343 |
// array to speed up indentation |
| 2344 |
indentSpaces := []string{ |
| 2345 |
"", |
| 2346 |
" ", |
| 2347 |
" ", |
| 2348 |
" ", |
| 2349 |
" ", |
| 2350 |
" ", |
| 2351 |
" ", |
| 2352 |
" ", |
| 2353 |
" ", |
| 2354 |
" ", |
| 2355 |
} |
| 2356 |
|
| 2357 |
// indent a specified number of spaces |
| 2358 |
doIndent := func(indt int) { |
| 2359 |
i := indt |
| 2360 |
for i > 9 { |
| 2361 |
proc(" ") |
| 2362 |
i -= 10 |
| 2363 |
} |
| 2364 |
if i < 0 { |
| 2365 |
return |
| 2366 |
} |
| 2367 |
proc(indentSpaces[i]) |
| 2368 |
} |
| 2369 |
|
| 2370 |
// xtract -mixed -pattern article -mask "table-wrap,alternatives,inline-formula" -element "*" |
| 2371 |
masked := make(map[string]bool) |
| 2372 |
|
| 2373 |
comma := strings.Split(mask, ",") |
| 2374 |
for _, msk := range comma { |
| 2375 |
masked[msk] = true |
| 2376 |
} |
| 2377 |
|
| 2378 |
// doSubtree recursive definition |
| 2379 |
var doSubtree func(*XMLNode, int) |
| 2380 |
|
| 2381 |
doSubtree = func(curr *XMLNode, depth int) { |
| 2382 |
|
| 2383 |
// suppress if it would be an empty self-closing tag |
| 2384 |
if !IsNotJustWhitespace(curr.Attributes) && curr.Contents == "" && curr.Children == nil { |
| 2385 |
return |
| 2386 |
} |
| 2387 |
|
| 2388 |
if style == INDENT { |
| 2389 |
doIndent(depth) |
| 2390 |
} |
| 2391 |
|
| 2392 |
if curr.Name != "" { |
| 2393 |
proc("<") |
| 2394 |
proc(curr.Name) |
| 2395 |
|
| 2396 |
if printAttrs { |
| 2397 |
|
| 2398 |
attr := strings.TrimSpace(curr.Attributes) |
| 2399 |
attr = CompressRunsOfSpaces(attr) |
| 2400 |
|
| 2401 |
if attr != "" { |
| 2402 |
|
| 2403 |
if wrapped { |
| 2404 |
|
| 2405 |
start := 0 |
| 2406 |
idx := 0 |
| 2407 |
|
| 2408 |
attlen := len(attr) |
| 2409 |
|
| 2410 |
for idx < attlen { |
| 2411 |
ch := attr[idx] |
| 2412 |
if ch == '=' { |
| 2413 |
str := attr[start:idx] |
| 2414 |
proc("\n") |
| 2415 |
doIndent(depth) |
| 2416 |
proc(" ") |
| 2417 |
proc(str) |
| 2418 |
// skip past equal sign and leading double quote |
| 2419 |
idx += 2 |
| 2420 |
start = idx |
| 2421 |
} else if ch == '"' || ch == '\'' { |
| 2422 |
str := attr[start:idx] |
| 2423 |
proc("=\"") |
| 2424 |
proc(str) |
| 2425 |
proc("\"") |
| 2426 |
// skip past trailing double quote and (possible) space |
| 2427 |
idx += 2 |
| 2428 |
start = idx |
| 2429 |
} else { |
| 2430 |
idx++ |
| 2431 |
} |
| 2432 |
} |
| 2433 |
|
| 2434 |
proc("\n") |
| 2435 |
doIndent(depth) |
| 2436 |
|
| 2437 |
} else { |
| 2438 |
|
| 2439 |
proc(" ") |
| 2440 |
proc(attr) |
| 2441 |
} |
| 2442 |
} |
| 2443 |
} |
| 2444 |
|
| 2445 |
// see if suitable for for self-closing tag |
| 2446 |
if curr.Contents == "" && curr.Children == nil { |
| 2447 |
proc("/>") |
| 2448 |
if style != COMPACT { |
| 2449 |
proc("\n") |
| 2450 |
} |
| 2451 |
return |
| 2452 |
} |
| 2453 |
|
| 2454 |
proc(">") |
| 2455 |
} |
| 2456 |
|
| 2457 |
if curr.Contents != "" { |
| 2458 |
|
| 2459 |
proc(curr.Contents[:]) |
| 2460 |
|
| 2461 |
} else { |
| 2462 |
|
| 2463 |
if style != COMPACT { |
| 2464 |
proc("\n") |
| 2465 |
} |
| 2466 |
|
| 2467 |
for chld := curr.Children; chld != nil; chld = chld.Next { |
| 2468 |
// use -mask to selectively skip descent into container |
| 2469 |
if mask != "" && masked[chld.Name] { |
| 2470 |
continue |
| 2471 |
} |
| 2472 |
doSubtree(chld, depth+1) |
| 2473 |
} |
| 2474 |
|
| 2475 |
if style == INDENT { |
| 2476 |
i := depth |
| 2477 |
for i > 9 { |
| 2478 |
proc(" ") |
| 2479 |
i -= 10 |
| 2480 |
} |
| 2481 |
proc(indentSpaces[i]) |
| 2482 |
} |
| 2483 |
} |
| 2484 |
|
| 2485 |
if curr.Name != "" { |
| 2486 |
proc("<") |
| 2487 |
proc("/") |
| 2488 |
proc(curr.Name) |
| 2489 |
proc(">") |
| 2490 |
} |
| 2491 |
|
| 2492 |
if style != COMPACT { |
| 2493 |
proc("\n") |
| 2494 |
} |
| 2495 |
} |
| 2496 |
|
| 2497 |
doSubtree(node, initial) |
| 2498 |
} |
| 2499 |
|
| 2500 |
// printASNtree prints ASN.1 selected by -element "." |
| 2501 |
func printASNtree(node *XMLNode, proc func(string)) { |
| 2502 |
|
| 2503 |
if node == nil || proc == nil { |
| 2504 |
return |
| 2505 |
} |
| 2506 |
|
| 2507 |
// array to speed up indentation |
| 2508 |
indentSpaces := []string{ |
| 2509 |
"", |
| 2510 |
" ", |
| 2511 |
" ", |
| 2512 |
" ", |
| 2513 |
" ", |
| 2514 |
" ", |
| 2515 |
" ", |
| 2516 |
" ", |
| 2517 |
" ", |
| 2518 |
" ", |
| 2519 |
} |
| 2520 |
|
| 2521 |
// indent a specified number of spaces |
| 2522 |
doIndent := func(indt int) { |
| 2523 |
i := indt |
| 2524 |
for i > 9 { |
| 2525 |
proc(" ") |
| 2526 |
i -= 10 |
| 2527 |
} |
| 2528 |
if i < 0 { |
| 2529 |
return |
| 2530 |
} |
| 2531 |
proc(indentSpaces[i]) |
| 2532 |
} |
| 2533 |
|
| 2534 |
afix := strings.NewReplacer( |
| 2535 |
"<", "<", |
| 2536 |
">", ">", |
| 2537 |
"&", "&", |
| 2538 |
"'", "'", |
| 2539 |
"'", "'", |
| 2540 |
""", "'", |
| 2541 |
""", "'", |
| 2542 |
"\"", "'", |
| 2543 |
) |
| 2544 |
|
| 2545 |
// doASNtree recursive definition |
| 2546 |
var doASNtree func(*XMLNode, int, bool) |
| 2547 |
|
| 2548 |
doASNtree = func(curr *XMLNode, depth int, comma bool) { |
| 2549 |
|
| 2550 |
// suppress if it would be an empty self-closing tag |
| 2551 |
if !IsNotJustWhitespace(curr.Attributes) && curr.Contents == "" && curr.Children == nil { |
| 2552 |
return |
| 2553 |
} |
| 2554 |
|
| 2555 |
name := curr.Name |
| 2556 |
if name == "" { |
| 2557 |
name = "_" |
| 2558 |
} |
| 2559 |
|
| 2560 |
// just an underscore - unnamed braces (element of SEQUENCE OF or SET OF structured objects) |
| 2561 |
// trailing underscore - unquoted value |
| 2562 |
// internal underscore - convert to space |
| 2563 |
show := true |
| 2564 |
quot := true |
| 2565 |
if name == "_" { |
| 2566 |
show = false |
| 2567 |
} else if strings.HasPrefix(name, "_") { |
| 2568 |
// if leading underscore, ignore remainder of name |
| 2569 |
show = false |
| 2570 |
} else if strings.HasSuffix(name, "_") { |
| 2571 |
name = strings.TrimSuffix(name, "_") |
| 2572 |
quot = false |
| 2573 |
} |
| 2574 |
name = strings.Replace(name, "_", " ", -1) |
| 2575 |
name = strings.TrimSpace(name) |
| 2576 |
|
| 2577 |
if curr.Contents != "" { |
| 2578 |
|
| 2579 |
doIndent(depth) |
| 2580 |
proc(name) |
| 2581 |
proc(" ") |
| 2582 |
|
| 2583 |
if quot { |
| 2584 |
proc("\"") |
| 2585 |
} |
| 2586 |
|
| 2587 |
str := curr.Contents[:] |
| 2588 |
if HasBadSpace(str) { |
| 2589 |
str = CleanupBadSpaces(str) |
| 2590 |
} |
| 2591 |
if IsNotASCII(str) { |
| 2592 |
str = TransformAccents(str, false, false) |
| 2593 |
} |
| 2594 |
if HasAdjacentSpaces(str) { |
| 2595 |
str = CompressRunsOfSpaces(str) |
| 2596 |
} |
| 2597 |
str = afix.Replace(str) |
| 2598 |
proc(str) |
| 2599 |
|
| 2600 |
if quot { |
| 2601 |
proc("\"") |
| 2602 |
} |
| 2603 |
|
| 2604 |
} else { |
| 2605 |
|
| 2606 |
doIndent(depth) |
| 2607 |
if show { |
| 2608 |
proc(name) |
| 2609 |
proc(" ") |
| 2610 |
} |
| 2611 |
if depth == 0 { |
| 2612 |
proc("::= ") |
| 2613 |
} |
| 2614 |
proc("{\n") |
| 2615 |
|
| 2616 |
for chld := curr.Children; chld != nil; chld = chld.Next { |
| 2617 |
// do not print comma after last child object in chain |
| 2618 |
doASNtree(chld, depth+1, (chld.Next != nil)) |
| 2619 |
} |
| 2620 |
|
| 2621 |
doIndent(depth) |
| 2622 |
proc("}") |
| 2623 |
} |
| 2624 |
|
| 2625 |
if comma { |
| 2626 |
proc(",") |
| 2627 |
} |
| 2628 |
proc("\n") |
| 2629 |
} |
| 2630 |
|
| 2631 |
doASNtree(node, 0, false) |
| 2632 |
} |
| 2633 |
|
| 2634 |
// printJSONtree prints JSON selected by -element "%" |
| 2635 |
func printJSONtree(node *XMLNode, proc func(string)) { |
| 2636 |
|
| 2637 |
// COPIED FROM printASNtree, MODIFICATIONS NOT YET COMPLETE |
| 2638 |
|
| 2639 |
if node == nil || proc == nil { |
| 2640 |
return |
| 2641 |
} |
| 2642 |
|
| 2643 |
// array to speed up indentation |
| 2644 |
indentSpaces := []string{ |
| 2645 |
"", |
| 2646 |
" ", |
| 2647 |
" ", |
| 2648 |
" ", |
| 2649 |
" ", |
| 2650 |
" ", |
| 2651 |
" ", |
| 2652 |
" ", |
| 2653 |
" ", |
| 2654 |
" ", |
| 2655 |
} |
| 2656 |
|
| 2657 |
// indent a specified number of spaces |
| 2658 |
doIndent := func(indt int) { |
| 2659 |
i := indt |
| 2660 |
for i > 9 { |
| 2661 |
proc(" ") |
| 2662 |
i -= 10 |
| 2663 |
} |
| 2664 |
if i < 0 { |
| 2665 |
return |
| 2666 |
} |
| 2667 |
proc(indentSpaces[i]) |
| 2668 |
} |
| 2669 |
|
| 2670 |
// doJSONtree recursive definition |
| 2671 |
var doJSONtree func(*XMLNode, int, bool) |
| 2672 |
|
| 2673 |
doJSONtree = func(curr *XMLNode, depth int, comma bool) { |
| 2674 |
|
| 2675 |
// suppress if it would be an empty self-closing tag |
| 2676 |
if !IsNotJustWhitespace(curr.Attributes) && curr.Contents == "" && curr.Children == nil { |
| 2677 |
return |
| 2678 |
} |
| 2679 |
|
| 2680 |
name := curr.Name |
| 2681 |
if name == "" { |
| 2682 |
name = "_" |
| 2683 |
} |
| 2684 |
|
| 2685 |
// just an underscore - unnamed brackets |
| 2686 |
// leading underscore - array instead of object |
| 2687 |
// trailing underscore - unquoted value |
| 2688 |
show := true |
| 2689 |
array := false |
| 2690 |
quot := true |
| 2691 |
if name == "_" { |
| 2692 |
show = false |
| 2693 |
} else if strings.HasPrefix(name, "_") { |
| 2694 |
array = true |
| 2695 |
} else if strings.HasSuffix(name, "_") { |
| 2696 |
name = strings.TrimSuffix(name, "_") |
| 2697 |
quot = false |
| 2698 |
} |
| 2699 |
name = strings.Replace(name, "_", " ", -1) |
| 2700 |
name = strings.TrimSpace(name) |
| 2701 |
|
| 2702 |
if curr.Contents != "" { |
| 2703 |
|
| 2704 |
doIndent(depth) |
| 2705 |
proc("\"") |
| 2706 |
proc(name) |
| 2707 |
proc("\": ") |
| 2708 |
|
| 2709 |
if quot { |
| 2710 |
proc("\"") |
| 2711 |
} |
| 2712 |
|
| 2713 |
str := curr.Contents[:] |
| 2714 |
if HasBadSpace(str) { |
| 2715 |
str = CleanupBadSpaces(str) |
| 2716 |
} |
| 2717 |
if IsNotASCII(str) { |
| 2718 |
str = TransformAccents(str, false, false) |
| 2719 |
} |
| 2720 |
if HasAdjacentSpaces(str) { |
| 2721 |
str = CompressRunsOfSpaces(str) |
| 2722 |
} |
| 2723 |
proc(str) |
| 2724 |
|
| 2725 |
if quot { |
| 2726 |
proc("\"") |
| 2727 |
} |
| 2728 |
|
| 2729 |
} else { |
| 2730 |
|
| 2731 |
doIndent(depth) |
| 2732 |
if show && depth > 0 { |
| 2733 |
proc("\"") |
| 2734 |
proc(name) |
| 2735 |
proc("\": ") |
| 2736 |
} |
| 2737 |
if array { |
| 2738 |
proc("[") |
| 2739 |
} else { |
| 2740 |
proc("{") |
| 2741 |
} |
| 2742 |
proc("\n") |
| 2743 |
|
| 2744 |
for chld := curr.Children; chld != nil; chld = chld.Next { |
| 2745 |
// do not print comma after last child object in chain |
| 2746 |
doJSONtree(chld, depth+1, (chld.Next != nil)) |
| 2747 |
} |
| 2748 |
|
| 2749 |
doIndent(depth) |
| 2750 |
if array { |
| 2751 |
proc("]") |
| 2752 |
} else { |
| 2753 |
proc("}") |
| 2754 |
} |
| 2755 |
} |
| 2756 |
|
| 2757 |
if comma { |
| 2758 |
proc(",") |
| 2759 |
} |
| 2760 |
proc("\n") |
| 2761 |
} |
| 2762 |
|
| 2763 |
doJSONtree(node, 0, false) |
| 2764 |
} |