Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The Spark grammar is implemented with a kind-of-fluent domain specific language. </p> <p>It's declared in a few layers. The rules which recognize the html syntax are declared in <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/Markup/MarkupGrammar.cs" rel="nofollow noreferrer">MarkupGrammar.cs</a> - those are based on grammar rules copied directly from the xml spec.</p> <p>The markup rules refer to a limited subset of csharp syntax rules declared in <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/Code/CodeGrammar.cs" rel="nofollow noreferrer">CodeGrammar.cs</a> - those are a subset because Spark only needs to recognize enough csharp to adjust single-quotes around strings to double-quotes, match curley braces, etc.</p> <p>The individual rules themselves are of type <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/ParseAction.cs" rel="nofollow noreferrer">ParseAction&lt;TValue&gt; delegate</a> which accept a <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/Position.cs" rel="nofollow noreferrer">Position</a> and return a <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/ParseResult.cs" rel="nofollow noreferrer">ParseResult</a>. The ParseResult is a simple class which contains the TValue data item parsed by the action and a new Position instance which has been advanced past the content which produced the TValue.</p> <p>That isn't very useful on it's own until you introduce a <a href="http://github.com/loudej/spark/blob/master/src/Spark/Parser/Position.cs" rel="nofollow noreferrer">small number of operators</a>, as described in <a href="http://en.wikipedia.org/wiki/Parsing_expression_grammar" rel="nofollow noreferrer">Parsing expression grammar</a>, which can combine single parse actions to build very detailed and robust expressions about the shape of different syntax constructs.</p> <p>The technique of using a delegate as a parse action came from a Luke H's blog post <a href="http://blogs.msdn.com/lukeh/archive/2007/08/19/monadic-parser-combinators-using-c-3-0.aspx" rel="nofollow noreferrer">Monadic Parser Combinators using C# 3.0</a>. I also wrote a post about <a href="http://whereslou.com/2008/05/15/creating-a-domain-specific-language-for-parsing" rel="nofollow noreferrer">Creating a Domain Specific Language for Parsing</a>.</p> <p>It's also entirely possible, if you like, to reference the Spark.dll assembly and inherit a class from the base CharGrammar to create an entirely new grammar for a particular syntax. It's probably the quickest way to start experimenting with this technique, and an example of that can be found in <a href="http://github.com/loudej/spark/blob/master/src/Spark.Tests/Parser/CharGrammarTester.cs" rel="nofollow noreferrer">CharGrammarTester.cs</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload