|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.arrow |
|
4 ~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexer for Arrow. |
|
7 |
|
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, bygroups, default, include |
|
13 from pygments.token import Text, Operator, Keyword, Punctuation, Name, \ |
|
14 String, Number |
|
15 |
|
16 __all__ = ['ArrowLexer'] |
|
17 |
|
18 TYPES = r'\b(int|bool|char)((?:\[\])*)(?=\s+)' |
|
19 IDENT = r'([a-zA-Z_][a-zA-Z0-9_]*)' |
|
20 DECL = TYPES + r'(\s+)' + IDENT |
|
21 |
|
22 |
|
23 class ArrowLexer(RegexLexer): |
|
24 """ |
|
25 Lexer for Arrow: https://pypi.org/project/py-arrow-lang/ |
|
26 |
|
27 .. versionadded:: 2.7 |
|
28 """ |
|
29 |
|
30 name = 'Arrow' |
|
31 aliases = ['arrow'] |
|
32 filenames = ['*.arw'] |
|
33 |
|
34 tokens = { |
|
35 'root': [ |
|
36 (r'\s+', Text), |
|
37 (r'^[|\s]+', Punctuation), |
|
38 include('blocks'), |
|
39 include('statements'), |
|
40 include('expressions'), |
|
41 ], |
|
42 'blocks': [ |
|
43 (r'(function)(\n+)(/-->)(\s*)' + |
|
44 DECL + # 4 groups |
|
45 r'(\()', bygroups( |
|
46 Keyword.Reserved, Text, Punctuation, |
|
47 Text, Keyword.Type, Punctuation, Text, |
|
48 Name.Function, Punctuation |
|
49 ), 'fparams'), |
|
50 (r'/-->$|\\-->$|/--<|\\--<|\^', Punctuation), |
|
51 ], |
|
52 'statements': [ |
|
53 (DECL, bygroups(Keyword.Type, Punctuation, Text, Name.Variable)), |
|
54 (r'\[', Punctuation, 'index'), |
|
55 (r'=', Operator), |
|
56 (r'require|main', Keyword.Reserved), |
|
57 (r'print', Keyword.Reserved, 'print'), |
|
58 ], |
|
59 'expressions': [ |
|
60 (r'\s+', Text), |
|
61 (r'[0-9]+', Number.Integer), |
|
62 (r'true|false', Keyword.Constant), |
|
63 (r"'", String.Char, 'char'), |
|
64 (r'"', String.Double, 'string'), |
|
65 (r'\{', Punctuation, 'array'), |
|
66 (r'==|!=|<|>|\+|-|\*|/|%', Operator), |
|
67 (r'and|or|not|length', Operator.Word), |
|
68 (r'(input)(\s+)(int|char\[\])', bygroups( |
|
69 Keyword.Reserved, Text, Keyword.Type |
|
70 )), |
|
71 (IDENT + r'(\()', bygroups( |
|
72 Name.Function, Punctuation |
|
73 ), 'fargs'), |
|
74 (IDENT, Name.Variable), |
|
75 (r'\[', Punctuation, 'index'), |
|
76 (r'\(', Punctuation, 'expressions'), |
|
77 (r'\)', Punctuation, '#pop'), |
|
78 ], |
|
79 'print': [ |
|
80 include('expressions'), |
|
81 (r',', Punctuation), |
|
82 default('#pop'), |
|
83 ], |
|
84 'fparams': [ |
|
85 (DECL, bygroups(Keyword.Type, Punctuation, Text, Name.Variable)), |
|
86 (r',', Punctuation), |
|
87 (r'\)', Punctuation, '#pop'), |
|
88 ], |
|
89 'escape': [ |
|
90 (r'\\(["\\/abfnrtv]|[0-9]{1,3}|x[0-9a-fA-F]{2}|u[0-9a-fA-F]{4})', |
|
91 String.Escape), |
|
92 ], |
|
93 'char': [ |
|
94 (r"'", String.Char, '#pop'), |
|
95 include('escape'), |
|
96 (r"[^'\\]", String.Char), |
|
97 ], |
|
98 'string': [ |
|
99 (r'"', String.Double, '#pop'), |
|
100 include('escape'), |
|
101 (r'[^"\\]+', String.Double), |
|
102 ], |
|
103 'array': [ |
|
104 include('expressions'), |
|
105 (r'\}', Punctuation, '#pop'), |
|
106 (r',', Punctuation), |
|
107 ], |
|
108 'fargs': [ |
|
109 include('expressions'), |
|
110 (r'\)', Punctuation, '#pop'), |
|
111 (r',', Punctuation), |
|
112 ], |
|
113 'index': [ |
|
114 include('expressions'), |
|
115 (r'\]', Punctuation, '#pop'), |
|
116 ], |
|
117 } |