Changeset 117
- Timestamp:
- 03/18/06 20:30:05 (7 years ago)
- Location:
- pyyaml/trunk
- Files:
-
- 3 added
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pyyaml/trunk/lib/yaml/scanner.py
r116 r117 337 337 def unwind_indent(self, column): 338 338 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 349 354 350 355 # In block context, we may need to issue the BLOCK-END tokens. … … 1120 1125 def scan_flow_scalar(self, double): 1121 1126 # 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. 1122 1132 chunks = [] 1123 1133 start_mark = self.reader.get_mark() 1124 indent = self.indent+11125 if indent == 0:1126 indent = 11127 1134 quote = self.reader.peek() 1128 1135 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)) 1130 1137 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)) 1133 1140 self.reader.forward() 1134 1141 end_mark = self.reader.get_mark() … … 1161 1168 } 1162 1169 1163 def scan_flow_scalar_non_spaces(self, double, indent,start_mark):1170 def scan_flow_scalar_non_spaces(self, double, start_mark): 1164 1171 # See the specification for details. 1165 1172 chunks = [] … … 1197 1204 elif ch in u'\r\n\x85\u2028\u2029': 1198 1205 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)) 1200 1207 else: 1201 1208 raise ScannerError("while scanning a double-quoted scalar", start_mark, … … 1204 1211 return chunks 1205 1212 1206 def scan_flow_scalar_spaces(self, double, indent,start_mark):1213 def scan_flow_scalar_spaces(self, double, start_mark): 1207 1214 # See the specification for details. 1208 1215 chunks = [] … … 1218 1225 elif ch in u'\r\n\x85\u2028\u2029': 1219 1226 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) 1221 1228 if line_break != u'\n': 1222 1229 chunks.append(line_break) … … 1228 1235 return chunks 1229 1236 1230 def scan_flow_scalar_breaks(self, double, indent,start_mark):1237 def scan_flow_scalar_breaks(self, double, start_mark): 1231 1238 # See the specification for details. 1232 1239 chunks = [] 1233 1240 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': 1241 1246 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()) 1245 1248 while self.reader.peek() in u' \t': 1246 1249 self.reader.forward() … … 1253 1256 # See the specification for details. 1254 1257 # 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 '?'. 1256 1259 # We also keep track of the `allow_simple_key` flag here. 1260 # Indentation rules are loosed for the flow context. 1257 1261 chunks = [] 1258 1262 start_mark = self.reader.get_mark() 1259 1263 end_mark = start_mark 1260 1264 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 1263 1269 spaces = [] 1264 1270 while True: … … 1281 1287 self.reader.forward(length) 1282 1288 end_mark = self.reader.get_mark() 1283 spaces = self.scan_plain_spaces(indent )1289 spaces = self.scan_plain_spaces(indent, start_mark) 1284 1290 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): 1286 1292 break 1287 1293 return ScalarToken(u''.join(chunks), True, start_mark, end_mark) 1288 1294 1289 def scan_plain_spaces(self, indent ):1295 def scan_plain_spaces(self, indent, start_mark): 1290 1296 # See the specification for details. 1291 1297 # The specification is really confusing about tabs in plain scalars. … … 1301 1307 line_break = self.scan_line_break() 1302 1308 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 1303 1313 breaks = [] 1304 1314 while self.reader.peek() in u' \r\n\x85\u2028\u2029': … … 1307 1317 else: 1308 1318 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 1309 1323 if line_break != u'\n': 1310 1324 chunks.append(line_break)
Note: See TracChangeset
for help on using the changeset viewer.
