diff options
author | David Corbett <corbett.dav@husky.neu.edu> | 2016-03-21 16:28:50 -0400 |
---|---|---|
committer | David Corbett <corbett.dav@husky.neu.edu> | 2016-03-21 16:28:50 -0400 |
commit | 3754b29cbb08de34a06c6803df7d049e466cafec (patch) | |
tree | e06a8b6f6e009805bdaae71f35c43ba5ff293d29 /pygments/lexers/scripting.py | |
parent | ce93b89a0b9d6b3b0eb828e049952c4ff84421fd (diff) | |
download | pygments-3754b29cbb08de34a06c6803df7d049e466cafec.tar.gz |
Improve LuaLexer
Diffstat (limited to 'pygments/lexers/scripting.py')
-rw-r--r-- | pygments/lexers/scripting.py | 62 |
1 files changed, 40 insertions, 22 deletions
diff --git a/pygments/lexers/scripting.py b/pygments/lexers/scripting.py index ac0f7533..5849161b 100644 --- a/pygments/lexers/scripting.py +++ b/pygments/lexers/scripting.py @@ -50,36 +50,47 @@ class LuaLexer(RegexLexer): filenames = ['*.lua', '*.wlua'] mimetypes = ['text/x-lua', 'application/x-lua'] + _comment_multiline = r'(?:--\[(?P<level>=*)\[[\w\W]*?\](?P=level)\])' + _comment_single = r'(?:--.*$)' + _space = r'(?:\s+)' + _s = r'(?:%s|%s|%s)' % (_comment_multiline, _comment_single, _space) + _name = r'(?:[^\W\d]\w*)' + tokens = { 'root': [ - # lua allows a file to start with a shebang - (r'#!(.*?)$', Comment.Preproc), + # Lua allows a file to start with a shebang. + (r'#!.*', Comment.Preproc), default('base'), ], + 'ws': [ + (_comment_multiline, Comment.Multiline), + (_comment_single, Comment.Single), + (_space, Text), + ], 'base': [ - (r'(?s)--\[(=*)\[.*?\]\1\]', Comment.Multiline), - ('--.*$', Comment.Single), + include('ws'), + (r'(?i)0x[\da-f]*(\.[\da-f]*)?(p[+-]?\d+)?', Number.Hex), (r'(?i)(\d*\.\d+|\d+\.\d*)(e[+-]?\d+)?', Number.Float), (r'(?i)\d+e[+-]?\d+', Number.Float), - ('(?i)0x[0-9a-f]*', Number.Hex), (r'\d+', Number.Integer), - (r'\n', Text), - (r'[^\S\n]', Text), # multiline strings (r'(?s)\[(=*)\[.*?\]\1\]', String), - (r'(==|~=|<=|>=|\.\.\.|\.\.|[=+\-*/%^<>#])', Operator), + (r'::', Punctuation, 'label'), + (r'\.{3}', Punctuation), + (r'[=<>|~&+\-*/%#^]+|\.\.', Operator), (r'[\[\]{}().,:;]', Punctuation), (r'(and|or|not)\b', Operator.Word), ('(break|do|else|elseif|end|for|if|in|repeat|return|then|until|' - r'while)\b', Keyword), + r'while)\b', Keyword.Reserved), + (r'goto\b', Keyword.Reserved, 'goto'), (r'(local)\b', Keyword.Declaration), (r'(true|false|nil)\b', Keyword.Constant), - (r'(function)\b', Keyword, 'funcname'), + (r'(function)\b', Keyword.Reserved, 'funcname'), (r'[A-Za-z_]\w*(\.[A-Za-z_]\w*)?', Name), @@ -88,31 +99,38 @@ class LuaLexer(RegexLexer): ], 'funcname': [ - (r'\s+', Text), - ('(?:([A-Za-z_]\w*)(\.))?([A-Za-z_]\w*)', - bygroups(Name.Class, Punctuation, Name.Function), '#pop'), + include('ws'), + (r'[.:]', Punctuation), + (r'%s(?=%s*[.:])' % (_name, _s), Name.Class), + (_name, Name.Function, '#pop'), # inline function ('\(', Punctuation, '#pop'), ], - # if I understand correctly, every character is valid in a lua string, - # so this state is only for later corrections - 'string': [ - ('.', String) + 'goto': [ + include('ws'), + (_name, Name.Label, '#pop'), + ], + + 'label': [ + include('ws'), + (r'::', Punctuation, '#pop'), + (_name, Name.Label), ], 'stringescape': [ - (r'''\\([abfnrtv\\"']|\d{1,3})''', String.Escape) + (r'\\([abfnrtv\\"\']|[\r\n]{1,2}|z\s*|x[0-9a-fA-F]{2}|\d{1,3}|' + r'u\{[0-9a-fA-F]+\})', String.Escape), ], 'sqs': [ - ("'", String, '#pop'), - include('string') + (r"'", String.Single, '#pop'), + (r"[^\\']+", String.Single), ], 'dqs': [ - ('"', String, '#pop'), - include('string') + (r'"', String.Double, '#pop'), + (r'[^\\"]+', String.Double), ] } |