0

I try to iterate over all comments in a C++ source file, but I fail to access M3.comments.

I tried iprintln, for-, switch- and visit-statements.

iprintln(m3) gives the following output:

m3(

  |file://bla.c|,

  macroExpansions={},

  methodOverrides={},

  includeDirectives={
    ...
  },

  inactiveIncludes={},

  comments=[

    |file://bla.c|(0,80),

    |file://bla.c|(82,34),

    ...
  ],

  macroDefinitions={},

  includeResolution={
    ...
  })
'''

The following code matches

visit (m3) { case comments: println("match"); }

but I am unable to get the locations.

E.g.

visit (m3) { case c:comments: println(c); }

gives back "Ambiguous code (internal error), c:comments:".

Next

visit (m3) { case comments(c): println("c"); }

does not match

And

iprintln(m3.comments);

gives back "Undeclared field: comments for M3".

How can I access the comments?

S.I.
  • 3,250
  • 12
  • 48
  • 77
Matty
  • 134
  • 1
  • 7

1 Answers1

1

You can project out the comments by this:

theComments = m3Model.comments;

That "." expression selects the comments field of the m3 data constructor.

If you want to match a keyword field like comments instead, you'd write a pattern like so:

m3(comments=theComments) := myM3Model

If the field is undeclared, you could declare it like so:

data M3(list[loc] comments=[]);

However, that should have been in the Claire library declarations already. Please report an issue on GitHub?

Jurgen Vinju
  • 6,393
  • 1
  • 15
  • 26
  • 1
    Thanks Jurgen for the fast response. Adding "data M3(list[loc] comments=[]);" in my own code solved the problem. I will submit an issue in GitHub. – Matty Jul 30 '19 at 09:28
  • 1
    The new release of ClaiR on the update site fixes the issue. – Matty Aug 07 '19 at 08:54