forked from shawalli/pygments-lexer-apex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygments_lexer_apex.py
78 lines (70 loc) · 3.24 KB
/
pygments_lexer_apex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""Pygments lexer for the Salesforce Apex language. This is largely a modified version of the Java Lexer."""
import re
from pygments.lexer import Lexer, RegexLexer, include, bygroups, using, \
this, combined, default, words
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
Number, Punctuation
from pygments.util import shebang_matches
from pygments import unistring as uni
__all__ = ['ApexLexer']
class ApexLexer(RegexLexer):
"""For Salesforce Apex source code."""
name = 'Apex'
aliases = ['apex']
filenames = ['*.apxc', '*.apxt', '*.cls']
flags = re.MULTILINE | re.DOTALL | re.UNICODE
tokens = {
'root': [
# General text
(r'[^\S\n]+', Text),
# Comments
(r'//.*?\n', Comment.Single),
(r'/\*.*?\*/', Comment.Multiline),
# Keywords: go before method names to avoid lexing "throw new XYZ" as a method signature
(r'(?i)(break|case|catch|continue|do|else|finally|for|if|instanceof|new|return|switch|this|throw|try|'
r'while)\b',
Keyword),
# DML keywords
(r'(?i)(delete|insert|merge|undelete|update|upsert)\b', Keyword),
# Method names
(r'((?:(?:[^\W\d]|\$)[\w.\[\]$<>]*\s+)+?)' # return arguments
r'((?:[^\W\d]|\$)[\w$]*)' # method name
r'(\s*)(\()', # signature start
bygroups(using(this), Name.Function, Text, Operator)),
# Annotations
(r'@[^\W\d][\w.]*', Name.Decorator),
# Apex class modifiers
(r'(?i)(abstract|const|enum|extends|final|global|implements|on|override|private|protected|public|static|'
r'super|throws|with sharing|without sharing)\b', Keyword.Declaration),
(r'(?i)(blob|boolean|date|datetime|decimal|double|float|id|integer|long|object|time)\b',
Keyword.Type),
# Constants
(r'(?i)(true|false|null)\b', Keyword.Constant),
(r'(?i)(class|interface|trigger)(\s+)', bygroups(Keyword.Declaration, Text),
'class'),
(r"'(\\\\|\\'|[^'])*'", String),
(r"'\\.'|'[^\\]'|'\\u[0-9a-fA-F]{4}'", String.Char),
(r'(\.)((?:[^\W\d]|\$)[\w$]*)',
bygroups(Operator, Name.Attribute)),
(r'^\s*([^\W\d]|\$)[\w$]*:', Name.Label),
(r'([^\W\d]|\$)[\w$]*', Name),
(r'([0-9][0-9_]*\.([0-9][0-9_]*)?|'
r'\.[0-9][0-9_]*)'
r'([eE][+\-]?[0-9][0-9_]*)?[fFdD]?|'
r'[0-9][eE][+\-]?[0-9][0-9_]*[fFdD]?|'
r'[0-9]([eE][+\-]?[0-9][0-9_]*)?[fFdD]|'
r'0[xX]([0-9a-fA-F][0-9a-fA-F_]*\.?|'
r'([0-9a-fA-F][0-9a-fA-F_]*)?\.[0-9a-fA-F][0-9a-fA-F_]*)'
r'[pP][+\-]?[0-9][0-9_]*[fFdD]?', Number.Float),
(r'0[xX][0-9a-fA-F][0-9a-fA-F_]*[lL]?', Number.Hex),
(r'0[bB][01][01_]*[lL]?', Number.Bin),
(r'0[0-7_]+[lL]?', Number.Oct),
(r'0|[1-9][0-9_]*[lL]?', Number.Integer),
(r'[~^*!%&\[\](){}<>|+=:;,./?-]', Operator),
(r'\n', Text)
],
'class': [
(r'([^\W\d]|\$)[\w$]*', Name.Class, '#pop')
],
}