From 3ef97107148fa459158c01e095a763b6268caed2 Mon Sep 17 00:00:00 2001 From: Adrian Iain Lam Date: Tue, 3 Feb 2015 14:17:45 -0800 Subject: [PATCH 1/1] --- gtk.py | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 gtk.py diff --git a/gtk.py b/gtk.py new file mode 100644 index 0000000..2770808 --- /dev/null +++ b/gtk.py @@ -0,0 +1,189 @@ +# -*- coding: utf-8 -*- +""" + pygments.lexers.gtk + ~~~~~~~~~~~~~~~~~~~~~~~~ + + Lexers for GTK+ in C. + + :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. + + Modified by Adrian Iain Lam in 2015. +""" + +import re +from string import Template + +from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \ + this, combined, inherit, do_insertions +from pygments.util import get_bool_opt, get_list_opt +from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ + Number, Punctuation, Error, Literal, Generic +from pygments.scanner import Scanner + + +__all__ = ['GtkLexer'] + + +class CFamilyLexer(RegexLexer): + """ + For C family source code. This is used as a base class to avoid repetitious + definitions. + """ + + #: optional Comment or Whitespace + _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+' + #: only one /* */ style comment + _ws1 = r':\s*/[*].*?[*]/\s*' + + tokens = { + 'whitespace': [ + # preprocessor directives: without whitespace + ('^#if\s+0', Comment.Preproc, 'if0'), + ('^#', Comment.Preproc, 'macro'), + # or with whitespace + ('^(' + _ws1 + r')(#if\s+0)', + bygroups(using(this), Comment.Preproc), 'if0'), + ('^(' + _ws1 + ')(#)', + bygroups(using(this), Comment.Preproc), 'macro'), + (r'^(\s*)([a-zA-Z_][a-zA-Z0-9_]*:(?!:))', + bygroups(Text, Name.Label)), + (r'\n', Text), + (r'\s+', Text), + (r'\\\n', Text), # line continuation + (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single), + (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), + ], + 'statements': [ + (r'L?"', String, 'string'), + (r"L?'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char), + (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*', Number.Float), + (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), + (r'0x[0-9a-fA-F]+[LlUu]*', Number.Hex), + (r'0[0-7]+[LlUu]*', Number.Oct), + (r'\d+[LlUu]*', Number.Integer), + (r'\*/', Error), + (r'[~!%^&*+=|?:<>/-]', Operator), + (r'[()\[\],.]', Punctuation), + (r'\b(case)(.+?)(:)', bygroups(Keyword, using(this), Text)), + (r'(auto|break|case|const|continue|default|do|else|enum|extern|' + r'for|goto|if|register|restricted|return|sizeof|static|struct|' + r'switch|typedef|union|volatile|while)\b', Keyword), + (r'(bool|int|long|float|short|double|char|unsigned|signed|void|' + r'[a-z_][a-z0-9_]*_t)\b', + Keyword.Type), + (r'(_{0,2}inline|naked|restrict|thread|typename)\b', Keyword.Reserved), + # Vector intrinsics + (r'(__(m128i|m128d|m128|m64))\b', Keyword.Reserved), + # Microsoft-isms + (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|' + r'declspec|finally|int64|try|leave|wchar_t|w64|unaligned|' + r'raise|noop|identifier|forceinline|assume)\b', Keyword.Reserved), + (r'(TRUE|FALSE|NULL)\b', Name.Builtin), + ('[a-zA-Z_][a-zA-Z0-9_]*', Name), + ], + 'root': [ + include('whitespace'), + # functions + (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments + r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name + r'(\s*\([^;]*?\))' # signature + r'(' + _ws + r')?({)', + bygroups(using(this), Name.Function, using(this), using(this), + Punctuation), + 'function'), + # function declarations + (r'((?:[a-zA-Z0-9_*\s])+?(?:\s|[*]))' # return arguments + r'([a-zA-Z_][a-zA-Z0-9_]*)' # method name + r'(\s*\([^;]*?\))' # signature + r'(' + _ws + r')?(;)', + bygroups(using(this), Name.Function, using(this), using(this), + Punctuation)), + ('', Text, 'statement'), + ], + 'statement' : [ + include('whitespace'), + include('statements'), + ('[{}]', Punctuation), + (';', Punctuation, '#pop'), + ], + 'function': [ + include('whitespace'), + include('statements'), + (';', Punctuation), + ('{', Punctuation, '#push'), + ('}', Punctuation, '#pop'), + ], + 'string': [ + (r'"', String, '#pop'), + (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|' + r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape), + (r'[^\\"\n]+', String), # all other characters + (r'\\\n', String), # line continuation + (r'\\', String), # stray backslash + ], + 'macro': [ + (r'[^/\n]+', Comment.Preproc), + (r'/[*](.|\n)*?[*]/', Comment.Multiline), + (r'//.*?\n', Comment.Single, '#pop'), + (r'/', Comment.Preproc), + (r'(?<=\\)\n', Comment.Preproc), + (r'\n', Comment.Preproc, '#pop'), + ], + 'if0': [ + (r'^\s*#if.*?(?