from pygments.lexer import RegexLexer, bygroups, include, default, words, using, this
from pygments.token import \
Text, Comment, Literal, Operator, Keyword, Name, String, Number, Punctuation
class ReformLexer(RegexLexer):
name = 'reform'
tokens = {
'whitespace': [
(r'\n', Text),
(r'\s+', Text),
(r'//(\n|[\w\W]*?[^\\]\n)', Comment.Single),
(r'/(\\\n)?[*][\w\W]*?[*](\\\n)?/', Comment.Multiline),
(r'/(\\\n)?[*][\w\W]*', Comment.Multiline),
],
'statements': [
(r'(L?)(")', bygroups(String.Affix, String), 'string'),
(r'\d+', Number.Integer),
(r'\.\.', Operator),
(r'[;(),]', Punctuation),
(r'[~!%^&*+=|?:<>/-]', Operator),
(words(('expr', 'id', 'apply', 'splitarg', 'inside', 'matchassign', 'if', 'else',
'multiply', 'mularg', 'argument', 'print'), suffix=r'\b'), Keyword),
include('expressions')
],
'expressions': [
(r'\d+', Number.Integer),
include('function'),
(r'([a-zA-Z_]\w*)\?', Name.Builtin), (r'\$([a-zA-Z_]\w*)', Name.Variable.Global), (r'([a-zA-Z_]\w*)(\s*)(\()',
bygroups(Name.Function, String, Punctuation)), (r'([a-zA-Z_]\w*)', Name.Variable), ],
'root': [
default('statement'),
],
'statement': [
include('whitespace'),
include('statements'),
(r'\{', Punctuation, '#push'),
(r'\}', Punctuation, '#pop'),
],
'string': [
(r'"', String, '#pop'),
(r'\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|'
r'u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})', String.Escape),
(r'[^\\"\n]+', String), (r'\\\n', String), (r'\\', String), ],
'function': [
(r'[\(\[]', Punctuation, '#push'),
(r'[\)\]]', Punctuation, '#pop'),
]
}