Parsing Comments with Sprache

6 February 2015 1 Minute History

I recently made a comment parser for the Sprache framework, and I wanted to give a basic run-down on how it works.

hero

This is a post in my Past Articles Series.

Several years before I started blogging at ian.wold.guru, I maintained a site with a CV and a small number of articles. I've preserved those articles on this site in this series.

I recently made a comment parser for the Sprache framework, and I wanted to give a basic run-down on how it works.

The CommentParser class gives you the option to define the header styles for comments, and it can parse both single- and multi-line comments. It's rather basic as of right now, but that's (hopefully) subject to change in the future.

Using CommentParser is pretty simple, but it's a tad different from the rest of the flow of Sprache as a combinator library. You'll need to make an instance of the CommentParser class, using the comment headers and (optional) newline character you require as arguments:

static CommentParser comments = new CommentParser("//", "/*", "*/");

From there, CommentParser gives you a couple parsers you can use to parse single- and multi-line comments:

static Parser<string> myParser = Parse.String("foobar").Text().Or(comments.AnyComment);

CommentParser.AnyComment will parse either single- or multi-line comments for you, while CommentParser.SingleLineComment and CommentParser.MultiLineComment will parse those individually.

A real, working example using the CommentParser class can be found in Sprache's XMLParser example.

In the future, it would be awesome if multiple comment headers could be included, and if whitespace could be defined to include comments. Some work towards this effort has been done on my GitHub here.

Hi, I'm Ian

I'm a software engineer, architect, and team leader in Minneapolis. My career has largely focused on .NET and web technologies, spread across several industries. Currently I'm working for Crate & Barrel on their ecommerce solutions. You can find me on this blog, contributing to open source repositories, and at conferences around the Midwest.


If you'd like to keep up with me, please subscribe to my book club or RSS feed. If you'd like to help me out with server costs, I would be forever grateful if you bought me a coffee!


Some other posts you might be interested in: