/console/app/pygments/lexers/compiled.py
Python | 1251 lines | 1089 code | 53 blank | 109 comment | 89 complexity | c35a71fe239c7fc3bcf58b1e9b5976ea MD5 | raw file
Possible License(s): GPL-3.0
Large files files are truncated, but you can click here to view the full file
- # -*- coding: utf-8 -*-
- """
- pygments.lexers.compiled
- ~~~~~~~~~~~~~~~~~~~~~~~~
- Lexers for compiled languages.
- :copyright: 2006-2008 by Georg Brandl, Armin Ronacher, Christoph Hack,
- Whitney Young, Kirk McDonald, Stou Sandalski, Krzysiek Goj.
- :license: BSD, see LICENSE for more details.
- """
- import re
- try:
- set
- except NameError:
- from sets import Set as set
- from pygments.scanner import Scanner
- from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
- this
- from pygments.util import get_bool_opt, get_list_opt
- from pygments.token import \
- Text, Comment, Operator, Keyword, Name, String, Number, Punctuation, \
- Error
- # backwards compatibility
- from pygments.lexers.functional import OcamlLexer
- __all__ = ['CLexer', 'CppLexer', 'DLexer', 'DelphiLexer', 'JavaLexer', 'ScalaLexer',
- 'DylanLexer', 'OcamlLexer', 'ObjectiveCLexer', 'FortranLexer']
- class CLexer(RegexLexer):
- """
- For C source code with preprocessor directives.
- """
- name = 'C'
- aliases = ['c']
- filenames = ['*.c', '*.h']
- mimetypes = ['text/x-chdr', 'text/x-csrc']
- #: optional Comment or Whitespace
- _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)+'
- tokens = {
- 'whitespace': [
- (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
- (r'^\s*#', Comment.Preproc, 'macro'),
- (r'\n', Text),
- (r'\s+', Text),
- (r'\\\n', Text), # line continuation
- (r'//(\n|(.|\n)*?[^\\]\n)', Comment),
- (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment),
- ],
- '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+[lL]?', Number.Float),
- (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
- (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
- (r'0[0-7]+[Ll]?', Number.Oct),
- (r'\d+[Ll]?', Number.Integer),
- (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|virtual|while)\b', Keyword),
- (r'(int|long|float|short|double|char|unsigned|signed|void)\b',
- Keyword.Type),
- (r'(_{0,2}inline|naked|restrict|thread|typename)\b', Keyword.Reserved),
- (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
- r'declspec|finally|int64|try|leave)\b', Keyword.Reserved),
- (r'(true|false|NULL)\b', Name.Builtin),
- ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label),
- ('[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}|[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),
- (r'//.*?\n', Comment, '#pop'),
- (r'/', Comment.Preproc),
- (r'(?<=\\)\n', Comment.Preproc),
- (r'\n', Comment.Preproc, '#pop'),
- ],
- 'if0': [
- (r'^\s*#if.*?(?<!\\)\n', Comment, '#push'),
- (r'^\s*#el(?:se|if).*\n', Comment.Preproc, '#pop'),
- (r'^\s*#endif.*?(?<!\\)\n', Comment, '#pop'),
- (r'.*?\n', Comment),
- ]
- }
- stdlib_types = ['size_t', 'ssize_t', 'off_t', 'wchar_t', 'ptrdiff_t',
- 'sig_atomic_t', 'fpos_t', 'clock_t', 'time_t', 'va_list',
- 'jmp_buf', 'FILE', 'DIR', 'div_t', 'ldiv_t', 'mbstate_t',
- 'wctrans_t', 'wint_t', 'wctype_t']
- c99_types = ['_Bool', '_Complex', 'int8_t', 'int16_t', 'int32_t', 'int64_t',
- 'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int_least8_t',
- 'int_least16_t', 'int_least32_t', 'int_least64_t',
- 'uint_least8_t', 'uint_least16_t', 'uint_least32_t',
- 'uint_least64_t', 'int_fast8_t', 'int_fast16_t', 'int_fast32_t',
- 'int_fast64_t', 'uint_fast8_t', 'uint_fast16_t', 'uint_fast32_t',
- 'uint_fast64_t', 'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t']
- def __init__(self, **options):
- self.stdlibhighlighting = get_bool_opt(options,
- 'stdlibhighlighting', True)
- self.c99highlighting = get_bool_opt(options,
- 'c99highlighting', True)
- RegexLexer.__init__(self, **options)
- def get_tokens_unprocessed(self, text):
- for index, token, value in \
- RegexLexer.get_tokens_unprocessed(self, text):
- if token is Name:
- if self.stdlibhighlighting and value in self.stdlib_types:
- token = Keyword.Type
- elif self.c99highlighting and value in self.c99_types:
- token = Keyword.Type
- yield index, token, value
- class CppLexer(RegexLexer):
- """
- For C++ source code with preprocessor directives.
- """
- name = 'C++'
- aliases = ['cpp', 'c++']
- filenames = ['*.cpp', '*.hpp', '*.c++', '*.h++', '*.cc', '*.hh', '*.cxx', '*.hxx']
- mimetypes = ['text/x-c++hdr', 'text/x-c++src']
- tokens = {
- 'root': [
- (r'^\s*#if\s+0', Comment.Preproc, 'if0'),
- (r'^\s*#', Comment.Preproc, 'macro'),
- (r'\n', Text),
- (r'\s+', Text),
- (r'\\\n', Text), # line continuation
- (r'/(\\\n)?/(\n|(.|\n)*?[^\\]\n)', Comment),
- (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment),
- (r'[{}]', Punctuation),
- (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+[lL]?', Number.Float),
- (r'(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float),
- (r'0x[0-9a-fA-F]+[Ll]?', Number.Hex),
- (r'0[0-7]+[Ll]?', Number.Oct),
- (r'\d+[Ll]?', Number.Integer),
- (r'[~!%^&*+=|?:<>/-]', Operator),
- (r'[()\[\],.;]', Punctuation),
- (r'(asm|auto|break|case|catch|const|const_cast|continue|'
- r'default|delete|do|dynamic_cast|else|enum|explicit|export|'
- r'extern|for|friend|goto|if|mutable|namespace|new|operator|'
- r'private|protected|public|register|reinterpret_cast|return|'
- r'restrict|sizeof|static|static_cast|struct|switch|template|'
- r'this|throw|throws|try|typedef|typeid|typename|union|using|'
- r'volatile|virtual|while)\b', Keyword),
- (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
- (r'(bool|int|long|float|short|double|char|unsigned|signed|'
- r'void|wchar_t)\b', Keyword.Type),
- (r'(_{0,2}inline|naked|thread)\b', Keyword.Reserved),
- (r'__(asm|int8|based|except|int16|stdcall|cdecl|fastcall|int32|'
- r'declspec|finally|int64|try|leave|wchar_t|w64|virtual_inheritance|'
- r'uuidof|unaligned|super|single_inheritance|raise|noop|'
- r'multiple_inheritance|m128i|m128d|m128|m64|interface|'
- r'identifier|forceinline|event|assume)\b', Keyword.Reserved),
- (r'(true|false)\b', Keyword.Constant),
- (r'NULL\b', Name.Builtin),
- ('[a-zA-Z_][a-zA-Z0-9_]*:(?!:)', Name.Label),
- ('[a-zA-Z_][a-zA-Z0-9_]*', Name),
- ],
- 'classname': [
- (r'[a-zA-Z_][a-zA-Z0-9_]*', Name.Class, '#pop'),
- # template specification
- (r'\s*(?=>)', Text, '#pop'),
- ],
- 'string': [
- (r'"', String, '#pop'),
- (r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|[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),
- (r'//.*?\n', Comment, '#pop'),
- (r'/', Comment.Preproc),
- (r'(?<=\\)\n', Comment.Preproc),
- (r'\n', Comment.Preproc, '#pop'),
- ],
- 'if0': [
- (r'^\s*#if.*?(?<!\\)\n', Comment, '#push'),
- (r'^\s*#endif.*?(?<!\\)\n', Comment, '#pop'),
- (r'.*?\n', Comment),
- ]
- }
- class DLexer(RegexLexer):
- """
- For D source.
- """
- name = 'D'
- filenames = ['*.d', '*.di']
- aliases = ['d']
- mimetypes = ['text/x-dsrc']