summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTinnet Coronam <tinnet@coronam.net>2012-10-17 01:43:24 +0200
committerTinnet Coronam <tinnet@coronam.net>2012-10-17 01:43:24 +0200
commitd24b6a7797c177a9832c6d99c4ac498484287e64 (patch)
treea39f30a75ca4e621e50c6bc2503b6ba9231d9833
parent02528e23b813468ed1b2489475a706a3ae828d81 (diff)
parentd2b65a6a1bd4093f64c43c92e493b17ef41586ea (diff)
downloadpygments-d24b6a7797c177a9832c6d99c4ac498484287e64.tar.gz
merged upstream into branch
-rw-r--r--pygments/lexers/_mapping.py1
-rw-r--r--pygments/lexers/monkey.py143
-rw-r--r--tests/examplefiles/example.monkey84
3 files changed, 228 insertions, 0 deletions
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index 0eca2682..0404640a 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -168,6 +168,7 @@ LEXERS = {
'ModelicaLexer': ('pygments.lexers.other', 'Modelica', ('modelica',), ('*.mo',), ('text/x-modelica',)),
'Modula2Lexer': ('pygments.lexers.compiled', 'Modula-2', ('modula2', 'm2'), ('*.def', '*.mod'), ('text/x-modula2',)),
'MoinWikiLexer': ('pygments.lexers.text', 'MoinMoin/Trac Wiki markup', ('trac-wiki', 'moin'), (), ('text/x-trac-wiki',)),
+ 'MonkeyLexer': ('pygments.lexers.monkey', 'Monkey', ('monkey',), ('*.monkey',), ()),
'MoonScriptLexer': ('pygments.lexers.agile', 'MoonScript', ('moon', 'moonscript'), ('*.moon',), ('text/x-moonscript', 'application/x-moonscript')),
'MscgenLexer': ('pygments.lexers.other', 'Mscgen', ('mscgen', 'msc'), ('*.msc',), ()),
'MuPADLexer': ('pygments.lexers.math', 'MuPAD', ('mupad',), ('*.mu',), ()),
diff --git a/pygments/lexers/monkey.py b/pygments/lexers/monkey.py
new file mode 100644
index 00000000..b7e4192c
--- /dev/null
+++ b/pygments/lexers/monkey.py
@@ -0,0 +1,143 @@
+# -*- 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'[?%#$]'
+
+ flags = re.MULTILINE
+
+ tokens = {
+ 'root': [
+ #Text
+ (r'\n', Text),
+ (r'\r', Text),
+ (r'\t+', Text),
+ (r'\s+', Text),
+ # Comments
+ (r"'.*", 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'^[^#].*', Comment.Multiline),
+ (r'(?i)^#End.*?', Comment.Multiline, "#pop"),
+ (r'^#\w+\b.*', Comment.Multiline, "#push"),
+ (r'.*', Comment.Multiline),
+ ],
+ } \ No newline at end of file
diff --git a/tests/examplefiles/example.monkey b/tests/examplefiles/example.monkey
new file mode 100644
index 00000000..5457fc88
--- /dev/null
+++ b/tests/examplefiles/example.monkey
@@ -0,0 +1,84 @@
+Strict
+
+#rem
+this is a
+#rem
+nested
+#end
+comment
+even other preproc keywords are nested within!
+#If TARGET
+#End
+#end
+
+Import mojo
+
+Const ONECONST:Int = 1
+Const TWOCONST := 2
+Const THREECONST := 3, FOURCONST:Int = 4
+
+Class Game Extends App
+
+ ' radial sprial with axis aligned phase
+
+ Function DrawSpiral(clock)
+ Local w=DeviceWidth/2
+ For Local i#=0 Until w*1.5 Step .2
+ Local x#,y#
+ x=w+i*Sin(i*3+clock)
+ y=w+i*Cos(i*2+clock)
+ DrawRect x,y,1,1
+ Next
+ hitbox.Collide(event.pos)
+ End
+
+ Field updateCount
+
+ Method OnCreate()
+ Print "spiral"
+
+ SetUpdateRate 60
+ End
+
+ Method OnUpdate()
+ updateCount+=1
+ End
+
+ Method OnRender()
+ Cls
+ DrawSpiral updateCount
+ DrawSpiral updateCount*1.1
+ End
+
+End
+
+Class Enemy
+ Method Die () Abstract
+End
+
+Class Hoodlum Extends Enemy
+ Local currentNode:list.Node<Vector2D>
+
+ ' Must implement Die method...
+
+ Method Die ()
+ Print "B'oss, he-- he killed me, b'oss!"
+ End
+
+End
+
+Class VectorNode Extends Node<Vector2D>
+End
+
+Interface Computer
+ Method Boot ()
+ Method Process ()
+ Method Display ()
+End
+
+Class PC Implements Computer
+End
+
+Function Main()
+ New Game()
+End