Changeset 117


Ignore:
Timestamp:
03/18/06 20:30:05 (8 years ago)
Author:
xi
Message:

Loose indentation rules for the following cases:

  • In the flow context, indentation level is not checked.
  • For quoted scalars, indentation level is not checked.
  • Flow scalars are not required to have at least 1 space indentation. Zero indentation is acceptable.
Location:
pyyaml/trunk
Files:
3 added
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • pyyaml/trunk/lib/yaml/scanner.py

    r116 r117  
    337337    def unwind_indent(self, column): 
    338338 
    339         # In flow context, tokens should respect indentation. 
    340         # Actually the condition should be `self.indent >= column` according to 
    341         # the spec. But this condition will prohibit intuitively correct 
    342         # constructions such as 
    343         # key : { 
    344         # } 
    345         if self.flow_level and self.indent > column: 
    346             raise ScannerError(None, None, 
    347                     "invalid intendation or unclosed '[' or '{'", 
    348                     self.reader.get_mark()) 
     339        ## In flow context, tokens should respect indentation. 
     340        ## Actually the condition should be `self.indent >= column` according to 
     341        ## the spec. But this condition will prohibit intuitively correct 
     342        ## constructions such as 
     343        ## key : { 
     344        ## } 
     345        #if self.flow_level and self.indent > column: 
     346        #    raise ScannerError(None, None, 
     347        #            "invalid intendation or unclosed '[' or '{'", 
     348        #            self.reader.get_mark()) 
     349 
     350        # In the flow context, indentation is ignored. We make the scanner less 
     351        # restrictive then specification requires. 
     352        if self.flow_level: 
     353            return 
    349354 
    350355        # In block context, we may need to issue the BLOCK-END tokens. 
     
    11201125    def scan_flow_scalar(self, double): 
    11211126        # See the specification for details. 
     1127        # Note that we loose indentation rules for quoted scalars. Quoted 
     1128        # scalars don't need to adhere indentation because " and ' clearly 
     1129        # mark the beginning and the end of them. Therefore we are less 
     1130        # restrictive then the specification requires. We only need to check 
     1131        # that document separators are not included in scalars. 
    11221132        chunks = [] 
    11231133        start_mark = self.reader.get_mark() 
    1124         indent = self.indent+1 
    1125         if indent == 0: 
    1126             indent = 1 
    11271134        quote = self.reader.peek() 
    11281135        self.reader.forward() 
    1129         chunks.extend(self.scan_flow_scalar_non_spaces(double, indent, start_mark)) 
     1136        chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark)) 
    11301137        while self.reader.peek() != quote: 
    1131             chunks.extend(self.scan_flow_scalar_spaces(double, indent, start_mark)) 
    1132             chunks.extend(self.scan_flow_scalar_non_spaces(double, indent, start_mark)) 
     1138            chunks.extend(self.scan_flow_scalar_spaces(double, start_mark)) 
     1139            chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark)) 
    11331140        self.reader.forward() 
    11341141        end_mark = self.reader.get_mark() 
     
    11611168    } 
    11621169 
    1163     def scan_flow_scalar_non_spaces(self, double, indent, start_mark): 
     1170    def scan_flow_scalar_non_spaces(self, double, start_mark): 
    11641171        # See the specification for details. 
    11651172        chunks = [] 
     
    11971204                elif ch in u'\r\n\x85\u2028\u2029': 
    11981205                    self.scan_line_break() 
    1199                     chunks.extend(self.scan_flow_scalar_breaks(double, indent, start_mark)) 
     1206                    chunks.extend(self.scan_flow_scalar_breaks(double, start_mark)) 
    12001207                else: 
    12011208                    raise ScannerError("while scanning a double-quoted scalar", start_mark, 
     
    12041211                return chunks 
    12051212 
    1206     def scan_flow_scalar_spaces(self, double, indent, start_mark): 
     1213    def scan_flow_scalar_spaces(self, double, start_mark): 
    12071214        # See the specification for details. 
    12081215        chunks = [] 
     
    12181225        elif ch in u'\r\n\x85\u2028\u2029': 
    12191226            line_break = self.scan_line_break() 
    1220             breaks = self.scan_flow_scalar_breaks(double, indent, start_mark) 
     1227            breaks = self.scan_flow_scalar_breaks(double, start_mark) 
    12211228            if line_break != u'\n': 
    12221229                chunks.append(line_break) 
     
    12281235        return chunks 
    12291236 
    1230     def scan_flow_scalar_breaks(self, double, indent, start_mark): 
     1237    def scan_flow_scalar_breaks(self, double, start_mark): 
    12311238        # See the specification for details. 
    12321239        chunks = [] 
    12331240        while True: 
    1234             while self.reader.column < indent and self.reader.peek() == u' ': 
    1235                 self.reader.forward() 
    1236             if self.reader.column < indent  \ 
    1237                     and self.reader.peek() not in u'\0\r\n\x85\u2028\u2029': 
    1238                 s = 's' 
    1239                 if indent == 1: 
    1240                     s = '' 
     1241            # Instead of checking indentation, we check for document 
     1242            # separators. 
     1243            prefix = self.reader.prefix(3) 
     1244            if (prefix == u'---' or prefix == u'...')   \ 
     1245                    and self.reader.peek(3) in u'\0 \t\r\n\x85\u2028\u2029': 
    12411246                raise ScannerError("while scanning a quoted scalar", start_mark, 
    1242                         "expected %d space%s indentation, but found %r" 
    1243                         % (indent, s, self.reader.peek().encode('utf-8')), 
    1244                         self.reader.get_mark()) 
     1247                        "found unexpected document separator", self.reader.get_mark()) 
    12451248            while self.reader.peek() in u' \t': 
    12461249                self.reader.forward() 
     
    12531256        # See the specification for details. 
    12541257        # We add an additional restriction for the flow context: 
    1255         #   plain scalars in the flow context cannot contain ':' and '?'. 
     1258        #   plain scalars in the flow context cannot contain ',', ':' and '?'. 
    12561259        # We also keep track of the `allow_simple_key` flag here. 
     1260        # Indentation rules are loosed for the flow context. 
    12571261        chunks = [] 
    12581262        start_mark = self.reader.get_mark() 
    12591263        end_mark = start_mark 
    12601264        indent = self.indent+1 
    1261         if indent == 0: 
    1262             indent = 1 
     1265        # We allow zero indentation for scalars, but then we need to check for 
     1266        # document separators at the beginning of the line. 
     1267        #if indent == 0: 
     1268        #    indent = 1 
    12631269        spaces = [] 
    12641270        while True: 
     
    12811287            self.reader.forward(length) 
    12821288            end_mark = self.reader.get_mark() 
    1283             spaces = self.scan_plain_spaces(indent) 
     1289            spaces = self.scan_plain_spaces(indent, start_mark) 
    12841290            if not spaces or self.reader.peek() == u'#' \ 
    1285                     or self.reader.column < indent: 
     1291                    or (not self.flow_level and self.reader.column < indent): 
    12861292                break 
    12871293        return ScalarToken(u''.join(chunks), True, start_mark, end_mark) 
    12881294 
    1289     def scan_plain_spaces(self, indent): 
     1295    def scan_plain_spaces(self, indent, start_mark): 
    12901296        # See the specification for details. 
    12911297        # The specification is really confusing about tabs in plain scalars. 
     
    13011307            line_break = self.scan_line_break() 
    13021308            self.allow_simple_key = True 
     1309            prefix = self.reader.prefix(3) 
     1310            if (prefix == u'---' or prefix == u'...')   \ 
     1311                    and self.reader.peek(3) in u'\0 \t\r\n\x85\u2028\u2029': 
     1312                return 
    13031313            breaks = [] 
    13041314            while self.reader.peek() in u' \r\n\x85\u2028\u2029': 
     
    13071317                else: 
    13081318                    breaks.append(self.scan_line_break()) 
     1319                    prefix = self.reader.prefix(3) 
     1320                    if (prefix == u'---' or prefix == u'...')   \ 
     1321                            and self.reader.peek(3) in u'\0 \t\r\n\x85\u2028\u2029': 
     1322                        return 
    13091323            if line_break != u'\n': 
    13101324                chunks.append(line_break) 
Note: See TracChangeset for help on using the changeset viewer.