mozilla

Revision 386239 of Text Preprocessor

  • Revision slug: Build/Text_Preprocessor
  • Revision title: Text Preprocessor
  • Revision id: 386239
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment CleanUp, TOCLimit(+3)

Revision Content

Introduction

The build preprocessor is similar to the C preprocessor, meant for processing files which have no built-in preprocessor such as XUL and JavaScript documents. It is implemented at {{Source("config/Preprocessor.py")}} and is typically invoked via JAR Manifests.

While used to preprocess CSS files, the directives are changed to begin with '%' instead of '#' to avoid conflict of the id selectors.

Preprocessor Directives

Variable Definition

define

#define variable
#define variable value

Defines a preprocessor variable; note that, unlike the C preprocessor, instances of this variable later in the source are not automatically replaced (see #filter). If value is not supplied, it defaults to 1.

Note that whitespace is significant, so "#define foo one" and "#define foo one " is different (in the second case, foo is defined to be a four-character string).

undef

#undef variable

Undefines a preprocessor variable.

Conditionals

if

#if variable
#if !variable
#if variable==string
#if variable!=string

Disables output if the conditional is false. This can be nested to arbitrary depths. Note that in the equality checks, the variable must come first, and the comparison operator must not be surrounded by any whitespace.

else

#else

Reverses the state of the previous conditional block; for example, if the last #if was true (output was enabled), an #else makes it off (output gets disabled).

Warning: an #else is relative to the last conditional block only, unlike the C preprocessor; it does not matter whether any blocks before it were true. This behavior changed on trunk (Gecko 1.9) on 2006-12-07; see {{Bug(277122)}} for details.
#if 1
  always included
#elif 1
  never included
#else
  always included
#endif

endif

#endif

Ends the conditional block.

ifdef / ifndef

#ifdef variable
#ifndef variable

A #if conditional that is true only if the preprocessor variable variable is defined (in the case of ifdef) or not defined (ifndef).

elif / elifdef / elifndef

#elif variable
#elif !variable
#elif variable == string
#elif variable != string
#elifdef variable
#elifndef variable

A shorthand to mean an #else combined with the relevant conditional. The following two blocks are equivalent:

#ifdef foo
  block 1
#elifdef bar
  block 2
#endif
#ifdef foo
  block 1
#else
#ifdef bar
  block 2
#endif
#endif
Warning: an #elif, #elifdef, or #elifndef is relative to the last conditional block only (as well as the condition it implies), unlike the C preprocessor; it does not matter whether any blocks before it were true. This behavior changed on trunk (Gecko 1.9) on 2006-12-07; see {{Bug(277122)}} for details.

File Inclusion

include

#include filename

The file specified by filename is processed as if the contents was placed at this position. This also means that preprocessor conditionals can even be started in one file and ended in another (but is highly discouraged). There is no limit on depth of inclusion, or repeated inclusion of the same file, or self inclusion; thus, care should be taken to avoid infinite loops.

includesubst

#includesubst @variable@filename

Same as a #include except that all instances of variable in the included file is also expanded as in #filter substitution

expand

#expand string

All variables wrapped in __ are replaced with their value, for this line only. If the variable is not defined, it expands to an empty string. For example, if foo has the value bar, and baz is not defined, then

#expand This <__foo__> <__baz__> gets expanded

Is expanded to

This <bar> <> gets expanded

filter / unfilter

#filter filter1 filter2 ... filterN
#unfilter filter1 filter2 ... filterN

#filter turns on the given filter; filters are run in alphabetical order on a per-line basis. #unfilter turns off the given filter. Available filters are:

emptyLines
strips blank lines from the output
slashslash
strips everything from the first two consecutive slash (/) characters until the end of the line
spaces
collapses consecutive sequences of spaces into a single space, and strips leading and trailing spaces
substitution
all variables wrapped in @ are replaced with their value. If the variable is not defined, it is a fatal error. Similar to #expand and #filter attemptSubstitution
attemptSubstitution
all variables wrapped in @ are replaced with their value, or an empty string if the variable is not defined. Similar to #expand.

literal

#literal string

Output the string (i.e. the rest of the line) literally, with no other fixups. This is useful to output lines starting with #, or to temporarily disable filters.

Other

#error

#error string

Cause a fatal error at this point, with the error message being the given string.

{{OrigDocInfo({ "author" : "Ian Hickson", "date" : "October 14, 2006", "original" : "http://lxr.mozilla.org/mozilla/source/config/preprocessor.txt" })}}

Revision Source

<h2 id="Introduction" name="Introduction">Introduction</h2>
<p>The build preprocessor is similar to the C preprocessor, meant for processing files which have no built-in preprocessor such as XUL and JavaScript documents. It is implemented at {{Source("config/Preprocessor.py")}} and is typically invoked via <a href="/en-US/docs/JAR_Manifests" title="JAR_Manifests">JAR Manifests</a>.</p>
<p>While used to preprocess CSS files, the directives are changed to begin with '%' instead of '#' to avoid conflict of the id selectors.</p>
<h2 id="Preprocessor_Directives" name="Preprocessor_Directives">Preprocessor Directives</h2>
<h3 id="Variable_Definition" name="Variable_Definition">Variable Definition</h3>
<h4 id="define" name="define">define</h4>
<pre>
#define <em>variable</em>
#define <em>variable</em> <em>value</em>
</pre>
<p>Defines a preprocessor variable; note that, unlike the C preprocessor, instances of this variable later in the source are not automatically replaced (see <a href="#filter_.2F_unfilter">#filter</a>). If <em>value</em> is not supplied, it defaults to <em>1</em>.</p>
<p>Note that whitespace is significant, so "<code>#define foo one</code>" and "<code>#define foo one </code>" is different (in the second case, <em>foo</em> is defined to be a four-character string).</p>
<h4 id="undef" name="undef">undef</h4>
<pre>
#undef <em>variable</em>
</pre>
<p>Undefines a preprocessor variable.</p>
<h3 id="Conditionals" name="Conditionals">Conditionals</h3>
<h4 id="if" name="if">if</h4>
<pre>
#if <em>variable</em>
#if&nbsp;!<em>variable</em>
#if <em>variable</em>==<em>string</em>
#if <em>variable</em>!=<em>string</em>
</pre>
<p>Disables output if the conditional is false. This can be nested to arbitrary depths. Note that in the equality checks, the variable <strong>must</strong> come first, and the comparison operator <strong>must</strong> not be surrounded by any whitespace.</p>
<h4 id="else" name="else">else</h4>
<pre>
#else
</pre>
<p>Reverses the state of the previous conditional block; for example, if the last <a href="#if">#if</a> was true (output was enabled), an <a href="#else">#else</a> makes it off (output gets disabled).</p>
<div class="warning">
  <strong>Warning</strong>: an <a href="#else">#else</a> is relative to the last conditional block <strong>only</strong>, unlike the C preprocessor; it does not matter whether any blocks before it were true. This behavior changed on trunk (Gecko 1.9) on 2006-12-07; see {{Bug(277122)}} for details.</div>
<pre>
#if 1
  always included
#elif 1
  never included
#else
  always included
#endif
</pre>
<h4 id="endif" name="endif">endif</h4>
<pre>
#endif
</pre>
<p>Ends the conditional block.</p>
<h4 id="ifdef_.2F_ifndef" name="ifdef_.2F_ifndef">ifdef / ifndef</h4>
<pre>
#ifdef <em>variable</em>
#ifndef <em>variable</em>
</pre>
<p>A <a href="#if">#if</a> conditional that is true only if the preprocessor variable <em>variable</em> is defined (in the case of <em>ifdef</em>) or not defined (<em>ifndef</em>).</p>
<h4 id="elif_.2F_elifdef_.2F_elifndef" name="elif_.2F_elifdef_.2F_elifndef">elif / elifdef / elifndef</h4>
<pre>
#elif <em>variable</em>
#elif&nbsp;!<em>variable</em>
#elif <em>variable</em> == <em>string</em>
#elif <em>variable</em>&nbsp;!= <em>string</em>
#elifdef <em>variable</em>
#elifndef <em>variable</em>
</pre>
<p>A shorthand to mean an <a href="#else">#else</a> combined with the relevant conditional. The following two blocks are equivalent:</p>
<pre>
#ifdef foo
  block 1
#elifdef bar
  block 2
#endif
</pre>
<pre>
#ifdef foo
  block 1
#else
#ifdef bar
  block 2
#endif
#endif
</pre>
<div class="warning">
  <strong>Warning</strong>: an #elif, #elifdef, or #elifndef is relative to the last conditional block <strong>only</strong> (as well as the condition it implies), unlike the C preprocessor; it does not matter whether any blocks before it were true. This behavior changed on trunk (Gecko 1.9) on 2006-12-07; see {{Bug(277122)}} for details.</div>
<h3 id="File_Inclusion" name="File_Inclusion">File Inclusion</h3>
<h4 id="include" name="include">include</h4>
<pre>
#include <em>filename</em>
</pre>
<p>The file specified by <em>filename</em> is processed as if the contents was placed at this position. This also means that preprocessor conditionals can even be started in one file and ended in another (but is highly discouraged). There is no limit on depth of inclusion, or repeated inclusion of the same file, or self inclusion; thus, care should be taken to avoid infinite loops.</p>
<h4 id="includesubst" name="includesubst">includesubst</h4>
<pre>
#includesubst @<em>variable</em>@<em>filename</em>
</pre>
<p>Same as a <a href="#include">#include</a> except that all instances of <em>variable</em> in the included file is also expanded as in <a href="#filter_.2F_unfilter">#filter substitution</a></p>
<h4 id="expand" name="expand">expand</h4>
<pre>
#expand <em>string</em>
</pre>
<p>All variables wrapped in __ are replaced with their value, for this line only. If the variable is not defined, it expands to an empty string. For example, if <em>foo</em> has the value <em>bar</em>, and <em>baz</em> is not defined, then</p>
<pre>
#expand This &lt;<em>__foo__</em>&gt; &lt;<em>__baz__</em>&gt; gets expanded
</pre>
<p>Is expanded to</p>
<pre>
This &lt;<em>bar</em>&gt; &lt;&gt; gets expanded
</pre>
<h4 id="filter_.2F_unfilter" name="filter_.2F_unfilter">filter / unfilter</h4>
<pre>
#filter <em>filter1</em> <em>filter2</em> ... <em>filterN</em>
#unfilter <em>filter1</em> <em>filter2</em> ... <em>filterN</em>
</pre>
<p><code>#filter</code> turns on the given filter; filters are run in alphabetical order on a per-line basis. <code>#unfilter</code> turns off the given filter. Available filters are:</p>
<dl>
  <dt>emptyLines</dt>
  <dd>strips blank lines from the output</dd>
  <dt>slashslash</dt>
  <dd>strips everything from the first two consecutive slash (/) characters until the end of the line</dd>
  <dt>spaces</dt>
  <dd>collapses consecutive sequences of spaces into a single space, and strips leading and trailing spaces</dd>
  <dt>substitution</dt>
  <dd>all variables wrapped in @ are replaced with their value. If the variable is not defined, it is a fatal error. Similar to <a href="#expand">#expand</a> and <a href="#filter_.2F_unfilter">#filter attemptSubstitution</a></dd>
  <dt>attemptSubstitution</dt>
  <dd>all variables wrapped in @ are replaced with their value, or an empty string if the variable is not defined. Similar to <a href="#expand">#expand</a>.</dd>
</dl>
<h4 id="literal" name="literal">literal</h4>
<pre>
#literal <em>string</em>
</pre>
<p>Output the string (i.e. the rest of the line) literally, with no other fixups. This is useful to output lines starting with #, or to temporarily disable filters.</p>
<h3 id="Other" name="Other">Other</h3>
<h4 id=".23error" name=".23error">#error</h4>
<pre>
#error <em>string</em>
</pre>
<p>Cause a fatal error at this point, with the error message being the given string.</p>
<p>{{OrigDocInfo({ "author" : "Ian Hickson", "date" : "October 14, 2006", "original" : "http://lxr.mozilla.org/mozilla/source/config/preprocessor.txt" })}}</p>
Revert to this revision