Parsing mfunctions with mtree
Afficher commentaires plus anciens
Given an Nx1 colum vector of strings containing lines of mfunction code, it is possible to use the undocumented function mtree() to parse it into its constituent functions. For example, given,
load Inputs
str1,
the function below will find the starting and ending lines of each of the three function blocks.
[firstLine,lastLine] = functionBlocks(str1)
This works for nested functions as well:
str2,
[firstLine,lastLine] = functionBlocks(str2)
However, I would really like to have the function be able to group the output separately into top-level function blocks and nested function blocks. I know it is possible to do this through a post-analysis of the outputs firstLine and lastLine, but I wonder if it is possible to get information about whether a function block is top-level or nested directly from mtree, i.e., by modifying,
fnSet = T.mtfind('Kind','FUNCTION');
Unfortunately, because mtree() is undocumented, it is difficult to fathom its full capabilities. Does anyone know if/how this can be done?
function [firstLine,lastLine] = functionBlocks(str)
T = mtree(strjoin(str,newline));
fnSet = T.mtfind('Kind','FUNCTION');
K=fnSet.count;
kset=fnSet.indices;
for k = K:-1:1
fn = fnSet.select(kset(k));
firstLine(k) = fn.lineno;
lastLine(k) = fn.lastone;
end
end
1 commentaire
Réponses (1)
Tridib
le 20 Fév 2026 à 5:39
0 votes
I understand you want to enhance your "mtree"-based parsing so you can automatically separate MATLAB function blocks into top‑level and nested groups. Here are the steps that will lead you to that outcome:
- First convert your input into a single source string so the syntax tree is built consistently, while also keeping individual lines available so you can map line numbers back later.
- Build the mtree for that source and locate all the FUNCTION nodes, then record each node’s starting and ending line numbers and sort them by starting line so that the structure is easier to reason about.
- Check whether your version of mtree exposes a parent link. If so, classify a function as nested when its parent node is itself a FUNCTION, otherwise treat it as top‑level.
- If parent information is not available, fall back to a simple span‑based rule, a function is nested if its starting line falls within the line range of any earlier function.
- Once you know which are nested and which are top‑level, determine each function’s nearest parent by finding the smallest enclosing range and compute the nesting level by incrementing from the parent.
- Use these masks to separate your start/end line ranges cleanly into “top‑level” and “nested” groups, keeping them in source order so the grouping is intuitive.
- If you need names, extract them directly from the corresponding source lines using a tolerant pattern, but keep the structural logic strictly based on line numbers.
- Validate your approach on simple cases first, files with only top‑level functions, then files with one nested function, then deeper nesting, so you see your masks, parents, and levels behaving correctly.
- If helpful, build a higher‑level grouping where each top‑level function is paired with all its descendants so you can easily inspect the hierarchy or display it.
Hope this helps!
1 commentaire
Rik
le 20 Fév 2026 à 11:46
Before letting some GenAI tool write the answer, perhaps you should have a look at the question.
You are personally responsible for what you post, good or bad. Did you check to see whether this question actually answers the question? Why didn't you at least implement the analyzer code that determines whether a function is a nested function?
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!