<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Lars Wilhelmsen &#187; Syntax+Highlighting</title>
	<atom:link href="http://www.larswilhelmsen.com/category/syntaxhighlighting/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.larswilhelmsen.com</link>
	<description>Computer Software Guy Living Between the Edge and Corner Cases</description>
	<lastBuildDate>Wed, 11 Jan 2012 13:05:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Syntax highlighting with MGrammar</title>
		<link>http://www.larswilhelmsen.com/2009/03/31/syntax-highlighting-with-mgrammar/</link>
		<comments>http://www.larswilhelmsen.com/2009/03/31/syntax-highlighting-with-mgrammar/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 20:37:54 +0000</pubDate>
		<dc:creator>larsw</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Codename+Oslo]]></category>
		<category><![CDATA[MGrammar]]></category>
		<category><![CDATA[Syntax+Highlighting]]></category>

		<guid isPermaLink="false">http://larsw.wordpress.com/2009/03/31/syntax-highlighting-with-mgrammar/</guid>
		<description><![CDATA[Since I started exploring the possibilities of the various bits of codename “Oslo”, there has been one thing that has really annoyed me (and this is not Oslo’s fault). The lack of a decent tool to do syntax highlighting of M, MGrammar &#38; custom DSLs is vital to be able to communicate the intentions of [...]]]></description>
			<content:encoded><![CDATA[<p>Since I started exploring the possibilities of the various bits of codename “Oslo”, there has been one thing that has really annoyed me (and this is not Oslo’s fault). The lack of a decent tool to do syntax highlighting of <em>M</em>, <em>MGrammar</em> &amp; custom DSLs is vital to be able to communicate the intentions of a bit of source code when you blog about it.</p>
<p>Since I’ve been using the bits in <em>System.Dataflow</em> in a couple of projects now, I knew of the existence of the <em>Lexer</em> etc. in the assembly. I started to investigate further with .NET Reflector and found one class that seemed quite relevant for the tool I wanted to write; <em>System.Dataflow.LexerReader</em>. You initialize the <em>LexerReader</em> with a <em>ParserContext</em> and a stream of input data (typically the source code) and iterate over the tokens that the <em>Lexer</em> discover.</p>
<p>So, the basic requirements for the utility I wanted to create were:</p>
<ul>
<li>Take a compiled <em>MGrammar</em> (<em>Mgx</em>) as input. </li>
<li>Take a piece of source code that complies to the MGrammar as input. </li>
<li>Output a HTML fragment with syntax highlighted source code. </li>
</ul>
<p>Since the <em>MGrammar</em> language has a notion of attributes, and more specific; supports the <em>@{Classification}</em> attribute that lets the language developer classify/group the different tokens into <strong>Keywords</strong>, <strong>Literals</strong>, <strong>Strings</strong>, <strong>Numerics</strong> etc., I started digging into the System.Dataflow to hopefully find a mechanism to retrieve the metadata during the Lexing phase.</p>
<p>After some hours of intensive searching with .NET Reflector and the Visual Studio debugger, I found the solution; when you iterate over the <em>LexerReader</em> instance, you end up with <em>ParseTokenReference</em> instances that both describes the token and its content. It doesn’t contain the classification information directly, and that was the big puzzle I had to solve. It turned out that the <em>DynamicParser</em> instance, that I used to load up the <em>Mgx</em> file and build the <em>ParseContext</em> had a <em>GetTokenInfo()</em> method that took an integer as the only parameter; <em>tokenTag</em> – and the <em>ParseTokenReference</em> instance had a .Tag property. Bingo!</p>
<p>So, I’ve put together a small spike that I’m intending to clean up – it’s located <a href="http://github.com/larsw/larsw.msyntaxhighlighter/tree/master" target="_blank">here</a> at the moment and will be licensed under the Apache License.</p>
<p>Below is a sample output&#160; from the utility – the input is a MGrammar that I wrote for a answer to a thread in the Oslo/MSDN forum.</p>
<p>For the first version it will probably be a command line tool – but it would probably be a good idea to create both a ASP.NET frontend and a Windows Live Writer addin for it.</p>
<div style="font-family:consolas, courier, monospace;"><span class="Keyword">module</span> LarsW.Languages     <br /><span class="Delimiter">{</span>     <br />&#160;&#160;&#160; <span class="Keyword">language</span> nnnAuthLang     <br />&#160;&#160;&#160; <span class="Delimiter">{</span>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> Main = ar:AuthRule* =&gt; Rules <span class="Delimiter">{</span>&#160;<span class="Keyword">valuesof</span><span class="Delimiter">(</span>ar<span class="Delimiter">)</span>&#160;<span class="Delimiter">}</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> AuthRule = ad:AllowDeny av:AuthVerb     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; tOpenParen rl:RoleList tCloseParen tSemiColon     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; =&gt; AuthRule <span class="Delimiter">{</span> Type <span class="Delimiter">{</span>ad<span class="Delimiter">}</span>, AuthType<span class="Delimiter">{</span>av<span class="Delimiter">}</span>, Roles    <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Delimiter">{</span> <span class="Keyword">valuesof</span><span class="Delimiter">(</span>rl<span class="Delimiter">)</span><span class="Delimiter">}</span>&#160;<span class="Delimiter">}</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> RoleList = ri:RoleItem&#160; =&gt; List <span class="Delimiter">{</span> ri <span class="Delimiter">}</span>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; | ri:RoleItem tComma rl:RoleList     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; =&gt; List <span class="Delimiter">{</span> ri, <span class="Keyword">valuesof</span><span class="Delimiter">(</span>rl<span class="Delimiter">)</span>&#160;<span class="Delimiter">}</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> RoleItem = tRoleName;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> AllowDeny = a:tAllow =&gt; a     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; | d:tDeny =&gt; d;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">syntax</span> AuthVerb = tText;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tText = <span class="Delimiter">(</span><span class="Literal">&quot;a&quot;</span>..<span class="Literal">&quot;z&quot;</span>|<span class="Literal">&quot;A&quot;</span>..<span class="Literal">&quot;Z&quot;</span><span class="Delimiter">)</span>+;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; @{Classification<span class="Delimiter">[</span><span class="Literal">&quot;Keyword&quot;</span><span class="Delimiter">]</span><span class="Delimiter">}</span><span class="Keyword">token</span> tAllow = <span class="Literal">&quot;Allow&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; @{Classification<span class="Delimiter">[</span><span class="Literal">&quot;Keyword&quot;</span><span class="Delimiter">]</span><span class="Delimiter">}</span><span class="Keyword">token</span> tDeny = <span class="Literal">&quot;Deny&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tOpenParen = <span class="Literal">&quot;(&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tCloseParen = <span class="Literal">&quot;)&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tSemiColon = <span class="Literal">&quot;;&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tComma = <span class="Literal">&quot;,&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> Whitespace = <span class="Literal">&quot; &quot;</span>|<span class="Literal">&quot;\t&quot;</span>|<span class="Literal">&quot;\r&quot;</span>|<span class="Literal">&quot;\n&quot;</span>;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">token</span> tRoleName = Language.Grammar.TextLiteral;     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="Keyword">interleave</span> Skippable = Whitespace;     <br />&#160;&#160;&#160; <span class="Delimiter">}</span>     <br /><span class="Delimiter">}</span></div>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2flarswilhelmsen.com%2f2009%2f03%2f31%2fsyntax-highlighting-with-mgrammar%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2flarswilhelmsen.com%2f2009%2f03%2f31%2fsyntax-highlighting-with-mgrammar%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larswilhelmsen.com/2009/03/31/syntax-highlighting-with-mgrammar/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

