|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.whiley |
|
4 ~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for the Whiley language. |
|
7 |
|
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, bygroups, words |
|
13 from pygments.token import Comment, Keyword, Name, Number, Operator, \ |
|
14 Punctuation, String, Text |
|
15 |
|
16 __all__ = ['WhileyLexer'] |
|
17 |
|
18 |
|
19 class WhileyLexer(RegexLexer): |
|
20 """ |
|
21 Lexer for the Whiley programming language. |
|
22 |
|
23 .. versionadded:: 2.2 |
|
24 """ |
|
25 name = 'Whiley' |
|
26 filenames = ['*.whiley'] |
|
27 aliases = ['whiley'] |
|
28 mimetypes = ['text/x-whiley'] |
|
29 |
|
30 # See the language specification: |
|
31 # http://whiley.org/download/WhileyLanguageSpec.pdf |
|
32 |
|
33 tokens = { |
|
34 'root': [ |
|
35 # Whitespace |
|
36 (r'\s+', Text), |
|
37 |
|
38 # Comments |
|
39 (r'//.*', Comment.Single), |
|
40 # don't parse empty comment as doc comment |
|
41 (r'/\*\*/', Comment.Multiline), |
|
42 (r'(?s)/\*\*.*?\*/', String.Doc), |
|
43 (r'(?s)/\*.*?\*/', Comment.Multiline), |
|
44 |
|
45 # Keywords |
|
46 (words(( |
|
47 'if', 'else', 'while', 'for', 'do', 'return', |
|
48 'switch', 'case', 'default', 'break', 'continue', |
|
49 'requires', 'ensures', 'where', 'assert', 'assume', |
|
50 'all', 'no', 'some', 'in', 'is', 'new', |
|
51 'throw', 'try', 'catch', 'debug', 'skip', 'fail', |
|
52 'finite', 'total'), suffix=r'\b'), Keyword.Reserved), |
|
53 (words(( |
|
54 'function', 'method', 'public', 'private', 'protected', |
|
55 'export', 'native'), suffix=r'\b'), Keyword.Declaration), |
|
56 # "constant" & "type" are not keywords unless used in declarations |
|
57 (r'(constant|type)(\s+)([a-zA-Z_]\w*)(\s+)(is)\b', |
|
58 bygroups(Keyword.Declaration, Text, Name, Text, Keyword.Reserved)), |
|
59 (r'(true|false|null)\b', Keyword.Constant), |
|
60 (r'(bool|byte|int|real|any|void)\b', Keyword.Type), |
|
61 # "from" is not a keyword unless used with import |
|
62 (r'(import)(\s+)(\*)([^\S\n]+)(from)\b', |
|
63 bygroups(Keyword.Namespace, Text, Punctuation, Text, Keyword.Namespace)), |
|
64 (r'(import)(\s+)([a-zA-Z_]\w*)([^\S\n]+)(from)\b', |
|
65 bygroups(Keyword.Namespace, Text, Name, Text, Keyword.Namespace)), |
|
66 (r'(package|import)\b', Keyword.Namespace), |
|
67 |
|
68 # standard library: https://github.com/Whiley/WhileyLibs/ |
|
69 (words(( |
|
70 # types defined in whiley.lang.Int |
|
71 'i8', 'i16', 'i32', 'i64', |
|
72 'u8', 'u16', 'u32', 'u64', |
|
73 'uint', 'nat', |
|
74 |
|
75 # whiley.lang.Any |
|
76 'toString'), suffix=r'\b'), Name.Builtin), |
|
77 |
|
78 # byte literal |
|
79 (r'[01]+b', Number.Bin), |
|
80 |
|
81 # decimal literal |
|
82 (r'[0-9]+\.[0-9]+', Number.Float), |
|
83 # match "1." but not ranges like "3..5" |
|
84 (r'[0-9]+\.(?!\.)', Number.Float), |
|
85 |
|
86 # integer literal |
|
87 (r'0x[0-9a-fA-F]+', Number.Hex), |
|
88 (r'[0-9]+', Number.Integer), |
|
89 |
|
90 # character literal |
|
91 (r"""'[^\\]'""", String.Char), |
|
92 (r"""(')(\\['"\\btnfr])(')""", |
|
93 bygroups(String.Char, String.Escape, String.Char)), |
|
94 |
|
95 # string literal |
|
96 (r'"', String, 'string'), |
|
97 |
|
98 # operators and punctuation |
|
99 (r'[{}()\[\],.;]', Punctuation), |
|
100 (u'[+\\-*/%&|<>^!~@=:?' |
|
101 # unicode operators |
|
102 u'\u2200\u2203\u2205\u2282\u2286\u2283\u2287' |
|
103 u'\u222A\u2229\u2264\u2265\u2208\u2227\u2228' |
|
104 u']', Operator), |
|
105 |
|
106 # identifier |
|
107 (r'[a-zA-Z_]\w*', Name), |
|
108 ], |
|
109 'string': [ |
|
110 (r'"', String, '#pop'), |
|
111 (r'\\[btnfr]', String.Escape), |
|
112 (r'\\u[0-9a-fA-F]{4}', String.Escape), |
|
113 (r'\\.', String), |
|
114 (r'[^\\"]+', String), |
|
115 ], |
|
116 } |