| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 analyzer.src.task.yaml; |
| 6 |
| 7 import 'package:analyzer/error/error.dart'; |
| 8 import 'package:analyzer/exception/exception.dart'; |
| 9 import 'package:analyzer/src/context/cache.dart'; |
| 10 import 'package:analyzer/src/dart/scanner/scanner.dart'; |
| 11 import 'package:analyzer/src/generated/engine.dart'; |
| 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/task/general.dart'; |
| 14 import 'package:analyzer/task/general.dart'; |
| 15 import 'package:analyzer/task/model.dart'; |
| 16 import 'package:analyzer/task/yaml.dart'; |
| 17 import 'package:source_span/source_span.dart'; |
| 18 import 'package:yaml/yaml.dart'; |
| 19 |
| 20 /** |
| 21 * A task that scans the content of a YAML file, producing a YAML document. |
| 22 */ |
| 23 class ParseYamlTask extends SourceBasedAnalysisTask { |
| 24 /** |
| 25 * The name of the input whose value is the content of the file. |
| 26 */ |
| 27 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; |
| 28 |
| 29 /** |
| 30 * The task descriptor describing this kind of task. |
| 31 */ |
| 32 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 33 'ParseYamlTask', |
| 34 createTask, |
| 35 buildInputs, |
| 36 <ResultDescriptor>[YAML_DOCUMENT, YAML_ERRORS, LINE_INFO], |
| 37 suitabilityFor: suitabilityFor); |
| 38 |
| 39 /** |
| 40 * Initialize a newly created task to access the content of the source |
| 41 * associated with the given [target] in the given [context]. |
| 42 */ |
| 43 ParseYamlTask(InternalAnalysisContext context, AnalysisTarget target) |
| 44 : super(context, target); |
| 45 |
| 46 @override |
| 47 TaskDescriptor get descriptor => DESCRIPTOR; |
| 48 |
| 49 @override |
| 50 void internalPerform() { |
| 51 Source source = target.source; |
| 52 String uri = source.uri.toString(); |
| 53 String content = getRequiredInput(CONTENT_INPUT_NAME); |
| 54 |
| 55 if (context.getModificationStamp(source) < 0) { |
| 56 String message = 'Content could not be read'; |
| 57 if (context is InternalAnalysisContext) { |
| 58 CacheEntry entry = |
| 59 (context as InternalAnalysisContext).getCacheEntry(target); |
| 60 CaughtException exception = entry.exception; |
| 61 if (exception != null) { |
| 62 message = exception.toString(); |
| 63 } |
| 64 } |
| 65 // |
| 66 // Record outputs. |
| 67 // |
| 68 outputs[YAML_DOCUMENT] = loadYamlDocument('', sourceUrl: uri); |
| 69 outputs[YAML_ERRORS] = <AnalysisError>[ |
| 70 new AnalysisError( |
| 71 source, 0, 0, ScannerErrorCode.UNABLE_GET_CONTENT, [message]) |
| 72 ]; |
| 73 outputs[LINE_INFO] = new LineInfo(<int>[0]); |
| 74 } else { |
| 75 YamlDocument document; |
| 76 List<AnalysisError> errors = <AnalysisError>[]; |
| 77 try { |
| 78 document = loadYamlDocument(content, sourceUrl: uri); |
| 79 } on YamlException catch (exception) { |
| 80 SourceSpan span = exception.span; |
| 81 int offset = span.start.offset; |
| 82 int length = span.end.offset - offset; |
| 83 errors.add(new AnalysisError(source, offset, length, |
| 84 YamlErrorCode.PARSE_ERROR, [exception.message])); |
| 85 } catch (exception, stackTrace) { |
| 86 throw new AnalysisException('Error while parsing ${source.fullName}', |
| 87 new CaughtException(exception, stackTrace)); |
| 88 } |
| 89 // |
| 90 // Record outputs. |
| 91 // |
| 92 outputs[YAML_DOCUMENT] = document; |
| 93 outputs[YAML_ERRORS] = errors; |
| 94 outputs[LINE_INFO] = new LineInfo.fromContent(content); |
| 95 } |
| 96 } |
| 97 |
| 98 /** |
| 99 * Return a map from the names of the inputs of this kind of task to the task |
| 100 * input descriptors describing those inputs for a task with the given |
| 101 * [source]. |
| 102 */ |
| 103 static Map<String, TaskInput> buildInputs(AnalysisTarget source) { |
| 104 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; |
| 105 } |
| 106 |
| 107 /** |
| 108 * Create a [ParseYamlTask] based on the given [target] in the given [context]
. |
| 109 */ |
| 110 static ParseYamlTask createTask( |
| 111 AnalysisContext context, AnalysisTarget target) { |
| 112 return new ParseYamlTask(context, target); |
| 113 } |
| 114 |
| 115 /** |
| 116 * Return an indication of how suitable this task is for the given [target]. |
| 117 */ |
| 118 static TaskSuitability suitabilityFor(AnalysisTarget target) { |
| 119 if (target is Source && target.shortName.endsWith('.yaml')) { |
| 120 return TaskSuitability.HIGHEST; |
| 121 } |
| 122 return TaskSuitability.NONE; |
| 123 } |
| 124 } |
| 125 |
| 126 /** |
| 127 * The error codes used for errors in YAML files. |
| 128 */ |
| 129 class YamlErrorCode extends ErrorCode { |
| 130 // TODO(brianwilkerson) Move this class to error.dart. |
| 131 |
| 132 /** |
| 133 * An error code indicating that there is a syntactic error in the file. |
| 134 * |
| 135 * Parameters: |
| 136 * 0: the error message from the parse error |
| 137 */ |
| 138 static const YamlErrorCode PARSE_ERROR = |
| 139 const YamlErrorCode('PARSE_ERROR', '{0}'); |
| 140 |
| 141 /** |
| 142 * Initialize a newly created error code to have the given [name]. The message |
| 143 * associated with the error will be created from the given [message] |
| 144 * template. The correction associated with the error will be created from the |
| 145 * given [correction] template. |
| 146 */ |
| 147 const YamlErrorCode(String name, String message, [String correction]) |
| 148 : super(name, message, correction); |
| 149 |
| 150 @override |
| 151 ErrorSeverity get errorSeverity => ErrorSeverity.ERROR; |
| 152 |
| 153 @override |
| 154 ErrorType get type => ErrorType.COMPILE_TIME_ERROR; |
| 155 } |
| OLD | NEW |