Skip to content

Privatize CommentReferenceParser.codeRef #3771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions lib/src/comment_references/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ const Map<String, String> operatorNames = {
class StringTrie {
final Map<int, StringTrie> children = {};

/// Does [this] node represent a valid entry in the trie?
/// Does `this` node represent a valid entry in the trie?
bool valid = false;

/// Greedily match on the string trie starting at the given index. Returns
/// the index of the first non-operator character if a match is valid,
/// Greedily matches on the string trie starting at the given index.
///
/// Returns the index of the first non-operator character if a match is valid,
/// otherwise -1.
int match(String toMatch, [int index = 0, int lastValid = -1]) {
if (index < 0 || index > toMatch.length) {
Expand Down Expand Up @@ -76,11 +77,11 @@ final StringTrie operatorParseTrie = () {
// TODO(jcollins-g): align with [CommentReference] from analyzer AST.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully, we won't need this parser in the future. All of this work is so complicated and the analyzer already does it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes yes yes :D

class CommentReferenceParser {
/// Original, unparsed reference.
final String codeRef;
final String _codeRef;

final int _referenceLength;

CommentReferenceParser(this.codeRef) : _referenceLength = codeRef.length;
CommentReferenceParser(this._codeRef) : _referenceLength = _codeRef.length;

int _index = 0;

Expand Down Expand Up @@ -194,28 +195,29 @@ class CommentReferenceParser {
};

/// Advances the index forward to the end of the operator if one is
/// present and returns the operator's name. Otherwise, leaves _index
/// unchanged and returns null.
/// present and returns the operator's name.
///
/// Otherwise, leaves `_index` unchanged and returns `null`.
String? _tryParseOperator() {
var tryIndex = _index;
if (tryIndex + _operatorKeyword.length < codeRef.length &&
codeRef.substring(tryIndex, tryIndex + _operatorKeyword.length) ==
if (tryIndex + _operatorKeyword.length < _codeRef.length &&
_codeRef.substring(tryIndex, tryIndex + _operatorKeyword.length) ==
_operatorKeyword) {
tryIndex = tryIndex + _operatorKeyword.length;
while (_whitespace.contains(codeRef.codeUnitAt(tryIndex))) {
while (_whitespace.contains(_codeRef.codeUnitAt(tryIndex))) {
tryIndex++;
}
}

var result = operatorParseTrie.match(codeRef, tryIndex);
var result = operatorParseTrie.match(_codeRef, tryIndex);
if (result == -1) {
return null;
}
_index = result;
return codeRef.substring(tryIndex, result);
return _codeRef.substring(tryIndex, result);
}

/// Parse a dartdoc identifier.
/// Parses a dartdoc identifier.
///
/// Dartdoc identifiers can include some operators.
_IdentifierParseResult _parseIdentifier() {
Expand All @@ -240,10 +242,10 @@ class CommentReferenceParser {
_index++;
}
return _IdentifierParseResult.ok(
IdentifierNode(codeRef.substring(startIndex, _index)));
IdentifierNode(_codeRef.substring(startIndex, _index)));
}

/// Parse a list of type variables (arguments or parameters).
/// Parses a list of type variables (arguments or parameters).
///
/// Dartdoc isolates these where present and potentially valid, but we don't
/// break them down.
Expand All @@ -254,7 +256,7 @@ class CommentReferenceParser {
var startIndex = _index;
if (_matchBraces($lt, $gt)) {
return _TypeVariablesParseResult.ok(
TypeVariablesNode(codeRef.substring(startIndex + 1, _index - 1)));
TypeVariablesNode(_codeRef.substring(startIndex + 1, _index - 1)));
}
return _TypeVariablesParseResult.notIdentifier;
}
Expand Down Expand Up @@ -282,7 +284,7 @@ class CommentReferenceParser {
if (_tryMatchLiteral(_callableHintSuffix)) {
if (_atEnd) {
return _SuffixParseResult.ok(
CallableHintEndNode(codeRef.substring(startIndex, _index)));
CallableHintEndNode(_codeRef.substring(startIndex, _index)));
}
return _SuffixParseResult.notSuffix;
}
Expand All @@ -299,7 +301,7 @@ class CommentReferenceParser {

bool get _atEnd => _index >= _referenceLength;
bool get _nextAtEnd => _index + 1 >= _referenceLength;
int get _thisChar => codeRef.codeUnitAt(_index);
int get _thisChar => _codeRef.codeUnitAt(_index);

/// Advances [_index] on match, preserves on non-match.
bool _tryMatchLiteral(String characters,
Expand All @@ -310,7 +312,7 @@ class CommentReferenceParser {
for (startIndex = _index;
_index - startIndex < characters.length;
_index++) {
if (codeRef.codeUnitAt(_index) !=
if (_codeRef.codeUnitAt(_index) !=
characters.codeUnitAt(_index - startIndex)) {
_index = startIndex;
return false;
Expand Down Expand Up @@ -343,8 +345,9 @@ class CommentReferenceParser {
return;
}

/// Returns `true` if we started with [startChar] and ended with [endChar]
/// with a matching number of braces.
/// Returns whether we started with [startChar] and ended with [endChar] with
/// a matching number of braces.
///
/// Restores [_index] to state when called if returning `false`.
bool _matchBraces(int startChar, int endChar) {
var braceCount = 0;
Expand Down Expand Up @@ -414,10 +417,14 @@ class _IdentifierParseResult {
}

enum _TypeVariablesResultType {
endOfFile, // Found end of file instead of the beginning of a list of type
// variables.
notTypeVariables, // Found something, but it isn't type variables.
parsedTypeVariables, // Found type variables.
/// Found end of file instead of the beginning of a list of type variables.
endOfFile,

/// Found something, but it isn't type variables.
notTypeVariables,

/// Found type variables.
parsedTypeVariables,
}

class _TypeVariablesParseResult {
Expand Down
Loading