/usr/share/gtk-doc/html/gtksourceview-3.0/lang-tutorial.html is in libgtksourceview-3.0-doc 3.22.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Language Definition v2.0 Tutorial: GtkSourceView 3 Reference Manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GtkSourceView 3 Reference Manual">
<link rel="up" href="pt02.html" title="Part II. Syntax highlighting reference">
<link rel="prev" href="pt02.html" title="Part II. Syntax highlighting reference">
<link rel="next" href="lang-reference.html" title="Language Definition v2.0 Reference">
<meta name="generator" content="GTK-Doc V1.25 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="pt02.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="pt02.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="lang-reference.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="refentry">
<a name="lang-tutorial"></a><div class="titlepage"></div>
<div class="refnamediv"><table width="100%"><tr>
<td valign="top">
<h2><span class="refentrytitle">Language Definition v2.0 Tutorial</span></h2>
<p>Language Definition v2.0 Tutorial —
Guide to the GtkSourceView language definition file format
</p>
</td>
<td class="gallery_image" valign="top" align="right"></td>
</tr></table></div>
<div class="refsect1">
<a name="tutorial"></a><h2>A language definition for the C language</h2>
<div class="note"><p>
The version 2 here refers to the language definition file format,
not to the version of GtkSourceView. So this tutorial is suitable
for GtkSourceView 2 and 3.
</p></div>
<p>
To describe the syntax of a language GtkSourceView uses an XML format which
defines nested context to be highlighted. Each context roughly corresponds
to a portion of the syntax which has to be highlighted (e.g. keywords,
strings, comments), and can contain nested contexts (e.g. escaped
characters.)
</p>
<p>
In this tutorial we will analyze a simple example to highlight a subset of
C, based on the full C language definition.
</p>
<p>
Like every well formed XML document, the language description starts with a
XML declaration:
</p>
<pre class="programlisting">
<?xml version="1.0" encoding="UTF-8"?>
</pre>
<p>
After the usual preamble, the main tag is the <code class="code"><language></code> element:
</p>
<pre class="programlisting">
<language id="c" _name="C" version="2.0" _section="Source">
</pre>
<p>
The attribute <code class="code">id</code> is used in external references and defines a standard
way to refer to this language definition, while the <code class="code">name</code> attribute is
the name presented to the user (it is translatable using gettext prepending a
<code class="code">_</code>.)
</p>
<p>
The attribute <code class="code">section</code>, also translatable, tells the category where
this language should be grouped when it is presented to the user. Currently
available categories in GtkSourceView are "Source", "Script", "Markup",
"Scientific" and "Other".
</p>
<p>
The attribute <code class="code">version</code> specifies the version of the xml syntax
used in your language definition file, so it should always be <code class="code">2.0</code>.
</p>
<p>
The <code class="code"><language></code> element contains three sections:
<code class="code"><metadata></code>, <code class="code"><styles></code> and
<code class="code"><definitions></code>
</p>
<pre class="programlisting">
<metadata>
</pre>
<p>
The <code class="code"><metadata></code> element is optional and provides a collection
of properties which specify arbitrary information about the language definition
file itself. It is particularly important to specify the conventional
<code class="code">mimetypes</code> and <code class="code">globs</code> properties that
GtkSourceView uses to automatically detect which syntax highlighting to use
for a given file. They respectively contain a semi-colon separated list of
mimetypes and filename extensions.
</p>
<pre class="programlisting">
<metadata>
<property name="mimetypes">text/x-c;text/x-csrc</property>
<property name="globs">*.c</property>
</metadata>
</pre>
<pre class="programlisting">
<styles>
</pre>
<p>
This element contains every association between the styles used in the
description and the defaults stored internally in GtkSourceView.
For each style there is a <code class="code"><style></code> element:
</p>
<pre class="programlisting">
<style id="comment" _name="Comment" map-to="def:comment"/>
</pre>
<p>
This defines a <code class="code">comment</code> style, which inherits the font
properties from the defaults style <code class="code">def:comment</code>.
The <code class="code">name</code> attribute contains the translatable name for this
style, which is the name to show to the user.
</p>
<p>
For each style used in the language definition there is a corresponding
<code class="code"><style></code> element; every style can be used in different
contexts, so they will share the same appearance.
</p>
<pre class="programlisting">
<style id="string" _name="String" map-to="def:string"/>
<style id="escaped-character" _name="Escaped Character" map-to="def:special-char"/>
<style id="preprocessor" _name="Preprocessor" map-to="def:preprocessor"/>
<style id="included-file" _name="Included File" map-to="def:string"/>
<style id="char" _name="Character" map-to="def:character"/>
<style id="keyword" _name="Keyword" map-to="def:keyword"/>
<style id="type" _name="Data Type" map-to="def:type"/>
</pre>
<p>
Following the <code class="code"><styles></code> element there is the
<code class="code"><definitions></code> element, which contains the
description proper of the syntax:
</p>
<pre class="programlisting">
<definitions>
</pre>
<p>
Here we should define a main context, the one we enter at the beginning of
the file: to do so we use the <code class="code"><context></code> tag, with an
<code class="code">id</code> equal to the <code class="code">id</code> of the
<code class="code"><language></code> element:
</p>
<pre class="programlisting">
<context id="c">
</pre>
<p>
The element <code class="code"><include></code> contains the list of sub-contexts
for the current context: as we are in the main context we should put here
the top level contexts for the C language:
</p>
<pre class="programlisting">
<include>
</pre>
<p>
The first context defined is the one for single-line C style comments: they
start with a double slash <code class="code">//</code> and end at the end of the line:
</p>
<pre class="programlisting">
<context id="comment" style-ref="comment">
<start>\/\/</start>
<end>$</end>
</context>
</pre>
<p>
The <code class="code"><start></code> element contains the regular expression telling
the highlighting engine to enter in the defined context, until the terminating
regular expression contained in the <code class="code"><end></code> element is found.
</p>
<p>
Those regular expressions are PCRE regular expressions in the form
<code class="code">/regex/options</code> (see the documentation of PCRE for details.) If
there are no options to be specified and you don't need to match the spaces at
the start and at the end of the regular expression, you can omit the slashes,
putting here only <code class="code">regex</code>.
</p>
<p>
The possible options are:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
<code class="code">i</code>: case insensitive;
</p></li>
<li class="listitem"><p>
<code class="code">x</code>: extended (spaces are ignored and it is possible to put comments
starting with <code class="code">#</code> and ending at the end of the line);
</p></li>
<li class="listitem"><p>
<code class="code">s</code>: the metacharacter <code class="code">.</code> matches the <code class="code">\n</code>.
</p></li>
</ul></div>
<p>
You can set the default options using the <code class="code"><default-regex-options</code> tag
before the <code class="code"><definitions></code> element. To disable a group of options,
instead, you have to preced them with a hyphen (<code class="code">-</code>).
[FIXME: add an example]
</p>
<p>
In GtkSourceView are available also some extensions to the standard perl
style regular expressions:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
<code class="code">\%[</code> and <code class="code">\%]</code> are custom word boundaries, which can
be redefined with the <code class="code"><keyword-char-class></code> tag (in
contrast with <code class="code">\b</code>);
</p></li>
<li class="listitem"><p>
<code class="code">\%{id}</code> will include the regular expression defined in the
<code class="code"><define-regex></code> tag with the same id, useful if you have
common portions of regular expressions used in different contexts;
</p></li>
<li class="listitem"><p>
<code class="code">\%{subpattern@start}</code> can be used only inside the
<code class="code"><end></code> tag and will be substituted with the
string matched in the corresponding
sub-pattern (can be a number or a name if named sub-patterns are
used) in the preceding <code class="code"><start></code> element. For an example
see the implementation of here-documents in the <code class="filename">sh.lang</code>
language description distribuited with GtkSourceView.
</p></li>
</ul></div>
<p>
The next context is for C-style strings. They start and end with a double
quote but they can contain escaped double quotes, so we sould make sure
we don't end the string prematurely:
</p>
<pre class="programlisting">
<context id="string" end-at-line-end="true" style-ref="string">
</pre>
<p>
The <code class="code">end-at-line-end</code> attribute tells the engine that the current context
should be forced to terminate at the end of the line, even if the ending
regular expression is not found, and that an error should be displayed.
</p>
<pre class="programlisting">
<start>"</start>
<end>"</end>
<include>
</pre>
<p>
To implement the escape handling we include a <code class="code">escape</code> context:
</p>
<pre class="programlisting">
<context id="escape" style-ref="escaped-character">
<match>\\.</match>
</context>
</pre>
<p>
This is a simple context matching a single regular expression, contained in
the <code class="code"><match></code> element. This context will extend its parent, causing the
ending regular expression of the <code class="code">"string"</code> context to not match the escaped
double quote.
</p>
<pre class="programlisting">
</include>
</context>
</pre>
<p>
Multiline C-style comment can span over multiple lines and cannot be
escaped, but to make things more interesting we want to highlight every
internet address contained:
</p>
<pre class="programlisting">
<context id="comment-multiline" style-ref="comment">
<start>\/\*</start>
<end>\*\/</end>
<include>
<context id="net-address" style-ref="net-address" extend-parent="false">
</pre>
<p>
In this case, the child should be terminated if the end of the parent is
found, so we use <code class="code">false</code> in the <code class="code">extend-parent</code> attribute.
</p>
<pre class="programlisting">
<match>http:\/\/[^\s]*</match>
</context>
</include>
</context>
</pre>
<p>
For instance in the following comment the string <code class="code">http://www.gnome.org*/</code>
matches the <code class="code">net-address</code> context but it contains the end of the parent
context (<code class="code">*/</code>.) As <code class="code">extend-parent</code> is false,
only <code class="code">http://www.gnome.org</code> is
highlighted as an address and <code class="code">*/</code> is correctly recognized as the end of
the comment.
</p>
<pre class="programlisting">
/* This is a comment http://www.gnome.org */
</pre>
<p>
Character constants in C are delimited by single quotes (<code class="code">'</code>) and can
contain escaped characters:
</p>
<pre class="programlisting">
<context id="char" end-at-line-end="true" style-ref="string">
<start>'</start>
<end>'</end>
<include>
<context ref="escape"/>
</pre>
<p>
The <code class="code">ref</code> attribute is used when we want to reuse a previously defined
context. Here we reuse the <code class="code">escape</code> context defined in the <code class="code">string</code>
context, without repeating its definition.
</p>
<pre class="programlisting">
</include>
</context>
</pre>
<p>
Using <code class="code">ref</code> it is also possible to refer to contexts defined in other
languages, preceding the id of the context with the id of the containing
language, separating them with a colon:
</p>
<pre class="programlisting">
<context ref="def:decimal"/>
<context ref="def:float"/>
</pre>
<p>
The definitions for decimal and float constants are in a external file,
with id <code class="code">def</code>, which is not associated with any language but contains
reusable contexts wich every language definition can import.
</p>
<p>
The <code class="code">def</code> language file contains an <code class="code">in-comment</code> context that can contain
addresses and tags such as FIXME and TODO, so we can write a new version of
our <code class="code">comment-multiline</code> context that uses the definitions from <code class="code">def.lang</code>.
</p>
<pre class="programlisting">
<context id="comment-multiline" style-ref="comment">
<start>\/\*</start>
<end>\*\/</end>
<include>
<context ref="def:in-comment"/>
</pre>
<pre class="programlisting">
</include>
</context>
</pre>
<p>
Keywords can be grouped in a context using a list of <code class="code"><keyword></code>
elements:
</p>
<pre class="programlisting">
<context id="keywords" style-ref="keyword">
<keyword>if</keyword>
<keyword>else</keyword>
<keyword>for</keyword>
<keyword>while</keyword>
<keyword>return</keyword>
<keyword>break</keyword>
<keyword>switch</keyword>
<keyword>case</keyword>
<keyword>default</keyword>
<keyword>do</keyword>
<keyword>continue</keyword>
<keyword>goto</keyword>
<keyword>sizeof</keyword>
</context>
</pre>
<p>
Keywords with different meaning can be grouped in different context, making
possible to highlight them differently:
</p>
<pre class="programlisting">
<context id="types" style-ref="type">
<keyword>char</keyword>
<keyword>const</keyword>
<keyword>double</keyword>
<keyword>enum</keyword>
<keyword>float</keyword>
<keyword>int</keyword>
<keyword>long</keyword>
<keyword>short</keyword>
<keyword>signed</keyword>
<keyword>static</keyword>
<keyword>struct</keyword>
<keyword>typedef</keyword>
<keyword>union</keyword>
<keyword>unsigned</keyword>
<keyword>void</keyword>
</context>
</pre>
<p>
You can also set a prefix (or a suffix) common to every keyword using the
<code class="code"><prefix></code> and <code class="code"><suffix></code> tags:
</p>
<pre class="programlisting">
<context id="preprocessor" style-ref="preprocessor">
<prefix>^#</prefix>
</pre>
<p>
If not specified, <code class="code"><prefix></code> and <code class="code"><suffix></code>
are set to, respectively, <code class="code">\%[</code> and
<code class="code">\%]</code>.
</p>
<pre class="programlisting">
<keyword>define</keyword>
<keyword>undef</keyword>
</pre>
<p>
Keep in mind that every keyword is a regular expression:
</p>
<pre class="programlisting">
<keyword>if(n?def)?</keyword>
<keyword>else</keyword>
<keyword>elif</keyword>
<keyword>endif</keyword>
</context>
</pre>
<p>
In C, there is a common pratice to use <code class="code">#if 0</code> to express multi-line
nesting comments. To make things easier to the user, we want to highlight
these pseudo-comments as comments:
</p>
<pre class="programlisting">
<context id="if0-comment" style-ref="comment">
<start>^#if 0\b</start>
<end>^#(endif|else|elif)\b</end>
<include>
</pre>
<p>
As <code class="code">#if 0</code> comments are nesting, we should consider that inside a comment
we can find other <code class="code">#if</code>s with the corresponding <code class="code">#endif</code>s, avoiding
the termination of the comment on the wrong <code class="code">#endif</code>. To do so we use a
nested context, that will extend the parent on every nested
<code class="code">#if</code>/<code class="code">#endif</code>:
</p>
<pre class="programlisting">
<context id="if-in-if0">
<start>^#if(n?def)?\b</start>
<end>^#endif\b</end>
<include>
</pre>
<p>
Nested contexts can be recursive:
</p>
<pre class="programlisting">
<context ref="if-in-if0"/>
</include>
</context>
</include>
</context>
</pre>
<p>
Because contexts defined before have higher priority, <code class="code">if0-comment</code> will
never be matched. To make things work we should move it before the
<code class="code">preprocessor</code> context, thus giving <code class="code">if0-comment</code> a higher priority.
</p>
<p>
For the <code class="code">#include</code> preprocessor directive it could be useful to highlight
differently the included file:
</p>
<pre class="programlisting">
<context id="include" style-ref="preprocessor">
<match>^#include (".*"|&lt;.*&gt;)</match>
<include>
</pre>
<p>
To do this we use grouping sub-patterns in the regular expression,
associating them with a context with the <code class="code">sub-pattern</code> attribute:
</p>
<pre class="programlisting">
<context id="included-file" sub-pattern="1"
style-ref="included-file"/>
</pre>
<p>
In the <code class="code">sub-pattern</code> attribute we could use:
</p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem"><p>
0: the whole regular expression;
</p></li>
<li class="listitem"><p>
1: the first sub-pattern (a sub-espression enclosed in
parenthesis);
</p></li>
<li class="listitem"><p>
2: the second;
</p></li>
<li class="listitem"><p>
...
</p></li>
<li class="listitem"><p>
<code class="code">name</code>: a named sub-pattern with name <code class="code">name</code> (see the PCRE
documentation.)
</p></li>
</ul></div>
<p>
We could also use a <code class="code">where</code> attribute with value
<code class="code">start</code> or <code class="code">end</code> to
specify the regular expression the context is referring, when we have both
the <code class="code"><start></code> and <code class="code"><end></code> element.
</p>
<pre class="programlisting">
</include>
</context>
</pre>
<p>
Having defined a good subset of the C syntax we close every tag still open:
</p>
<pre class="programlisting">
</include>
</context>
</definitions>
</language>
</pre>
</div>
<div class="refsect1">
<a name="id-1.3.2.4"></a><h2>The full language definition</h2>
<p>
This is the full language definition for the subset of C taken in consideration
for this tutorial:
</p>
<pre class="programlisting">
<?xml version="1.0" encoding="UTF-8"?>
<language id="c" _name="C" version="2.0" _section="Source">
<metadata>
<property name="mimetypes">text/x-c;text/x-csrc</property>
<property name="globs">*.c</property>
</metadata>
<styles>
<style id="comment" _name="Comment" map-to="def:comment"/>
<style id="string" _name="String" map-to="def:string"/>
<style id="escaped-character" _name="Escaped Character" map-to="def:special-char"/>
<style id="preprocessor" _name="Preprocessor" map-to="def:preprocessor"/>
<style id="included-file" _name="Included File" map-to="def:string"/>
<style id="char" _name="Character" map-to="def:character"/>
<style id="keyword" _name="Keyword" map-to="def:keyword"/>
<style id="type" _name="Data Type" map-to="def:type"/>
</styles>
<definitions>
<context id="c">
<include>
<context id="comment" style-ref="comment">
<start>\/\/</start>
<end>$</end>
</context>
<context id="string" end-at-line-end="true" style-ref="string">
<start>"</start>
<end>"</end>
<include>
<context id="escape" style-ref="escaped-character">
<match>\\.</match>
</context>
</include>
</context>
<context id="comment-multiline" style-ref="comment">
<start>\/\*</start>
<end>\*\/</end>
<include>
<context ref="def:in-comment"/>
</include>
</context>
<context id="char" end-at-line-end="true" style-ref="string">
<start>'</start>
<end>'</end>
<include>
<context ref="escape"/>
</include>
</context>
<context ref="def:decimal"/>
<context ref="def:float"/>
<context id="keywords" style-ref="keyword">
<keyword>if</keyword>
<keyword>else</keyword>
<keyword>for</keyword>
<keyword>while</keyword>
<keyword>return</keyword>
<keyword>break</keyword>
<keyword>switch</keyword>
<keyword>case</keyword>
<keyword>default</keyword>
<keyword>do</keyword>
<keyword>continue</keyword>
<keyword>goto</keyword>
<keyword>sizeof</keyword>
</context>
<context id="types" style-ref="type">
<keyword>char</keyword>
<keyword>const</keyword>
<keyword>double</keyword>
<keyword>enum</keyword>
<keyword>float</keyword>
<keyword>int</keyword>
<keyword>long</keyword>
<keyword>short</keyword>
<keyword>signed</keyword>
<keyword>static</keyword>
<keyword>struct</keyword>
<keyword>typedef</keyword>
<keyword>union</keyword>
<keyword>unsigned</keyword>
<keyword>void</keyword>
</context>
<context id="if0-comment" style-ref="comment">
<start>^#if 0\b</start>
<end>^#(endif|else|elif)\b</end>
<include>
<context id="if-in-if0">
<start>^#if(n?def)?\b</start>
<end>^#endif\b</end>
<include>
<context ref="if-in-if0"/>
</include>
</context>
</include>
</context>
<context id="preprocessor" style-ref="preprocessor">
<prefix>^#</prefix>
<keyword>define</keyword>
<keyword>undef</keyword>
<keyword>if(n?def)?</keyword>
<keyword>else</keyword>
<keyword>elif</keyword>
<keyword>endif</keyword>
</context>
<context id="include" style-ref="preprocessor">
<match>^#include (".*"|&lt;.*&gt;)</match>
<include>
<context id="included-file"
sub-pattern="1"
style-ref="included-file"/>
</include>
</context>
</include>
</context>
</definitions>
</language>
</pre>
</div>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.25</div>
</body>
</html>
|