summaryrefslogtreecommitdiff
path: root/pygments/lexers/pony.py
blob: 13239047c3fdee4c379f4d5e6f22b783e0458d41 (plain)
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
# -*- coding: utf-8 -*-
"""
    pygments.lexers.pony
    ~~~~~~~~~~~~~~~~~~~~

    Lexers for Pony and related languages.

    :copyright: Copyright 2006-2016 by the Pygments team, see AUTHORS.
    :license: BSD, see LICENSE for details.
"""

from pygments.lexer import RegexLexer, bygroups, words
from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
    Number, Punctuation

__all__ = ['PonyLexer']


class PonyLexer(RegexLexer):
    """
    For Pony source code.

    .. versionadded:: 2.4
    """

    name = 'Pony'
    aliases = ['pony']
    filenames = ['*.pony']

    _caps = r'(iso|trn|ref|val|box|tag)'

    tokens = {
        'root': [
            (r'\n', Text),
            (r'[^\S\n]+', Text),
            (r'//.*\n', Comment.Single),
            (r'/\*', Comment.Multiline, 'nested_comment'),
            (r'"""(?:.|\n)*?"""', String.Doc),
            (r'"', String, 'string'),
            (r'\'.*\'', String.Char),
            (r'=>|[]{}:().~;,|&!^?[]', Punctuation),
            (words((
                'addressof', 'and', 'as', 'consume', 'digestof', 'is', 'isnt',
                'not', 'or'),
                suffix=r'\b'),
             Operator.Word),
            (r'!=|==|<<|>>|[-+/*%=<>]', Operator),
            (words((
                'box', 'break', 'compile_error', 'compile_intrinsic',
                'continue', 'do', 'else', 'elseif', 'embed', 'end', 'error',
                'for', 'if', 'ifdef', 'in', 'iso', 'lambda', 'let', 'match',
                'object', 'recover', 'ref', 'repeat', 'return', 'tag', 'then',
                'this', 'trn', 'try', 'until', 'use', 'var', 'val', 'where',
                'while', 'with', '#any', '#read', '#send', '#share'),
                suffix=r'\b'),
             Keyword),
            (r'(actor|class|struct|primitive|interface|trait|type)((?:\s)+)',
             bygroups(Keyword, Text), 'typename'),
            (r'(new|fun|be)((?:\s)+)', bygroups(Keyword, Text), 'methodname'),
            (words((
                'I8', 'U8', 'I16', 'U16', 'I32', 'U32', 'I64', 'U64', 'I128',
                'U128', 'ILong', 'ULong', 'ISize', 'USize', 'F32', 'F64',
                'Bool', 'Pointer', 'None', 'Any', 'Array', 'String',
                'Iterator'),
                suffix=r'\b'),
             Name.Builtin.Type),
            (r'_?[A-Z]\w*', Name.Type),
            (r'(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+', Number.Float),
            (r'0x[0-9a-fA-F]+', Number.Hex),
            (r'\d+', Number.Integer),
            (r'(true|false)\b', Name.Builtin),
            (r'_\d*', Name),
            (r'_?[a-z][\w\'_]*', Name)
        ],
        'typename': [
            (_caps + r'?((?:\s)*)(_?[A-Z]\w*)',
             bygroups(Keyword, Text, Name.Class), '#pop')
        ],
        'methodname': [
            (_caps + r'?((?:\s)*)(_?[a-z]\w*)',
             bygroups(Keyword, Text, Name.Function), '#pop')
        ],
        'nested_comment': [
            (r'[^*/]+', Comment.Multiline),
            (r'/\*', Comment.Multiline, '#push'),
            (r'\*/', Comment.Multiline, '#pop'),
            (r'[*/]', Comment.Multiline)
        ],
        'string': [
            (r'"', String, '#pop'),
            (r'\\"', String),
            (r'[^\\"]+', String)
        ]
    }