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