Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: mojo/public/dart/third_party/yaml/lib/src/scanner.dart

Issue 1346773002: Stop running pub get at gclient sync time and fix build bugs (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 library yaml.scanner;
6
7 import 'package:collection/collection.dart';
8 import 'package:string_scanner/string_scanner.dart';
9 import 'package:source_span/source_span.dart';
10
11 import 'style.dart';
12 import 'token.dart';
13 import 'utils.dart';
14 import 'yaml_exception.dart';
15
16 /// A scanner that reads a string of Unicode characters and emits [Token]s.
17 ///
18 /// This is based on the libyaml scanner, available at
19 /// https://github.com/yaml/libyaml/blob/master/src/scanner.c. The license for
20 /// that is available in ../../libyaml-license.txt.
21 class Scanner {
22 static const TAB = 0x9;
23 static const LF = 0xA;
24 static const CR = 0xD;
25 static const SP = 0x20;
26 static const DOLLAR = 0x24;
27 static const LEFT_PAREN = 0x28;
28 static const RIGHT_PAREN = 0x29;
29 static const PLUS = 0x2B;
30 static const COMMA = 0x2C;
31 static const HYPHEN = 0x2D;
32 static const PERIOD = 0x2E;
33 static const QUESTION = 0x3F;
34 static const COLON = 0x3A;
35 static const SEMICOLON = 0x3B;
36 static const EQUALS = 0x3D;
37 static const LEFT_SQUARE = 0x5B;
38 static const RIGHT_SQUARE = 0x5D;
39 static const LEFT_CURLY = 0x7B;
40 static const RIGHT_CURLY = 0x7D;
41 static const HASH = 0x23;
42 static const AMPERSAND = 0x26;
43 static const ASTERISK = 0x2A;
44 static const EXCLAMATION = 0x21;
45 static const VERTICAL_BAR = 0x7C;
46 static const LEFT_ANGLE = 0x3C;
47 static const RIGHT_ANGLE = 0x3E;
48 static const SINGLE_QUOTE = 0x27;
49 static const DOUBLE_QUOTE = 0x22;
50 static const PERCENT = 0x25;
51 static const AT = 0x40;
52 static const GRAVE_ACCENT = 0x60;
53 static const TILDE = 0x7E;
54
55 static const NULL = 0x0;
56 static const BELL = 0x7;
57 static const BACKSPACE = 0x8;
58 static const VERTICAL_TAB = 0xB;
59 static const FORM_FEED = 0xC;
60 static const ESCAPE = 0x1B;
61 static const SLASH = 0x2F;
62 static const BACKSLASH = 0x5C;
63 static const UNDERSCORE = 0x5F;
64 static const NEL = 0x85;
65 static const NBSP = 0xA0;
66 static const LINE_SEPARATOR = 0x2028;
67 static const PARAGRAPH_SEPARATOR = 0x2029;
68 static const BOM = 0xFEFF;
69
70 static const NUMBER_0 = 0x30;
71 static const NUMBER_9 = 0x39;
72
73 static const LETTER_A = 0x61;
74 static const LETTER_B = 0x62;
75 static const LETTER_E = 0x65;
76 static const LETTER_F = 0x66;
77 static const LETTER_N = 0x6E;
78 static const LETTER_R = 0x72;
79 static const LETTER_T = 0x74;
80 static const LETTER_U = 0x75;
81 static const LETTER_V = 0x76;
82 static const LETTER_X = 0x78;
83 static const LETTER_Z = 0x7A;
84
85 static const LETTER_CAP_A = 0x41;
86 static const LETTER_CAP_F = 0x46;
87 static const LETTER_CAP_L = 0x4C;
88 static const LETTER_CAP_N = 0x4E;
89 static const LETTER_CAP_P = 0x50;
90 static const LETTER_CAP_U = 0x55;
91 static const LETTER_CAP_X = 0x58;
92 static const LETTER_CAP_Z = 0x5A;
93
94 /// The underlying [SpanScanner] used to read characters from the source text.
95 ///
96 /// This is also used to track line and column information and to generate
97 /// [SourceSpan]s.
98 final SpanScanner _scanner;
99
100 /// Whether this scanner has produced a [TokenType.STREAM_START] token
101 /// indicating the beginning of the YAML stream.
102 var _streamStartProduced = false;
103
104 /// Whether this scanner has produced a [TokenType.STREAM_END] token
105 /// indicating the end of the YAML stream.
106 var _streamEndProduced = false;
107
108 /// The queue of tokens yet to be emitted.
109 ///
110 /// These are queued up in advance so that [TokenType.KEY] tokens can be
111 /// inserted once the scanner determines that a series of tokens represents a
112 /// mapping key.
113 final _tokens = new QueueList<Token>();
114
115 /// The number of tokens that have been emitted.
116 ///
117 /// This doesn't count tokens in [tokens].
118 var _tokensParsed = 0;
119
120 /// Whether the next token in [_tokens] is ready to be returned.
121 ///
122 /// It might not be ready if there may still be a [TokenType.KEY] inserted
123 /// before it.
124 var _tokenAvailable = false;
125
126 /// The stack of indent levels for the current nested block contexts.
127 ///
128 /// The YAML spec specifies that the initial indentation level is -1 spaces.
129 final _indents = <int>[-1];
130
131 /// Whether a simple key is allowed in this context.
132 ///
133 /// A simple key refers to any mapping key that doesn't have an explicit "?".
134 var _simpleKeyAllowed = true;
135
136 /// The stack of potential simple keys for each level of flow nesting.
137 ///
138 /// Entries in this list may be `null`, indicating that there is no valid
139 /// simple key for the associated level of nesting.
140 ///
141 /// When a ":" is parsed and there's a simple key available, a [TokenType.KEY]
142 /// token is inserted in [_tokens] before that key's token. This allows the
143 /// parser to tell that the key is intended to be a mapping key.
144 final _simpleKeys = <_SimpleKey>[null];
145
146 /// The current indentation level.
147 int get _indent => _indents.last;
148
149 /// Whether the scanner's currently positioned in a block-level structure (as
150 /// opposed to flow-level).
151 bool get _inBlockContext => _simpleKeys.length == 1;
152
153 /// Whether the current character is a line break or the end of the source.
154 bool get _isBreakOrEnd => _scanner.isDone || _isBreak;
155
156 /// Whether the current character is a line break.
157 bool get _isBreak => _isBreakAt(0);
158
159 /// Whether the current character is whitespace or the end of the source.
160 bool get _isBlankOrEnd => _isBlankOrEndAt(0);
161
162 /// Whether the current character is whitespace.
163 bool get _isBlank => _isBlankAt(0);
164
165 /// Whether the current character is a valid tag name character.
166 ///
167 /// See http://yaml.org/spec/1.2/spec.html#ns-tag-name.
168 bool get _isTagChar {
169 var char = _scanner.peekChar();
170 if (char == null) return false;
171 switch (char) {
172 case HYPHEN:
173 case SEMICOLON:
174 case SLASH:
175 case COLON:
176 case AT:
177 case AMPERSAND:
178 case EQUALS:
179 case PLUS:
180 case DOLLAR:
181 case PERIOD:
182 case TILDE:
183 case QUESTION:
184 case ASTERISK:
185 case SINGLE_QUOTE:
186 case LEFT_PAREN:
187 case RIGHT_PAREN:
188 case PERCENT:
189 return true;
190 default:
191 return (char >= NUMBER_0 && char <= NUMBER_9) ||
192 (char >= LETTER_A && char <= LETTER_Z) ||
193 (char >= LETTER_CAP_A && char <= LETTER_CAP_Z);
194 }
195 }
196
197 /// Whether the current character is a valid anchor name character.
198 ///
199 /// See http://yaml.org/spec/1.2/spec.html#ns-anchor-name.
200 bool get _isAnchorChar {
201 if (!_isNonSpace) return false;
202
203 switch (_scanner.peekChar()) {
204 case COMMA:
205 case LEFT_SQUARE:
206 case RIGHT_SQUARE:
207 case LEFT_CURLY:
208 case RIGHT_CURLY:
209 return false;
210 default:
211 return true;
212 }
213 }
214
215 /// Whether the character at the current position is a decimal digit.
216 bool get _isDigit {
217 var char = _scanner.peekChar();
218 return char != null && (char >= NUMBER_0 && char <= NUMBER_9);
219 }
220
221 /// Whether the character at the current position is a hexidecimal
222 /// digit.
223 bool get _isHex {
224 var char = _scanner.peekChar();
225 if (char == null) return false;
226 return (char >= NUMBER_0 && char <= NUMBER_9) ||
227 (char >= LETTER_A && char <= LETTER_F) ||
228 (char >= LETTER_CAP_A && char <= LETTER_CAP_F);
229 }
230
231 /// Whether the character at the current position is a plain character.
232 ///
233 /// See http://yaml.org/spec/1.2/spec.html#ns-plain-char(c).
234 bool get _isPlainChar => _isPlainCharAt(0);
235
236 /// Whether the character at the current position is a printable character
237 /// other than a line break or byte-order mark.
238 ///
239 /// See http://yaml.org/spec/1.2/spec.html#nb-char.
240 bool get _isNonBreak {
241 var char = _scanner.peekChar();
242 if (char == null) return false;
243 switch (char) {
244 case LF:
245 case CR:
246 case BOM:
247 return false;
248 case TAB:
249 case NEL:
250 return true;
251 default:
252 return (char >= 0x00020 && char <= 0x00007E) ||
253 (char >= 0x000A0 && char <= 0x00D7FF) ||
254 (char >= 0x0E000 && char <= 0x00FFFD) ||
255 (char >= 0x10000 && char <= 0x10FFFF);
256 }
257 }
258
259 /// Whether the character at the current position is a printable character
260 /// other than whitespace.
261 ///
262 /// See http://yaml.org/spec/1.2/spec.html#nb-char.
263 bool get _isNonSpace {
264 var char = _scanner.peekChar();
265 if (char == null) return false;
266 switch (char) {
267 case LF:
268 case CR:
269 case BOM:
270 case SP:
271 return false;
272 case NEL:
273 return true;
274 default:
275 return (char >= 0x00020 && char <= 0x00007E) ||
276 (char >= 0x000A0 && char <= 0x00D7FF) ||
277 (char >= 0x0E000 && char <= 0x00FFFD) ||
278 (char >= 0x10000 && char <= 0x10FFFF);
279 }
280 }
281
282 /// Returns Whether or not the current character begins a documentation
283 /// indicator.
284 ///
285 /// If so, this sets the scanner's last match to that indicator.
286 bool get _isDocumentIndicator {
287 return _scanner.column == 0 && _isBlankOrEndAt(3) &&
288 (_scanner.matches('---') || _scanner.matches('...'));
289 }
290
291 /// Creates a scanner that scans [source].
292 ///
293 /// [sourceUrl] can be a String or a [Uri].
294 Scanner(String source, {sourceUrl})
295 : _scanner = new SpanScanner(source, sourceUrl: sourceUrl);
296
297 /// Consumes and returns the next token.
298 Token scan() {
299 if (_streamEndProduced) throw new StateError("Out of tokens.");
300 if (!_tokenAvailable) _fetchMoreTokens();
301
302 var token = _tokens.removeFirst();
303 _tokenAvailable = false;
304 _tokensParsed++;
305 _streamEndProduced = token is Token &&
306 token.type == TokenType.STREAM_END;
307 return token;
308 }
309
310 /// Consumes the next token and returns the one after that.
311 Token advance() {
312 scan();
313 return peek();
314 }
315
316 /// Returns the next token without consuming it.
317 Token peek() {
318 if (_streamEndProduced) return null;
319 if (!_tokenAvailable) _fetchMoreTokens();
320 return _tokens.first;
321 }
322
323 /// Ensures that [_tokens] contains at least one token which can be returned.
324 void _fetchMoreTokens() {
325 while (true) {
326 if (_tokens.isNotEmpty) {
327 _staleSimpleKeys();
328
329 // If the current token could be a simple key, we need to scan more
330 // tokens until we determine whether it is or not. Otherwise we might
331 // not emit the `KEY` token before we emit the value of the key.
332 if (!_simpleKeys.any((key) =>
333 key != null && key.tokenNumber == _tokensParsed)) {
334 break;
335 }
336 }
337
338 _fetchNextToken();
339 }
340 _tokenAvailable = true;
341 }
342
343 /// The dispatcher for token fetchers.
344 void _fetchNextToken() {
345 if (!_streamStartProduced) {
346 _fetchStreamStart();
347 return;
348 }
349
350 _scanToNextToken();
351 _staleSimpleKeys();
352 _unrollIndent(_scanner.column);
353
354 if (_scanner.isDone) {
355 _fetchStreamEnd();
356 return;
357 }
358
359 if (_scanner.column == 0) {
360 if (_scanner.peekChar() == PERCENT) {
361 _fetchDirective();
362 return;
363 }
364
365 if (_isBlankOrEndAt(3)) {
366 if (_scanner.matches('---')) {
367 _fetchDocumentIndicator(TokenType.DOCUMENT_START);
368 return;
369 }
370
371 if (_scanner.matches('...')) {
372 _fetchDocumentIndicator(TokenType.DOCUMENT_END);
373 return;
374 }
375 }
376 }
377
378 switch (_scanner.peekChar()) {
379 case LEFT_SQUARE:
380 _fetchFlowCollectionStart(TokenType.FLOW_SEQUENCE_START);
381 return;
382 case LEFT_CURLY:
383 _fetchFlowCollectionStart(TokenType.FLOW_MAPPING_START);
384 return;
385 case RIGHT_SQUARE:
386 _fetchFlowCollectionEnd(TokenType.FLOW_SEQUENCE_END);
387 return;
388 case RIGHT_CURLY:
389 _fetchFlowCollectionEnd(TokenType.FLOW_MAPPING_END);
390 return;
391 case COMMA:
392 _fetchFlowEntry();
393 return;
394 case ASTERISK:
395 _fetchAnchor(anchor: false);
396 return;
397 case AMPERSAND:
398 _fetchAnchor(anchor: true);
399 return;
400 case EXCLAMATION:
401 _fetchTag();
402 return;
403 case SINGLE_QUOTE:
404 _fetchFlowScalar(singleQuote: true);
405 return;
406 case DOUBLE_QUOTE:
407 _fetchFlowScalar(singleQuote: false);
408 return;
409 case VERTICAL_BAR:
410 if (!_inBlockContext) _invalidScalarCharacter();
411 _fetchBlockScalar(literal: true);
412 return;
413 case RIGHT_ANGLE:
414 if (!_inBlockContext) _invalidScalarCharacter();
415 _fetchBlockScalar(literal: false);
416 return;
417 case PERCENT:
418 case AT:
419 case GRAVE_ACCENT:
420 _invalidScalarCharacter();
421 return;
422
423 // These characters may sometimes begin plain scalars.
424 case HYPHEN:
425 if (_isPlainCharAt(1)) {
426 _fetchPlainScalar();
427 } else {
428 _fetchBlockEntry();
429 }
430 return;
431 case QUESTION:
432 if (_isPlainCharAt(1)) {
433 _fetchPlainScalar();
434 } else {
435 _fetchKey();
436 }
437 return;
438 case COLON:
439 if (!_inBlockContext && _tokens.isNotEmpty) {
440 // If a colon follows a "JSON-like" value (an explicit map or list, or
441 // a quoted string) it isn't required to have whitespace after it
442 // since it unambiguously describes a map.
443 var token = _tokens.last;
444 if (token.type == TokenType.FLOW_SEQUENCE_END ||
445 token.type == TokenType.FLOW_MAPPING_END ||
446 (token.type == TokenType.SCALAR && token.style.isQuoted)) {
447 _fetchValue();
448 return;
449 }
450 }
451
452 if (_isPlainCharAt(1)) {
453 _fetchPlainScalar();
454 } else {
455 _fetchValue();
456 }
457 return;
458 default:
459 if (!_isNonBreak) _invalidScalarCharacter();
460
461 _fetchPlainScalar();
462 return;
463 }
464
465 throw 'Inaccessible';
466 }
467
468 /// Throws an error about a disallowed character.
469 void _invalidScalarCharacter() =>
470 _scanner.error("Unexpected character.", length: 1);
471
472 /// Checks the list of potential simple keys and remove the positions that
473 /// cannot contain simple keys anymore.
474 void _staleSimpleKeys() {
475 for (var i = 0; i < _simpleKeys.length; i++) {
476 var key = _simpleKeys[i];
477 if (key == null) continue;
478
479 // libyaml requires that all simple keys be a single line and no longer
480 // than 1024 characters. However, in section 7.4.2 of the spec
481 // (http://yaml.org/spec/1.2/spec.html#id2790832), these restrictions are
482 // only applied when the curly braces are omitted. It's difficult to
483 // retain enough context to know which keys need to have the restriction
484 // placed on them, so for now we go the other direction and allow
485 // everything but multiline simple keys in a block context.
486 if (!_inBlockContext) continue;
487
488 if (key.location.line == _scanner.line) continue;
489
490 if (key.required) {
491 throw new YamlException("Expected ':'.", _scanner.emptySpan);
492 }
493
494 _simpleKeys[i] = null;
495 }
496 }
497
498 /// Checks if a simple key may start at the current position and saves it if
499 /// so.
500 void _saveSimpleKey() {
501 // A simple key is required at the current position if the scanner is in the
502 // block context and the current column coincides with the indentation
503 // level.
504 var required = _inBlockContext && _indent == _scanner.column;
505
506 // A simple key is required only when it is the first token in the current
507 // line. Therefore it is always allowed. But we add a check anyway.
508 assert(_simpleKeyAllowed || !required);
509
510 if (!_simpleKeyAllowed) return;
511
512 // If the current position may start a simple key, save it.
513 _removeSimpleKey();
514 _simpleKeys[_simpleKeys.length - 1] = new _SimpleKey(
515 _tokensParsed + _tokens.length,
516 _scanner.location,
517 required: required);
518 }
519
520 /// Removes a potential simple key at the current flow level.
521 void _removeSimpleKey() {
522 var key = _simpleKeys.last;
523 if (key != null && key.required) {
524 throw new YamlException("Could not find expected ':' for simple key.",
525 key.location.pointSpan());
526 }
527
528 _simpleKeys[_simpleKeys.length - 1] = null;
529 }
530
531 /// Increases the flow level and resizes the simple key list.
532 void _increaseFlowLevel() {
533 _simpleKeys.add(null);
534 }
535
536 /// Decreases the flow level.
537 void _decreaseFlowLevel() {
538 if (_inBlockContext) return;
539 _simpleKeys.removeLast();
540 }
541
542 /// Pushes the current indentation level to the stack and sets the new level
543 /// if [column] is greater than [_indent].
544 ///
545 /// If it is, appends or inserts the specified token into [_tokens]. If
546 /// [tokenNumber] is provided, the corresponding token will be replaced;
547 /// otherwise, the token will be added at the end.
548 void _rollIndent(int column, TokenType type, SourceLocation location,
549 {int tokenNumber}) {
550 if (!_inBlockContext) return;
551 if (_indent != -1 && _indent >= column) return;
552
553 // Push the current indentation level to the stack and set the new
554 // indentation level.
555 _indents.add(column);
556
557 // Create a token and insert it into the queue.
558 var token = new Token(type, location.pointSpan());
559 if (tokenNumber == null) {
560 _tokens.add(token);
561 } else {
562 _tokens.insert(tokenNumber - _tokensParsed, token);
563 }
564 }
565
566 /// Pops indentation levels from [_indents] until the current level becomes
567 /// less than or equal to [column].
568 ///
569 /// For each indentation level, appends a [TokenType.BLOCK_END] token.
570 void _unrollIndent(int column) {
571 if (!_inBlockContext) return;
572
573 while (_indent > column) {
574 _tokens.add(new Token(TokenType.BLOCK_END, _scanner.emptySpan));
575 _indents.removeLast();
576 }
577 }
578
579 /// Pops indentation levels from [_indents] until the current level resets to
580 /// -1.
581 ///
582 /// For each indentation level, appends a [TokenType.BLOCK_END] token.
583 void _resetIndent() => _unrollIndent(-1);
584
585 /// Produces a [TokenType.STREAM_START] token.
586 void _fetchStreamStart() {
587 // Much of libyaml's initialization logic here is done in variable
588 // initializers instead.
589 _streamStartProduced = true;
590 _tokens.add(new Token(TokenType.STREAM_START, _scanner.emptySpan));
591 }
592
593 /// Produces a [TokenType.STREAM_END] token.
594 void _fetchStreamEnd() {
595 _resetIndent();
596 _removeSimpleKey();
597 _simpleKeyAllowed = false;
598 _tokens.add(new Token(TokenType.STREAM_END, _scanner.emptySpan));
599 }
600
601 /// Produces a [TokenType.VERSION_DIRECTIVE] or [TokenType.TAG_DIRECTIVE]
602 /// token.
603 void _fetchDirective() {
604 _resetIndent();
605 _removeSimpleKey();
606 _simpleKeyAllowed = false;
607 var directive = _scanDirective();
608 if (directive != null) _tokens.add(directive);
609 }
610
611 /// Produces a [TokenType.DOCUMENT_START] or [TokenType.DOCUMENT_END] token.
612 void _fetchDocumentIndicator(TokenType type) {
613 _resetIndent();
614 _removeSimpleKey();
615 _simpleKeyAllowed = false;
616
617 // Consume the indicator token.
618 var start = _scanner.state;
619 _scanner.readChar();
620 _scanner.readChar();
621 _scanner.readChar();
622
623 _tokens.add(new Token(type, _scanner.spanFrom(start)));
624 }
625
626 /// Produces a [TokenType.FLOW_SEQUENCE_START] or
627 /// [TokenType.FLOW_MAPPING_START] token.
628 void _fetchFlowCollectionStart(TokenType type) {
629 _saveSimpleKey();
630 _increaseFlowLevel();
631 _simpleKeyAllowed = true;
632 _addCharToken(type);
633 }
634
635 /// Produces a [TokenType.FLOW_SEQUENCE_END] or [TokenType.FLOW_MAPPING_END]
636 /// token.
637 void _fetchFlowCollectionEnd(TokenType type) {
638 _removeSimpleKey();
639 _decreaseFlowLevel();
640 _simpleKeyAllowed = false;
641 _addCharToken(type);
642 }
643
644 /// Produces a [TokenType.FLOW_ENTRY] token.
645 void _fetchFlowEntry() {
646 _removeSimpleKey();
647 _simpleKeyAllowed = true;
648 _addCharToken(TokenType.FLOW_ENTRY);
649 }
650
651 /// Produces a [TokenType.BLOCK_ENTRY] token.
652 void _fetchBlockEntry() {
653 if (_inBlockContext) {
654 if (!_simpleKeyAllowed) {
655 throw new YamlException(
656 "Block sequence entries are not allowed here.",
657 _scanner.emptySpan);
658 }
659
660 _rollIndent(
661 _scanner.column,
662 TokenType.BLOCK_SEQUENCE_START,
663 _scanner.emptySpan.start);
664 } else {
665 // It is an error for the '-' indicator to occur in the flow context, but
666 // we let the Parser detect and report it because it's able to point to
667 // the context.
668 }
669
670 _removeSimpleKey();
671 _simpleKeyAllowed = true;
672 _addCharToken(TokenType.BLOCK_ENTRY);
673 }
674
675 /// Produces the [TokenType.KEY] token.
676 void _fetchKey() {
677 if (_inBlockContext) {
678 if (!_simpleKeyAllowed) {
679 throw new YamlException("Mapping keys are not allowed here.",
680 _scanner.emptySpan);
681 }
682
683 _rollIndent(
684 _scanner.column,
685 TokenType.BLOCK_MAPPING_START,
686 _scanner.emptySpan.start);
687 }
688
689 // Simple keys are allowed after `?` in a block context.
690 _simpleKeyAllowed = _inBlockContext;
691 _addCharToken(TokenType.KEY);
692 }
693
694 /// Produces the [TokenType.VALUE] token.
695 void _fetchValue() {
696 var simpleKey = _simpleKeys.last;
697 if (simpleKey != null) {
698 // Add a [TokenType.KEY] directive before the first token of the simple
699 // key so the parser knows that it's part of a key/value pair.
700 _tokens.insert(simpleKey.tokenNumber - _tokensParsed,
701 new Token(TokenType.KEY, simpleKey.location.pointSpan()));
702
703 // In the block context, we may need to add the
704 // [TokenType.BLOCK_MAPPING_START] token.
705 _rollIndent(
706 simpleKey.location.column,
707 TokenType.BLOCK_MAPPING_START,
708 simpleKey.location,
709 tokenNumber: simpleKey.tokenNumber);
710
711 // Remove the simple key.
712 _simpleKeys[_simpleKeys.length - 1] = null;
713
714 // A simple key cannot follow another simple key.
715 _simpleKeyAllowed = false;
716 } else if (_inBlockContext) {
717 if (!_simpleKeyAllowed) {
718 throw new YamlException(
719 "Mapping values are not allowed here. Did you miss a colon "
720 "earlier?",
721 _scanner.emptySpan);
722 }
723
724 // If we're here, we've found the ':' indicator following a complex key.
725
726 _rollIndent(
727 _scanner.column,
728 TokenType.BLOCK_MAPPING_START,
729 _scanner.location);
730 _simpleKeyAllowed = true;
731 } else if (_simpleKeyAllowed) {
732 // If we're here, we've found the ':' indicator with an empty key. This
733 // behavior differs from libyaml, which disallows empty implicit keys.
734 _simpleKeyAllowed = false;
735 _addCharToken(TokenType.KEY);
736 }
737
738 _addCharToken(TokenType.VALUE);
739 }
740
741 /// Adds a token with [type] to [_tokens].
742 ///
743 /// The span of the new token is the current character.
744 void _addCharToken(TokenType type) {
745 var start = _scanner.state;
746 _scanner.readChar();
747 _tokens.add(new Token(type, _scanner.spanFrom(start)));
748 }
749
750 /// Produces a [TokenType.ALIAS] or [TokenType.ANCHOR] token.
751 void _fetchAnchor({bool anchor: true}) {
752 _saveSimpleKey();
753 _simpleKeyAllowed = false;
754 _tokens.add(_scanAnchor(anchor: anchor));
755 }
756
757 /// Produces a [TokenType.TAG] token.
758 void _fetchTag() {
759 _saveSimpleKey();
760 _simpleKeyAllowed = false;
761 _tokens.add(_scanTag());
762 }
763
764 /// Produces a [TokenType.SCALAR] token with style [ScalarStyle.LITERAL] or
765 /// [ScalarStyle.FOLDED].
766 void _fetchBlockScalar({bool literal: false}) {
767 _removeSimpleKey();
768 _simpleKeyAllowed = true;
769 _tokens.add(_scanBlockScalar(literal: literal));
770 }
771
772 /// Produces a [TokenType.SCALAR] token with style [ScalarStyle.SINGLE_QUOTED]
773 /// or [ScalarStyle.DOUBLE_QUOTED].
774 void _fetchFlowScalar({bool singleQuote: false}) {
775 _saveSimpleKey();
776 _simpleKeyAllowed = false;
777 _tokens.add(_scanFlowScalar(singleQuote: singleQuote));
778 }
779
780 /// Produces a [TokenType.SCALAR] token with style [ScalarStyle.PLAIN].
781 void _fetchPlainScalar() {
782 _saveSimpleKey();
783 _simpleKeyAllowed = false;
784 _tokens.add(_scanPlainScalar());
785 }
786
787 /// Eats whitespace and comments until the next token is found.
788 void _scanToNextToken() {
789 var afterLineBreak = false;
790 while (true) {
791 // Allow the BOM to start a line.
792 if (_scanner.column == 0) _scanner.scan("\uFEFF");
793
794 // Eat whitespace.
795 //
796 // libyaml disallows tabs after "-", "?", or ":", but the spec allows
797 // them. See section 6.2: http://yaml.org/spec/1.2/spec.html#id2778241.
798 while (_scanner.peekChar() == SP ||
799 ((!_inBlockContext || !afterLineBreak) &&
800 _scanner.peekChar() == TAB)) {
801 _scanner.readChar();
802 }
803
804 if (_scanner.peekChar() == TAB) {
805 _scanner.error("Tab characters are not allowed as indentation.",
806 length: 1);
807 }
808
809 // Eat a comment until a line break.
810 _skipComment();
811
812 // If we're at a line break, eat it.
813 if (_isBreak) {
814 _skipLine();
815
816 // In the block context, a new line may start a simple key.
817 if (_inBlockContext) _simpleKeyAllowed = true;
818 afterLineBreak = true;
819 } else {
820 // Otherwise we've found a token.
821 break;
822 }
823 }
824 }
825
826 /// Scans a [TokenType.YAML_DIRECTIVE] or [TokenType.TAG_DIRECTIVE] token.
827 ///
828 /// %YAML 1.2 # a comment \n
829 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
830 /// %TAG !yaml! tag:yaml.org,2002: \n
831 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
832 Token _scanDirective() {
833 var start = _scanner.state;
834
835 // Eat '%'.
836 _scanner.readChar();
837
838 var token;
839 var name = _scanDirectiveName();
840 if (name == "YAML") {
841 token = _scanVersionDirectiveValue(start);
842 } else if (name == "TAG") {
843 token = _scanTagDirectiveValue(start);
844 } else {
845 warn("Warning: unknown directive.", _scanner.spanFrom(start));
846
847 // libyaml doesn't support unknown directives, but the spec says to ignore
848 // them and warn: http://yaml.org/spec/1.2/spec.html#id2781147.
849 while (!_isBreakOrEnd) {
850 _scanner.readChar();
851 }
852
853 return null;
854 }
855
856 // Eat the rest of the line, including any comments.
857 _skipBlanks();
858 _skipComment();
859
860 if (!_isBreakOrEnd) {
861 throw new YamlException(
862 "Expected comment or line break after directive.",
863 _scanner.spanFrom(start));
864 }
865
866 _skipLine();
867 return token;
868 }
869
870 /// Scans a directive name.
871 ///
872 /// %YAML 1.2 # a comment \n
873 /// ^^^^
874 /// %TAG !yaml! tag:yaml.org,2002: \n
875 /// ^^^
876 String _scanDirectiveName() {
877 // libyaml only allows word characters in directive names, but the spec
878 // disagrees: http://yaml.org/spec/1.2/spec.html#ns-directive-name.
879 var start = _scanner.position;
880 while (_isNonSpace) {
881 _scanner.readChar();
882 }
883
884 var name = _scanner.substring(start);
885 if (name.isEmpty) {
886 throw new YamlException("Expected directive name.", _scanner.emptySpan);
887 } else if (!_isBlankOrEnd) {
888 throw new YamlException(
889 "Unexpected character in directive name.", _scanner.emptySpan);
890 }
891
892 return name;
893 }
894
895 /// Scans the value of a version directive.
896 ///
897 /// %YAML 1.2 # a comment \n
898 /// ^^^^^^
899 Token _scanVersionDirectiveValue(LineScannerState start) {
900 _skipBlanks();
901
902 var major = _scanVersionDirectiveNumber();
903 _scanner.expect('.');
904 var minor = _scanVersionDirectiveNumber();
905
906 return new VersionDirectiveToken(_scanner.spanFrom(start), major, minor);
907 }
908
909 /// Scans the version number of a version directive.
910 ///
911 /// %YAML 1.2 # a comment \n
912 /// ^
913 /// %YAML 1.2 # a comment \n
914 /// ^
915 int _scanVersionDirectiveNumber() {
916 var start = _scanner.position;
917 while (_isDigit) {
918 _scanner.readChar();
919 }
920
921 var number = _scanner.substring(start);
922 if (number.isEmpty) {
923 throw new YamlException("Expected version number.", _scanner.emptySpan);
924 }
925
926 return int.parse(number);
927 }
928
929 /// Scans the value of a tag directive.
930 ///
931 /// %TAG !yaml! tag:yaml.org,2002: \n
932 /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
933 Token _scanTagDirectiveValue(LineScannerState start) {
934 _skipBlanks();
935
936 var handle = _scanTagHandle(directive: true);
937 if (!_isBlank) {
938 throw new YamlException("Expected whitespace.", _scanner.emptySpan);
939 }
940
941 _skipBlanks();
942
943 var prefix = _scanTagUri();
944 if (!_isBlankOrEnd) {
945 throw new YamlException("Expected whitespace.", _scanner.emptySpan);
946 }
947
948 return new TagDirectiveToken(_scanner.spanFrom(start), handle, prefix);
949 }
950
951 /// Scans a [TokenType.ANCHOR] token.
952 Token _scanAnchor({bool anchor: true}) {
953 var start = _scanner.state;
954
955 // Eat the indicator character.
956 _scanner.readChar();
957
958 // libyaml only allows word characters in anchor names, but the spec
959 // disagrees: http://yaml.org/spec/1.2/spec.html#ns-anchor-char.
960 var startPosition = _scanner.position;
961 while (_isAnchorChar) {
962 _scanner.readChar();
963 }
964 var name = _scanner.substring(startPosition);
965
966 var next = _scanner.peekChar();
967 if (name.isEmpty ||
968 (!_isBlankOrEnd && next != QUESTION && next != COLON &&
969 next != COMMA && next != RIGHT_SQUARE && next != RIGHT_CURLY &&
970 next != PERCENT && next != AT && next != GRAVE_ACCENT)) {
971 throw new YamlException("Expected alphanumeric character.",
972 _scanner.emptySpan);
973 }
974
975 if (anchor) {
976 return new AnchorToken(_scanner.spanFrom(start), name);
977 } else {
978 return new AliasToken(_scanner.spanFrom(start), name);
979 }
980 }
981
982 /// Scans a [TokenType.TAG] token.
983 Token _scanTag() {
984 var handle;
985 var suffix;
986 var start = _scanner.state;
987
988 // Check if the tag is in the canonical form.
989 if (_scanner.peekChar(1) == LEFT_ANGLE) {
990 // Eat '!<'.
991 _scanner.readChar();
992 _scanner.readChar();
993
994 handle = '';
995 suffix = _scanTagUri();
996
997 _scanner.expect('>');
998 } else {
999 // The tag has either the '!suffix' or the '!handle!suffix' form.
1000
1001 // First, try to scan a handle.
1002 handle = _scanTagHandle();
1003
1004 if (handle.length > 1 && handle.startsWith('!') && handle.endsWith('!')) {
1005 suffix = _scanTagUri(flowSeparators: false);
1006 } else {
1007 suffix = _scanTagUri(head: handle, flowSeparators: false);
1008
1009 // There was no explicit handle.
1010 if (suffix.isEmpty) {
1011 // This is the special '!' tag.
1012 handle = null;
1013 suffix = '!';
1014 } else {
1015 handle = '!';
1016 }
1017 }
1018 }
1019
1020 // libyaml insists on whitespace after a tag, but example 7.2 indicates
1021 // that it's not required: http://yaml.org/spec/1.2/spec.html#id2786720.
1022
1023 return new TagToken(_scanner.spanFrom(start), handle, suffix);
1024 }
1025
1026 /// Scans a tag handle.
1027 String _scanTagHandle({bool directive: false}) {
1028 _scanner.expect('!');
1029
1030 var buffer = new StringBuffer('!');
1031
1032 // libyaml only allows word characters in tags, but the spec disagrees:
1033 // http://yaml.org/spec/1.2/spec.html#ns-tag-char.
1034 var start = _scanner.position;
1035 while (_isTagChar) {
1036 _scanner.readChar();
1037 }
1038 buffer.write(_scanner.substring(start));
1039
1040 if (_scanner.peekChar() == EXCLAMATION) {
1041 buffer.writeCharCode(_scanner.readChar());
1042 } else {
1043 // It's either the '!' tag or not really a tag handle. If it's a %TAG
1044 // directive, it's an error. If it's a tag token, it must be part of a
1045 // URI.
1046 if (directive && buffer.toString() != '!') _scanner.expect('!');
1047 }
1048
1049 return buffer.toString();
1050 }
1051
1052 /// Scans a tag URI.
1053 ///
1054 /// [head] is the initial portion of the tag that's already been scanned.
1055 /// [flowSeparators] indicates whether the tag URI can contain flow
1056 /// separators.
1057 String _scanTagUri({String head, bool flowSeparators: true}) {
1058 var length = head == null ? 0 : head.length;
1059 var buffer = new StringBuffer();
1060
1061 // Copy the head if needed.
1062 //
1063 // Note that we don't copy the leading '!' character.
1064 if (length > 1) buffer.write(head.substring(1));
1065
1066 // The set of characters that may appear in URI is as follows:
1067 //
1068 // '0'-'9', 'A'-'Z', 'a'-'z', '_', '-', ';', '/', '?', ':', '@', '&',
1069 // '=', '+', '$', ',', '.', '!', '~', '*', '\'', '(', ')', '[', ']',
1070 // '%'.
1071 //
1072 // In a shorthand tag annotation, the flow separators ',', '[', and ']' are
1073 // disallowed.
1074 var start = _scanner.position;
1075 var char = _scanner.peekChar();
1076 while (_isTagChar || (flowSeparators &&
1077 (char == COMMA || char == LEFT_SQUARE || char == RIGHT_SQUARE))) {
1078 _scanner.readChar();
1079 char = _scanner.peekChar();
1080 }
1081
1082 // libyaml manually decodes the URL, but we don't have to do that.
1083 return Uri.decodeFull(_scanner.substring(start));
1084 }
1085
1086 /// Scans a block scalar.
1087 Token _scanBlockScalar({bool literal: false}) {
1088 var start = _scanner.state;
1089
1090 // Eat the indicator '|' or '>'.
1091 _scanner.readChar();
1092
1093 // Check for a chomping indicator.
1094 var chomping = _Chomping.CLIP;
1095 var increment = 0;
1096 var char = _scanner.peekChar();
1097 if (char == PLUS || char == HYPHEN) {
1098 chomping = char == PLUS ? _Chomping.KEEP : _Chomping.STRIP;
1099 _scanner.readChar();
1100
1101 // Check for an indentation indicator.
1102 if (_isDigit) {
1103 // Check that the indentation is greater than 0.
1104 if (_scanner.peekChar() == NUMBER_0) {
1105 throw new YamlException(
1106 "0 may not be used as an indentation indicator.",
1107 _scanner.spanFrom(start));
1108 }
1109
1110 increment = _scanner.readChar() - NUMBER_0;
1111 }
1112 } else if (_isDigit) {
1113 // Do the same as above, but in the opposite order.
1114 if (_scanner.peekChar() == NUMBER_0) {
1115 throw new YamlException(
1116 "0 may not be used as an indentation indicator.",
1117 _scanner.spanFrom(start));
1118 }
1119
1120 increment = _scanner.readChar() - NUMBER_0;
1121
1122 char = _scanner.peekChar();
1123 if (char == PLUS || char == HYPHEN) {
1124 chomping = char == PLUS ? _Chomping.KEEP : _Chomping.STRIP;
1125 _scanner.readChar();
1126 }
1127 }
1128
1129 // Eat whitespace and comments to the end of the line.
1130 _skipBlanks();
1131 _skipComment();
1132
1133 // Check if we're at the end of the line.
1134 if (!_isBreakOrEnd) {
1135 throw new YamlException("Expected comment or line break.",
1136 _scanner.emptySpan);
1137 }
1138
1139 _skipLine();
1140
1141 // If the block scalar has an explicit indentation indicator, add that to
1142 // the current indentation to get the indentation level for the scalar's
1143 // contents.
1144 var indent = 0;
1145 if (increment != 0) {
1146 indent = _indent >= 0 ? _indent + increment : increment;
1147 }
1148
1149 // Scan the leading line breaks to determine the indentation level if
1150 // needed.
1151 var pair = _scanBlockScalarBreaks(indent);
1152 indent = pair.first;
1153 var trailingBreaks = pair.last;
1154
1155 // Scan the block scalar contents.
1156 var buffer = new StringBuffer();
1157 var leadingBreak = '';
1158 var leadingBlank = false;
1159 var trailingBlank = false;
1160 var end = _scanner.state;
1161 while (_scanner.column == indent && !_scanner.isDone) {
1162 // Check for a document indicator. libyaml doesn't do this, but the spec
1163 // mandates it. See example 9.5:
1164 // http://yaml.org/spec/1.2/spec.html#id2801606.
1165 if (_isDocumentIndicator) break;
1166
1167 // We are at the beginning of a non-empty line.
1168
1169 // Is there trailing whitespace?
1170 trailingBlank = _isBlank;
1171
1172 // Check if we need to fold the leading line break.
1173 if (!literal && leadingBreak.isNotEmpty && !leadingBlank &&
1174 !trailingBlank) {
1175 // Do we need to join the lines with a space?
1176 if (trailingBreaks.isEmpty) buffer.writeCharCode(SP);
1177 } else {
1178 buffer.write(leadingBreak);
1179 }
1180 leadingBreak = '';
1181
1182 // Append the remaining line breaks.
1183 buffer.write(trailingBreaks);
1184
1185 // Is there leading whitespace?
1186 leadingBlank = _isBlank;
1187
1188 var startPosition = _scanner.position;
1189 while (!_isBreakOrEnd) {
1190 _scanner.readChar();
1191 }
1192 buffer.write(_scanner.substring(startPosition));
1193 end = _scanner.state;
1194
1195 // libyaml always reads a line here, but this breaks on block scalars at
1196 // the end of the document that end without newlines. See example 8.1:
1197 // http://yaml.org/spec/1.2/spec.html#id2793888.
1198 if (!_scanner.isDone) leadingBreak = _readLine();
1199
1200 // Eat the following indentation and spaces.
1201 var pair = _scanBlockScalarBreaks(indent);
1202 indent = pair.first;
1203 trailingBreaks = pair.last;
1204 }
1205
1206 // Chomp the tail.
1207 if (chomping != _Chomping.STRIP) buffer.write(leadingBreak);
1208 if (chomping == _Chomping.KEEP) buffer.write(trailingBreaks);
1209
1210 return new ScalarToken(_scanner.spanFrom(start, end), buffer.toString(),
1211 literal ? ScalarStyle.LITERAL : ScalarStyle.FOLDED);
1212 }
1213
1214 /// Scans indentation spaces and line breaks for a block scalar.
1215 ///
1216 /// Determines the intendation level if needed. Returns the new indentation
1217 /// level and the text of the line breaks.
1218 Pair<int, String> _scanBlockScalarBreaks(int indent) {
1219 var maxIndent = 0;
1220 var breaks = new StringBuffer();
1221
1222 while (true) {
1223 while ((indent == 0 || _scanner.column < indent) &&
1224 _scanner.peekChar() == SP) {
1225 _scanner.readChar();
1226 }
1227
1228 if (_scanner.column > maxIndent) maxIndent = _scanner.column;
1229
1230 // libyaml throws an error here if a tab character is detected, but the
1231 // spec treats tabs like any other non-space character. See example 8.2:
1232 // http://yaml.org/spec/1.2/spec.html#id2794311.
1233
1234 if (!_isBreak) break;
1235 breaks.write(_readLine());
1236 }
1237
1238 if (indent == 0) {
1239 indent = maxIndent;
1240 if (indent < _indent + 1) indent = _indent + 1;
1241
1242 // libyaml forces indent to be at least 1 here, but that doesn't seem to
1243 // be supported by the spec.
1244 }
1245
1246 return new Pair(indent, breaks.toString());
1247 }
1248
1249 // Scans a quoted scalar.
1250 Token _scanFlowScalar({bool singleQuote: false}) {
1251 var start = _scanner.state;
1252 var buffer = new StringBuffer();
1253
1254 // Eat the left quote.
1255 _scanner.readChar();
1256
1257 while (true) {
1258 // Check that there are no document indicators at the beginning of the
1259 // line.
1260 if (_isDocumentIndicator) {
1261 _scanner.error("Unexpected document indicator.");
1262 }
1263
1264 if (_scanner.isDone) {
1265 throw new YamlException("Unexpected end of file.", _scanner.emptySpan);
1266 }
1267
1268 var leadingBlanks = false;
1269 while (!_isBlankOrEnd) {
1270 var char = _scanner.peekChar();
1271 if (singleQuote && char == SINGLE_QUOTE &&
1272 _scanner.peekChar(1) == SINGLE_QUOTE) {
1273 // An escaped single quote.
1274 _scanner.readChar();
1275 _scanner.readChar();
1276 buffer.writeCharCode(SINGLE_QUOTE);
1277 } else if (char == (singleQuote ? SINGLE_QUOTE : DOUBLE_QUOTE)) {
1278 // The closing quote.
1279 break;
1280 } else if (!singleQuote && char == BACKSLASH && _isBreakAt(1)) {
1281 // An escaped newline.
1282 _scanner.readChar();
1283 _skipLine();
1284 leadingBlanks = true;
1285 break;
1286 } else if (!singleQuote && char == BACKSLASH) {
1287 var escapeStart = _scanner.state;
1288
1289 // An escape sequence.
1290 var codeLength = null;
1291 switch (_scanner.peekChar(1)) {
1292 case NUMBER_0:
1293 buffer.writeCharCode(NULL);
1294 break;
1295 case LETTER_A:
1296 buffer.writeCharCode(BELL);
1297 break;
1298 case LETTER_B:
1299 buffer.writeCharCode(BACKSPACE);
1300 break;
1301 case LETTER_T:
1302 case TAB:
1303 buffer.writeCharCode(TAB);
1304 break;
1305 case LETTER_N:
1306 buffer.writeCharCode(LF);
1307 break;
1308 case LETTER_V:
1309 buffer.writeCharCode(VERTICAL_TAB);
1310 break;
1311 case LETTER_F:
1312 buffer.writeCharCode(FORM_FEED);
1313 break;
1314 case LETTER_R:
1315 buffer.writeCharCode(CR);
1316 break;
1317 case LETTER_E:
1318 buffer.writeCharCode(ESCAPE);
1319 break;
1320 case SP:
1321 case DOUBLE_QUOTE:
1322 case SLASH:
1323 case BACKSLASH:
1324 // libyaml doesn't support an escaped forward slash, but it was
1325 // added in YAML 1.2. See section 5.7:
1326 // http://yaml.org/spec/1.2/spec.html#id2776092
1327 buffer.writeCharCode(_scanner.peekChar(1));
1328 break;
1329 case LETTER_CAP_N:
1330 buffer.writeCharCode(NEL);
1331 break;
1332 case UNDERSCORE:
1333 buffer.writeCharCode(NBSP);
1334 break;
1335 case LETTER_CAP_L:
1336 buffer.writeCharCode(LINE_SEPARATOR);
1337 break;
1338 case LETTER_CAP_P:
1339 buffer.writeCharCode(PARAGRAPH_SEPARATOR);
1340 break;
1341 case LETTER_X:
1342 codeLength = 2;
1343 break;
1344 case LETTER_U:
1345 codeLength = 4;
1346 break;
1347 case LETTER_CAP_U:
1348 codeLength = 8;
1349 break;
1350 default:
1351 throw new YamlException("Unknown escape character.",
1352 _scanner.spanFrom(escapeStart));
1353 }
1354
1355 _scanner.readChar();
1356 _scanner.readChar();
1357
1358 if (codeLength != null) {
1359 var value = 0;
1360 for (var i = 0; i < codeLength; i++) {
1361 if (!_isHex) {
1362 _scanner.readChar();
1363 throw new YamlException(
1364 "Expected $codeLength-digit hexidecimal number.",
1365 _scanner.spanFrom(escapeStart));
1366 }
1367
1368 value = (value << 4) + _asHex(_scanner.readChar());
1369 }
1370
1371 // Check the value and write the character.
1372 if ((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
1373 throw new YamlException(
1374 "Invalid Unicode character escape code.",
1375 _scanner.spanFrom(escapeStart));
1376 }
1377
1378 buffer.writeCharCode(value);
1379 }
1380 } else {
1381 buffer.writeCharCode(_scanner.readChar());
1382 }
1383 }
1384
1385 // Check if we're at the end of a scalar.
1386 if (_scanner.peekChar() == (singleQuote ? SINGLE_QUOTE : DOUBLE_QUOTE)) {
1387 break;
1388 }
1389
1390 var whitespace = new StringBuffer();
1391 var leadingBreak = '';
1392 var trailingBreaks = new StringBuffer();
1393 while (_isBlank || _isBreak) {
1394 if (_isBlank) {
1395 // Consume a space or a tab.
1396 if (!leadingBlanks) {
1397 whitespace.writeCharCode(_scanner.readChar());
1398 } else {
1399 _scanner.readChar();
1400 }
1401 } else {
1402 // Check if it's a first line break.
1403 if (!leadingBlanks) {
1404 whitespace.clear();
1405 leadingBreak = _readLine();
1406 leadingBlanks = true;
1407 } else {
1408 trailingBreaks.write(_readLine());
1409 }
1410 }
1411 }
1412
1413 // Join the whitespace or fold line breaks.
1414 if (leadingBlanks) {
1415 if (leadingBreak.isNotEmpty && trailingBreaks.isEmpty) {
1416 buffer.writeCharCode(SP);
1417 } else {
1418 buffer.write(trailingBreaks);
1419 }
1420 } else {
1421 buffer.write(whitespace);
1422 whitespace.clear();
1423 }
1424 }
1425
1426 // Eat the right quote.
1427 _scanner.readChar();
1428
1429 return new ScalarToken(_scanner.spanFrom(start), buffer.toString(),
1430 singleQuote ? ScalarStyle.SINGLE_QUOTED : ScalarStyle.DOUBLE_QUOTED);
1431 }
1432
1433 /// Scans a plain scalar.
1434 Token _scanPlainScalar() {
1435 var start = _scanner.state;
1436 var end = _scanner.state;
1437 var buffer = new StringBuffer();
1438 var leadingBreak = '';
1439 var trailingBreaks = '';
1440 var whitespace = new StringBuffer();
1441 var indent = _indent + 1;
1442
1443 while (true) {
1444 // Check for a document indicator.
1445 if (_isDocumentIndicator) break;
1446
1447 // Check for a comment.
1448 if (_scanner.peekChar() == HASH) break;
1449
1450 if (_isPlainChar) {
1451 // Join the whitespace or fold line breaks.
1452 if (leadingBreak.isNotEmpty) {
1453 if (trailingBreaks.isEmpty) {
1454 buffer.writeCharCode(SP);
1455 } else {
1456 buffer.write(trailingBreaks);
1457 }
1458 leadingBreak = '';
1459 trailingBreaks = '';
1460 } else {
1461 buffer.write(whitespace);
1462 whitespace.clear();
1463 }
1464 }
1465
1466 // libyaml's notion of valid identifiers differs substantially from YAML
1467 // 1.2's. We use [_isPlainChar] instead of libyaml's character here.
1468 var startPosition = _scanner.position;
1469 while (_isPlainChar) {
1470 _scanner.readChar();
1471 }
1472 buffer.write(_scanner.substring(startPosition));
1473 end = _scanner.state;
1474
1475 // Is it the end?
1476 if (!_isBlank && !_isBreak) break;
1477
1478 while (_isBlank || _isBreak) {
1479 if (_isBlank) {
1480 // Check for a tab character messing up the intendation.
1481 if (leadingBreak.isNotEmpty && _scanner.column < indent &&
1482 _scanner.peekChar() == TAB) {
1483 _scanner.error("Expected a space but found a tab.", length: 1);
1484 }
1485
1486 if (leadingBreak.isEmpty) {
1487 whitespace.writeCharCode(_scanner.readChar());
1488 } else {
1489 _scanner.readChar();
1490 }
1491 } else {
1492 // Check if it's a first line break.
1493 if (leadingBreak.isEmpty) {
1494 leadingBreak = _readLine();
1495 whitespace.clear();
1496 } else {
1497 trailingBreaks = _readLine();
1498 }
1499 }
1500 }
1501
1502 // Check the indentation level.
1503 if (_inBlockContext && _scanner.column < indent) break;
1504 }
1505
1506 // Allow a simple key after a plain scalar with leading blanks.
1507 if (leadingBreak.isNotEmpty) _simpleKeyAllowed = true;
1508
1509 return new ScalarToken(_scanner.spanFrom(start, end), buffer.toString(),
1510 ScalarStyle.PLAIN);
1511 }
1512
1513 /// Moves past the current line break, if there is one.
1514 void _skipLine() {
1515 var char = _scanner.peekChar();
1516 if (char != CR && char != LF) return;
1517 _scanner.readChar();
1518 if (char == CR && _scanner.peekChar() == LF) _scanner.readChar();
1519 }
1520
1521 // Moves past the current line break and returns a newline.
1522 String _readLine() {
1523 var char = _scanner.peekChar();
1524
1525 // libyaml supports NEL, PS, and LS characters as line separators, but this
1526 // is explicitly forbidden in section 5.4 of the YAML spec.
1527 if (char != CR && char != LF) {
1528 throw new YamlException("Expected newline.", _scanner.emptySpan);
1529 }
1530
1531 _scanner.readChar();
1532 // CR LF | CR | LF -> LF
1533 if (char == CR && _scanner.peekChar() == LF) _scanner.readChar();
1534 return "\n";
1535 }
1536
1537 // Returns whether the character at [offset] is whitespace.
1538 bool _isBlankAt(int offset) {
1539 var char = _scanner.peekChar(offset);
1540 return char == SP || char == TAB;
1541 }
1542
1543 // Returns whether the character at [offset] is a line break.
1544 bool _isBreakAt(int offset) {
1545 // Libyaml considers NEL, LS, and PS to be line breaks as well, but that's
1546 // contrary to the spec.
1547 var char = _scanner.peekChar(offset);
1548 return char == CR || char == LF;
1549 }
1550
1551 // Returns whether the character at [offset] is whitespace or past the end of
1552 // the source.
1553 bool _isBlankOrEndAt(int offset) {
1554 var char = _scanner.peekChar(offset);
1555 return char == null || char == SP || char == TAB || char == CR ||
1556 char == LF;
1557 }
1558
1559 /// Returns whether the character at [offset] is a plain character.
1560 ///
1561 /// See http://yaml.org/spec/1.2/spec.html#ns-plain-char(c).
1562 bool _isPlainCharAt(int offset) {
1563 switch (_scanner.peekChar(offset)) {
1564 case COLON:
1565 return _isPlainSafeAt(offset + 1);
1566 case HASH:
1567 var previous = _scanner.peekChar(offset - 1);
1568 return previous != SP && previous != TAB;
1569 default:
1570 return _isPlainSafeAt(offset);
1571 }
1572 }
1573
1574 /// Returns whether the character at [offset] is a plain-safe character.
1575 ///
1576 /// See http://yaml.org/spec/1.2/spec.html#ns-plain-safe(c).
1577 bool _isPlainSafeAt(int offset) {
1578 var char = _scanner.peekChar(offset);
1579 switch (char) {
1580 case COMMA:
1581 case LEFT_SQUARE:
1582 case RIGHT_SQUARE:
1583 case LEFT_CURLY:
1584 case RIGHT_CURLY:
1585 // These characters are delimiters in a flow context and thus are only
1586 // safe in a block context.
1587 return _inBlockContext;
1588 case SP:
1589 case TAB:
1590 case LF:
1591 case CR:
1592 case BOM:
1593 return false;
1594 case NEL:
1595 return true;
1596 default:
1597 return char != null &&
1598 ((char >= 0x00020 && char <= 0x00007E) ||
1599 (char >= 0x000A0 && char <= 0x00D7FF) ||
1600 (char >= 0x0E000 && char <= 0x00FFFD) ||
1601 (char >= 0x10000 && char <= 0x10FFFF));
1602 }
1603 }
1604
1605 /// Returns the hexidecimal value of [char].
1606 int _asHex(int char) {
1607 if (char <= NUMBER_9) return char - NUMBER_0;
1608 if (char <= LETTER_CAP_F) return 10 + char - LETTER_CAP_A;
1609 return 10 + char - LETTER_A;
1610 }
1611
1612 /// Moves the scanner past any blank characters.
1613 void _skipBlanks() {
1614 while (_isBlank) {
1615 _scanner.readChar();
1616 }
1617 }
1618
1619 /// Moves the scanner past a comment, if one starts at the current position.
1620 void _skipComment() {
1621 if (_scanner.peekChar() != HASH) return;
1622 while (!_isBreakOrEnd) {
1623 _scanner.readChar();
1624 }
1625 }
1626 }
1627
1628 /// A record of the location of a potential simple key.
1629 class _SimpleKey {
1630 /// The index of the token that begins the simple key.
1631 ///
1632 /// This is the index relative to all tokens emitted, rather than relative to
1633 /// [_tokens].
1634 final int tokenNumber;
1635
1636 /// The source location of the beginning of the simple key.
1637 ///
1638 /// This is used for error reporting and for determining when a simple key is
1639 /// no longer on the current line.
1640 final SourceLocation location;
1641
1642 /// Whether this key must exist for the document to be scanned.
1643 final bool required;
1644
1645 _SimpleKey(this.tokenNumber, this.location, {bool required})
1646 : required = required;
1647 }
1648
1649 /// An enum of chomping indicators that describe how to handle trailing
1650 /// whitespace for a block scalar.
1651 ///
1652 /// See http://yaml.org/spec/1.2/spec.html#id2794534.
1653 class _Chomping {
1654 /// All trailing whitespace is discarded.
1655 static const STRIP = const _Chomping("STRIP");
1656
1657 /// A single trailing newline is retained.
1658 static const CLIP = const _Chomping("CLIP");
1659
1660 /// All trailing whitespace is preserved.
1661 static const KEEP = const _Chomping("KEEP");
1662
1663 final String name;
1664
1665 const _Chomping(this.name);
1666
1667 String toString() => name;
1668 }
OLDNEW
« no previous file with comments | « mojo/public/dart/third_party/yaml/lib/src/parser.dart ('k') | mojo/public/dart/third_party/yaml/lib/src/style.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698