|
| 1 | +import 'package:text/text.dart'; |
| 2 | + |
| 3 | +class ParserErrorMessage { |
| 4 | + ParserErrorMessage(this.message, this.start, this.end) { |
| 5 | + if (end < 0) { |
| 6 | + throw ArgumentError.value(end, 'end'); |
| 7 | + } |
| 8 | + |
| 9 | + if (start < 0 || start > end) { |
| 10 | + throw ArgumentError.value(start, 'start'); |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + /// End position of error. |
| 15 | + final int end; |
| 16 | + |
| 17 | + /// Error message. |
| 18 | + final String message; |
| 19 | + |
| 20 | + /// Start position of error. |
| 21 | + final int start; |
| 22 | +} |
| 23 | + |
| 24 | +class ParserErrorFormatter { |
| 25 | + /// Returns formatted error as strings. |
| 26 | + /// |
| 27 | + /// Parameters: |
| 28 | + /// [String] source |
| 29 | + /// Text of source code. |
| 30 | + /// |
| 31 | + /// [List]<[ParserErrorMessage]> error |
| 32 | + /// List of parser error messages. |
| 33 | + /// |
| 34 | + /// [int] lineLimit |
| 35 | + /// Length limit of the formatted line. |
| 36 | + /// |
| 37 | + /// [int] offset |
| 38 | + /// Offset to be added to the values "start" and "end". |
| 39 | + /// |
| 40 | + /// [String] title |
| 41 | + /// Title of parser error |
| 42 | + static List<String> format(String source, List<ParserErrorMessage> messages, |
| 43 | + {int lineLimit = 80, int offset = 0, String title = 'Format exception'}) { |
| 44 | + if (lineLimit < 1) { |
| 45 | + throw ArgumentError.value(lineLimit, 'lineLimit'); |
| 46 | + } |
| 47 | + |
| 48 | + if (offset < 0) { |
| 49 | + throw ArgumentError.value(offset, 'offset'); |
| 50 | + } |
| 51 | + |
| 52 | + final List<String> result = <String>[]; |
| 53 | + final Text text = Text(source); |
| 54 | + final int sourceLength = source.length; |
| 55 | + for (ParserErrorMessage error in messages) { |
| 56 | + int position = error.end + offset; |
| 57 | + if (error.start != error.end) { |
| 58 | + position = error.start + offset; |
| 59 | + } |
| 60 | + |
| 61 | + Location? location; |
| 62 | + Line? line; |
| 63 | + String locationString = ''; |
| 64 | + if (position < sourceLength) { |
| 65 | + line = text.lineAt(position); |
| 66 | + location = text.locationAt(position); |
| 67 | + locationString = ' (${location.toString()})'; |
| 68 | + } |
| 69 | + |
| 70 | + result.add('$title$locationString: ${error.message}'); |
| 71 | + if (line != null) { |
| 72 | + String string = String.fromCharCodes(line.characters); |
| 73 | + string = string.replaceAll('\n', ''); |
| 74 | + string = string.replaceAll('\r', ''); |
| 75 | + int indicatorLength = 1; |
| 76 | + int indicatorPosition = 0; |
| 77 | + if (location != null) { |
| 78 | + indicatorPosition = location.column - 1; |
| 79 | + } |
| 80 | + if (error.end != error.start) { |
| 81 | + indicatorLength = error.end - error.start; |
| 82 | + } |
| 83 | + |
| 84 | + if (indicatorLength > lineLimit) { |
| 85 | + indicatorLength = lineLimit; |
| 86 | + } |
| 87 | + |
| 88 | + if (indicatorPosition + indicatorLength > lineLimit) { |
| 89 | + if (indicatorPosition < lineLimit || indicatorLength < lineLimit) { |
| 90 | + final int delta = (indicatorPosition + indicatorLength) - lineLimit; |
| 91 | + string = string.substring(delta); |
| 92 | + indicatorPosition -= delta; |
| 93 | + } else { |
| 94 | + string = string.substring(indicatorPosition); |
| 95 | + indicatorPosition = 0; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (string.length > lineLimit) { |
| 100 | + string = string.substring(0, lineLimit); |
| 101 | + } |
| 102 | + |
| 103 | + final String prefix = ''.padRight(indicatorPosition, ' '); |
| 104 | + final String suffix = ''.padRight(indicatorLength, '^'); |
| 105 | + final String indicator = '$prefix$suffix'; |
| 106 | + result.add(string); |
| 107 | + result.add(indicator); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | + } |
| 113 | +} |
0 commit comments