Click here to Skip to main content
15,881,803 members
Articles / Web Development / ASP.NET

Multiple Language Syntax Highlighting, Part 1: JScript

Rate me:
Please Sign up or sign in to vote.
4.96/5 (58 votes)
12 Mar 200312 min read 283.9K   6.1K   170  
Makes automaticaly highlighting source code in web page a reality (for C,C++,JScript, VBScript, XML)
<?xml version="1.0" encoding="ISO-8859-1"?>
<highlight needs-build="yes" save-build="no">
    <!-- families of keywords -->
    <keywordlists>
        <!-- C keywords -->
        <keywordlist id="kwc">
            <kw>auto</kw>
            <kw>const</kw>
            <kw>double</kw>
            <kw>float</kw>
            <kw>int</kw>
            <kw>short</kw>
            <kw>struct</kw>
            <kw>unsigned</kw>
            <kw>break</kw>
            <kw>continue</kw>
            <kw>else</kw>
            <kw>for</kw>
            <kw>long</kw>
            <kw>signed</kw>
            <kw>switch</kw>
            <kw>void</kw>
            <kw>case</kw>
            <kw>default</kw>
            <kw>enum</kw>
            <kw>goto</kw>
            <kw>register</kw>
            <kw>sizeof</kw>
            <kw>typedef</kw>
            <kw>volatile</kw>
            <kw>char</kw>
            <kw>do</kw>
            <kw>extern</kw>
            <kw>if</kw>
            <kw>return</kw>
            <kw>static</kw>
            <kw>union</kw>
            <kw>while</kw>
        </keywordlist>
        <!-- C++ keywords -->
        <keywordlist id="kwcpp">
            <kw>asm</kw>
            <kw>dynamic_cast</kw>
            <kw>namespace</kw>
            <kw>reinterpret_cast</kw>
            <kw>try</kw>
            <kw>bool</kw>
            <kw>explicit</kw>
            <kw>new</kw>
            <kw>static_cast</kw>
            <kw>typeid</kw>
            <kw>catch</kw>
            <kw>false</kw>
            <kw>operator</kw>
            <kw>template</kw>
            <kw>typename</kw>
            <kw>class</kw>
            <kw>friend</kw>
            <kw>private</kw>
            <kw>this</kw>
            <kw>using</kw>
            <kw>const_cast</kw>
            <kw>inline</kw>
            <kw>public</kw>
            <kw>throw</kw>
            <kw>virtual</kw>
            <kw>delete</kw>
            <kw>mutable</kw>
            <kw>protected</kw>
            <kw>true</kw>
            <kw>wchar_t</kw>
        </keywordlist>
        <!-- C++ preprocessor keywords -->
        <keywordlist id="kwpreprocessor" pre="#">
            <kw>define</kw>
            <kw>error</kw>
            <kw>import</kw>
            <kw>undef</kw>
            <kw>elif</kw>
            <kw>if</kw>
            <kw>include</kw>
            <kw>else</kw>
            <kw>ifdef</kw>
            <kw>line</kw>
            <kw>endif</kw>
            <kw>ifndef</kw>
            <kw>pragma</kw>
        </keywordlist>
        <!-- STL keywords -->
        <keywordlist id="kwstl" pre="std::">
            <kw>std</kw>
            <kw>vector</kw>
            <kw>set</kw>
            <kw>map</kw>
            <kw>list</kw>
            <kw>stack</kw>
            <kw>deque</kw>
            <kw>multimap</kw>
            <kw>multiset</kw>
        </keywordlist>
    </keywordlists>
    <!-- Languages syntax specification -->
    <languages>
        <!-- C++ language specification -->
        <language id="cpp">
            <!-- Code contexts: default (most common) is code. -->
            <contexts default="code">
                <!-- basic source code context -->
                <context id="code" attribute="code">
                    <!-- /* block of comment */ -->
                    <detect2chars attribute="hidden" context="blockdoc" char="/" char1="*" />
                    <!-- // line of comment -->
                    <detect2chars attribute="hidden" context="linedoc" char="/" char1="/" />
                    <!-- " " literals -->
                    <!--				<regexp attribute="literal" context="code" expression='"(.|\\"|\\\r\n)*?((\\\\)+"|[^\\]{1}")' /> -->
                    <regexp attribute="literal" context="code" expression="&quot;(.|\\&quot;|\\\r\n)*?((\\\\)+&quot;|[^\\]{1}&quot;)" />
                    <!-- keyword c -->
                    <keyword attribute="reservedkeyword" context="code" family="kwc" />
                    <!-- keyword c++ -->
                    <keyword attribute="reservedkeyword" context="code" family="kwcpp" />
                    <!-- keyword stl -->
                    <keyword attribute="reservedkeyword" context="code" family="kwstl" />
                    <!-- preprocessor keywords -->
                    <keyword attribute="reservedkeyword" context="code" family="kwpreprocessor" />
                </context>
                <!-- block comment /* ... �*/ -->
                <context id="blockdoc" attribute="blockdoc">
                    <!-- finish line of comment, start block -->
                    <detect2chars attribute="hidden" context="code" char="*" char1="/" />
                </context>
                <!-- line comment // ... -->
                <context id="linedoc" attribute="linedoc">
                    <!-- finish line of comment, start block -->
                    <detect2chars attribute="hidden" context="blockdoc" char="/" char1="*" />
                    <!-- finish line of comment end of line -->
                    <linecontinue attribute="hidden" context="code" />
                </context>
            </contexts>
        </language>
    </languages>
</highlight>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions