From f7a006d85e3553de6c5852e1fbb70fd923510603 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Fri, 19 Sep 2014 23:53:18 +0200 Subject: split up web lexers --- pygments/lexers/javascript.py | 1192 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1192 insertions(+) create mode 100644 pygments/lexers/javascript.py (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py new file mode 100644 index 00000000..6e4e07e8 --- /dev/null +++ b/pygments/lexers/javascript.py @@ -0,0 +1,1192 @@ +# -*- coding: utf-8 -*- +""" + pygments.lexers.javascript + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + + Lexers for JavaScript and related languages. + + :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +import re + +from pygments.lexer import RegexLexer, include, bygroups, default, \ + using, this +from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ + Number, Punctuation, Other +from pygments.util import get_bool_opt, iteritems + +__all__ = ['JavascriptLexer', 'KalLexer', 'LiveScriptLexer', 'DartLexer', + 'TypeScriptLexer', 'LassoLexer', 'ObjectiveJLexer', + 'CoffeeScriptLexer', 'MaskLexer'] + + +class JavascriptLexer(RegexLexer): + """ + For JavaScript source code. + """ + + name = 'JavaScript' + aliases = ['js', 'javascript'] + filenames = ['*.js', ] + mimetypes = ['application/javascript', 'application/x-javascript', + 'text/x-javascript', 'text/javascript', ] + + flags = re.DOTALL + tokens = { + 'commentsandwhitespace': [ + (r'\s+', Text), + (r')?', Other, 'delimiters'), + (r'\s+', Other), + default(('delimiters', 'lassofile')), + ], + 'delimiters': [ + (r'\[no_square_brackets\]', Comment.Preproc, 'nosquarebrackets'), + (r'\[noprocess\]', Comment.Preproc, 'noprocess'), + (r'\[', Comment.Preproc, 'squarebrackets'), + (r'<\?(LassoScript|lasso|=)', Comment.Preproc, 'anglebrackets'), + (r'<(!--.*?-->)?', Other), + (r'[^[<]+', Other), + ], + 'nosquarebrackets': [ + (r'<\?(LassoScript|lasso|=)', Comment.Preproc, 'anglebrackets'), + (r'<', Other), + (r'[^<]+', Other), + ], + 'noprocess': [ + (r'\[/noprocess\]', Comment.Preproc, '#pop'), + (r'\[', Other), + (r'[^[]', Other), + ], + 'squarebrackets': [ + (r'\]', Comment.Preproc, '#pop'), + include('lasso'), + ], + 'anglebrackets': [ + (r'\?>', Comment.Preproc, '#pop'), + include('lasso'), + ], + 'lassofile': [ + (r'\]|\?>', Comment.Preproc, '#pop'), + include('lasso'), + ], + 'whitespacecomments': [ + (r'\s+', Text), + (r'//.*?\n', Comment.Single), + (r'/\*\*!.*?\*/', String.Doc), + (r'/\*.*?\*/', Comment.Multiline), + ], + 'lasso': [ + # whitespace/comments + include('whitespacecomments'), + + # literals + (r'\d*\.\d+(e[+-]?\d+)?', Number.Float), + (r'0x[\da-f]+', Number.Hex), + (r'\d+', Number.Integer), + (r'([+-]?)(infinity|NaN)\b', bygroups(Operator, Number)), + (r"'", String.Single, 'singlestring'), + (r'"', String.Double, 'doublestring'), + (r'`[^`]*`', String.Backtick), + + # names + (r'\$[a-z_][\w.]*', Name.Variable), + (r'#([a-z_][\w.]*|\d+)', Name.Variable.Instance), + (r"(\.)('[a-z_][\w.]*')", + bygroups(Name.Builtin.Pseudo, Name.Variable.Class)), + (r"(self)(\s*->\s*)('[a-z_][\w.]*')", + bygroups(Name.Builtin.Pseudo, Operator, Name.Variable.Class)), + (r'(\.\.?)([a-z_][\w.]*(=(?!=))?)', + bygroups(Name.Builtin.Pseudo, Name.Other.Member)), + (r'(->\\?\s*|&\s*)([a-z_][\w.]*(=(?!=))?)', + bygroups(Operator, Name.Other.Member)), + (r'(self|inherited)\b', Name.Builtin.Pseudo), + (r'-[a-z_][\w.]*', Name.Attribute), + (r'::\s*[a-z_][\w.]*', Name.Label), + (r'(error_(code|msg)_\w+|Error_AddError|Error_ColumnRestriction|' + r'Error_DatabaseConnectionUnavailable|Error_DatabaseTimeout|' + r'Error_DeleteError|Error_FieldRestriction|Error_FileNotFound|' + r'Error_InvalidDatabase|Error_InvalidPassword|' + r'Error_InvalidUsername|Error_ModuleNotFound|' + r'Error_NoError|Error_NoPermission|Error_OutOfMemory|' + r'Error_ReqColumnMissing|Error_ReqFieldMissing|' + r'Error_RequiredColumnMissing|Error_RequiredFieldMissing|' + r'Error_UpdateError)\b', Name.Exception), + + # definitions + (r'(define)(\s+)([a-z_][\w.]*)(\s*=>\s*)(type|trait|thread)\b', + bygroups(Keyword.Declaration, Text, Name.Class, Operator, Keyword)), + (r'(define)(\s+)([a-z_][\w.]*)(\s*->\s*)([a-z_][\w.]*=?|[-+*/%])', + bygroups(Keyword.Declaration, Text, Name.Class, Operator, + Name.Function), 'signature'), + (r'(define)(\s+)([a-z_][\w.]*)', + bygroups(Keyword.Declaration, Text, Name.Function), 'signature'), + (r'(public|protected|private|provide)(\s+)(([a-z_][\w.]*=?|[-+*/%])' + r'(?=\s*\())', bygroups(Keyword, Text, Name.Function), + 'signature'), + (r'(public|protected|private|provide)(\s+)([a-z_][\w.]*)', + bygroups(Keyword, Text, Name.Function)), + + # keywords + (r'(true|false|none|minimal|full|all|void)\b', Keyword.Constant), + (r'(local|var|variable|global|data(?=\s))\b', Keyword.Declaration), + (r'(array|date|decimal|duration|integer|map|pair|string|tag|xml|' + r'null|bytes|list|queue|set|stack|staticarray|tie)\b', Keyword.Type), + (r'([a-z_][\w.]*)(\s+)(in)\b', bygroups(Name, Text, Keyword)), + (r'(let|into)(\s+)([a-z_][\w.]*)', bygroups(Keyword, Text, Name)), + (r'require\b', Keyword, 'requiresection'), + (r'(/?)(Namespace_Using)\b', bygroups(Punctuation, Keyword.Namespace)), + (r'(/?)(Cache|Database_Names|Database_SchemaNames|' + r'Database_TableNames|Define_Tag|Define_Type|Email_Batch|' + r'Encode_Set|HTML_Comment|Handle|Handle_Error|Header|If|Inline|' + r'Iterate|LJAX_Target|Link|Link_CurrentAction|Link_CurrentGroup|' + r'Link_CurrentRecord|Link_Detail|Link_FirstGroup|' + r'Link_FirstRecord|Link_LastGroup|Link_LastRecord|Link_NextGroup|' + r'Link_NextRecord|Link_PrevGroup|Link_PrevRecord|Log|Loop|' + r'NoProcess|Output_None|Portal|Private|Protect|Records|Referer|' + r'Referrer|Repeating|ResultSet|Rows|Search_Args|Search_Arguments|' + r'Select|Sort_Args|Sort_Arguments|Thread_Atomic|Value_List|While|' + r'Abort|Case|Else|If_Empty|If_False|If_Null|If_True|Loop_Abort|' + r'Loop_Continue|Loop_Count|Params|Params_Up|Return|Return_Value|' + r'Run_Children|SOAP_DefineTag|SOAP_LastRequest|SOAP_LastResponse|' + r'Tag_Name|ascending|average|by|define|descending|do|equals|' + r'frozen|group|handle_failure|import|in|into|join|let|match|max|' + r'min|on|order|parent|protected|provide|public|require|returnhome|' + r'skip|split_thread|sum|take|thread|to|trait|type|where|with|' + r'yield|yieldhome)\b', + bygroups(Punctuation, Keyword)), + + # other + (r',', Punctuation, 'commamember'), + (r'(and|or|not)\b', Operator.Word), + (r'([a-z_][\w.]*)(\s*::\s*[a-z_][\w.]*)?(\s*=(?!=))', + bygroups(Name, Name.Label, Operator)), + (r'(/?)([\w.]+)', bygroups(Punctuation, Name.Other)), + (r'(=)(n?bw|n?ew|n?cn|lte?|gte?|n?eq|n?rx|ft)\b', + bygroups(Operator, Operator.Word)), + (r':=|[-+*/%=<>&|!?\\]+', Operator), + (r'[{}():;,@^]', Punctuation), + ], + 'singlestring': [ + (r"'", String.Single, '#pop'), + (r"[^'\\]+", String.Single), + include('escape'), + (r"\\", String.Single), + ], + 'doublestring': [ + (r'"', String.Double, '#pop'), + (r'[^"\\]+', String.Double), + include('escape'), + (r'\\', String.Double), + ], + 'escape': [ + (r'\\(U[\da-f]{8}|u[\da-f]{4}|x[\da-f]{1,2}|[0-7]{1,3}|:[^:]+:|' + r'[abefnrtv?\"\'\\]|$)', String.Escape), + ], + 'signature': [ + (r'=>', Operator, '#pop'), + (r'\)', Punctuation, '#pop'), + (r'[(,]', Punctuation, 'parameter'), + include('lasso'), + ], + 'parameter': [ + (r'\)', Punctuation, '#pop'), + (r'-?[a-z_][\w.]*', Name.Attribute, '#pop'), + (r'\.\.\.', Name.Builtin.Pseudo), + include('lasso'), + ], + 'requiresection': [ + (r'(([a-z_][\w.]*=?|[-+*/%])(?=\s*\())', Name, 'requiresignature'), + (r'(([a-z_][\w.]*=?|[-+*/%])(?=(\s*::\s*[\w.]+)?\s*,))', Name), + (r'[a-z_][\w.]*=?|[-+*/%]', Name, '#pop'), + (r'::\s*[a-z_][\w.]*', Name.Label), + (r',', Punctuation), + include('whitespacecomments'), + ], + 'requiresignature': [ + (r'(\)(?=(\s*::\s*[\w.]+)?\s*,))', Punctuation, '#pop'), + (r'\)', Punctuation, '#pop:2'), + (r'-?[a-z_][\w.]*', Name.Attribute), + (r'::\s*[a-z_][\w.]*', Name.Label), + (r'\.\.\.', Name.Builtin.Pseudo), + (r'[(,]', Punctuation), + include('whitespacecomments'), + ], + 'commamember': [ + (r'(([a-z_][\w.]*=?|[-+*/%])' + r'(?=\s*(\(([^()]*\([^()]*\))*[^)]*\)\s*)?(::[\w.\s]+)?=>))', + Name.Function, 'signature'), + include('whitespacecomments'), + default('#pop'), + ], + } + + def __init__(self, **options): + self.builtinshighlighting = get_bool_opt( + options, 'builtinshighlighting', True) + self.requiredelimiters = get_bool_opt( + options, 'requiredelimiters', False) + + self._builtins = set() + self._members = set() + if self.builtinshighlighting: + from pygments.lexers._lassobuiltins import BUILTINS, MEMBERS + for key, value in iteritems(BUILTINS): + self._builtins.update(value) + for key, value in iteritems(MEMBERS): + self._members.update(value) + RegexLexer.__init__(self, **options) + + def get_tokens_unprocessed(self, text): + stack = ['root'] + if self.requiredelimiters: + stack.append('delimiters') + for index, token, value in \ + RegexLexer.get_tokens_unprocessed(self, text, stack): + if (token is Name.Other and value.lower() in self._builtins or + token is Name.Other.Member and + value.lower().rstrip('=') in self._members): + yield index, Name.Builtin, value + continue + yield index, token, value + + def analyse_text(text): + rv = 0.0 + if 'bin/lasso9' in text: + rv += 0.8 + if re.search(r'<\?(=|lasso)|\A\[', text, re.I): + rv += 0.4 + if re.search(r'local\(', text, re.I): + rv += 0.4 + if '?>' in text: + rv += 0.1 + return rv + + +class ObjectiveJLexer(RegexLexer): + """ + For Objective-J source code with preprocessor directives. + + .. versionadded:: 1.3 + """ + + name = 'Objective-J' + aliases = ['objective-j', 'objectivej', 'obj-j', 'objj'] + filenames = ['*.j'] + mimetypes = ['text/x-objective-j'] + + #: optional Comment or Whitespace + _ws = r'(?:\s|//.*?\n|/[*].*?[*]/)*' + + flags = re.DOTALL | re.MULTILINE + + tokens = { + 'root': [ + include('whitespace'), + + # function definition + (r'^(' + _ws + r'[\+-]' + _ws + r')([\(a-zA-Z_].*?[^\(])(' + _ws + '{)', + bygroups(using(this), using(this, state='function_signature'), + using(this))), + + # class definition + (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text), + 'classname'), + (r'(@class|@protocol)(\s*)', bygroups(Keyword, Text), + 'forward_classname'), + (r'(\s*)(@end)(\s*)', bygroups(Text, Keyword, Text)), + + include('statements'), + ('[{\(\)}]', Punctuation), + (';', Punctuation), + ], + 'whitespace': [ + (r'(@import)(\s+)("(?:\\\\|\\"|[^"])*")', + bygroups(Comment.Preproc, Text, String.Double)), + (r'(@import)(\s+)(<(?:\\\\|\\>|[^>])*>)', + bygroups(Comment.Preproc, Text, String.Double)), + (r'(#(?:include|import))(\s+)("(?:\\\\|\\"|[^"])*")', + bygroups(Comment.Preproc, Text, String.Double)), + (r'(#(?:include|import))(\s+)(<(?:\\\\|\\>|[^>])*>)', + bygroups(Comment.Preproc, Text, String.Double)), + + (r'#if\s+0', Comment.Preproc, 'if0'), + (r'#', Comment.Preproc, 'macro'), + + (r'\n', Text), + (r'\s+', Text), + (r'\\\n', Text), # line continuation + (r'//(\n|(.|\n)*?[^\\]\n)', Comment.Single), + (r'/(\\\n)?[*](.|\n)*?[*](\\\n)?/', Comment.Multiline), + (r')?', Other), + (r'[^[<]+', Other), ], 'noprocess': [ (r'\[/noprocess\]', Comment.Preproc, '#pop'), -- cgit v1.2.1 From 6d93c83ae39162b4289b1e3bc449610fc5813c36 Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Fri, 14 Nov 2014 10:11:58 -0500 Subject: allow space in data member accessor --- pygments/lexers/javascript.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index 0cd68d24..3e47821a 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -586,7 +586,7 @@ class LassoLexer(RegexLexer): # names (r'\$[a-z_][\w.]*', Name.Variable), (r'#([a-z_][\w.]*|\d+)', Name.Variable.Instance), - (r"(\.)('[a-z_][\w.]*')", + (r"(\.\s*)('[a-z_][\w.]*')", bygroups(Name.Builtin.Pseudo, Name.Variable.Class)), (r"(self)(\s*->\s*)('[a-z_][\w.]*')", bygroups(Name.Builtin.Pseudo, Operator, Name.Variable.Class)), -- cgit v1.2.1 From 63c009b4c1712660de4823ad7546ed46c46d493c Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Fri, 14 Nov 2014 10:21:22 -0500 Subject: improved list of built-in Lasso types --- pygments/lexers/javascript.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index 3e47821a..178d63de 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -625,7 +625,8 @@ class LassoLexer(RegexLexer): (r'(true|false|none|minimal|full|all|void)\b', Keyword.Constant), (r'(local|var|variable|global|data(?=\s))\b', Keyword.Declaration), (r'(array|date|decimal|duration|integer|map|pair|string|tag|xml|' - r'null|bytes|list|queue|set|stack|staticarray|tie)\b', Keyword.Type), + r'null|boolean|bytes|keyword|list|locale|queue|set|stack|' + r'staticarray)\b', Keyword.Type), (r'([a-z_][\w.]*)(\s+)(in)\b', bygroups(Name, Text, Keyword)), (r'(let|into)(\s+)([a-z_][\w.]*)', bygroups(Keyword, Text, Name)), (r'require\b', Keyword, 'requiresection'), -- cgit v1.2.1 From 971c89f5f04b1737446e67589adcabb348411868 Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Mon, 24 Nov 2014 12:15:46 -0500 Subject: add currentcapture & givenblock to list of special Lasso builtins --- pygments/lexers/javascript.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index 178d63de..eb6297c0 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -594,7 +594,7 @@ class LassoLexer(RegexLexer): bygroups(Name.Builtin.Pseudo, Name.Other.Member)), (r'(->\\?\s*|&\s*)([a-z_][\w.]*(=(?!=))?)', bygroups(Operator, Name.Other.Member)), - (r'(self|inherited)\b', Name.Builtin.Pseudo), + (r'(self|inherited|currentcapture|givenblock)\b', Name.Builtin.Pseudo), (r'-(?!infinity)[a-z_][\w.]*', Name.Attribute), (r'::\s*[a-z_][\w.]*', Name.Label), (r'(error_(code|msg)_\w+|Error_AddError|Error_ColumnRestriction|' -- cgit v1.2.1 From 8f30b8acaab6f616a934d4749c37e6dd1e54c849 Mon Sep 17 00:00:00 2001 From: EricFromCanada Date: Mon, 24 Nov 2014 13:36:46 -0500 Subject: account for when self and givenblock appear as member methods --- pygments/lexers/javascript.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index eb6297c0..30ec0600 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -594,7 +594,8 @@ class LassoLexer(RegexLexer): bygroups(Name.Builtin.Pseudo, Name.Other.Member)), (r'(->\\?\s*|&\s*)([a-z_][\w.]*(=(?!=))?)', bygroups(Operator, Name.Other.Member)), - (r'(self|inherited|currentcapture|givenblock)\b', Name.Builtin.Pseudo), + (r'(?)(self|inherited|currentcapture|givenblock)\b', + Name.Builtin.Pseudo), (r'-(?!infinity)[a-z_][\w.]*', Name.Attribute), (r'::\s*[a-z_][\w.]*', Name.Label), (r'(error_(code|msg)_\w+|Error_AddError|Error_ColumnRestriction|' -- cgit v1.2.1 From 89240ed7d7807346291eb20625a17441f6900157 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Wed, 21 Jan 2015 08:33:25 +0100 Subject: Copyright year update. --- pygments/lexers/javascript.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index 8e258f9a..fa7dca41 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -5,7 +5,7 @@ Lexers for JavaScript and related languages. - :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. + :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS. :license: BSD, see LICENSE for details. """ -- cgit v1.2.1 From 9a4cce44cc5b89ea414e8d8c769155d57617bd99 Mon Sep 17 00:00:00 2001 From: Miikka Salminen Date: Sat, 7 Mar 2015 22:30:17 +0200 Subject: Added a new token type Comment.Hashbang with its documentation, and a support for it in Javascript, Perl, Python, Ruby and Bash lexers. --- pygments/lexers/javascript.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pygments/lexers/javascript.py') diff --git a/pygments/lexers/javascript.py b/pygments/lexers/javascript.py index fa7dca41..aed8438e 100644 --- a/pygments/lexers/javascript.py +++ b/pygments/lexers/javascript.py @@ -60,7 +60,7 @@ class JavascriptLexer(RegexLexer): (r'\n', Text, '#pop') ], 'root': [ - (r'\A#! ?/.*?\n', Comment), # shebang lines are recognized by node.js + (r'\A#! ?/.*?\n', Comment.Hashbang), # recognized by node.js (r'^(?=\s|/|