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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# -*- coding: utf-8 -*-
"""
pygments.lexers.monkey
~~~~~~~~~~~~~~~~~~~~~~
Lexer for the monkey language
:copyright: Copyright 2012
:license: BSD, see LICENSE for details.
"""
import re
from pygments.lexer import RegexLexer, bygroups, include
from pygments.token import Punctuation, Text, Comment, Operator, Keyword, Name, String, Number, Literal, Other
__all__ = ['MonkeyLexer']
class MonkeyLexer(RegexLexer):
"""
For
`Monkey <https://en.wikipedia.org/wiki/Monkey_(programming_language)>`_
source code.
"""
name = 'Monkey'
aliases = ['monkey']
filenames = ['*.monkey']
mimetypes = [] # TODO
name_variable = r'[a-z_][a-zA-Z0-9_]*'
name_function = r'[A-Z][a-zA-Z0-9_]*'
name_constant = r'[A-Z_][A-Z0-9_]*'
name_class = r'[A-Z][a-zA-Z0-9_]*'
name_module = r'[a-z0-9_]*'
keyword_type = r'(Int|Float|String|Bool|Object|Array|Void)'
# ? == Bool // % == Int // # == Float // $ == String
keyword_type_special = r'[?%#$]'
tokens = {
'root': [
#Text
(r'\n', Text),
(r'\t+', Text),
(r'\s+', Text),
# Comments
(r"'.*\n", Comment),
(r'(?i)^#rem\b', Comment.Multiline, 'comment'),
(r'(?i)^(?:#If|#ElseIf|#Else|#End|#EndIf|#Print|#Error)\s?.*?$', Comment.Preproc),
# String
('"', String.Double, 'string'),
# Numbers
(r'[0-9]+\.[0-9]*(?!\.)', Number.Float),
(r'\.[0-9]+(?!\.)', Number.Float),
(r'[0-9]+', Number.Integer),
(r'\$[0-9a-f]+', Number.Hex),
(r'\%[10]+', Number), # Binary
# Native data types
(r'\b%s\b' % keyword_type, Keyword.Type),
# Exception handling
(r'(?i)\b(?:Try|Catch|Throw)\b', Keyword.Reserved),
(r'Throwable', Name.Exception),
# Builtins
(r'(?i)\b(?:Null|True|False)\b', Name.Builtin),
(r'(?i)\b(?:Self|Super)\b', Name.Builtin.Pseudo),
(r'\b(?:HOST|LANG|TARGET|CONFIG)\b', Name.Constant),
# Keywords
(r'(?i)^(Import)(\s+)(.*)(\n)', bygroups(Keyword.Namespace, Text, Name.Namespace, Text)),
(r'(?i)^Strict\b.*\n', Keyword.Reserved),
(r'(?i)(Const|Local|Global|Field)(\s+)', bygroups(Keyword.Declaration, Text), 'variables'),
(r'(?i)(New|Class|Interface|Extends|Implements)(\s+)', bygroups(Keyword.Reserved, Text), 'classname'),
(r'(?i)(Function|Method)(\s+)', bygroups(Keyword.Reserved, Text), 'funcname'),
(r'(?i)(?:End|Return|Public|Private|Extern|Property|Final|Abstract)\b', Keyword.Reserved),
# Flow Control stuff
(r'(?i)If|Then|Else|ElseIf|EndIf|'
r'Select|Case|Default|'
r'While|Wend|'
r'Repeat|Until|Forever|'
r'For|To|Until|Step|EachIn|Next|'
r'Exit|Continue\s+', Keyword.Reserved),
# not used yet
(r'(?i)\b(Module|Inline)\b', Keyword.Reserved),
# Other
(r'<=|>=|<>|[*]=|/=|[+]=|-=|&=|~=|[|]=|[-&*/^+=<>]', Operator),
(r'Not|Mod|Shl|Shr|And|Or', Operator.Word),
(r'[\]]', Punctuation, "array"),
(r'[\(\){}!#,.:]', Punctuation),
# catch the rest
(r'%s\b' % name_constant, Name.Constant),
(r'%s\b' % name_function, Name.Function),
(r'%s\b' % name_variable, Name.Variable),
],
'funcname': [
(r'(?i)%s\b' % name_function, Name.Function),
(r':', Punctuation, 'classname'),
(r'\s+', Text),
(r'\(', Punctuation, 'variables'),
(r'\)', Punctuation, '#pop')
],
'classname': [
(r'%s\.' % name_module, Name.Namespace),
(r'%s\b' % keyword_type, Keyword.Type),
(r'%s\b' % name_class, Name.Class),
(r'\s+(?!<)', Text,'#pop'),
(r'<', Punctuation),
(r'>', Punctuation),
(r'\[', Punctuation, 'array'),
(r'\n', Punctuation, '#pop'),
(r'', Text, '#pop')
],
'array' : [
# direct access myArray[3] and size definition myArray:String[100]
(r'[0-9]+', Number.Integer),
# slicing myArray[1..2]
(r'\.\.', Punctuation),
(r'\]', Punctuation, '#pop'),
(r'\[', Punctuation),
],
'variables': [
(r'%s\b' % name_constant, Name.Constant),
(r'%s\b' % name_variable, Name.Variable),
(r'%s' % keyword_type_special, Keyword.Type),
(r'\s+', Text),
(r':', Punctuation, 'classname'),
(r',', Punctuation, '#push'),
(r'', Text, '#pop')
],
'string': [
(r'""', String.Double),
(r'"C?', String.Double, '#pop'),
(r'[^"]+', String.Double)
],
'comment' : [
(r'[^#].*\n', Comment.Multiline),
(r'(?i)#End.*?\n', Comment.Multiline, "#pop"),
(r'^#', Comment.Multiline, "#push"),
],
}
|