Skip to content

Instantly share code, notes, and snippets.

@zzamboni
Last active November 25, 2018 14:30
Show Gist options
  • Save zzamboni/571230 to your computer and use it in GitHub Desktop.
Save zzamboni/571230 to your computer and use it in GitHub Desktop.
Lexer for cfengine3 policy files, for use with Pygments
# -*- coding: utf-8 -*-
"""
pygments.lexers.cfengine
~~~~~~~~~~~~~~~~~~~~~
Lexer for cfengine3 policy files. Should probably be incorporated into other.py
Written by Diego Zamboni <diego@zzamboni.org>
"""
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),
],
}
@zzamboni
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment