Last active
November 25, 2018 14:30
-
-
Save zzamboni/571230 to your computer and use it in GitHub Desktop.
Lexer for cfengine3 policy files, for use with Pygments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
pygments.lexers.cfengine | |
~~~~~~~~~~~~~~~~~~~~~ | |
Lexer for cfengine3 policy files. Should probably be incorporated into other.py | |
Written by Diego Zamboni <[email protected]> | |
""" | |
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 <http://cfengine.org>` 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|int|real|slist|ilist|rlist)(\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'->', Operator), | |
(r'\d+\.\d+', Number.Float), | |
(r'\d+', Number.Integer), | |
(r'\w+', Name.Function), | |
(r'\s+', Text), | |
], | |
'string': [ | |
(r'\$[\{\(]', String.Interpol, 'interpol'), | |
(r'\\.', String.Escape), | |
(r'"', String, '#pop'), | |
(r'\n', String), | |
(r'.', String), | |
], | |
'interpol': [ | |
(r'\$[\{\(]', String.Interpol, '#push'), | |
(r'[\}\)]', String.Interpol, '#pop'), | |
(r'[^\$\{\(\)\}]+', String.Interpol), | |
], | |
'arglist': [ | |
(r'\)', Punctuation, '#pop'), | |
(r',', Punctuation), | |
(r'\w+', Name.Variable), | |
(r'\s+', Text), | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See here for how to install it: http://blog.zzamboni.org/cfengine3-lexer-for-pygments