summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Miller <paul@paulmillr.com>2012-06-26 18:40:19 +0300
committerPaul Miller <paul@paulmillr.com>2012-06-26 18:40:19 +0300
commit6ed33835b12d715c4dcb6c8a0e6670c6ada34c86 (patch)
tree9699e53fd8677b42992a6cf48ddcd9e9aa4865ea
parent3a1c374c00e9953856e88d51c5840bda4ad9b95a (diff)
downloadpygments-6ed33835b12d715c4dcb6c8a0e6670c6ada34c86.tar.gz
Add LiveScript support and fix coffeescript.
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/web.py112
-rw-r--r--tests/examplefiles/livescript-demo.ls35
3 files changed, 145 insertions, 3 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 8bcc1744..47085cc5 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -136,6 +136,7 @@ LEXERS = {
'JuliaConsoleLexer': ('pygments.lexers.math', 'Julia console', ('jlcon',), (), ()),
'LighttpdConfLexer': ('pygments.lexers.text', 'Lighttpd configuration file', ('lighty', 'lighttpd'), (), ('text/x-lighttpd-conf',)),
'LiterateHaskellLexer': ('pygments.lexers.functional', 'Literate Haskell', ('lhs', 'literate-haskell'), ('*.lhs',), ('text/x-literate-haskell',)),
+ 'LiveScriptLexer': ('pygments.lexers.web', 'LiveScript', ('livescript',), ('*.ls',), ('text/livescript',)),
'LlvmLexer': ('pygments.lexers.asm', 'LLVM', ('llvm',), ('*.ll',), ('text/x-llvm',)),
'LogtalkLexer': ('pygments.lexers.other', 'Logtalk', ('logtalk',), ('*.lgt',), ('text/x-logtalk',)),
'LuaLexer': ('pygments.lexers.agile', 'Lua', ('lua',), ('*.lua', '*.wlua'), ('text/x-lua', 'application/x-lua')),
diff --git a/pygments/lexers/web.py b/pygments/lexers/web.py
index 6b788e82..64827f61 100644
--- a/pygments/lexers/web.py
+++ b/pygments/lexers/web.py
@@ -25,8 +25,9 @@ from pygments.lexers.compiled import ScalaLexer
__all__ = ['HtmlLexer', 'XmlLexer', 'JavascriptLexer', 'JSONLexer', 'CssLexer',
'PhpLexer', 'ActionScriptLexer', 'XsltLexer', 'ActionScript3Lexer',
'MxmlLexer', 'HaxeLexer', 'HamlLexer', 'SassLexer', 'ScssLexer',
- 'ObjectiveJLexer', 'CoffeeScriptLexer', 'DuelLexer', 'ScamlLexer',
- 'JadeLexer', 'XQueryLexer', 'DtdLexer', 'DartLexer']
+ 'ObjectiveJLexer', 'CoffeeScriptLexer', 'LiveScriptLexer',
+ 'DuelLexer', 'ScamlLexer', 'JadeLexer', 'XQueryLexer',
+ 'DtdLexer', 'DartLexer']
class JavascriptLexer(RegexLexer):
@@ -1823,7 +1824,8 @@ class CoffeeScriptLexer(RegexLexer):
(r'\([^()]*\)\s*->', Name.Function),
(r'[{(\[;,]', Punctuation, 'slashstartsregex'),
(r'[})\].]', Punctuation),
- (r'(for|in|of|while|break|return|continue|switch|when|then|if|else|'
+ (r'(for|own|in|of|while|until|loop|break|return|continue|'
+ r'switch|when|then|if|unless|else|'
r'throw|try|catch|finally|new|delete|typeof|instanceof|super|'
r'extends|this|class|by)\b', Keyword, 'slashstartsregex'),
(r'(true|false|yes|no|on|off|null|NaN|Infinity|undefined)\b',
@@ -1880,6 +1882,110 @@ class CoffeeScriptLexer(RegexLexer):
],
}
+
+class LiveScriptLexer(RegexLexer):
+ """
+ For `LiveScript`_ source code.
+
+ .. LiveScript: http://gkz.github.com/LiveScript/
+ """
+
+ name = 'LiveScript'
+ aliases = ['live-script', 'livescript']
+ filenames = ['*.ls']
+ mimetypes = ['text/livescript']
+
+ flags = re.DOTALL
+ tokens = {
+ 'commentsandwhitespace': [
+ (r'\s+', Text),
+ (r'/\*.*?\*/', Comment.Multiline),
+ (r'#.*?\n', Comment.Single),
+ ],
+ 'multilineregex': [
+ include('commentsandwhitespace'),
+ (r'//([gim]+\b|\B)', String.Regex, '#pop'),
+ (r'/', String.Regex),
+ (r'[^/#]+', String.Regex)
+ ],
+ 'slashstartsregex': [
+ include('commentsandwhitespace'),
+ (r'//', String.Regex, ('#pop', 'multilineregex')),
+ (r'/(?! )(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
+ r'([gim]+\b|\B)', String.Regex, '#pop'),
+ (r'', Text, '#pop'),
+ ],
+ 'root': [
+ # this next expr leads to infinite loops root -> slashstartsregex
+ #(r'^(?=\s|/|<!--)', Text, 'slashstartsregex'),
+ include('commentsandwhitespace'),
+ (r'\+\+|--|~|&&|\band\b|\bor\b|\bis\b|\bisnt\b|\bnot\b|\?|:|=|'
+ r'\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*`%&\|\^/])=?',
+ Operator, 'slashstartsregex'),
+ (r'\([^()]*\)\s*->', Name.Function),
+ (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
+ (r'[})\].]', Punctuation),
+ (r'(for|own|in|of|while|until|loop|break|return|continue|'
+ r'switch|when|then|if|unless|else|'
+ r'throw|try|catch|finally|new|delete|typeof|instanceof|super|'
+ r'extends|this|class|by|const|var|to|til)\b', Keyword,
+ 'slashstartsregex'),
+ (r'(true|false|yes|no|on|off|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'eval|isFinite|isNaN|parseFloat|parseInt|document|window)\b',
+ Name.Builtin),
+ (r'[$a-zA-Z_][a-zA-Z0-9_\.\-:]*\s*[:=]\s', Name.Variable,
+ 'slashstartsregex'),
+ (r'@[$a-zA-Z_][a-zA-Z0-9_\.\-:]*\s*[:=]\s', Name.Variable.Instance,
+ 'slashstartsregex'),
+ (r'@', Name.Other, 'slashstartsregex'),
+ (r'@?[$a-zA-Z_][a-zA-Z0-9_\-]*', Name.Other, 'slashstartsregex'),
+ (r'[0-9]+\.[0-9]+([eE][0-9]+)?[fd]?(?:[a-zA-Z_]+)?', Number.Float),
+ (r'0x[0-9a-fA-F]+([a-zA-Z_]+)?', Number.Hex),
+ (r'[0-9]+(?:[a-zA-Z_]+)?', Number.Integer),
+ ('"""', String, 'tdqs'),
+ ("'''", String, 'tsqs'),
+ ('"', String, 'dqs'),
+ ("'", String, 'sqs'),
+ (r'\\[\w$-]+', String),
+ ],
+ 'strings': [
+ (r'[^#\\\'"]+', String),
+ # note that all coffee script strings are multi-line.
+ # hashmarks, quotes and backslashes must be parsed one at a time
+ ],
+ 'interpoling_string' : [
+ (r'}', String.Interpol, "#pop"),
+ include('root')
+ ],
+ 'dqs': [
+ (r'"', String, '#pop'),
+ (r'\\.|\'', String), # double-quoted string don't need ' escapes
+ (r'#{', String.Interpol, "interpoling_string"),
+ include('strings')
+ ],
+ 'sqs': [
+ (r"'", String, '#pop'),
+ (r'#|\\.|"', String), # single quoted strings don't need " escapses
+ include('strings')
+ ],
+ 'tdqs': [
+ (r'"""', String, '#pop'),
+ (r'\\.|\'|"', String), # no need to escape quotes in triple-string
+ (r'#{', String.Interpol, "interpoling_string"),
+ include('strings'),
+ ],
+ 'tsqs': [
+ (r"'''", String, '#pop'),
+ (r'#|\\.|\'|"', String), # no need to escape quotes in triple-strings
+ include('strings')
+ ],
+ }
+
+
class DuelLexer(RegexLexer):
"""
Lexer for Duel Views Engine (formerly JBST) markup with JavaScript code blocks.
diff --git a/tests/examplefiles/livescript-demo.ls b/tests/examplefiles/livescript-demo.ls
new file mode 100644
index 00000000..22a58628
--- /dev/null
+++ b/tests/examplefiles/livescript-demo.ls
@@ -0,0 +1,35 @@
+a = -> 1
+const b = --> 2
+var c = ~> 3
+d = ~~> 10_000_000km * 500ms
+e = (a) -> (b) ~> (c) --> (d, e) ~~> 5
+dashes-identifiers = ->
+ a - a
+ b -- c
+ 1-1 1- -1
+ a- a
+ a -a
+underscores_i$d = ->
+/regexp1/ and //regexp2//g
+'strings' and "strings" and \strings
+([2 til 10] or [1 to 50])
+ |> map (* 2)
+ |> filter (> 5)
+ |> fold (+)
+
+class Class extends Anc-est-or
+ (args) ->
+
+copy = (from, to, callback) -->
+ error, data <- read file
+ return callback error if error?
+ error <~ write file, data
+ return callback error if error?
+ callback()
+
+->
+~>
+~~>
+-->
+# Comment
+/* Comment */