source: pyyaml/trunk/lib3/yaml/scanner.py @ 354

Revision 354, 50.7 KB checked in by xi, 5 years ago (diff)

Fixed a problem with a scanner error not detected when no line break at the end of the stream.

RevLine 
[39]1
[55]2# Scanner produces tokens of the following types:
[118]3# STREAM-START
4# STREAM-END
[55]5# DIRECTIVE(name, value)
6# DOCUMENT-START
7# DOCUMENT-END
8# BLOCK-SEQUENCE-START
9# BLOCK-MAPPING-START
10# BLOCK-END
11# FLOW-SEQUENCE-START
12# FLOW-MAPPING-START
13# FLOW-SEQUENCE-END
14# FLOW-MAPPING-END
15# BLOCK-ENTRY
16# FLOW-ENTRY
17# KEY
18# VALUE
19# ALIAS(value)
20# ANCHOR(value)
21# TAG(value)
[222]22# SCALAR(value, plain, style)
[57]23#
24# Read comments in the Scanner code for more details.
25#
[43]26
[46]27__all__ = ['Scanner', 'ScannerError']
[43]28
[328]29from .error import MarkedYAMLError
30from .tokens import *
[39]31
[52]32class ScannerError(MarkedYAMLError):
33    pass
[51]34
[328]35class SimpleKey:
[51]36    # See below simple keys treatment.
37
[116]38    def __init__(self, token_number, required, index, line, column, mark):
[43]39        self.token_number = token_number
40        self.required = required
41        self.index = index
42        self.line = line
43        self.column = column
[116]44        self.mark = mark
[43]45
[328]46class Scanner:
[39]47
[136]48    def __init__(self):
[39]49        """Initialize the scanner."""
[136]50        # It is assumed that Scanner and Reader will have a common descendant.
51        # Reader do the dirty work of checking for BOM and converting the
52        # input data to Unicode. It also adds NUL to the end.
[39]53        #
[46]54        # Reader supports the following methods
[136]55        #   self.peek(i=0)       # peek the next i-th character
56        #   self.prefix(l=1)     # peek the next l characters
57        #   self.forward(l=1)    # read the next l characters and move the pointer.
[39]58
59        # Had we reached the end of the stream?
60        self.done = False
61
62        # The number of unclosed '{' and '['. `flow_level == 0` means block
63        # context.
64        self.flow_level = 0
65
66        # List of processed tokens that are not yet emitted.
67        self.tokens = []
68
[118]69        # Add the STREAM-START token.
70        self.fetch_stream_start()
71
[39]72        # Number of tokens that were emitted through the `get_token` method.
73        self.tokens_taken = 0
74
75        # The current indentation level.
76        self.indent = -1
77
78        # Past indentation levels.
79        self.indents = []
80
[43]81        # Variables related to simple keys treatment.
[39]82
83        # A simple key is a key that is not denoted by the '?' indicator.
84        # Example of simple keys:
85        #   ---
86        #   block simple key: value
87        #   ? not a simple key:
88        #   : { flow simple key: value }
89        # We emit the KEY token before all keys, so when we find a potential
90        # simple key, we try to locate the corresponding ':' indicator.
91        # Simple keys should be limited to a single line and 1024 characters.
92
[43]93        # Can a simple key start at the current position? A simple key may
94        # start:
95        # - at the beginning of the line, not counting indentation spaces
96        #       (in block context),
97        # - after '{', '[', ',' (in the flow context),
98        # - after '?', ':', '-' (in the block context).
[60]99        # In the block context, this flag also signifies if a block collection
[43]100        # may start at the current position.
101        self.allow_simple_key = True
[39]102
103        # Keep track of possible simple keys. This is a dictionary. The key
104        # is `flow_level`; there can be no more that one possible simple key
[43]105        # for each level. The value is a SimpleKey record:
[116]106        #   (token_number, required, index, line, column, mark)
[43]107        # A simple key may start with ALIAS, ANCHOR, TAG, SCALAR(flow),
108        # '[', or '{' tokens.
[39]109        self.possible_simple_keys = {}
110
[51]111    # Public methods.
[39]112
[136]113    def check_token(self, *choices):
[51]114        # Check if the next token is one of the given types.
[43]115        while self.need_more_tokens():
[39]116            self.fetch_more_tokens()
117        if self.tokens:
[136]118            if not choices:
119                return True
[51]120            for choice in choices:
121                if isinstance(self.tokens[0], choice):
122                    return True
123        return False
124
[136]125    def peek_token(self):
[51]126        # Return the next token, but do not delete if from the queue.
127        while self.need_more_tokens():
128            self.fetch_more_tokens()
129        if self.tokens:
[39]130            return self.tokens[0]
131
[136]132    def get_token(self):
[51]133        # Return the next token.
[39]134        while self.need_more_tokens():
135            self.fetch_more_tokens()
136        if self.tokens:
137            self.tokens_taken += 1
138            return self.tokens.pop(0)
139
[43]140    # Private methods.
[39]141
142    def need_more_tokens(self):
143        if self.done:
144            return False
145        if not self.tokens:
146            return True
147        # The current token may be a potential simple key, so we
148        # need to look further.
[43]149        self.stale_possible_simple_keys()
[39]150        if self.next_possible_simple_key() == self.tokens_taken:
151            return True
152
153    def fetch_more_tokens(self):
154
155        # Eat whitespaces and comments until we reach the next token.
[43]156        self.scan_to_next_token()
[39]157
[43]158        # Remove obsolete possible simple keys.
159        self.stale_possible_simple_keys()
160
[39]161        # Compare the current indentation and column. It may add some tokens
[43]162        # and decrease the current indentation level.
[136]163        self.unwind_indent(self.column)
[39]164
165        # Peek the next character.
[136]166        ch = self.peek()
[39]167
[48]168        # Is it the end of stream?
[328]169        if ch == '\0':
[48]170            return self.fetch_stream_end()
[39]171
172        # Is it a directive?
[328]173        if ch == '%' and self.check_directive():
[39]174            return self.fetch_directive()
175
176        # Is it the document start?
[328]177        if ch == '-' and self.check_document_start():
[39]178            return self.fetch_document_start()
179
180        # Is it the document end?
[328]181        if ch == '.' and self.check_document_end():
[39]182            return self.fetch_document_end()
183
[52]184        # TODO: support for BOM within a stream.
[328]185        #if ch == '\uFEFF':
[52]186        #    return self.fetch_bom()    <-- issue BOMToken
187
[39]188        # Note: the order of the following checks is NOT significant.
189
190        # Is it the flow sequence start indicator?
[328]191        if ch == '[':
[39]192            return self.fetch_flow_sequence_start()
193
194        # Is it the flow mapping start indicator?
[328]195        if ch == '{':
[39]196            return self.fetch_flow_mapping_start()
197
198        # Is it the flow sequence end indicator?
[328]199        if ch == ']':
[39]200            return self.fetch_flow_sequence_end()
201
202        # Is it the flow mapping end indicator?
[328]203        if ch == '}':
[39]204            return self.fetch_flow_mapping_end()
205
[51]206        # Is it the flow entry indicator?
[328]207        if ch == ',':
[51]208            return self.fetch_flow_entry()
[43]209
[51]210        # Is it the block entry indicator?
[328]211        if ch == '-' and self.check_block_entry():
[51]212            return self.fetch_block_entry()
213
[39]214        # Is it the key indicator?
[328]215        if ch == '?' and self.check_key():
[39]216            return self.fetch_key()
217
218        # Is it the value indicator?
[328]219        if ch == ':' and self.check_value():
[39]220            return self.fetch_value()
221
222        # Is it an alias?
[328]223        if ch == '*':
[39]224            return self.fetch_alias()
225
226        # Is it an anchor?
[328]227        if ch == '&':
[39]228            return self.fetch_anchor()
229
[43]230        # Is it a tag?
[328]231        if ch == '!':
[39]232            return self.fetch_tag()
233
[43]234        # Is it a literal scalar?
[328]235        if ch == '|' and not self.flow_level:
[39]236            return self.fetch_literal()
237
238        # Is it a folded scalar?
[328]239        if ch == '>' and not self.flow_level:
[39]240            return self.fetch_folded()
241
242        # Is it a single quoted scalar?
[328]243        if ch == '\'':
[39]244            return self.fetch_single()
245
246        # Is it a double quoted scalar?
[328]247        if ch == '\"':
[39]248            return self.fetch_double()
249
[43]250        # It must be a plain scalar then.
[39]251        if self.check_plain():
252            return self.fetch_plain()
253
[43]254        # No? It's an error. Let's produce a nice error message.
[48]255        raise ScannerError("while scanning for the next token", None,
[328]256                "found character %r that cannot start any token" % ch,
257                self.get_mark())
[39]258
[43]259    # Simple keys treatment.
260
261    def next_possible_simple_key(self):
262        # Return the number of the nearest possible simple key. Actually we
263        # don't need to loop through the whole dictionary. We may replace it
264        # with the following code:
265        #   if not self.possible_simple_keys:
266        #       return None
267        #   return self.possible_simple_keys[
268        #           min(self.possible_simple_keys.keys())].token_number
269        min_token_number = None
270        for level in self.possible_simple_keys:
271            key = self.possible_simple_keys[level]
272            if min_token_number is None or key.token_number < min_token_number:
273                min_token_number = key.token_number
274        return min_token_number
275
276    def stale_possible_simple_keys(self):
277        # Remove entries that are no longer possible simple keys. According to
278        # the YAML specification, simple keys
279        # - should be limited to a single line,
280        # - should be no longer than 1024 characters.
281        # Disabling this procedure will allow simple keys of any length and
282        # height (may cause problems if indentation is broken though).
[328]283        for level in list(self.possible_simple_keys):
[43]284            key = self.possible_simple_keys[level]
[136]285            if key.line != self.line  \
286                    or self.index-key.index > 1024:
[43]287                if key.required:
[116]288                    raise ScannerError("while scanning a simple key", key.mark,
[136]289                            "could not found expected ':'", self.get_mark())
[43]290                del self.possible_simple_keys[level]
291
292    def save_possible_simple_key(self):
293        # The next token may start a simple key. We check if it's possible
294        # and save its position. This function is called for
295        #   ALIAS, ANCHOR, TAG, SCALAR(flow), '[', and '{'.
296
297        # Check if a simple key is required at the current position.
[136]298        required = not self.flow_level and self.indent == self.column
[43]299
[47]300        # A simple key is required only if it is the first token in the current
301        # line. Therefore it is always allowed.
302        assert self.allow_simple_key or not required
303
[43]304        # The next token might be a simple key. Let's save it's number and
305        # position.
306        if self.allow_simple_key:
307            self.remove_possible_simple_key()
308            token_number = self.tokens_taken+len(self.tokens)
309            key = SimpleKey(token_number, required,
[136]310                    self.index, self.line, self.column, self.get_mark())
[43]311            self.possible_simple_keys[self.flow_level] = key
312
313    def remove_possible_simple_key(self):
314        # Remove the saved possible key position at the current flow level.
315        if self.flow_level in self.possible_simple_keys:
316            key = self.possible_simple_keys[self.flow_level]
[47]317           
[188]318            if key.required:
319                raise ScannerError("while scanning a simple key", key.mark,
320                        "could not found expected ':'", self.get_mark())
[43]321
[188]322            del self.possible_simple_keys[self.flow_level]
323
[43]324    # Indentation functions.
325
326    def unwind_indent(self, column):
327
[117]328        ## In flow context, tokens should respect indentation.
329        ## Actually the condition should be `self.indent >= column` according to
330        ## the spec. But this condition will prohibit intuitively correct
331        ## constructions such as
332        ## key : {
333        ## }
334        #if self.flow_level and self.indent > column:
335        #    raise ScannerError(None, None,
336        #            "invalid intendation or unclosed '[' or '{'",
[136]337        #            self.get_mark())
[43]338
[117]339        # In the flow context, indentation is ignored. We make the scanner less
340        # restrictive then specification requires.
341        if self.flow_level:
342            return
343
[43]344        # In block context, we may need to issue the BLOCK-END tokens.
345        while self.indent > column:
[136]346            mark = self.get_mark()
[43]347            self.indent = self.indents.pop()
[116]348            self.tokens.append(BlockEndToken(mark, mark))
[43]349
350    def add_indent(self, column):
351        # Check if we need to increase indentation.
352        if self.indent < column:
353            self.indents.append(self.indent)
354            self.indent = column
355            return True
356        return False
357
358    # Fetchers.
359
[118]360    def fetch_stream_start(self):
361        # We always add STREAM-START as the first token and STREAM-END as the
362        # last token.
363
364        # Read the token.
[136]365        mark = self.get_mark()
[118]366       
[130]367        # Add STREAM-START.
368        self.tokens.append(StreamStartToken(mark, mark,
[136]369            encoding=self.encoding))
[118]370       
371
[48]372    def fetch_stream_end(self):
[39]373
374        # Set the current intendation to -1.
[43]375        self.unwind_indent(-1)
[39]376
[354]377        # Reset simple keys.
378        self.remove_possible_simple_key()
[43]379        self.allow_simple_key = False
[39]380        self.possible_simple_keys = {}
381
[43]382        # Read the token.
[136]383        mark = self.get_mark()
[43]384       
[118]385        # Add STREAM-END.
[116]386        self.tokens.append(StreamEndToken(mark, mark))
[39]387
[136]388        # The steam is finished.
[39]389        self.done = True
390
[43]391    def fetch_directive(self):
392       
393        # Set the current intendation to -1.
394        self.unwind_indent(-1)
[39]395
[43]396        # Reset simple keys.
397        self.remove_possible_simple_key()
398        self.allow_simple_key = False
[39]399
[43]400        # Scan and add DIRECTIVE.
[47]401        self.tokens.append(self.scan_directive())
[39]402
403    def fetch_document_start(self):
[44]404        self.fetch_document_indicator(DocumentStartToken)
[39]405
[43]406    def fetch_document_end(self):
[44]407        self.fetch_document_indicator(DocumentEndToken)
[43]408
409    def fetch_document_indicator(self, TokenClass):
410
[39]411        # Set the current intendation to -1.
[43]412        self.unwind_indent(-1)
[39]413
[43]414        # Reset simple keys. Note that there could not be a block collection
415        # after '---'.
416        self.remove_possible_simple_key()
417        self.allow_simple_key = False
[39]418
[43]419        # Add DOCUMENT-START or DOCUMENT-END.
[136]420        start_mark = self.get_mark()
421        self.forward(3)
422        end_mark = self.get_mark()
[116]423        self.tokens.append(TokenClass(start_mark, end_mark))
[39]424
[43]425    def fetch_flow_sequence_start(self):
[44]426        self.fetch_flow_collection_start(FlowSequenceStartToken)
[39]427
[43]428    def fetch_flow_mapping_start(self):
[44]429        self.fetch_flow_collection_start(FlowMappingStartToken)
[43]430
431    def fetch_flow_collection_start(self, TokenClass):
432
[44]433        # '[' and '{' may start a simple key.
434        self.save_possible_simple_key()
435
[43]436        # Increase the flow level.
437        self.flow_level += 1
438
439        # Simple keys are allowed after '[' and '{'.
440        self.allow_simple_key = True
441
442        # Add FLOW-SEQUENCE-START or FLOW-MAPPING-START.
[136]443        start_mark = self.get_mark()
444        self.forward()
445        end_mark = self.get_mark()
[116]446        self.tokens.append(TokenClass(start_mark, end_mark))
[39]447
[43]448    def fetch_flow_sequence_end(self):
[44]449        self.fetch_flow_collection_end(FlowSequenceEndToken)
[39]450
[43]451    def fetch_flow_mapping_end(self):
[44]452        self.fetch_flow_collection_end(FlowMappingEndToken)
[43]453
454    def fetch_flow_collection_end(self, TokenClass):
455
456        # Reset possible simple key on the current level.
457        self.remove_possible_simple_key()
458
459        # Decrease the flow level.
460        self.flow_level -= 1
461
462        # No simple keys after ']' or '}'.
463        self.allow_simple_key = False
464
465        # Add FLOW-SEQUENCE-END or FLOW-MAPPING-END.
[136]466        start_mark = self.get_mark()
467        self.forward()
468        end_mark = self.get_mark()
[116]469        self.tokens.append(TokenClass(start_mark, end_mark))
[39]470
[51]471    def fetch_flow_entry(self):
[39]472
[51]473        # Simple keys are allowed after ','.
474        self.allow_simple_key = True
475
476        # Reset possible simple key on the current level.
477        self.remove_possible_simple_key()
478
479        # Add FLOW-ENTRY.
[136]480        start_mark = self.get_mark()
481        self.forward()
482        end_mark = self.get_mark()
[116]483        self.tokens.append(FlowEntryToken(start_mark, end_mark))
[51]484
485    def fetch_block_entry(self):
486
[43]487        # Block context needs additional checks.
488        if not self.flow_level:
[39]489
[43]490            # Are we allowed to start a new entry?
491            if not self.allow_simple_key:
[47]492                raise ScannerError(None, None,
493                        "sequence entries are not allowed here",
[136]494                        self.get_mark())
[39]495
[43]496            # We may need to add BLOCK-SEQUENCE-START.
[136]497            if self.add_indent(self.column):
498                mark = self.get_mark()
[116]499                self.tokens.append(BlockSequenceStartToken(mark, mark))
[39]500
[51]501        # It's an error for the block entry to occur in the flow context,
502        # but we let the parser detect this.
503        else:
504            pass
505
506        # Simple keys are allowed after '-'.
[43]507        self.allow_simple_key = True
[39]508
[43]509        # Reset possible simple key on the current level.
510        self.remove_possible_simple_key()
[39]511
[51]512        # Add BLOCK-ENTRY.
[136]513        start_mark = self.get_mark()
514        self.forward()
515        end_mark = self.get_mark()
[116]516        self.tokens.append(BlockEntryToken(start_mark, end_mark))
[39]517
[43]518    def fetch_key(self):
519       
520        # Block context needs additional checks.
521        if not self.flow_level:
[39]522
[43]523            # Are we allowed to start a key (not nessesary a simple)?
524            if not self.allow_simple_key:
[47]525                raise ScannerError(None, None,
526                        "mapping keys are not allowed here",
[136]527                        self.get_mark())
[43]528
529            # We may need to add BLOCK-MAPPING-START.
[136]530            if self.add_indent(self.column):
531                mark = self.get_mark()
[116]532                self.tokens.append(BlockMappingStartToken(mark, mark))
[43]533
534        # Simple keys are allowed after '?' in the block context.
535        self.allow_simple_key = not self.flow_level
536
537        # Reset possible simple key on the current level.
538        self.remove_possible_simple_key()
539
540        # Add KEY.
[136]541        start_mark = self.get_mark()
542        self.forward()
543        end_mark = self.get_mark()
[116]544        self.tokens.append(KeyToken(start_mark, end_mark))
[39]545
[43]546    def fetch_value(self):
[39]547
[43]548        # Do we determine a simple key?
549        if self.flow_level in self.possible_simple_keys:
[39]550
[43]551            # Add KEY.
552            key = self.possible_simple_keys[self.flow_level]
553            del self.possible_simple_keys[self.flow_level]
554            self.tokens.insert(key.token_number-self.tokens_taken,
[116]555                    KeyToken(key.mark, key.mark))
[39]556
[43]557            # If this key starts a new block mapping, we need to add
558            # BLOCK-MAPPING-START.
559            if not self.flow_level:
560                if self.add_indent(key.column):
561                    self.tokens.insert(key.token_number-self.tokens_taken,
[116]562                            BlockMappingStartToken(key.mark, key.mark))
[37]563
[43]564            # There cannot be two simple keys one after another.
565            self.allow_simple_key = False
[37]566
[43]567        # It must be a part of a complex key.
568        else:
569           
[47]570            # Block context needs additional checks.
571            # (Do we really need them? They will be catched by the parser
572            # anyway.)
573            if not self.flow_level:
574
575                # We are allowed to start a complex value if and only if
576                # we can start a simple key.
577                if not self.allow_simple_key:
578                    raise ScannerError(None, None,
579                            "mapping values are not allowed here",
[136]580                            self.get_mark())
[47]581
[188]582            # If this value starts a new block mapping, we need to add
583            # BLOCK-MAPPING-START.  It will be detected as an error later by
584            # the parser.
585            if not self.flow_level:
586                if self.add_indent(self.column):
587                    mark = self.get_mark()
588                    self.tokens.append(BlockMappingStartToken(mark, mark))
589
[43]590            # Simple keys are allowed after ':' in the block context.
591            self.allow_simple_key = not self.flow_level
[37]592
[43]593            # Reset possible simple key on the current level.
594            self.remove_possible_simple_key()
[37]595
[43]596        # Add VALUE.
[136]597        start_mark = self.get_mark()
598        self.forward()
599        end_mark = self.get_mark()
[116]600        self.tokens.append(ValueToken(start_mark, end_mark))
[37]601
[43]602    def fetch_alias(self):
[37]603
[43]604        # ALIAS could be a simple key.
605        self.save_possible_simple_key()
[37]606
[43]607        # No simple keys after ALIAS.
608        self.allow_simple_key = False
[37]609
[43]610        # Scan and add ALIAS.
[47]611        self.tokens.append(self.scan_anchor(AliasToken))
[37]612
[43]613    def fetch_anchor(self):
[37]614
[43]615        # ANCHOR could start a simple key.
616        self.save_possible_simple_key()
[37]617
[43]618        # No simple keys after ANCHOR.
619        self.allow_simple_key = False
[37]620
[43]621        # Scan and add ANCHOR.
[47]622        self.tokens.append(self.scan_anchor(AnchorToken))
[37]623
[43]624    def fetch_tag(self):
[37]625
[43]626        # TAG could start a simple key.
627        self.save_possible_simple_key()
[37]628
[43]629        # No simple keys after TAG.
630        self.allow_simple_key = False
[37]631
[43]632        # Scan and add TAG.
[47]633        self.tokens.append(self.scan_tag())
[37]634
[43]635    def fetch_literal(self):
[130]636        self.fetch_block_scalar(style='|')
[37]637
[43]638    def fetch_folded(self):
[130]639        self.fetch_block_scalar(style='>')
[37]640
[130]641    def fetch_block_scalar(self, style):
[37]642
[43]643        # A simple key may follow a block scalar.
644        self.allow_simple_key = True
[37]645
[43]646        # Reset possible simple key on the current level.
647        self.remove_possible_simple_key()
[37]648
[43]649        # Scan and add SCALAR.
[130]650        self.tokens.append(self.scan_block_scalar(style))
[37]651
[43]652    def fetch_single(self):
[130]653        self.fetch_flow_scalar(style='\'')
[37]654
[43]655    def fetch_double(self):
[130]656        self.fetch_flow_scalar(style='"')
[37]657
[130]658    def fetch_flow_scalar(self, style):
[37]659
[43]660        # A flow scalar could be a simple key.
661        self.save_possible_simple_key()
[37]662
[43]663        # No simple keys after flow scalars.
664        self.allow_simple_key = False
[37]665
[43]666        # Scan and add SCALAR.
[130]667        self.tokens.append(self.scan_flow_scalar(style))
[37]668
[43]669    def fetch_plain(self):
[37]670
[43]671        # A plain scalar could be a simple key.
672        self.save_possible_simple_key()
[37]673
[43]674        # No simple keys after plain scalars. But note that `scan_plain` will
675        # change this flag if the scan is finished at the beginning of the
676        # line.
677        self.allow_simple_key = False
[37]678
[43]679        # Scan and add SCALAR. May change `allow_simple_key`.
[47]680        self.tokens.append(self.scan_plain())
[37]681
[43]682    # Checkers.
[37]683
[43]684    def check_directive(self):
[37]685
[43]686        # DIRECTIVE:        ^ '%' ...
687        # The '%' indicator is already checked.
[136]688        if self.column == 0:
[43]689            return True
[37]690
[43]691    def check_document_start(self):
[37]692
[43]693        # DOCUMENT-START:   ^ '---' (' '|'\n')
[136]694        if self.column == 0:
[328]695            if self.prefix(3) == '---'  \
696                    and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029':
[43]697                return True
[37]698
[43]699    def check_document_end(self):
[37]700
[43]701        # DOCUMENT-END:     ^ '...' (' '|'\n')
[136]702        if self.column == 0:
[328]703            if self.prefix(3) == '...'  \
704                    and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029':
[43]705                return True
[37]706
[51]707    def check_block_entry(self):
[43]708
[51]709        # BLOCK-ENTRY:      '-' (' '|'\n')
[328]710        return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029'
[43]711
712    def check_key(self):
713
714        # KEY(flow context):    '?'
715        if self.flow_level:
[37]716            return True
[43]717
718        # KEY(block context):   '?' (' '|'\n')
[37]719        else:
[328]720            return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029'
[37]721
[43]722    def check_value(self):
723
724        # VALUE(flow context):  ':'
725        if self.flow_level:
[37]726            return True
[43]727
728        # VALUE(block context): ':' (' '|'\n')
[37]729        else:
[328]730            return self.peek(1) in '\0 \t\r\n\x85\u2028\u2029'
[37]731
[43]732    def check_plain(self):
[37]733
[48]734        # A plain scalar may start with any non-space character except:
735        #   '-', '?', ':', ',', '[', ']', '{', '}',
736        #   '#', '&', '*', '!', '|', '>', '\'', '\"',
737        #   '%', '@', '`'.
738        #
739        # It may also start with
740        #   '-', '?', ':'
741        # if it is followed by a non-space character.
742        #
743        # Note that we limit the last rule to the block context (except the
744        # '-' character) because we want the flow context to be space
745        # independent.
[136]746        ch = self.peek()
[328]747        return ch not in '\0 \t\r\n\x85\u2028\u2029-?:,[]{}#&*!|>\'\"%@`'  \
748                or (self.peek(1) not in '\0 \t\r\n\x85\u2028\u2029'
749                        and (ch == '-' or (not self.flow_level and ch in '?:')))
[48]750
[43]751    # Scanners.
752
753    def scan_to_next_token(self):
[47]754        # We ignore spaces, line breaks and comments.
755        # If we find a line break in the block context, we set the flag
756        # `allow_simple_key` on.
[51]757        # The byte order mark is stripped if it's the first character in the
758        # stream. We do not yet support BOM inside the stream as the
759        # specification requires. Any such mark will be considered as a part
760        # of the document.
[52]761        #
762        # TODO: We need to make tab handling rules more sane. A good rule is
763        #   Tabs cannot precede tokens
764        #   BLOCK-SEQUENCE-START, BLOCK-MAPPING-START, BLOCK-END,
765        #   KEY(block), VALUE(block), BLOCK-ENTRY
766        # So the checking code is
767        #   if <TAB>:
768        #       self.allow_simple_keys = False
769        # We also need to add the check for `allow_simple_keys == True` to
770        # `unwind_indent` before issuing BLOCK-END.
771        # Scanners for block, flow, and plain scalars need to be modified.
772
[328]773        if self.index == 0 and self.peek() == '\uFEFF':
[136]774            self.forward()
[43]775        found = False
776        while not found:
[328]777            while self.peek() == ' ':
[136]778                self.forward()
[328]779            if self.peek() == '#':
780                while self.peek() not in '\0\r\n\x85\u2028\u2029':
[136]781                    self.forward()
[47]782            if self.scan_line_break():
[43]783                if not self.flow_level:
784                    self.allow_simple_key = True
[37]785            else:
[43]786                found = True
[37]787
[43]788    def scan_directive(self):
[48]789        # See the specification for details.
[136]790        start_mark = self.get_mark()
791        self.forward()
[116]792        name = self.scan_directive_name(start_mark)
[48]793        value = None
[328]794        if name == 'YAML':
[116]795            value = self.scan_yaml_directive_value(start_mark)
[136]796            end_mark = self.get_mark()
[328]797        elif name == 'TAG':
[116]798            value = self.scan_tag_directive_value(start_mark)
[136]799            end_mark = self.get_mark()
[43]800        else:
[136]801            end_mark = self.get_mark()
[328]802            while self.peek() not in '\0\r\n\x85\u2028\u2029':
[136]803                self.forward()
[116]804        self.scan_directive_ignored_line(start_mark)
805        return DirectiveToken(name, value, start_mark, end_mark)
[48]806
[116]807    def scan_directive_name(self, start_mark):
[48]808        # See the specification for details.
809        length = 0
[136]810        ch = self.peek(length)
[328]811        while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z'  \
812                or ch in '-_':
[48]813            length += 1
[136]814            ch = self.peek(length)
[48]815        if not length:
[116]816            raise ScannerError("while scanning a directive", start_mark,
[52]817                    "expected alphabetic or numeric character, but found %r"
[328]818                    % ch, self.get_mark())
[136]819        value = self.prefix(length)
820        self.forward(length)
821        ch = self.peek()
[328]822        if ch not in '\0 \r\n\x85\u2028\u2029':
[116]823            raise ScannerError("while scanning a directive", start_mark,
[48]824                    "expected alphabetic or numeric character, but found %r"
[328]825                    % ch, self.get_mark())
[48]826        return value
827
[116]828    def scan_yaml_directive_value(self, start_mark):
[48]829        # See the specification for details.
[328]830        while self.peek() == ' ':
[136]831            self.forward()
[116]832        major = self.scan_yaml_directive_number(start_mark)
[136]833        if self.peek() != '.':
[116]834            raise ScannerError("while scanning a directive", start_mark,
[328]835                    "expected a digit or '.', but found %r" % self.peek(),
[136]836                    self.get_mark())
837        self.forward()
[116]838        minor = self.scan_yaml_directive_number(start_mark)
[328]839        if self.peek() not in '\0 \r\n\x85\u2028\u2029':
[116]840            raise ScannerError("while scanning a directive", start_mark,
[328]841                    "expected a digit or ' ', but found %r" % self.peek(),
[136]842                    self.get_mark())
[48]843        return (major, minor)
[37]844
[116]845    def scan_yaml_directive_number(self, start_mark):
[48]846        # See the specification for details.
[136]847        ch = self.peek()
[328]848        if not ('0' <= ch <= '9'):
[116]849            raise ScannerError("while scanning a directive", start_mark,
[328]850                    "expected a digit, but found %r" % ch, self.get_mark())
[48]851        length = 0
[328]852        while '0' <= self.peek(length) <= '9':
[48]853            length += 1
[136]854        value = int(self.prefix(length))
855        self.forward(length)
[48]856        return value
857
[116]858    def scan_tag_directive_value(self, start_mark):
[48]859        # See the specification for details.
[328]860        while self.peek() == ' ':
[136]861            self.forward()
[116]862        handle = self.scan_tag_directive_handle(start_mark)
[328]863        while self.peek() == ' ':
[136]864            self.forward()
[116]865        prefix = self.scan_tag_directive_prefix(start_mark)
[48]866        return (handle, prefix)
867
[116]868    def scan_tag_directive_handle(self, start_mark):
[48]869        # See the specification for details.
[116]870        value = self.scan_tag_handle('directive', start_mark)
[136]871        ch = self.peek()
[328]872        if ch != ' ':
[116]873            raise ScannerError("while scanning a directive", start_mark,
[328]874                    "expected ' ', but found %r" % ch, self.get_mark())
[48]875        return value
876
[116]877    def scan_tag_directive_prefix(self, start_mark):
[48]878        # See the specification for details.
[116]879        value = self.scan_tag_uri('directive', start_mark)
[136]880        ch = self.peek()
[328]881        if ch not in '\0 \r\n\x85\u2028\u2029':
[116]882            raise ScannerError("while scanning a directive", start_mark,
[328]883                    "expected ' ', but found %r" % ch, self.get_mark())
[48]884        return value
885
[116]886    def scan_directive_ignored_line(self, start_mark):
[48]887        # See the specification for details.
[328]888        while self.peek() == ' ':
[136]889            self.forward()
[328]890        if self.peek() == '#':
891            while self.peek() not in '\0\r\n\x85\u2028\u2029':
[136]892                self.forward()
893        ch = self.peek()
[328]894        if ch not in '\0\r\n\x85\u2028\u2029':
[116]895            raise ScannerError("while scanning a directive", start_mark,
[48]896                    "expected a comment or a line break, but found %r"
[328]897                        % ch, self.get_mark())
[48]898        self.scan_line_break()
899
[43]900    def scan_anchor(self, TokenClass):
[48]901        # The specification does not restrict characters for anchors and
902        # aliases. This may lead to problems, for instance, the document:
903        #   [ *alias, value ]
904        # can be interpteted in two ways, as
905        #   [ "value" ]
906        # and
907        #   [ *alias , "value" ]
908        # Therefore we restrict aliases to numbers and ASCII letters.
[136]909        start_mark = self.get_mark()
910        indicator = self.peek()
[48]911        if indicator == '*':
912            name = 'alias'
913        else:
914            name = 'anchor'
[136]915        self.forward()
[48]916        length = 0
[136]917        ch = self.peek(length)
[328]918        while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z'  \
919                or ch in '-_':
[48]920            length += 1
[136]921            ch = self.peek(length)
[48]922        if not length:
[116]923            raise ScannerError("while scanning an %s" % name, start_mark,
[52]924                    "expected alphabetic or numeric character, but found %r"
[328]925                    % ch, self.get_mark())
[136]926        value = self.prefix(length)
927        self.forward(length)
928        ch = self.peek()
[328]929        if ch not in '\0 \t\r\n\x85\u2028\u2029?:,]}%@`':
[116]930            raise ScannerError("while scanning an %s" % name, start_mark,
[48]931                    "expected alphabetic or numeric character, but found %r"
[328]932                    % ch, self.get_mark())
[136]933        end_mark = self.get_mark()
[116]934        return TokenClass(value, start_mark, end_mark)
[37]935
[43]936    def scan_tag(self):
[48]937        # See the specification for details.
[136]938        start_mark = self.get_mark()
939        ch = self.peek(1)
[328]940        if ch == '<':
[48]941            handle = None
[136]942            self.forward(2)
[116]943            suffix = self.scan_tag_uri('tag', start_mark)
[328]944            if self.peek() != '>':
[116]945                raise ScannerError("while parsing a tag", start_mark,
[328]946                        "expected '>', but found %r" % self.peek(),
[136]947                        self.get_mark())
948            self.forward()
[328]949        elif ch in '\0 \t\r\n\x85\u2028\u2029':
[48]950            handle = None
[328]951            suffix = '!'
[136]952            self.forward()
[48]953        else:
954            length = 1
955            use_handle = False
[328]956            while ch not in '\0 \r\n\x85\u2028\u2029':
957                if ch == '!':
[48]958                    use_handle = True
959                    break
960                length += 1
[136]961                ch = self.peek(length)
[328]962            handle = '!'
[48]963            if use_handle:
[116]964                handle = self.scan_tag_handle('tag', start_mark)
[48]965            else:
[328]966                handle = '!'
[136]967                self.forward()
[116]968            suffix = self.scan_tag_uri('tag', start_mark)
[136]969        ch = self.peek()
[328]970        if ch not in '\0 \r\n\x85\u2028\u2029':
[116]971            raise ScannerError("while scanning a tag", start_mark,
[328]972                    "expected ' ', but found %r" % ch, self.get_mark())
[48]973        value = (handle, suffix)
[136]974        end_mark = self.get_mark()
[116]975        return TagToken(value, start_mark, end_mark)
[43]976
[130]977    def scan_block_scalar(self, style):
[48]978        # See the specification for details.
979
[130]980        if style == '>':
981            folded = True
982        else:
983            folded = False
984
[48]985        chunks = []
[136]986        start_mark = self.get_mark()
[48]987
988        # Scan the header.
[136]989        self.forward()
[116]990        chomping, increment = self.scan_block_scalar_indicators(start_mark)
991        self.scan_block_scalar_ignored_line(start_mark)
[48]992
993        # Determine the indentation level and go to the first non-empty line.
994        min_indent = self.indent+1
995        if min_indent < 1:
996            min_indent = 1
997        if increment is None:
[116]998            breaks, max_indent, end_mark = self.scan_block_scalar_indentation()
[48]999            indent = max(min_indent, max_indent)
1000        else:
1001            indent = min_indent+increment-1
[116]1002            breaks, end_mark = self.scan_block_scalar_breaks(indent)
[328]1003        line_break = ''
[48]1004
1005        # Scan the inner part of the block scalar.
[328]1006        while self.column == indent and self.peek() != '\0':
[48]1007            chunks.extend(breaks)
[328]1008            leading_non_space = self.peek() not in ' \t'
[48]1009            length = 0
[328]1010            while self.peek(length) not in '\0\r\n\x85\u2028\u2029':
[48]1011                length += 1
[136]1012            chunks.append(self.prefix(length))
1013            self.forward(length)
[48]1014            line_break = self.scan_line_break()
[116]1015            breaks, end_mark = self.scan_block_scalar_breaks(indent)
[328]1016            if self.column == indent and self.peek() != '\0':
[130]1017
[48]1018                # Unfortunately, folding rules are ambiguous.
1019                #
1020                # This is the folding according to the specification:
[51]1021               
[328]1022                if folded and line_break == '\n'    \
1023                        and leading_non_space and self.peek() not in ' \t':
[51]1024                    if not breaks:
[328]1025                        chunks.append(' ')
[51]1026                else:
1027                    chunks.append(line_break)
1028               
1029                # This is Clark Evans's interpretation (also in the spec
1030                # examples):
[48]1031                #
[328]1032                #if folded and line_break == '\n':
[48]1033                #    if not breaks:
[136]1034                #        if self.peek() not in ' \t':
[328]1035                #            chunks.append(' ')
[51]1036                #        else:
1037                #            chunks.append(line_break)
[48]1038                #else:
1039                #    chunks.append(line_break)
1040            else:
1041                break
1042
1043        # Chomp the tail.
1044        if chomping is not False:
1045            chunks.append(line_break)
1046        if chomping is True:
1047            chunks.extend(breaks)
1048
1049        # We are done.
[328]1050        return ScalarToken(''.join(chunks), False, start_mark, end_mark,
[130]1051                style)
[48]1052
[116]1053    def scan_block_scalar_indicators(self, start_mark):
[48]1054        # See the specification for details.
1055        chomping = None
1056        increment = None
[136]1057        ch = self.peek()
[328]1058        if ch in '+-':
[48]1059            if ch == '+':
1060                chomping = True
1061            else:
1062                chomping = False
[136]1063            self.forward()
1064            ch = self.peek()
[328]1065            if ch in '0123456789':
[48]1066                increment = int(ch)
1067                if increment == 0:
[116]1068                    raise ScannerError("while scanning a block scalar", start_mark,
[48]1069                            "expected indentation indicator in the range 1-9, but found 0",
[136]1070                            self.get_mark())
1071                self.forward()
[328]1072        elif ch in '0123456789':
[48]1073            increment = int(ch)
1074            if increment == 0:
[116]1075                raise ScannerError("while scanning a block scalar", start_mark,
[48]1076                        "expected indentation indicator in the range 1-9, but found 0",
[136]1077                        self.get_mark())
1078            self.forward()
1079            ch = self.peek()
[328]1080            if ch in '+-':
[48]1081                if ch == '+':
1082                    chomping = True
1083                else:
1084                    chomping = False
[136]1085                self.forward()
1086        ch = self.peek()
[328]1087        if ch not in '\0 \r\n\x85\u2028\u2029':
[116]1088            raise ScannerError("while scanning a block scalar", start_mark,
[48]1089                    "expected chomping or indentation indicators, but found %r"
[328]1090                    % ch, self.get_mark())
[48]1091        return chomping, increment
1092
[116]1093    def scan_block_scalar_ignored_line(self, start_mark):
[48]1094        # See the specification for details.
[328]1095        while self.peek() == ' ':
[136]1096            self.forward()
[328]1097        if self.peek() == '#':
1098            while self.peek() not in '\0\r\n\x85\u2028\u2029':
[136]1099                self.forward()
1100        ch = self.peek()
[328]1101        if ch not in '\0\r\n\x85\u2028\u2029':
[116]1102            raise ScannerError("while scanning a block scalar", start_mark,
[328]1103                    "expected a comment or a line break, but found %r" % ch,
1104                    self.get_mark())
[48]1105        self.scan_line_break()
[43]1106
[48]1107    def scan_block_scalar_indentation(self):
1108        # See the specification for details.
1109        chunks = []
1110        max_indent = 0
[136]1111        end_mark = self.get_mark()
[328]1112        while self.peek() in ' \r\n\x85\u2028\u2029':
1113            if self.peek() != ' ':
[48]1114                chunks.append(self.scan_line_break())
[136]1115                end_mark = self.get_mark()
[48]1116            else:
[136]1117                self.forward()
1118                if self.column > max_indent:
1119                    max_indent = self.column
[116]1120        return chunks, max_indent, end_mark
[48]1121
1122    def scan_block_scalar_breaks(self, indent):
1123        # See the specification for details.
1124        chunks = []
[136]1125        end_mark = self.get_mark()
[328]1126        while self.column < indent and self.peek() == ' ':
[136]1127            self.forward()
[328]1128        while self.peek() in '\r\n\x85\u2028\u2029':
[48]1129            chunks.append(self.scan_line_break())
[136]1130            end_mark = self.get_mark()
[328]1131            while self.column < indent and self.peek() == ' ':
[136]1132                self.forward()
[116]1133        return chunks, end_mark
[48]1134
[130]1135    def scan_flow_scalar(self, style):
[48]1136        # See the specification for details.
[117]1137        # Note that we loose indentation rules for quoted scalars. Quoted
1138        # scalars don't need to adhere indentation because " and ' clearly
1139        # mark the beginning and the end of them. Therefore we are less
1140        # restrictive then the specification requires. We only need to check
1141        # that document separators are not included in scalars.
[130]1142        if style == '"':
1143            double = True
1144        else:
1145            double = False
[48]1146        chunks = []
[136]1147        start_mark = self.get_mark()
1148        quote = self.peek()
1149        self.forward()
[117]1150        chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
[136]1151        while self.peek() != quote:
[117]1152            chunks.extend(self.scan_flow_scalar_spaces(double, start_mark))
1153            chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
[136]1154        self.forward()
1155        end_mark = self.get_mark()
[328]1156        return ScalarToken(''.join(chunks), False, start_mark, end_mark,
[130]1157                style)
[48]1158
1159    ESCAPE_REPLACEMENTS = {
[328]1160        '0':    '\0',
1161        'a':    '\x07',
1162        'b':    '\x08',
1163        't':    '\x09',
1164        '\t':   '\x09',
1165        'n':    '\x0A',
1166        'v':    '\x0B',
1167        'f':    '\x0C',
1168        'r':    '\x0D',
1169        'e':    '\x1B',
1170        ' ':    '\x20',
1171        '\"':   '\"',
1172        '\\':   '\\',
1173        'N':    '\x85',
1174        '_':    '\xA0',
1175        'L':    '\u2028',
1176        'P':    '\u2029',
[48]1177    }
1178
1179    ESCAPE_CODES = {
[328]1180        'x':    2,
1181        'u':    4,
1182        'U':    8,
[48]1183    }
1184
[117]1185    def scan_flow_scalar_non_spaces(self, double, start_mark):
[48]1186        # See the specification for details.
1187        chunks = []
1188        while True:
1189            length = 0
[328]1190            while self.peek(length) not in '\'\"\\\0 \t\r\n\x85\u2028\u2029':
[48]1191                length += 1
1192            if length:
[136]1193                chunks.append(self.prefix(length))
1194                self.forward(length)
1195            ch = self.peek()
[328]1196            if not double and ch == '\'' and self.peek(1) == '\'':
1197                chunks.append('\'')
[136]1198                self.forward(2)
[328]1199            elif (double and ch == '\'') or (not double and ch in '\"\\'):
[48]1200                chunks.append(ch)
[136]1201                self.forward()
[328]1202            elif double and ch == '\\':
[136]1203                self.forward()
1204                ch = self.peek()
[48]1205                if ch in self.ESCAPE_REPLACEMENTS:
1206                    chunks.append(self.ESCAPE_REPLACEMENTS[ch])
[136]1207                    self.forward()
[48]1208                elif ch in self.ESCAPE_CODES:
1209                    length = self.ESCAPE_CODES[ch]
[136]1210                    self.forward()
[48]1211                    for k in range(length):
[328]1212                        if self.peek(k) not in '0123456789ABCDEFabcdef':
[116]1213                            raise ScannerError("while scanning a double-quoted scalar", start_mark,
[48]1214                                    "expected escape sequence of %d hexdecimal numbers, but found %r" %
[328]1215                                        (length, self.peek(k)), self.get_mark())
[136]1216                    code = int(self.prefix(length), 16)
[328]1217                    chunks.append(chr(code))
[136]1218                    self.forward(length)
[328]1219                elif ch in '\r\n\x85\u2028\u2029':
[48]1220                    self.scan_line_break()
[117]1221                    chunks.extend(self.scan_flow_scalar_breaks(double, start_mark))
[48]1222                else:
[116]1223                    raise ScannerError("while scanning a double-quoted scalar", start_mark,
[328]1224                            "found unknown escape character %r" % ch, self.get_mark())
[37]1225            else:
[48]1226                return chunks
[37]1227
[117]1228    def scan_flow_scalar_spaces(self, double, start_mark):
[48]1229        # See the specification for details.
1230        chunks = []
1231        length = 0
[328]1232        while self.peek(length) in ' \t':
[48]1233            length += 1
[136]1234        whitespaces = self.prefix(length)
1235        self.forward(length)
1236        ch = self.peek()
[328]1237        if ch == '\0':
[116]1238            raise ScannerError("while scanning a quoted scalar", start_mark,
[136]1239                    "found unexpected end of stream", self.get_mark())
[328]1240        elif ch in '\r\n\x85\u2028\u2029':
[48]1241            line_break = self.scan_line_break()
[117]1242            breaks = self.scan_flow_scalar_breaks(double, start_mark)
[328]1243            if line_break != '\n':
[48]1244                chunks.append(line_break)
1245            elif not breaks:
[328]1246                chunks.append(' ')
[48]1247            chunks.extend(breaks)
1248        else:
1249            chunks.append(whitespaces)
1250        return chunks
1251
[117]1252    def scan_flow_scalar_breaks(self, double, start_mark):
[48]1253        # See the specification for details.
1254        chunks = []
1255        while True:
[117]1256            # Instead of checking indentation, we check for document
1257            # separators.
[136]1258            prefix = self.prefix(3)
[328]1259            if (prefix == '---' or prefix == '...')   \
1260                    and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029':
[116]1261                raise ScannerError("while scanning a quoted scalar", start_mark,
[136]1262                        "found unexpected document separator", self.get_mark())
[328]1263            while self.peek() in ' \t':
[136]1264                self.forward()
[328]1265            if self.peek() in '\r\n\x85\u2028\u2029':
[48]1266                chunks.append(self.scan_line_break())
1267            else:
1268                return chunks
1269
[43]1270    def scan_plain(self):
[48]1271        # See the specification for details.
1272        # We add an additional restriction for the flow context:
[117]1273        #   plain scalars in the flow context cannot contain ',', ':' and '?'.
[48]1274        # We also keep track of the `allow_simple_key` flag here.
[117]1275        # Indentation rules are loosed for the flow context.
[48]1276        chunks = []
[136]1277        start_mark = self.get_mark()
[116]1278        end_mark = start_mark
[43]1279        indent = self.indent+1
[117]1280        # We allow zero indentation for scalars, but then we need to check for
1281        # document separators at the beginning of the line.
1282        #if indent == 0:
1283        #    indent = 1
[48]1284        spaces = []
[43]1285        while True:
[48]1286            length = 0
[328]1287            if self.peek() == '#':
[43]1288                break
[48]1289            while True:
[136]1290                ch = self.peek(length)
[328]1291                if ch in '\0 \t\r\n\x85\u2028\u2029'    \
1292                        or (not self.flow_level and ch == ':' and
1293                                self.peek(length+1) in '\0 \t\r\n\x85\u2028\u2029') \
1294                        or (self.flow_level and ch in ',:?[]{}'):
[48]1295                    break
1296                length += 1
[149]1297            # It's not clear what we should do with ':' in the flow context.
[328]1298            if (self.flow_level and ch == ':'
1299                    and self.peek(length+1) not in '\0 \t\r\n\x85\u2028\u2029,[]{}'):
[149]1300                self.forward(length)
1301                raise ScannerError("while scanning a plain scalar", start_mark,
1302                    "found unexpected ':'", self.get_mark(),
1303                    "Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.")
[48]1304            if length == 0:
[43]1305                break
[48]1306            self.allow_simple_key = False
1307            chunks.extend(spaces)
[136]1308            chunks.append(self.prefix(length))
1309            self.forward(length)
1310            end_mark = self.get_mark()
[117]1311            spaces = self.scan_plain_spaces(indent, start_mark)
[328]1312            if not spaces or self.peek() == '#' \
[136]1313                    or (not self.flow_level and self.column < indent):
[48]1314                break
[328]1315        return ScalarToken(''.join(chunks), True, start_mark, end_mark)
[37]1316
[117]1317    def scan_plain_spaces(self, indent, start_mark):
[48]1318        # See the specification for details.
1319        # The specification is really confusing about tabs in plain scalars.
1320        # We just forbid them completely. Do not use tabs in YAML!
1321        chunks = []
1322        length = 0
[328]1323        while self.peek(length) in ' ':
[48]1324            length += 1
[136]1325        whitespaces = self.prefix(length)
1326        self.forward(length)
1327        ch = self.peek()
[328]1328        if ch in '\r\n\x85\u2028\u2029':
[48]1329            line_break = self.scan_line_break()
1330            self.allow_simple_key = True
[136]1331            prefix = self.prefix(3)
[328]1332            if (prefix == '---' or prefix == '...')   \
1333                    and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029':
[117]1334                return
[48]1335            breaks = []
[328]1336            while self.peek() in ' \r\n\x85\u2028\u2029':
[136]1337                if self.peek() == ' ':
1338                    self.forward()
[48]1339                else:
1340                    breaks.append(self.scan_line_break())
[136]1341                    prefix = self.prefix(3)
[328]1342                    if (prefix == '---' or prefix == '...')   \
1343                            and self.peek(3) in '\0 \t\r\n\x85\u2028\u2029':
[117]1344                        return
[328]1345            if line_break != '\n':
[48]1346                chunks.append(line_break)
1347            elif not breaks:
[328]1348                chunks.append(' ')
[48]1349            chunks.extend(breaks)
1350        elif whitespaces:
1351            chunks.append(whitespaces)
1352        return chunks
1353
[116]1354    def scan_tag_handle(self, name, start_mark):
[48]1355        # See the specification for details.
1356        # For some strange reasons, the specification does not allow '_' in
1357        # tag handles. I have allowed it anyway.
[136]1358        ch = self.peek()
[328]1359        if ch != '!':
[116]1360            raise ScannerError("while scanning a %s" % name, start_mark,
[328]1361                    "expected '!', but found %r" % ch, self.get_mark())
[48]1362        length = 1
[136]1363        ch = self.peek(length)
[328]1364        if ch != ' ':
1365            while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z'  \
1366                    or ch in '-_':
[48]1367                length += 1
[136]1368                ch = self.peek(length)
[328]1369            if ch != '!':
[136]1370                self.forward(length)
[116]1371                raise ScannerError("while scanning a %s" % name, start_mark,
[328]1372                        "expected '!', but found %r" % ch, self.get_mark())
[48]1373            length += 1
[136]1374        value = self.prefix(length)
1375        self.forward(length)
[48]1376        return value
1377
[116]1378    def scan_tag_uri(self, name, start_mark):
[48]1379        # See the specification for details.
1380        # Note: we do not check if URI is well-formed.
1381        chunks = []
1382        length = 0
[136]1383        ch = self.peek(length)
[328]1384        while '0' <= ch <= '9' or 'A' <= ch <= 'Z' or 'a' <= ch <= 'z'  \
1385                or ch in '-;/?:@&=+$,_.!~*\'()[]%':
1386            if ch == '%':
[136]1387                chunks.append(self.prefix(length))
1388                self.forward(length)
[48]1389                length = 0
[116]1390                chunks.append(self.scan_uri_escapes(name, start_mark))
[48]1391            else:
1392                length += 1
[136]1393            ch = self.peek(length)
[48]1394        if length:
[136]1395            chunks.append(self.prefix(length))
1396            self.forward(length)
[48]1397            length = 0
1398        if not chunks:
[116]1399            raise ScannerError("while parsing a %s" % name, start_mark,
[328]1400                    "expected URI, but found %r" % ch, self.get_mark())
1401        return ''.join(chunks)
[48]1402
[116]1403    def scan_uri_escapes(self, name, start_mark):
[48]1404        # See the specification for details.
[328]1405        codes = []
[136]1406        mark = self.get_mark()
[328]1407        while self.peek() == '%':
[136]1408            self.forward()
[48]1409            for k in range(2):
[328]1410                if self.peek(k) not in '0123456789ABCDEFabcdef':
[116]1411                    raise ScannerError("while scanning a %s" % name, start_mark,
[328]1412                            "expected URI escape sequence of 2 hexdecimal numbers, but found %r"
1413                            % self.peek(k), self.get_mark())
1414            codes.append(int(self.prefix(2), 16))
[136]1415            self.forward(2)
[48]1416        try:
[328]1417            value = bytes(codes).decode('utf-8')
1418        except UnicodeDecodeError as exc:
[116]1419            raise ScannerError("while scanning a %s" % name, start_mark, str(exc), mark)
[48]1420        return value
1421
[47]1422    def scan_line_break(self):
1423        # Transforms:
1424        #   '\r\n'      :   '\n'
1425        #   '\r'        :   '\n'
1426        #   '\n'        :   '\n'
1427        #   '\x85'      :   '\n'
1428        #   '\u2028'    :   '\u2028'
1429        #   '\u2029     :   '\u2029'
1430        #   default     :   ''
[136]1431        ch = self.peek()
[328]1432        if ch in '\r\n\x85':
1433            if self.prefix(2) == '\r\n':
[136]1434                self.forward(2)
[47]1435            else:
[136]1436                self.forward()
[328]1437            return '\n'
1438        elif ch in '\u2028\u2029':
[136]1439            self.forward()
[47]1440            return ch
[328]1441        return ''
[47]1442
[45]1443#try:
1444#    import psyco
1445#    psyco.bind(Scanner)
1446#except ImportError:
1447#    pass
1448
Note: See TracBrowser for help on using the repository browser.