Added a Resolver example to the API documentation.

This commit is contained in:
Ferdinand Majerech 2011-10-18 16:29:16 +02:00
parent 8ad650e089
commit 93b66da54c
3 changed files with 53 additions and 8 deletions

Binary file not shown.

View file

@ -69,10 +69,11 @@
</dt> </dt>
<dd><p>Add an implicit scalar resolver. <dd><p>Add an implicit scalar resolver.
</p> </p>
<p>If a scalar matches <b>regexp</b> and starts with one of the characters in <b>first</b>, <p>If a scalar matches <b>regexp</b> and starts with any character in <b>first</b>,
its tag is set to <b>tag</b>. If the scalar matches more than one resolver its tag is set to <b>tag</b>. If it matches more than one resolver regexp
regular expression, resolvers added first override those added later. resolvers added first override ones added later. Default resolvers
Default resolvers override any user specified resolvers. override any user specified resolvers, but they can be disabled in
Resolver constructor.
<br> <br>
If a scalar is not resolved to anything, it is assigned the default If a scalar is not resolved to anything, it is assigned the default
@ -86,6 +87,26 @@
<tr><td valign=top>string <b>first</b></td> <tr><td valign=top>string <b>first</b></td>
<td valign=top>String of possible starting characters of the scalar.</td></tr> <td valign=top>String of possible starting characters of the scalar.</td></tr>
</table></div> </table></div>
<b>Examples:</b><div class="pbr">Resolve scalars starting with 'A' to !<b>tag</b> :
<pre class="d_code"> <span class="d_keyword">import</span> std.regex;
<span class="d_keyword">import</span> yaml;
<span class="d_keyword">void</span> main()
{
<span class="d_keyword">auto</span> loader = Loader(<span class="d_string">"file.txt"</span>);
<span class="d_keyword">auto</span> resolver = <span class="d_keyword">new</span> Resolver();
resolver.<span class="d_psymbol">addImplicitResolver</span>(<span class="d_string">"!tag"</span>, std.regex.regex(<span class="d_string">"A*"</span>), <span class="d_string">"A"</span>);
loader.resolver = resolver;
<span class="d_comment">//Note that we have no constructor from tag "!tag", so we can't
</span> <span class="d_comment">//actually load anything that resolves to this tag.
</span> <span class="d_comment">//See Constructor API documentation and tutorial for more information.
</span>
<span class="d_keyword">auto</span> node = loader.load();
}
</pre>
</div>
</dd> </dd>
<dt class="d_decl">package const @property Tag <a name="defaultScalarTag"></a><span class="ddoc_psymbol">defaultScalarTag</span>(); <dt class="d_decl">package const @property Tag <a name="defaultScalarTag"></a><span class="ddoc_psymbol">defaultScalarTag</span>();

View file

@ -76,10 +76,11 @@ final class Resolver
/** /**
* Add an implicit scalar resolver. * Add an implicit scalar resolver.
* *
* If a scalar matches regexp and starts with one of the characters in first, * If a scalar matches regexp and starts with any character in first,
* its _tag is set to tag. If the scalar matches more than one resolver * its _tag is set to tag. If it matches more than one resolver _regexp
* regular expression, resolvers added _first override those added later. * resolvers added _first override ones added later. Default resolvers
* Default resolvers override any user specified resolvers. * override any user specified resolvers, but they can be disabled in
* Resolver constructor.
* *
* If a scalar is not resolved to anything, it is assigned the default * If a scalar is not resolved to anything, it is assigned the default
* YAML _tag for strings. * YAML _tag for strings.
@ -87,6 +88,29 @@ final class Resolver
* Params: tag = Tag to resolve to. * Params: tag = Tag to resolve to.
* regexp = Regular expression the scalar must match to have this _tag. * regexp = Regular expression the scalar must match to have this _tag.
* first = String of possible starting characters of the scalar. * first = String of possible starting characters of the scalar.
*
* Examples:
*
* Resolve scalars starting with 'A' to !tag :
* --------------------
* import std.regex;
*
* import yaml;
*
* void main()
* {
* auto loader = Loader("file.txt");
* auto resolver = new Resolver();
* resolver.addImplicitResolver("!tag", std.regex.regex("A*"), "A");
* loader.resolver = resolver;
*
* //Note that we have no constructor from tag "!tag", so we can't
* //actually load anything that resolves to this tag.
* //See Constructor API documentation and tutorial for more information.
*
* auto node = loader.load();
* }
* --------------------
*/ */
void addImplicitResolver(string tag, Regex!char regexp, in string first) void addImplicitResolver(string tag, Regex!char regexp, in string first)
{ {