| | 696 | Tokens are produced by a YAML scanner. They are not really useful except for low-level YAML |
| | 697 | applications such as syntax highlighting. |
| | 698 | |
| | 699 | The PyYAML scanner produces the following types of tokens: |
| | 700 | {{{ |
| | 701 | #!python |
| | 702 | StreamStartToken(encoding, start_mark, end_mark) # Start of the stream. |
| | 703 | StreamEndToken(start_mark, end_mark) # End of the stream. |
| | 704 | DirectiveToken(name, value, start_mark, end_mark) # YAML directive, either %YAML or %TAG. |
| | 705 | DocumentStartToken(start_mark, end_mark) # '---'. |
| | 706 | DocumentEndToken(start_mark, end_mark) # '...'. |
| | 707 | BlockSequenceStartToken(start_mark, end_mark) # Start of a new block sequence. |
| | 708 | BlockMappingStartToken(start_mark, end_mark) # Start of a new block mapping. |
| | 709 | BlockEndToken(start_mark, end_mark) # End of a block collection. |
| | 710 | FlowSequenceStartToken(start_mark, end_mark) # '['. |
| | 711 | FlowMappingStartToken(start_mark, end_mark) # '{'. |
| | 712 | FlowSequenceEndToken(start_mark, end_mark) # ']'. |
| | 713 | FlowMappingEndToken(start_mark, end_mark) # '}'. |
| | 714 | KeyToken(start_mark, end_mark) # Either '?' or start of a simple key. |
| | 715 | ValueToken(start_mark, end_mark) # ':'. |
| | 716 | BlockEntryToken(start_mark, end_mark) # '-'. |
| | 717 | FlowEntryToken(start_mark, end_mark) # ','. |
| | 718 | AliasToken(value, start_mark, end_mark) # '*value'. |
| | 719 | AnchorToken(value, start_mark, end_mark) # '&value'. |
| | 720 | TagToken(value, start_mark, end_mark) # '!value'. |
| | 721 | ScalarToken(value, plain, style, start_mark, end_mark) # 'value'. |
| | 722 | }}} |
| | 723 | |
| | 724 | '''`start_mark`''' and '''`end_mark`''' denote the beginning and the and of a token. |
| | 725 | |
| | 726 | Example: |
| | 727 | {{{ |
| | 728 | #!python |
| | 729 | >>> document = """ |
| | 730 | ... --- |
| | 731 | ... block sequence: |
| | 732 | ... - BlockEntryToken |
| | 733 | ... block mapping: |
| | 734 | ... ? KeyToken |
| | 735 | ... : ValueToken |
| | 736 | ... flow sequence: [FlowEntryToken, FlowEntryToken] |
| | 737 | ... flow mapping: {KeyToken: ValueToken} |
| | 738 | ... anchors and tags: |
| | 739 | ... - &A !!int '5' |
| | 740 | ... - *A |
| | 741 | ... ... |
| | 742 | ... """ |
| | 743 | |
| | 744 | >>> for token in yaml.scan(document): |
| | 745 | ... print token |
| | 746 | |
| | 747 | StreamStartToken(encoding='utf-8') |
| | 748 | |
| | 749 | DocumentStartToken() |
| | 750 | |
| | 751 | BlockMappingStartToken() |
| | 752 | |
| | 753 | KeyToken() |
| | 754 | ScalarToken(plain=True, style=None, value=u'block sequence') |
| | 755 | |
| | 756 | ValueToken() |
| | 757 | BlockEntryToken() |
| | 758 | ScalarToken(plain=True, style=None, value=u'BlockEntryToken') |
| | 759 | |
| | 760 | KeyToken() |
| | 761 | ScalarToken(plain=True, style=None, value=u'block mapping') |
| | 762 | |
| | 763 | ValueToken() |
| | 764 | BlockMappingStartToken() |
| | 765 | KeyToken() |
| | 766 | ScalarToken(plain=True, style=None, value=u'KeyToken') |
| | 767 | ValueToken() |
| | 768 | ScalarToken(plain=True, style=None, value=u'ValueToken') |
| | 769 | BlockEndToken() |
| | 770 | |
| | 771 | KeyToken() |
| | 772 | ScalarToken(plain=True, style=None, value=u'flow sequence') |
| | 773 | |
| | 774 | ValueToken() |
| | 775 | FlowSequenceStartToken() |
| | 776 | ScalarToken(plain=True, style=None, value=u'FlowEntryToken') |
| | 777 | FlowEntryToken() |
| | 778 | ScalarToken(plain=True, style=None, value=u'FlowEntryToken') |
| | 779 | FlowSequenceEndToken() |
| | 780 | |
| | 781 | KeyToken() |
| | 782 | ScalarToken(plain=True, style=None, value=u'flow mapping') |
| | 783 | |
| | 784 | ValueToken() |
| | 785 | FlowMappingStartToken() |
| | 786 | KeyToken() |
| | 787 | ScalarToken(plain=True, style=None, value=u'KeyToken') |
| | 788 | ValueToken() |
| | 789 | ScalarToken(plain=True, style=None, value=u'ValueToken') |
| | 790 | FlowMappingEndToken() |
| | 791 | |
| | 792 | KeyToken() |
| | 793 | ScalarToken(plain=True, style=None, value=u'anchors and tags') |
| | 794 | |
| | 795 | ValueToken() |
| | 796 | BlockEntryToken() |
| | 797 | AnchorToken(value=u'A') |
| | 798 | TagToken(value=(u'!!', u'int')) |
| | 799 | ScalarToken(plain=False, style="'", value=u'5') |
| | 800 | BlockEntryToken() |
| | 801 | AliasToken(value=u'A') |
| | 802 | |
| | 803 | BlockEndToken() |
| | 804 | |
| | 805 | DocumentEndToken() |
| | 806 | |
| | 807 | StreamEndToken() |
| | 808 | }}} |
| | 809 | |
| | 813 | Events are used by the low-level Parser and Emitter interfaces, which are similar to the SAX API. |
| | 814 | While the Parser parses a YAML stream and produces a sequence of events, the Emitter accepts |
| | 815 | a sequence of events and emits a YAML stream. |
| | 816 | |
| | 817 | The following events are defined: |
| | 818 | {{{ |
| | 819 | #!python |
| | 820 | StreamStartEvent(encoding, start_mark, end_mark) |
| | 821 | StreamEndEvent(start_mark, end_mark) |
| | 822 | DocumentStartEvent(explicit, version, tags, start_mark, end_mark) |
| | 823 | DocumentEndEvent(start_mark, end_mark) |
| | 824 | SequenceStartEvent(anchor, tag, implicit, flow_style, start_mark, end_mark) |
| | 825 | SequenceEndEvent(start_mark, end_mark) |
| | 826 | MappingStartEvent(anchor, tag, implicit, flow_style, start_mark, end_mark) |
| | 827 | MappingEndEvent(start_mark, end_mark) |
| | 828 | AliasEvent(anchor, start_mark, end_mark) |
| | 829 | ScalarEvent(anchor, tag, implicit, value, style, start_mark, end_mark) |
| | 830 | }}} |
| | 831 | |
| | 832 | The '''`flow_style`''' flag indicates if a collection is block or flow. The possible values are |
| | 833 | `None`, `True`, `False`. The '''`style`''' flag of a scalar event indicates the style of the scalar. |
| | 834 | Possible values are `None`, `''`, `'\''`, `'"'`, `'|'`, `'>'`. The '''`implicit`''' flag of a collection |
| | 835 | start event indicates if the tag may be omitted when the collection is emitted. The '''`implicit`''' flag |
| | 836 | of a scalar event is a pair of boolean values that indicate if the tag may be omitted when the scalar |
| | 837 | is emitted in a plain and non-plain style correspondingly. |
| | 838 | |
| | 839 | Example: |
| | 840 | {{{ |
| | 841 | #!python |
| | 842 | >>> document = """ |
| | 843 | ... scalar: &A !!int '5' |
| | 844 | ... alias: *A |
| | 845 | ... sequence: [1, 2, 3] |
| | 846 | ... mapping: [1: one, 2: two, 3: three] |
| | 847 | ... """ |
| | 848 | |
| | 849 | >>> for event in yaml.parse(document): |
| | 850 | ... print event |
| | 851 | |
| | 852 | StreamStartEvent() |
| | 853 | |
| | 854 | DocumentStartEvent() |
| | 855 | |
| | 856 | MappingStartEvent(anchor=None, tag=None, implicit=True) |
| | 857 | |
| | 858 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'scalar') |
| | 859 | ScalarEvent(anchor=u'A', tag=u'tag:yaml.org,2002:int', implicit=(False, False), value=u'5') |
| | 860 | |
| | 861 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'alias') |
| | 862 | AliasEvent(anchor=u'A') |
| | 863 | |
| | 864 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'sequence') |
| | 865 | SequenceStartEvent(anchor=None, tag=None, implicit=True) |
| | 866 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'1') |
| | 867 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'2') |
| | 868 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'3') |
| | 869 | SequenceEndEvent() |
| | 870 | |
| | 871 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'mapping') |
| | 872 | MappingStartEvent(anchor=None, tag=None, implicit=True) |
| | 873 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'1') |
| | 874 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'one') |
| | 875 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'2') |
| | 876 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'two') |
| | 877 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'3') |
| | 878 | ScalarEvent(anchor=None, tag=None, implicit=(True, False), value=u'three') |
| | 879 | MappingEndEvent() |
| | 880 | |
| | 881 | MappingEndEvent() |
| | 882 | |
| | 883 | DocumentEndEvent() |
| | 884 | |
| | 885 | StreamEndEvent() |
| | 886 | |
| | 887 | >>> print yaml.emit([ |
| | 888 | ... yaml.StreamStartEvent(encoding='utf-8'), |
| | 889 | ... yaml.DocumentStartEvent(explicit=True), |
| | 890 | ... yaml.MappingStartEvent(anchor=None, tag=u'tag:yaml.org,2002:map', implicit=True, flow_style=False), |
| | 891 | ... yaml.ScalarEvent(anchor=None, tag=u'tag:yaml.org,2002:str', implicit=(True, True), value=u'agile languages'), |
| | 892 | ... yaml.SequenceStartEvent(anchor=None, tag=u'tag:yaml.org,2002:seq', implicit=True, flow_style=True), |
| | 893 | ... yaml.ScalarEvent(anchor=None, tag=u'tag:yaml.org,2002:str', implicit=(True, True), value=u'Python'), |
| | 894 | ... yaml.ScalarEvent(anchor=None, tag=u'tag:yaml.org,2002:str', implicit=(True, True), value=u'Perl'), |
| | 895 | ... yaml.ScalarEvent(anchor=None, tag=u'tag:yaml.org,2002:str', implicit=(True, True), value=u'Ruby'), |
| | 896 | ... yaml.SequenceEndEvent(), |
| | 897 | ... yaml.MappingEndEvent(), |
| | 898 | ... yaml.DocumentEndEvent(explicit=True), |
| | 899 | ... yaml.StreamEndEvent(), |
| | 900 | ... ]) |
| | 901 | |
| | 902 | --- |
| | 903 | agile languages: [Python, Perl, Ruby] |
| | 904 | ... |
| | 905 | }}} |
| | 906 | |