From 8bbf4e73e62ccb29a61e4b51ef1a9fada9fefbd5 Mon Sep 17 00:00:00 2001 From: sah Date: Sat, 1 Mar 2008 19:01:51 +0100 Subject: added Tcl lexer --- pygments/lexers/_mapping.py | 1 + pygments/lexers/agile.py | 133 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 131 insertions(+), 3 deletions(-) (limited to 'pygments') diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 3691377c..ec5df71a 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -103,6 +103,7 @@ LEXERS = { 'SourcesListLexer': ('pygments.lexers.text', 'Debian Sourcelist', ('sourceslist', 'sources.list'), ('sources.list',), ()), 'SqlLexer': ('pygments.lexers.other', 'SQL', ('sql',), ('*.sql',), ('text/x-sql',)), 'SquidConfLexer': ('pygments.lexers.text', 'SquidConf', ('squidconf', 'squid.conf', 'squid'), ('squid.conf',), ('text/x-squidconf',)), + 'TclLexer': ('pygments.lexers.agile', 'Tcl', ('tcl',), ('*.tcl',), ('text/x-tcl', 'text/x-script.tcl', 'application/x-tcl')), 'TexLexer': ('pygments.lexers.text', 'TeX', ('tex', 'latex'), ('*.tex', '*.aux', '*.toc'), ('text/x-tex', 'text/x-latex')), 'TextLexer': ('pygments.lexers.special', 'Text only', ('text',), ('*.txt',), ('text/plain',)), 'VbNetLexer': ('pygments.lexers.dotnet', 'VB.net', ('vb.net', 'vbnet'), ('*.vb', '*.bas'), ('text/x-vbnet', 'text/x-vba')), diff --git a/pygments/lexers/agile.py b/pygments/lexers/agile.py index 30adea9a..ecee4295 100644 --- a/pygments/lexers/agile.py +++ b/pygments/lexers/agile.py @@ -5,9 +5,9 @@ Lexers for agile languages. - :copyright: 2006-2007 by Georg Brandl, Armin Ronacher, + :copyright: 2006-2008 by Georg Brandl, Armin Ronacher, Lukas Meuser, Tim Hatch, Jarrett Billingsley, - Tassilo Schweyer. + Tassilo Schweyer, Steven Hazel. :license: BSD, see LICENSE for more details. """ @@ -26,7 +26,7 @@ from pygments.util import get_bool_opt, get_list_opt, shebang_matches __all__ = ['PythonLexer', 'PythonConsoleLexer', 'PythonTracebackLexer', 'RubyLexer', 'RubyConsoleLexer', 'PerlLexer', 'LuaLexer', - 'MiniDLexer'] + 'MiniDLexer', 'TclLexer'] # b/w compatibility from pygments.lexers.functional import SchemeLexer @@ -961,3 +961,130 @@ class MiniDLexer(RegexLexer): (r'[+/]', Comment), ], } + + +class TclLexer(RegexLexer): + """ + For Tcl source code. + """ + + keyword_cmds_re = ( + r'\b(after|apply|array|break|catch|continue|else|elseif|error|' + r'eval|expr|for|foreach|global|if|namespace|proc|rename|return|' + r'set|switch|then|trace|unset|update|uplevel|upvar|variable|' + r'vwait|while)' + ) + + builtin_cmds_re = ( + r'\b(append|bgerror|binary|cd|chan|clock|close|concat|dde|dict|' + r'encoding|eof|exec|exit|fblocked|fconfigure|fcopy|file|' + r'fileevent|flush|format|gets|glob|history|http|incr|info|interp|' + r'join|lappend|lassign|lindex|linsert|list|llength|load|loadTk|' + r'lrange|lrepeat|lreplace|lreverse|lsearch|lset|lsort|mathfunc|' + r'mathop|memory|msgcat|open|package|pid|pkg::create|pkg_mkIndex|' + r'platform|platform::shell|puts|pwd|re_syntax|read|refchan|' + r'regexp|registry|regsub|scan|seek|socket|source|split|string|' + r'subst|tell|time|tm|unknown|unload)' + ) + + name = 'Tcl' + aliases = ['tcl'] + filenames = ['*.tcl'] + mimetypes = ['text/x-tcl', 'text/x-script.tcl', 'application/x-tcl'] + + def _gen_command_rules(keyword_cmds_re, builtin_cmds_re, context=""): + return [ + (keyword_cmds_re, Keyword, 'params' + context), + (builtin_cmds_re, Name.Builtin, 'params' + context), + (r'([\w\.\-]+)', Name.Variable, 'params' + context), + (r'#', Comment, 'comment'), + ] + + tokens = { + 'root': [ + include('command'), + include('basic'), + include('data'), + ], + 'command': _gen_command_rules(keyword_cmds_re, builtin_cmds_re), + 'command-in-brace': _gen_command_rules(keyword_cmds_re, + builtin_cmds_re, + "-in-brace"), + 'command-in-bracket': _gen_command_rules(keyword_cmds_re, + builtin_cmds_re, + "-in-bracket"), + 'command-in-paren': _gen_command_rules(keyword_cmds_re, + builtin_cmds_re, + "-in-paren"), + 'basic': [ + (r'\(', Keyword, 'paren'), + (r'\[', Keyword, 'bracket'), + (r'\{', Keyword, 'brace'), + (r'"', String.Double, 'string'), + (r'(eq|ne|in|ni)\b', Operator.Word), + (r'!=|==|<<|>>|<=|>=|&&|\|\||\*\*|[-+~!*/%<>&^|?:]', Operator), + ], + 'data': [ + (r'\s+', Text), + (r'0x[a-fA-F0-9]+', Number.Hex), + (r'0[0-7]+', Number.Oct), + (r'\d+\.\d+', Number.Float), + (r'\d+', Number.Integer), + (r'\$([\w\.\-\:]+)', Name.Variable), + (r'([\w\.\-\:]+)', Text), + ], + 'params': [ + (r';', Keyword, '#pop'), + (r'\n', Text, '#pop'), + (r'(else|elseif|then)', Keyword), + include('basic'), + include('data'), + ], + 'params-in-brace': [ + (r'}', Keyword, ('#pop', '#pop')), + include('params') + ], + 'params-in-paren': [ + (r'\)', Keyword, ('#pop', '#pop')), + include('params') + ], + 'params-in-bracket': [ + (r'\]', Keyword, ('#pop', '#pop')), + include('params') + ], + 'string': [ + (r'\[', String.Double, 'string-square'), + (r'(\\\\|\\[0-7]+|\\.|[^"])', String.Double), + (r'"', String.Double, '#pop') + ], + 'string-square': [ + (r'\[', String.Double, 'string-square'), + (r'(\\\\|\\[0-7]+|\\.|[^\]])', String.Double), + (r'\]', String.Double, '#pop') + ], + 'brace': [ + (r'}', Keyword, '#pop'), + include('command-in-brace'), + include('basic'), + include('data'), + ], + 'paren': [ + (r'\)', Keyword, '#pop'), + include('command-in-paren'), + include('basic'), + include('data'), + ], + 'bracket': [ + (r'\]', Keyword, '#pop'), + include('command-in-bracket'), + include('basic'), + include('data'), + ], + 'comment': [ + (r'.*[^\\]\n', Comment, '#pop'), + (r'.*\\\n', Comment), + ], + } + + def analyse_text(text): + return shebang_matches(text, r'(tcl)') -- cgit v1.2.1