blob: 6defad6542e70777cf562bceb8ac6cea94f10491 [file] [log] [blame] [raw]
Marc Tiehuisffd04ac2018-09-07 17:28:13 +12001// Copyright (c) 2018, Marc Tiehuis
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6//
7// * Redistributions of source code must retain the above copyright notice,
8// this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23// POSSIBILITY OF SUCH DAMAGE.
24
25"use strict";
RabsRincon8015e5f2019-07-22 15:15:58 +020026var monaco = require('monaco-editor');
Marc Tiehuisffd04ac2018-09-07 17:28:13 +120027
28function definition() {
29 return {
30 defaultToken: 'invalid',
31
32 keywords: [
33 'const', 'var', 'extern', 'packed', 'export', 'pub', 'noalias', 'inline',
emekoi5b0f7852019-10-17 08:28:13 -050034 'comptime', 'nakedcc', 'stdcallcc', 'volatile', 'align', 'section', 'linksection',
Marc Tiehuisffd04ac2018-09-07 17:28:13 +120035 'struct', 'enum', 'union',
36 'break', 'return', 'continue', 'asm', 'defer', 'errdefer', 'unreachable',
37 'try', 'catch', 'async', 'await', 'suspend', 'resume', 'cancel',
38 'if', 'else', 'switch', 'and', 'or', 'orelse',
39 'while', 'for',
emekoi5b0f7852019-10-17 08:28:13 -050040 'null', 'undefined',
41 'fn', 'usingnamespace', 'use', 'test',
42 'this',
Marc Tiehuisffd04ac2018-09-07 17:28:13 +120043 ],
44 typeKeywords: [
emekoi5b0f7852019-10-17 08:28:13 -050045 'bool', 'f32', 'f64', 'f128', 'void', 'noreturn', 'type', 'error', 'anyerror', 'promise', 'anyframe',
Marc Tiehuisffd04ac2018-09-07 17:28:13 +120046 'isize', 'usize', 'c_short', 'c_ushort', 'c_int', 'c_uint', 'c_long', 'c_ulong',
47 'c_longlong', 'c_ulonglong', 'c_longdouble', 'c_void'
48 ],
49 operators: [
50 '+', '+%', '-', '-%', '/', '*', '*%', '=', '^', '&', '?', '|',
51 '!', '>', '<', '%', '<<', '<<%', '>>',
52 '+=', '+%=', '-=', '-%=', '/=', '*=', '*%=', '==', '^=', '&=',
53 '?=', '|=', '!=', '>=', '<=', '%=', '<<=', '<<%=', '>>='
54 ],
55
56 symbols: /[=><!~?:&|+\-*/^%]+/,
57
58 escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
59
60 tokenizer: {
61 root: [
62 // u0/i0 integer types
63 [/[iu]\d+/, 'keyword'],
64
65 // identifiers and keywords
66 [/[a-z_$][\w$]*/, {
67 cases: {
68 '@typeKeywords': 'keyword',
69 '@keywords': 'keyword',
70 '@default': 'identifier'
71 }
72 }],
73
74 [/@[a-zA-Z_$]*/, 'builtin.identifier'],
75
76 [/[A-Z][\w$]*/, 'type.identifier'], // to show class names nicely
77
78 // whitespace
79 {include: '@whitespace'},
80
81 // delimiters and operators
82 [/[{}()[\]]/, '@brackets'],
83 [/[<>](?!@symbols)/, '@brackets'],
84 [/@symbols/, {
85 cases: {
86 '@operators': 'operator',
87 '@default': ''
88 }
89 }],
90
91 // numbers
92 [/\d*\.\d+([eE][-+]?\d+)?[fFdD]?/, 'number.float'],
93 [/0[xX][0-9a-fA-F_]*[0-9a-fA-F][Ll]?/, 'number.hex'],
94 [/0o[0-7_]*[0-7][Ll]?/, 'number.octal'],
95 [/0[bB][0-1_]*[0-1][Ll]?/, 'number.binary'],
96 [/\d+/, 'number'],
97
98 // delimiter: after number because of .\d floats
99 [/[;,.]/, 'delimiter'],
100
101 // strings
102 [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-teminated string
103 [/c?\\\\.*$/, 'string'],
104 [/c?"/, 'string', '@string'],
105
106 // characters
107 [/'[^\\']'/, 'string'],
108 [/(')(@escapes)(')/, ['string', 'string.escape', 'string']],
109 [/'/, 'string.invalid']
110 ],
111
112 whitespace: [
113 [/[ \r\n]+/, 'white'],
114 [/\/\*/, 'comment', '@comment'],
115 [/\/\+/, 'comment', '@comment'],
116 [/\/\/.*$/, 'comment'],
117 [/\t/, 'comment.invalid']
118 ],
119
120 comment: [
121 [/[^/*]+/, 'comment'],
122 [/\/\*/, 'comment.invalid'],
123 [/[/*]/, 'comment']
124 ],
125
126 string: [
127 [/[^\\"]+/, 'string'],
128 [/@escapes/, 'string.escape'],
129 [/\\./, 'string.escape.invalid'],
130 [/"/, 'string', '@pop']
131 ]
132 }
133 };
134}
135
136monaco.languages.register({id: 'zig'});
137monaco.languages.setMonarchTokensProvider('zig', definition());