Skip to content

Commit c87b93c

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Add withTokenPreviousNext to AST printer.
Bug: #56355 Change-Id: I0cd58070853cfe0fa59788b3ed4586fc156e27d3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388054 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent a409a1c commit c87b93c

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed

pkg/analyzer/test/src/dart/parser/top_level_function_test.dart

+71
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,77 @@ FunctionDeclaration
6161
''');
6262
}
6363

64+
test_recovery_body_issue56355() {
65+
// https://github.com/dart-lang/sdk/issues/56355
66+
var parseResult = parseStringWithErrors(r'''
67+
void get() {
68+
http.Response response = http
69+
}
70+
''');
71+
72+
// Note, there is a cycle that should not be there.
73+
var node = parseResult.findNode.singleFunctionDeclaration;
74+
assertParsedNodeText(
75+
node,
76+
withTokenPreviousNext: true,
77+
withOffsets: true,
78+
r'''
79+
FunctionDeclaration
80+
returnType: NamedType
81+
name: T0 void @0
82+
next: T1 |get|
83+
name: T1 get @5
84+
previous: T0 |void|
85+
next: T2 |(|
86+
functionExpression: FunctionExpression
87+
parameters: FormalParameterList
88+
leftParenthesis: T2 ( @8
89+
previous: T1 |get|
90+
next: T3 |)|
91+
rightParenthesis: T3 ) @9
92+
previous: T2 |(|
93+
next: T4 |{|
94+
body: BlockFunctionBody
95+
block: Block
96+
leftBracket: T4 { @11
97+
previous: T3 |)|
98+
next: T5 |http|
99+
statements
100+
ExpressionStatement
101+
expression: PrefixedIdentifier
102+
prefix: SimpleIdentifier
103+
token: T5 http @15
104+
previous: T4 |{|
105+
next: T6 |.|
106+
period: T6 . @19
107+
previous: T5 |http|
108+
next: T7 |Response|
109+
identifier: SimpleIdentifier
110+
token: T7 Response @20
111+
previous: T6 |.|
112+
next: T8 |response|
113+
semicolon: T9 ; @45 <synthetic>
114+
previousX: T10 http @40
115+
previousX: T11 = @38
116+
previous: T8 |response|
117+
next: T10 |http|
118+
next: T9 |;|
119+
next: T8 |response|
120+
ExpressionStatement
121+
expression: SimpleIdentifier
122+
token: T8 response @29
123+
previous: T9 |;|
124+
next: T12 |;|
125+
semicolon: T12 ; @45 <synthetic>
126+
previous: T8 |response|
127+
next: T13 |}|
128+
rightBracket: T13 } @45
129+
previous: T12 |;|
130+
next: T14 ||
131+
''',
132+
);
133+
}
134+
64135
test_setter_augment() {
65136
var parseResult = parseStringWithErrors(r'''
66137
augment set foo(int _) {}

pkg/analyzer/test/src/diagnostics/parser_diagnostics.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ class ParserDiagnosticsTest {
2323
String expected, {
2424
bool withCheckingLinking = false,
2525
bool withOffsets = false,
26+
bool withTokenPreviousNext = false,
2627
}) {
2728
var actual = _parsedNodeText(
2829
node,
2930
withCheckingLinking: withCheckingLinking,
3031
withOffsets: withOffsets,
32+
withTokenPreviousNext: withTokenPreviousNext,
3133
);
3234
if (actual != expected) {
3335
print(actual);
@@ -68,6 +70,7 @@ class ParserDiagnosticsTest {
6870
AstNode node, {
6971
required bool withCheckingLinking,
7072
required bool withOffsets,
73+
required bool withTokenPreviousNext,
7174
}) {
7275
var buffer = StringBuffer();
7376
var sink = TreeStringSink(
@@ -83,7 +86,8 @@ class ParserDiagnosticsTest {
8386
sink: sink,
8487
elementPrinter: elementPrinter,
8588
configuration: ResolvedNodeTextConfiguration()
86-
..withCheckingLinking = withCheckingLinking,
89+
..withCheckingLinking = withCheckingLinking
90+
..withTokenPreviousNext = withTokenPreviousNext,
8791
withResolution: false,
8892
withOffsets: withOffsets,
8993
),

pkg/analyzer/test/src/summary/resolved_ast_printer.dart

+48-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class ResolvedAstPrinter extends ThrowingAstVisitor<void> {
2929
/// If `true`, resolution should be printed.
3030
final bool _withResolution;
3131

32+
final Map<Token, String> _tokenIdMap = Map.identity();
33+
3234
ResolvedAstPrinter({
3335
required TreeStringSink sink,
3436
required ElementPrinter elementPrinter,
@@ -1758,6 +1760,13 @@ Expected parent: (${parent.runtimeType}) $parent
17581760
}
17591761
}
17601762

1763+
String _getTokenId(Token? token) {
1764+
if (token == null) {
1765+
return '<null>';
1766+
}
1767+
return _tokenIdMap[token] ??= 'T${_tokenIdMap.length}';
1768+
}
1769+
17611770
void _writeDeclaredElement(Element? element) {
17621771
if (_withResolution) {
17631772
if (element is LocalVariableElement) {
@@ -1973,16 +1982,49 @@ Expected parent: (${parent.runtimeType}) $parent
19731982
}
19741983

19751984
void _writeToken(String name, Token? token) {
1976-
if (token != null) {
1977-
_sink.writeWithIndent('$name: ');
1985+
if (token == null) {
1986+
return;
1987+
}
1988+
1989+
_sink.writeIndentedLine(() {
1990+
_sink.write('$name: ');
1991+
if (configuration.withTokenPreviousNext) {
1992+
_sink.write(_getTokenId(token));
1993+
_sink.write(' ');
1994+
}
19781995
_sink.write(token.lexeme.ifNotEmptyOrElse('<empty>'));
19791996
if (_withOffsets) {
19801997
_sink.write(' @${token.offset}');
19811998
}
19821999
if (token.isSynthetic) {
19832000
_sink.write(' <synthetic>');
19842001
}
1985-
_sink.writeln();
2002+
});
2003+
2004+
if (configuration.withTokenPreviousNext) {
2005+
_sink.withIndent(() {
2006+
if (token.previous case var previous?) {
2007+
if (!previous.isEof) {
2008+
if (_tokenIdMap[previous] == null) {
2009+
_writeToken('previousX', previous);
2010+
} else {
2011+
_sink.writelnWithIndent(
2012+
'previous: ${_getTokenId(previous)} |${previous.lexeme}|',
2013+
);
2014+
}
2015+
}
2016+
} else {
2017+
_sink.writelnWithIndent('previous: <null>');
2018+
}
2019+
2020+
if (token.next case var next?) {
2021+
_sink.writelnWithIndent(
2022+
'next: ${_getTokenId(next)} |${next.lexeme}|',
2023+
);
2024+
} else {
2025+
_sink.writelnWithIndent('next: <null>');
2026+
}
2027+
});
19862028
}
19872029
}
19882030

@@ -2089,4 +2131,7 @@ class ResolvedNodeTextConfiguration {
20892131
/// If `true`, `redirectedConstructor` properties of [ConstructorElement]s
20902132
/// should be printer.
20912133
bool withRedirectedConstructors = false;
2134+
2135+
/// If `true`, print IDs of each token, `previous` and `next` tokens.
2136+
bool withTokenPreviousNext = false;
20922137
}

0 commit comments

Comments
 (0)