diff options
author | Tom Stuart <tom@experthuman.com> | 2012-11-11 01:01:45 +0000 |
---|---|---|
committer | Tom Stuart <tom@experthuman.com> | 2012-11-11 01:01:45 +0000 |
commit | e2bfc76c1dc5fec4e290612f76b50a6707505b91 (patch) | |
tree | 5dbd708d4afb3c296e3185659edf05c95c7d78e6 /pygments | |
parent | bc1fea9cf1220e27625e0ec33c8ef9d81e8987dc (diff) | |
download | pygments-e2bfc76c1dc5fec4e290612f76b50a6707505b91.tar.gz |
Add Treetop lexer
Diffstat (limited to 'pygments')
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/parsers.py | 80 |
2 files changed, 80 insertions, 1 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 0eca2682..e3c45a47 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -253,6 +253,7 @@ LEXERS = { 'TeaTemplateLexer': ('pygments.lexers.templates', 'Tea', ('tea',), ('*.tea',), ('text/x-tea',)), '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',)), + 'TreetopLexer': ('pygments.lexers.parsers', 'Treetop', ('treetop',), ('*.treetop', '*.tt'), ()), 'UrbiscriptLexer': ('pygments.lexers.other', 'UrbiScript', ('urbiscript',), ('*.u',), ('application/x-urbiscript',)), 'VGLLexer': ('pygments.lexers.other', 'VGL', ('vgl',), ('*.rpf',), ()), 'ValaLexer': ('pygments.lexers.compiled', 'Vala', ('vala', 'vapi'), ('*.vala', '*.vapi'), ('text/x-vala',)), diff --git a/pygments/lexers/parsers.py b/pygments/lexers/parsers.py index 2b5f954f..70e07dc5 100644 --- a/pygments/lexers/parsers.py +++ b/pygments/lexers/parsers.py @@ -28,7 +28,8 @@ __all__ = ['RagelLexer', 'RagelEmbeddedLexer', 'RagelCLexer', 'RagelDLexer', 'AntlrPerlLexer', 'AntlrRubyLexer', 'AntlrCppLexer', #'AntlrCLexer', 'AntlrCSharpLexer', 'AntlrObjectiveCLexer', - 'AntlrJavaLexer', "AntlrActionScriptLexer"] + 'AntlrJavaLexer', "AntlrActionScriptLexer", + 'TreetopLexer'] class RagelLexer(RegexLexer): @@ -693,3 +694,80 @@ class AntlrActionScriptLexer(DelegatingLexer): def analyse_text(text): return AntlrLexer.analyse_text(text) and \ re.search(r'^\s*language\s*=\s*ActionScript\s*;', text, re.M) + +class TreetopBaseLexer(RegexLexer): + """ + A base lexer for `Treetop <http://treetop.rubyforge.org/>`_ grammars. + Not for direct use; use TreetopLexer instead. + """ + + tokens = { + 'root': [ + include('space'), + (r'require[ \t]+[^\n\r]+[\n\r]', Other), + (r'module\b', Keyword.Namespace, 'module'), + (r'grammar\b', Keyword, 'grammar'), + ], + 'module': [ + include('space'), + include('end'), + (r'module\b', Keyword, '#push'), + (r'grammar\b', Keyword, 'grammar'), + (r'[A-Z][A-Za-z_0-9]*(?:::[A-Z][A-Za-z_0-9]*)*', Name.Namespace), + ], + 'grammar': [ + include('space'), + include('end'), + (r'rule\b', Keyword, 'rule'), + (r'include\b', Keyword, 'include'), + (r'[A-Z][A-Za-z_0-9]*', Name), + ], + 'include': [ + include('space'), + (r'[A-Z][A-Za-z_0-9]*(?:::[A-Z][A-Za-z_0-9]*)*', Name.Class, '#pop'), + ], + 'rule': [ + include('space'), + include('end'), + (r'"(\\\\|\\"|[^"])*"', String.Double), + (r"'(\\\\|\\'|[^'])*'", String.Single), + (r'([A-Za-z_][A-Za-z_0-9]*)(:)', bygroups(Name.Label, Punctuation)), + (r'[A-Za-z_][A-Za-z_0-9]*', Name), + (r'[()]', Punctuation), + (r'[?+*/&!~]', Operator), + (r'\[(?:\\.|\[:\^?[a-z]+:\]|[^\\\]])+\]', String.Regex), + (r'([0-9]*)(\.\.)([0-9]*)', bygroups(Number.Integer, Operator, Number.Integer)), + (r'(<)([^>]+)(>)', bygroups(Punctuation, Name.Class, Punctuation)), + (r'{', Punctuation, 'inline_module'), + (r'\.', String.Regex), + ], + 'inline_module': [ + (r'{', Other, 'ruby'), + (r'}', Punctuation, '#pop'), + (r'[^{}]*', Other), + ], + 'ruby': [ + (r'{', Other, '#push'), + (r'}', Other, '#pop'), + (r'[^{}]*', Other), + ], + 'space': [ + (r'[ \t\n\r]+', Whitespace), + (r'#[^\n]*', Comment.Single), + ], + 'end': [ + (r'end\b', Keyword, '#pop'), + ], + } + +class TreetopLexer(DelegatingLexer): + """ + A lexer for `Treetop <http://treetop.rubyforge.org/>`_ grammars. + """ + + name = 'Treetop' + aliases = ['treetop'] + filenames = ['*.treetop', '*.tt'] + + def __init__(self, **options): + super(TreetopLexer, self).__init__(RubyLexer, TreetopBaseLexer, **options) |