<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Bonjour,</p>
<p>Pour mon programme d'interfaçage graphique de fichier Org
(<a class="moz-txt-link-freetext" href="https://github.com/alainbebe/org-mode-gtk.raku">https://github.com/alainbebe/org-mode-gtk.raku</a>), j'aimerais lire
les fichiers Org à l'aide la grammaire de Raku.</p>
<p>Je suis parvenu à lire des fichiers en liste et/ou en récursif,
mais quand le fichier contient une nouvelle tâche avec une
indentation en moins, je coince.<br>
Le code ci-dessous match chaque fichier (simulé dans $file), mais
, dernier exemple, place en niveau 2 une tâche qui devrait être en
niveau 1. Ce qui me paraît normal vu le code. J'ai essayé la ligne
12, mais ça ne match pas.<br>
<br>
Une idée ?</p>
<p>(posté déjà sur developpez.com)</p>
<p>AlainBB<br>
</p>
<pre class="bbcode_code" style="height:204px;"><pre style="margin: 0"><span style="color: #808080;">#!/usr/bin/env perl6</span>
<span style="color: #0000ff;">use</span> v6;
<span style="color: #0000ff;">use</span> Data::Dump;
<span style="color: #0000ff;">use</span> Grammar::Tracer;
<span style="color: #0000ff;">my</span> <span style="color: #339933;">$level</span>;
<span style="color: #0000ff;">grammar</span> OrgMode <span class="br0">{</span>
<span style="color: #0000ff;">rule</span> TOP <span class="br0">{</span> ^ <span style="color: #339933;"><tasks></span> $ <span class="br0">}</span>
<span style="color: #0000ff;">rule</span> tasks <span class="br0">{</span> <span style="color: #800000;">\n</span>?<span style="color: #339933;"><task></span>+ %% <span style="color: #800000;">\n</span><span class="br0">}</span>
<span style="color: #0000ff;">token</span> task <span class="br0">{</span> <span style="color: #339933;"><content></span> <span style="color: #339933;"><tasks></span>? <span class="br0">{</span><span style="color: #339933;">$level</span>=<span style="color: #FF0000;">""</span><span class="br0">}</span><span class="br0">}</span>
<span style="color: #808080;"># token task { <content> <tasks>? {$level=$level.substr(0, *-1)}}</span>
<span style="color: #0000ff;">token</span> content <span class="br0">{</span> ^^ <span class="br0">(</span><span style="color: #339933;">$level</span> <span style="color: #FF0000;">"*"</span>+<span class="br0">)</span><span style="color: #FF0000;">" "</span> .+? $$ <span class="br0">{</span><span style="color: #339933;">$level</span>=<span style="color: #339933;">$0</span>.<span style="color: #0080ff;">Str</span><span class="br0">}</span><span class="br0">}</span>
<span class="br0">}</span>
<span style="color: #0000ff;">class</span> OM-actions <span class="br0">{</span>
<span style="color: #0000ff;">method</span> TOP<span class="br0">(</span><span style="color: #339933;">$/</span><span class="br0">)</span> <span class="br0">{</span>
make <span style="color: #339933;">$<tasks></span>.made;
<span class="br0">}</span>
<span style="color: #0000ff;">method</span> tasks<span class="br0">(</span><span style="color: #339933;">$/</span><span class="br0">)</span> <span class="br0">{</span>
make <span style="color: #339933;">$<task></span>».made ;
<span class="br0">}</span>
<span style="color: #0000ff;">method</span> task<span class="br0">(</span><span style="color: #339933;">$/</span><span class="br0">)</span> <span class="br0">{</span>
<span style="color: #0000ff;">my</span> <span style="color: #339933;">%task</span>;
<span style="color: #339933;">%task</span><span class="br0">{</span><span style="color: #FF0000;">"task"</span><span class="br0">}</span>=<span style="color: #339933;">$<content></span>.made;
<span style="color: #339933;">%task</span><span class="br0">{</span><span style="color: #FF0000;">"sub-task"</span><span class="br0">}</span>=<span style="color: #339933;">$<tasks></span>.made <span style="color: #0000ff;">if</span> <span style="color: #339933;">$<tasks></span>.made;
make <span style="color: #339933;">%task</span>;
<span class="br0">}</span>
<span style="color: #0000ff;">method</span> content<span class="br0">(</span><span style="color: #339933;">$/</span><span class="br0">)</span> <span class="br0">{</span>
make <span style="color: #339933;">$/</span>.<span style="color: #0080ff;">Str</span> ;
<span class="br0">}</span>
<span class="br0">}</span>
<span style="color: #0000ff;">my</span> <span style="color: #339933;">$file</span> =
<span style="color: #FF0000;">"* juste un header 1</span>
<span style="color: #FF0000;">* juste deux header 1</span>
<span style="color: #FF0000;">* header 2</span>
<span style="color: #FF0000;">* juste 3 header 1</span>
<span style="color: #FF0000;">* header 2</span>
<span style="color: #FF0000;">* header 3</span>
<span style="color: #FF0000;">* juste un header 1 et un sub</span>
<span style="color: #FF0000;">** sub-header 1</span>
<span style="color: #FF0000;">* juste un header 1 et deux sub *</span>
<span style="color: #FF0000;">** sub-header 1</span>
<span style="color: #FF0000;">** sub-header 2</span>
<span style="color: #FF0000;">* juste un header 1 et un sub et un sub-sub</span>
<span style="color: #FF0000;">** sub-header 1</span>
<span style="color: #FF0000;">*** sub-sub-header 1</span>
<span style="color: #FF0000;">* 2 header 1 et un sub au milieu</span>
<span style="color: #FF0000;">** sub-header 1</span>
<span style="color: #FF0000;">* header 2"</span>
;
say <span style="color: #FF0000;">"<span style="color: #800000;">\n</span>"</span> x <span style="color: #cc66cc;">10</span>;
<span style="color: #0000ff;">sub</span> parse_file<span class="br0">(</span><span style="color: #339933;">$file</span><span class="br0">)</span> <span class="br0">{</span>
say <span style="color: #339933;">$file</span>;
say <span style="color: #FF0000;">""</span>;
<span style="color: #339933;">$level</span>=<span style="color: #FF0000;">""</span>;
<span style="color: #808080;"># say OrgMode.parse($file);</span>
say Dump OrgMode.parse<span class="br0">(</span><span style="color: #339933;">$file</span>,:actions<span class="br0">(</span>OM-actions<span class="br0">)</span><span class="br0">)</span>.made;
say <span style="color: #FF0000;">"---------------------------------------------------------------------------------------"</span>;
<span class="br0">}</span>
parse_file<span class="br0">(</span><span style="color: #339933;">$_</span><span class="br0">)</span> <span style="color: #0000ff;">for</span> split<span class="br0">(</span><span style="color: #FF0000;">"<span style="color: #800000;">\n</span><span style="color: #800000;">\n</span>"</span>,<span style="color: #339933;">$file</span><span class="br0">)</span>;</pre></pre>
</body>
</html>