WorldMaker.netBlog2009February › 17

Code Snippet of the Moment: Inform 7 Lexer for Pygments

1 year, 6 months ago

This is a silly hack that turns out isn't entirely useful, but I'm going to post it to preserve it "just in case" and because I probably spent an hour too much on it. Basically, I've been playing around with writing interactive fiction in Inform 7. Inform 7 is a unique natural language-based approach to interactive fiction. Because of that is has a deceptively simple subset of highlight-able syntax, with comparison to most other programming or even interactive fiction description languages. I use Pygments as a common syntax highlighter in a number of situations, including and particularly for syntax highlighting of fragments I post to my blog. Thinking ahead to wanting to post snippets of my works to my blog I set about creating a simple lexer for Pygments.

Unfortunately, it isn't all that useful. Due to the unique nature of the language it is best expressed in a non-fixed-width font with "word wrapping", both of which are entirely unusual for syntax highlighting and don't have existing support in Pygments. I think my best bet will be to attempt to use Inform's existing HTML output or to hand optimize some reST-based solution.

Here's the lexer in case it might find some use further down the road:

from pygments.lexer import RegexLexer
from pygments.token import *
import re

I7_HEADINGS = ['Volume', 'Book', 'Part', 'Chapter', 'Section', 'Table']

class Inform7Lexer(RegexLexer):
    """
    Inform 7 is a natural language-based approach to buiding interactive
    fiction. Because of the English-based nature of the language there
    is little overt syntax in the classic sense that might be highlighted.
    """
    name='Inform 7'
    aliases=['I7']
    filenames=['*.inform', '*.i7x', '*.ni']
    flags=re.IGNORECASE | re.MULTILINE

    tokens = {
        'root': [
            (r'^"[^"]*" by ("[^"]*"|[\w ]+)$', Generic.Heading),
            (r'^(%s)[^\n]*$' % "|".join(I7_HEADINGS), Generic.Heading),
            (r'\[', Comment, "comment"),
            (r'"', String, "string ...
Colophon Copyright © 1999-2010 Max Battcher / WorldMaker. Some Rights Reserved.

With our thoughts, we make our worlds. Font Hosting by Kernest.com. I only buy the best Robots — All my Robots are GSD ServDev® Brand.