Commits

Tim Hatch committed 5f79107 Merge

Merge lexer for Monkey support (Pull Request 117)

Comments (0)

Files changed (3)

pygments/lexers/_mapping.py

     '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.compiled', 'Monkey', ('monkey',), ('*.monkey',), ('text/x-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',), ()),

pygments/lexers/compiled.py

            'DylanLexer', 'ObjectiveCLexer', 'FortranLexer', 'GLShaderLexer',
            'PrologLexer', 'CythonLexer', 'ValaLexer', 'OocLexer', 'GoLexer',
            'FelixLexer', 'AdaLexer', 'Modula2Lexer', 'BlitzMaxLexer',
-           'NimrodLexer', 'FantomLexer', 'RustLexer', 'CUDALexer']
+           'NimrodLexer', 'FantomLexer', 'RustLexer', 'CUDALexer', 'MonkeyLexer']
 
 
 class CLexer(RegexLexer):
                 elif value in self.functions:
                     token = Name.Function
             yield index, token, value
+
+
+class MonkeyLexer(RegexLexer):
+    """
+    For
+    `Monkey <https://en.wikipedia.org/wiki/Monkey_(programming_language)>`_
+    source code.
+    """
+
+    name = 'Monkey'
+    aliases = ['monkey']
+    filenames = ['*.monkey']
+    mimetypes = ['text/x-monkey'] 
+
+    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'\s+', Text),
+            # Comments
+            (r"'.*", Comment),
+            (r'(?i)^#rem\b', Comment.Multiline, 'comment'),
+            # preprocessor directives
+            (r'(?i)^(?:#If|#ElseIf|#Else|#EndIf|#End|#Print|#Error)\b', Comment.Preproc),
+            # preprocessor variable (any line starting with '#' that is not a directive)
+            (r'^#', Comment.Preproc, 'variables'),
+            # 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-fA-Z]+', 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),
+            # Array
+            (r'[\[\]]', Punctuation),
+            # Other
+            (r'<=|>=|<>|[*]=|/=|[+]=|-=|&=|~=|[|]=|[-&*/^+=<>]', Operator),
+            (r'(?i)(?:Not|Mod|Shl|Shr|And|Or)', Operator.Word),
+            (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),
+            # array (of given size)
+            (r'(\[)\s*(\d*)\s*(\])', bygroups(Punctuation, Number.Integer, Punctuation)),
+            # generics
+            (r'\s+(?!<)', Text, '#pop'),
+            (r'<', Punctuation, '#push'),
+            (r'>', Punctuation, '#pop'),
+            (r'\n', Text, '#pop'),
+            (r'', Text, '#pop')
+        ],
+        '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'~q|~n|~r|~r|~t|~z|~~', String.Escape),
+            (r'"', String.Double, '#pop'),
+        ],
+        'comment' : [
+            (r'(?i)^#rem.*?', Comment.Multiline, "#push"),
+            (r'(?i)^#end.*?', Comment.Multiline, "#pop"),
+            (r'\n', Comment.Multiline),
+            (r'.*', Comment.Multiline),
+        ],
+    }

tests/examplefiles/example.monkey

+Strict
+
+' single line comment
+
+#rem
+multi
+line
+comment
+#end
+
+#rem
+nested
+#rem
+multi
+line
+#end
+comment
+#end
+
+Import mojo
+
+Const ONECONST:Int = 1
+Const TWOCONST := 2
+Const THREECONST := 3, FOURCONST:Int = 4
+
+Global someVariable:Int = 4
+
+' sample class from the documentation
+Class Game Extends App
+
+    Function New()
+    End
+
+    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
+
+' extending
+Class Hoodlum Extends Enemy
+    ' field
+    Field testField:Bool = True
+
+    ' naming class with modulepath
+    Local currentNode:list.Node<Vector2D>
+
+    Method Die ()
+        Print "B'oss, he-- he killed me, b'oss!"
+    End
+End
+
+' extending with generics
+Class VectorNode Extends Node<Vector2D>
+End
+
+' interfaces
+Interface Computer
+  Method Boot ()
+  Method Process ()
+  Method Display ()
+End
+
+Class PC Implements Computer
+End
+
+' array syntax
+Global listOfStuff:String[42]
+Global lessStuff:String[5] = listOfStuff[4..8]
+Global oneStuff:String = listOfStuff[23]
+
+'a comma separated sequence
+Global scores:Int[]=[10,20,30]
+'a comma separated sequence
+Global text:String[]=["Hello","There","World"]
+Global worstCase:worst.List<String[]>
+
+' string type
+Global string1:String = "Hello world"
+Global string2$ = "Hello world"
+
+' escape characers in strings
+Global string3 := "Hello~zWorld"
+Global string4 := "~qHello World~q"
+Global string5 := "~tIndented~n"
+Global string6 := "tilda is wavey... ~~"
+
+' string pseudofunctions
+Print "  Hello World  ~n".Trim()    ' prints "Hello World"
+Print "Hello World".ToUpper()       ' prints "HELLO WORLD"
+
+' Boolean shorttype
+Global boolVariable1:Bool = True
+Global boolVariable2? = False
+
+' number formats
+Global hexNum1:Int = $3d0dead
+Global hexNum2% = $CAFEBABE
+
+Global floatNum1:Float = 3.141516
+Global floatNum2# = 3.141516
+Global floatNum3 := .141516
+
+' preprocessor keywords
+#If TARGET = "android"
+DoStuff()
+#ElseIf TARGET = "ios"
+DoOtherStuff()
+#End
+
+' preprocessor variable
+#SOMETHING = True
+#Print SOMETHING
+#If SOMETHING
+#End
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.