394
396
entries.append(Comment(lineno, col_offset + cindex, comment))
395
397
return WhiteList(initial_lineno, col_offset, entries)
401
""" node representing a syntax error """
402
msg = F("message", str)
406
""" node representing a bit of text """
407
text = F("text", str)
410
class FieldOverride(Node):
411
""" node representing a single override statement """
413
value = F("value to apply (override value)", Text)
414
pattern = F("pattern that selects things to override", Re)
418
text: str, lineno: int=1, col_offset: int=0
419
) -> "Union[FieldOverride, Error]":
421
Parse a single test plan field override line
423
Using correct syntax will result in a FieldOverride node with
424
appropriate data in the ``value`` and ``pattern`` fields. Note that
425
``pattern`` may be either a :class:`RePattern` or a :class:`ReFixed`.
427
>>> FieldOverride.parse("apply new-value to pattern")
428
... # doctest: +NORMALIZE_WHITESPACE
429
FieldOverride(lineno=1, col_offset=0,
430
value=Text(lineno=1, col_offset=0, text='new-value'),
431
pattern=ReFixed(lineno=1, col_offset=0,
433
>>> FieldOverride.parse("apply blocker to .*")
434
... # doctest: +NORMALIZE_WHITESPACE
435
FieldOverride(lineno=1, col_offset=0,
436
value=Text(lineno=1, col_offset=0, text='blocker'),
437
pattern=RePattern(lineno=1, col_offset=0, text='.*',
438
re=re.compile('.*')))
440
Using incorrect syntax will result in a single Error node being
441
returned. The message (``msg``) field contains useful information on
442
the cause of the problem, as depicted below:
444
>>> FieldOverride.parse("")
445
Error(lineno=1, col_offset=0, msg="expected 'apply' near ''")
446
>>> FieldOverride.parse("apply")
447
Error(lineno=1, col_offset=0, msg='expected override value')
448
>>> FieldOverride.parse("apply value")
449
Error(lineno=1, col_offset=0, msg="expected 'to' near ''")
450
>>> FieldOverride.parse("apply value to")
451
Error(lineno=1, col_offset=0, msg='expected override pattern')
452
>>> FieldOverride.parse("apply value to pattern junk")
453
Error(lineno=1, col_offset=0, msg="unexpected garbage: 'junk'")
455
Lastly, shell-style comments are supported. They are discarded by the
458
>>> FieldOverride.parse("apply value to pattern # comment")
459
... # doctest: +NORMALIZE_WHITESPACE
460
FieldOverride(lineno=1, col_offset=0,
461
value=Text(lineno=1, col_offset=0, text='value'),
462
pattern=ReFixed(lineno=1, col_offset=0,
466
# XXX Until our home-grown scanner is ready col_offset values below
467
# are all dummy. This is not strictly critical but should be improved
469
scanner = WordScanner(text)
471
token, lexeme = scanner.get_token()
472
if token != scanner.TokenEnum.WORD or lexeme != 'apply':
473
return Error(lineno, col_offset,
474
_("expected {!a} near {!r}").format('apply', lexeme))
476
token, lexeme = scanner.get_token()
477
if token != scanner.TokenEnum.WORD:
478
return Error(lineno, col_offset, _("expected override value"))
479
value = Text(lineno, col_offset, lexeme)
480
# 'APPLY' VALUE 'TO' ...
481
token, lexeme = scanner.get_token()
482
if token != scanner.TokenEnum.WORD or lexeme != 'to':
483
return Error(lineno, col_offset,
484
_("expected {!a} near {!r}").format('to', lexeme))
485
# 'APPLY' VALUE 'TO' PATTERN...
486
token, lexeme = scanner.get_token()
487
if token != scanner.TokenEnum.WORD:
488
return Error(lineno, col_offset, _("expected override pattern"))
489
pattern = Re.parse(lexeme, lineno, col_offset)
490
# 'APPLY' VALUE 'TO' PATTERN <EOF>
491
token, lexeme = scanner.get_token()
492
if token != scanner.TokenEnum.EOF:
493
return Error(lineno, col_offset,
494
_("unexpected garbage: {!r}").format(lexeme))
495
return FieldOverride(lineno, col_offset, value, pattern)
498
class OverrideFieldList(Node):
499
""" node representing a whole plainbox field override list"""
501
entries = pod.Field("a list of comments and patterns", list,
502
initial_fn=list, assign_filter_list=[
503
pod.typed, pod.typed.sequence(Node), pod.const])
507
text: str, lineno: int=1, col_offset: int=0
508
) -> "OverrideFieldList":
510
initial_lineno = lineno
511
# NOTE: lineno is consciously shadowed below
512
for lineno, line in enumerate(text.splitlines(), lineno):
513
entries.append(FieldOverride.parse(line, lineno, col_offset))
514
return OverrideFieldList(initial_lineno, col_offset, entries)