Forum: Cfengine Help
Subject: Cfengine syntax highlighting using Pygments
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,18139,18139#msg-18139
Hi,
I have written a cfengine lexer for Pygments, a very nice and very popular
open-source syntax-highlighting program. It behaves nicely on all the files I
have thrown at it, but please feel free to try it out and let me know if
there's anything for which it produces incorrect output.
I have posted the details on my blog, but I'm posting the code below as well
for posterity. You can view sample output here (this is using pygments' default
output style, which of course can be changed).
Please let me know what you think, and if you find it useful (it'd be cool if
the cfengine snippets on this forum could be syntax-highlighted).
# -*- coding: utf-8 -*-
"""
pygments.lexers.cfengine
~~~~~~~~~~~~~~~~~~~~~
Lexer for cfengine3 policy files. Should probably be incorporated into
other.py
Written by Diego Zamboni
"""
import re
from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
this, do_insertions
from pygments.token import Error, Punctuation, \
Text, Comment, Operator, Keyword, Name, String, Number, Generic
__all__ = ['Cfengine3Lexer']
class Cfengine3Lexer(RegexLexer):
"""
Lexer for `Cfengine3 ` policy files.
"""
name = 'Cfengine3'
aliases = ['cfengine3', 'cf3']
filenames = [ '*.cf' ]
mimetypes = []
tokens = {
'root': [
(r'#.*?\n', Comment),
(r'(body)(\s+)(\S+)(\s+)(control)',
bygroups(Keyword, Text, Keyword, Text, Keyword)),
(r'(body|bundle)(\s+)(\S+)(\s+)(\w+)(\()',
bygroups(Keyword, Text, Keyword, Text, Name.Function,
Punctuation), 'arglist'),
(r'(body|bundle)(\s+)(\S+)(\s+)(\w+)',
bygroups(Keyword, Text, Keyword, Text, Name.Function)),
(r'(")([^"]+)(")(\s+)(string|slist|int|real)(\s*)(=>)(\s*)',
bygroups(Punctuation,Name.Variable,Punctuation,
Text,Keyword.Type,Text,Operator,Text)),
(r'(\S+)(\s*)(=>)(\s*)',
bygroups(Keyword.Reserved,Text,Operator,Text)),
(r'"', String, 'string'),
(r'(\w+)(\()', bygroups(Name.Function, Punctuation)),
(r'([\w.!&|]+)(::)', bygroups(Name.Class, Punctuation)),
(r'(\w+)(:)', bygroups(Keyword.Declaration,Punctuation)),
(r'@[\{\(][^\)\}]+[\}\)]', Name.Variable),
(r'[(){},;]', Punctuation),
(r'=>', Operator),
(r'\d+\.\d+', Number.Float),
(r'\d+', Number.Integer),
(r'\w+', Name.Function),
(r'\s+', Text),
],
'string': [
(r'\$[\{\(][^\)\}]+[\}\)]', String.Interpol),
(r'\\.', String.Escape),
(r'"', String, '#pop'),
(r'.', String),
],
'arglist': [
(r'\)', Punctuation, '#pop'),
(r',', Punctuation),
(r'\w+', Name.Variable),
(r'\s+', Text),
],
}
_______________________________________________
Help-cfengine mailing list
[email protected]
https://cfengine.org/mailman/listinfo/help-cfengine