diff options
author | Georg Brandl <georg@python.org> | 2013-01-09 15:34:02 +0100 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2013-01-09 15:34:02 +0100 |
commit | c6ecb6ea2d522d79ef092ed7fd3c05327d283eec (patch) | |
tree | 55273eb185075f8b1656650c59e8809f01859f52 | |
parent | 77f09a6cbff2c43e44c4d30330b4d696a1296823 (diff) | |
parent | 3fd41c691d82557537a4337b0cd0821080bea4dc (diff) | |
download | pygments-c6ecb6ea2d522d79ef092ed7fd3c05327d283eec.tar.gz |
Merged in agilbert/pygments-main/typescript (pull request #114: Add TypeScript Lexer)
-rw-r--r-- | AUTHORS | 3 | ||||
-rw-r--r-- | CHANGES | 1 | ||||
-rw-r--r-- | pygments/lexers/_mapping.py | 1 | ||||
-rw-r--r-- | pygments/lexers/web.py | 78 | ||||
-rw-r--r-- | tests/examplefiles/example.ts | 28 |
5 files changed, 109 insertions, 2 deletions
@@ -37,17 +37,18 @@ Other contributors, listed alphabetically, are: * Naveen Garg -- Autohotkey lexer * Laurent Gautier -- R/S lexer * Alex Gaynor -- PyPy log lexer +* Alain Gilbert -- TypeScript lexer * Bertrand Goetzmann -- Groovy lexer * Krzysiek Goj -- Scala lexer * Matt Good -- Genshi, Cheetah lexers * Patrick Gotthardt -- PHP namespaces support * Olivier Guibe -- Asymptote lexer +* Jordi Gutiérrez Hermoso -- Octave lexer * Martin Harriman -- SNOBOL lexer * Matthew Harrison -- SVG formatter * Steven Hazel -- Tcl lexer * Aslak Hellesøy -- Gherkin lexer * Greg Hendershott -- Racket lexer -* Jordi Gutiérrez Hermoso -- Octave lexer * David Hess, Fish Software, Inc. -- Objective-J lexer * Varun Hiremath -- Debian control lexer * Doug Hogan -- Mscgen lexer @@ -38,6 +38,7 @@ Version 1.6 * SourcePawn (PR#39) * Stan (PR#89) * Treetop (PR#125) + * TypeScript (PR#114) * VGL (PR#12) * Windows Registry (#819) * Xtend (PR#68) diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py index 2d9149ef..8a3f0eb5 100644 --- a/pygments/lexers/_mapping.py +++ b/pygments/lexers/_mapping.py @@ -272,6 +272,7 @@ LEXERS = { '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'), ()), + 'TypeScriptLexer': ('pygments.lexers.web', 'TypeScript', ('ts',), ('*.ts',), ('text/x-typescript',)), '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/web.py b/pygments/lexers/web.py index 6df65131..dedd82d9 100644 --- a/pygments/lexers/web.py +++ b/pygments/lexers/web.py @@ -27,7 +27,7 @@ __all__ = ['HtmlLexer', 'XmlLexer', 'JavascriptLexer', 'JsonLexer', 'CssLexer', 'MxmlLexer', 'HaxeLexer', 'HamlLexer', 'SassLexer', 'ScssLexer', 'ObjectiveJLexer', 'CoffeeScriptLexer', 'LiveScriptLexer', 'DuelLexer', 'ScamlLexer', 'JadeLexer', 'XQueryLexer', - 'DtdLexer', 'DartLexer', 'LassoLexer', 'QmlLexer'] + 'DtdLexer', 'DartLexer', 'LassoLexer', 'QmlLexer', 'TypeScriptLexer'] class JavascriptLexer(RegexLexer): @@ -2988,6 +2988,82 @@ class DartLexer(RegexLexer): } +class TypeScriptLexer(RegexLexer): + """ + For `TypeScript <http://www.python.org>`_ source code. + + *New in Pygments 1.6.* + """ + + name = 'TypeScript' + aliases = ['ts'] + filenames = ['*.ts'] + mimetypes = ['text/x-typescript'] + + flags = re.DOTALL + tokens = { + 'commentsandwhitespace': [ + (r'\s+', Text), + (r'<!--', Comment), + (r'//.*?\n', Comment.Single), + (r'/\*.*?\*/', Comment.Multiline) + ], + 'slashstartsregex': [ + include('commentsandwhitespace'), + (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' + r'([gim]+\b|\B)', String.Regex, '#pop'), + (r'(?=/)', Text, ('#pop', 'badregex')), + (r'', Text, '#pop') + ], + 'badregex': [ + (r'\n', Text, '#pop') + ], + 'root': [ + (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'), + include('commentsandwhitespace'), + (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' + r'(<<|>>>?|==?|!=?|[-<>+*%&\|\^/])=?', Operator, 'slashstartsregex'), + (r'[{(\[;,]', Punctuation, 'slashstartsregex'), + (r'[})\].]', Punctuation), + (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|' + r'throw|try|catch|finally|new|delete|typeof|instanceof|void|' + r'this)\b', Keyword, 'slashstartsregex'), + (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'), + (r'(abstract|boolean|byte|char|class|const|debugger|double|enum|export|' + r'extends|final|float|goto|implements|import|int|interface|long|native|' + r'package|private|protected|public|short|static|super|synchronized|throws|' + r'transient|volatile)\b', Keyword.Reserved), + (r'(true|false|null|NaN|Infinity|undefined)\b', Keyword.Constant), + (r'(Array|Boolean|Date|Error|Function|Math|netscape|' + r'Number|Object|Packages|RegExp|String|sun|decodeURI|' + r'decodeURIComponent|encodeURI|encodeURIComponent|' + r'Error|eval|isFinite|isNaN|parseFloat|parseInt|document|this|' + r'window)\b', Name.Builtin), + # Match stuff like: module name {...} + (r'\b(module)(\s*)(\s*[a-zA-Z0-9_?.$][\w?.$]*)(\s*)', + bygroups(Keyword.Reserved, Text, Name.Other, Text), 'slashstartsregex'), + # Match variable type keywords + (r'\b(string|bool|number)\b', Keyword.Type), + # Match stuff like: constructor + (r'\b(constructor|declare|interface|as|AS)\b', Keyword.Reserved), + # Match stuff like: super(argument, list) + (r'(super)(\s*)\(([a-zA-Z0-9,_?.$\s]+\s*)\)', + bygroups(Keyword.Reserved, Text), 'slashstartsregex'), + # Match stuff like: function() {...} + (r'([a-zA-Z_?.$][\w?.$]*)\(\) \{', Name.Other, 'slashstartsregex'), + # Match stuff like: (function: return type) + (r'([a-zA-Z0-9_?.$][\w?.$]*)(\s*:\s*)([a-zA-Z0-9_?.$][\w?.$]*)', + bygroups(Name.Other, Text, Keyword.Type)), + (r'[$a-zA-Z_][a-zA-Z0-9_]*', Name.Other), + (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float), + (r'0x[0-9a-fA-F]+', Number.Hex), + (r'[0-9]+', Number.Integer), + (r'"(\\\\|\\"|[^"])*"', String.Double), + (r"'(\\\\|\\'|[^'])*'", String.Single), + ] + } + + class LassoLexer(RegexLexer): """ For `Lasso <http://www.lassosoft.com/>`_ source code, covering both Lasso 9 diff --git a/tests/examplefiles/example.ts b/tests/examplefiles/example.ts new file mode 100644 index 00000000..545c6cf5 --- /dev/null +++ b/tests/examplefiles/example.ts @@ -0,0 +1,28 @@ +class Animal { + constructor(public name) { } + move(meters) { + alert(this.name + " moved " + meters + "m."); + } +} + +class Snake extends Animal { + constructor(name) { super(name); } + move() { + alert("Slithering..."); + super.move(5); + } +} + +class Horse extends Animal { + constructor(name) { super(name); } + move() { + alert("Galloping..."); + super.move(45); + } +} + +var sam = new Snake("Sammy the Python") +var tom: Animal = new Horse("Tommy the Palomino") + +sam.move() +tom.move(34) |