Created
April 8, 2016 16:34
-
-
Save persquare/42d1931ba2762d7cdbd85e6ee0a91ca7 to your computer and use it in GitHub Desktop.
A CalvinScript lexer for pygments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pygments.lexer import RegexLexer, bygroups, include, using | |
| from pygments.lexers.data import JsonLexer | |
| from pygments.token import * | |
| __all__ = ['CalvinLexer'] | |
| class CalvinLexer(RegexLexer): | |
| name = 'CalvinScript' | |
| aliases = ['Calvinscript', 'calvinscript'] | |
| filenames = ['*.calvin'] | |
| tokens = { | |
| 'whitespace': [ | |
| (r'\s+', Text), | |
| ], | |
| 'constdef': [ | |
| (r'(^\s*)(define)(\s+)([a-zA-Z][a-zA-Z0-9_]*)(\s*)(=)(\s*)(.+)(\s*)$', | |
| bygroups(Text, Keyword, Text, Name.Constant, Text, Operator, Text, using(JsonLexer), Text)) | |
| ], | |
| 'root': [ | |
| include('whitespace'), | |
| include('constdef'), | |
| include('comment'), | |
| include('component'), | |
| include('block') | |
| ], | |
| 'comment': [ | |
| (r'#!.*$', Comment.Hashbang), | |
| (r'#.*$', Comment.Single), | |
| # Deprecated comment | |
| (r'//.*$', Comment.Single), | |
| (r'/\*', Comment.Multiline, 'comment_multiline') | |
| ], | |
| 'comment_multiline': [ | |
| (r'[^*/]', Comment.Multiline), | |
| (r'/\*', Comment.Multiline, '#push'), | |
| (r'\*/', Comment.Multiline, '#pop'), | |
| (r'[*/]', Comment.Multiline) | |
| ], | |
| 'comment_block': [ | |
| (r'"""(?:[\s\S]+?)?"""', Comment.Special) | |
| ], | |
| 'block': [ | |
| include('whitespace'), | |
| include('comment'), | |
| include('instance'), | |
| include('connection'), | |
| ], | |
| 'instance': [ | |
| (r'(\s*)([a-zA-Z][a-zA-Z0-9_]*)(\s*)(:)(\s*)(\S+?)(\s*)(\()', | |
| bygroups(Text, Name.Variable.Instance, Text, Operator, Text, Name.Class, Text, Punctuation), 'named_args') | |
| ], | |
| 'named_args': [ | |
| include('whitespace'), | |
| # CONST argument | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)(\s*)(=)(\s*)([a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(Name.Attribute, Text, Operator, Text, Name.Constant)), | |
| # VALUE argument | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)(\s*)(=)(\s*)(.+?)(?:(?=,\s*[a-zA-Z][a-zA-Z0-9_]*\s*=)|(?=\)\s*$))', | |
| bygroups(Name.Attribute, Text, Operator, Text, using(JsonLexer))), | |
| (r'\,', Punctuation), | |
| (r'\)', Punctuation, '#pop'), | |
| ], | |
| 'connection': [ | |
| include('whitespace'), | |
| # Normal | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)(\s*)(>)(\s*)([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(Name.Variable.Instance, Punctuation, Name.Label, Text, Operator, Text, Name.Variable.Instance, Punctuation, Name.Label)), | |
| # Proxy inport | |
| (r'(\.[a-zA-Z][a-zA-Z0-9_]*)(\s*)(>)(\s*)([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(Name.Decorator, Text, Operator, Text, Name.Variable.Instance, Punctuation, Name.Label)), | |
| # Proxy outport | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)(\s*)(>)(\s*)(\.[a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(Name.Variable.Instance, Punctuation, Name.Label, Text, Operator, Text, Name.Decorator)), | |
| # Literal inport (CONST) | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)(\s*)(>)(\s*)([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(Name.Constant, Text, Operator, Text, Name.Variable.Instance, Punctuation, Name.Label)), | |
| # Literal inport (VALUE) | |
| (r'([^\}][^>]*?)(>)(\s*)([a-zA-Z][a-zA-Z0-9_]*)(\.)([a-zA-Z][a-zA-Z0-9_]*)', | |
| bygroups(using(JsonLexer), Operator, Text, Name.Variable.Instance, Punctuation, Name.Label)) | |
| ], | |
| 'component': [ | |
| (r'(component)(\s+)([a-zA-Z][a-zA-Z0-9_]*)(\s*)(\()', | |
| bygroups(Keyword, Text, Name.Class, Text, Punctuation), 'argdecl') | |
| ], | |
| 'arglist': [ | |
| include('whitespace'), | |
| (r'([a-zA-Z][a-zA-Z0-9_]*)', Name.Label), | |
| (r'\,', Punctuation) | |
| ], | |
| 'argdecl': [ | |
| include('arglist'), | |
| (r'\)', Punctuation, 'inportdecl'), | |
| ], | |
| 'inportdecl': [ | |
| include('arglist'), | |
| (r'->', Operator, 'outportdecl'), | |
| ], | |
| 'outportdecl': [ | |
| include('arglist'), | |
| (r'\{', Punctuation, 'compdef'), | |
| ], | |
| 'compdef': [ | |
| include('comment_block'), | |
| include('block'), | |
| (r'\}', Punctuation, '#pop:4') | |
| ] | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment