Created
September 16, 2012 10:00
-
-
Save KamilaBorowska/3731848 to your computer and use it in GitHub Desktop.
This file contains 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
class Perl6Lexer(ExtendedRegexLexer): | |
""" | |
For `Perl 6 <http://www.perl6.org>`_ source code. | |
""" | |
name = 'Perl 6' | |
aliases = ['Perl6', 'perl 6', 'perl6', 'pl6'] | |
filenames = ['*.pl', '*.pm', '*.p6', '*.pm6'] | |
mimetypes = ['text/x-perl6', 'application/x-perl6'] | |
flags = re.DOTALL | re.MULTILINE | re.UNICODE | |
def balanced_parser(type): | |
def balanced_parser(lexer, match, ctx): | |
actual_type = type | |
characters = { | |
'{': '}', | |
'[': ']', | |
'<': '>', | |
"\u00AB": "\u00BB", | |
} | |
starter = match.group(1) | |
starter_len = len(starter) | |
ender = (characters.get(starter[0]) or starter[0]) * starter_len | |
end = match.end() | |
depth = 1 | |
length = len(ctx.text) | |
i = end | |
while i < length: | |
if ctx.text[i:i + starter_len] == ender: | |
depth -= 1 | |
i += starter_len | |
elif ctx.text[i:i + starter_len] == starter: | |
depth += 1 | |
i += starter_len | |
else: | |
i += 1 | |
if depth == 0: | |
break | |
result = ctx.text[match.start():i] | |
if depth: | |
actual_type = Error | |
yield match.start(), actual_type, result | |
ctx.pos = i | |
return balanced_parser | |
tokens = { | |
'root': [ | |
(r'#`\s*(([[{<])\2*|.)', balanced_parser(Comment.Multiline)), | |
], | |
} | |
def analyse_text(text): | |
if shebang_matches(text, r'perl6|rakudo|niecza|pugs'): | |
return True | |
if 'use v6' in text or 'use 6' in text: | |
return True | |
# Perl6::Classes is Perl 5 module which adds Perl 6 classes syntax | |
if ('class ' in text and 'method ' in text and 'has ' in text and | |
'Perl6::Classes' not in text): | |
return 0.95 | |
if 'my $' in text and 'module ' in text: | |
return 0.95 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment