Skip to content

Commit 5311533

Browse files
authored
Merge pull request #18 from philberty/nala/enhance/ast-dump
Update AST dump
2 parents 3ee1720 + 1b6ecb5 commit 5311533

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

gcc/rust/ast/rust-ast-full-test.cc

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -200,33 +200,26 @@ DelimTokenTree::as_string () const
200200
end_delim = "}";
201201
break;
202202
default:
203-
// error
204-
return "";
203+
fprintf (stderr, "Invalid delimiter type, "
204+
"Should be PARENS, SQUARE, or CURLY.");
205+
return "Invalid delimiter type";
205206
}
206207
::std::string str = start_delim;
207-
if (token_trees.empty ())
208+
if (!token_trees.empty ())
208209
{
209-
str += "none";
210-
}
211-
else
212-
{
213-
auto i = token_trees.begin ();
214-
auto e = token_trees.end ();
215-
216-
// DEBUG: null pointer check
217-
if (*i == NULL)
210+
for (const auto &tree : token_trees)
218211
{
219-
fprintf (stderr,
220-
"something really terrible has gone wrong - null pointer "
221-
"token tree in delim token tree.");
222-
return "NULL_POINTER_MARK";
223-
}
212+
// DEBUG: null pointer check
213+
if (tree == NULL)
214+
{
215+
fprintf (
216+
stderr,
217+
"something really terrible has gone wrong - null pointer "
218+
"token tree in delim token tree.");
219+
return "NULL_POINTER_MARK";
220+
}
224221

225-
for (; i != e; i++)
226-
{
227-
str += (*i)->as_string ();
228-
if (e != i + 1)
229-
str += ", ";
222+
str += tree->as_string ();
230223
}
231224
}
232225
str += end_delim;
@@ -242,7 +235,8 @@ Token::as_string () const
242235
// return get_token_description(token_id);
243236

244237
// maybe fixed - stores everything as string though, so storage-inefficient
245-
return str;
238+
::std::string quote = is_string_lit () ? "\"" : "";
239+
return quote + str + quote;
246240
}
247241

248242
::std::string
@@ -411,7 +405,7 @@ StaticItem::as_string () const
411405
{
412406
::std::string str = VisItem::as_string ();
413407

414-
str += "static";
408+
str += indent_spaces (stay) + "static";
415409

416410
if (has_mut)
417411
{
@@ -427,7 +421,7 @@ StaticItem::as_string () const
427421
"pointer type in static item.");
428422
return "NULL_POINTER_MARK";
429423
}
430-
str += "\n Type: " + type->as_string ();
424+
str += "\n" + indent_spaces (stay) + "Type: " + type->as_string ();
431425

432426
// DEBUG: null pointer check
433427
if (expr == NULL)
@@ -436,7 +430,7 @@ StaticItem::as_string () const
436430
"pointer expr in static item.");
437431
return "NULL_POINTER_MARK";
438432
}
439-
str += "\n Expression: " + expr->as_string ();
433+
str += "\n" + indent_spaces (stay) + "Expression: " + expr->as_string ();
440434

441435
return str + "\n";
442436
}
@@ -1528,7 +1522,8 @@ MacroRulesDefinition::as_string () const
15281522
::std::string
15291523
MacroInvocation::as_string () const
15301524
{
1531-
return path.as_string () + "!" + token_tree.as_string ();
1525+
return "MacroInvocation: " + path.as_string () + "!"
1526+
+ token_tree.as_string ();
15321527
}
15331528

15341529
::std::string
@@ -3326,13 +3321,15 @@ LetStmt::as_string () const
33263321
{
33273322
// note that this does not print them with "outer attribute" syntax -
33283323
// just the body
3324+
indent_spaces (enter);
33293325
for (const auto &attr : outer_attrs)
33303326
{
3331-
str += "\n " + attr.as_string ();
3327+
str += "\n" + indent_spaces (stay) + attr.as_string ();
33323328
}
3329+
indent_spaces (out);
33333330
}
33343331

3335-
str += "\nlet " + variables_pattern->as_string ();
3332+
str += "\n" + indent_spaces (stay) + "let " + variables_pattern->as_string ();
33363333

33373334
if (has_type ())
33383335
{

gcc/rust/ast/rust-ast.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,18 @@ class Token : public TokenTree, public MacroMatch
213213
}
214214
}
215215

216+
inline bool is_string_lit () const
217+
{
218+
switch (token_id)
219+
{
220+
case STRING_LITERAL:
221+
case BYTE_STRING_LITERAL:
222+
return true;
223+
default:
224+
return false;
225+
}
226+
}
227+
216228
::std::string as_string () const;
217229

218230
virtual void accept_vis (ASTVisitor &vis) OVERRIDE;

0 commit comments

Comments
 (0)