Commits

Paul Miller  committed 2dbd4d2

Add LiveScript support and fix coffeescript.

  • Participants
  • Parent commits f450362

Comments (0)

Files changed (3)

File pygments/lexers/_mapping.py

     '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')),

File pygments/lexers/web.py

 __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):
             (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',
         ],
     }
 
+
+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.

File tests/examplefiles/livescript-demo.ls

+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 */