Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find programmatically all classes, grammars, and roles in a Raku package

How can I find programmatically all the classes, grammars, and roles in a Raku package? (Specified with a string.)

I examined discussions/posts similar to the ones linked below, but the code I came up with is very hard to use. (And does not do the job.)

  • Meta-programming: what, why and how (perl6advent)

  • Day 19 – Introspection (perl6advent)

  • How do I access a module's symbol table dynamically at runtime in Raku? (stackoverflow)

  • Is there a way to get a list of all known types in a Perl 6 program? (stackoverflow)

Motivation

I would like to automatically generate UML class diagrams for Raku packages.

See the PlantUML diagrams for the Raku package: ML::StreamsBlendingRecommender.

I considered the steps:

  • Design parser(s) of the code of classes in software packages made with languages like C++, Java, Kotlin, Raku

  • Generate the corresponding PlantUML code over the parsing results

(Such parsers might not be that hard to derive. Probably, the work of DrForr provides good starts.)

But given Raku's introspection abilities, instead of parsing Raku code I should be able to "just" traverse package namespaces and classes. (Instead of making a parser.)

like image 347
Anton Antonov Avatar asked Aug 02 '21 13:08

Anton Antonov


People also ask

What are some examples of Raku code?

For example, Raku is parsed and executed using a Raku-style grammar. An example that's more practical to the common Raku user is the JSON::Tiny module, which can deserialize any valid JSON file; however, the deserializing code is written in less than 100 lines of simple, extensible code.

How to find classes in a package in Java?

Hence, finding classes in a package is essentially a file system operation rather than one done by using Java Reflection. However, we can write our own class loaders or examine the classpath to find classes inside a package. 3. Finding Classes in a Java Package

Why can't I parse a string in raku?

The reason is that Raku, in contrast to RFC 2616, is Unicode conformant, and needs to be interpreted as a sole , thus preventing the grammar to properly parse a string containing in the sense expected by the HTTP protocol. Notice how attribute invalid is local to each component (e.g., the value for <type> is True, but for <path> is False ).

What is the best way to list all classes in Java?

The most robust mechanism for listing all classes in a given package is currently ClassGraph, because it handles the widest possible array of classpath specification mechanisms, including the new JPMS module system. (I am the author.)


1 Answers

There is no "central" dictionary of classes in Raku. And making the question even harder to solve, classes only now about their parent classes and roles they consume. But they do not know about any classes that inherit from them. Or if you look at a role, which other roles and classes consume that role.

Classes and roles in Raku are therefore irresponsible parents :-)

I guess there could be a way to do some trickery in the MOP, but that could have significant performance effects and cause memory leaks (as many temporary classes wouldn't be garbage collected anymore, because the record keeping would keep it alive).

like image 161
Elizabeth Mattijsen Avatar answered Oct 19 '22 20:10

Elizabeth Mattijsen