RabsRincon | 6ef87b5 | 2018-02-27 14:58:21 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012, Matt Godbolt |
Matt Godbolt | c60cad2 | 2014-11-26 08:24:40 -0600 | [diff] [blame] | 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. |
Matt Godbolt | da98a1e | 2012-07-03 15:45:30 -0500 | [diff] [blame] | 24 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 25 | "use strict"; |
RabsRincon | 8015e5f | 2019-07-22 15:15:58 +0200 | [diff] [blame] | 26 | var monaco = require('monaco-editor'); |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 27 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 28 | function definition() { |
| 29 | return { |
| 30 | // Set defaultToken to invalid to see what you do not tokenize yet |
| 31 | defaultToken: 'invalid', |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 32 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 33 | // C# style strings |
| 34 | escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 35 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 36 | registers: /%?\b(r[0-9]+[dbw]?|([er]?([abcd][xhl]|cs|fs|ds|ss|sp|bp|ip|sil?|dil?))|[xyz]mm[0-9]+|sp|fp|lr)\b/, |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 37 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 38 | intelOperators: /PTR|(D|Q|[XYZ]MM)?WORD/, |
Matt Godbolt | b72bd48 | 2017-01-16 12:26:26 -0600 | [diff] [blame] | 39 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 40 | tokenizer: { |
| 41 | root: [ |
| 42 | // Error document |
| 43 | [/^<.*>$/, {token: 'annotation'}], |
| 44 | // Label definition |
Matt Godbolt | eb0a45d | 2019-03-31 11:44:54 -0500 | [diff] [blame] | 45 | [/^[.a-zA-Z0-9_$?@].*:/, {token: 'type.identifier'}], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 46 | // Label definition (ARM style) |
Matt Godbolt | eb0a45d | 2019-03-31 11:44:54 -0500 | [diff] [blame] | 47 | [/^\s*[|][^|]*[|]/, {token: 'type.identifier'}], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 48 | // Label definition (CL style) |
Matt Godbolt | eb0a45d | 2019-03-31 11:44:54 -0500 | [diff] [blame] | 49 | [/^\s*[.a-zA-Z0-9_$|]*\s*(PROC|ENDP|DB|DD)/, {token: 'type.identifier'}], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 50 | // Constant definition |
Matt Godbolt | eb0a45d | 2019-03-31 11:44:54 -0500 | [diff] [blame] | 51 | [/^[.a-zA-Z0-9_$?@][^=]*=/, {token: 'type.identifier'}], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 52 | // opcode |
| 53 | [/[.a-zA-Z_][.a-zA-Z_0-9]*/, {token: 'keyword', next: '@rest'}], |
Matt Godbolt | ac8d24a | 2018-05-07 18:22:58 -0500 | [diff] [blame] | 54 | // braces and parentheses at the start of the line (e.g. nvcc output) |
| 55 | [/[(){}]/, {token: 'operator', next: '@rest'}], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 56 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 57 | // whitespace |
| 58 | {include: '@whitespace'} |
| 59 | ], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 60 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 61 | rest: [ |
| 62 | // pop at the beginning of the next line and rematch |
| 63 | [/^.*$/, {token: '@rematch', next: '@pop'}], |
Matt Godbolt | b72bd48 | 2017-01-16 12:26:26 -0600 | [diff] [blame] | 64 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 65 | [/@registers/, 'variable.predefined'], |
| 66 | [/@intelOperators/, 'annotation'], |
| 67 | // brackets |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 68 | [/[{}<>()[\]]/, '@brackets'], |
Matt Godbolt | ea7964d | 2017-02-13 22:46:24 -0600 | [diff] [blame] | 69 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 70 | // ARM-style label reference |
| 71 | [/[|][^|]*[|]*/, 'type.identifier'], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 72 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 73 | // numbers |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 74 | [/\d*\.\d+([eE][-+]?\d+)?/, 'number.float'], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 75 | [/([$]|0[xX])[0-9a-fA-F]+/, 'number.hex'], |
| 76 | [/\d+/, 'number'], |
| 77 | // ARM-style immediate numbers (which otherwise look like comments) |
| 78 | [/#-?\d+/, 'number'], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 79 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 80 | // operators |
Matt Godbolt | ac8d24a | 2018-05-07 18:22:58 -0500 | [diff] [blame] | 81 | [/[-+,*/!:&{}()]/, 'operator'], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 82 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 83 | // strings |
| 84 | [/"([^"\\]|\\.)*$/, 'string.invalid'], // non-terminated string |
| 85 | [/"/, {token: 'string.quote', bracket: '@open', next: '@string'}], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 86 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 87 | // characters |
| 88 | [/'[^\\']'/, 'string'], |
| 89 | [/(')(@escapes)(')/, ['string', 'string.escape', 'string']], |
| 90 | [/'/, 'string.invalid'], |
Matt Godbolt | b72bd48 | 2017-01-16 12:26:26 -0600 | [diff] [blame] | 91 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 92 | // Assume anything else is a label reference |
| 93 | [/%?[.?_$a-zA-Z@][.?_$a-zA-Z0-9@]*/, 'type.identifier'], |
Matt Godbolt | b72bd48 | 2017-01-16 12:26:26 -0600 | [diff] [blame] | 94 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 95 | // whitespace |
| 96 | {include: '@whitespace'} |
| 97 | ], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 98 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 99 | comment: [ |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 100 | [/[^/*]+/, 'comment'], |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 101 | [/\/\*/, 'comment', '@push'], // nested comment |
| 102 | ["\\*/", 'comment', '@pop'], |
RabsRincon | 670afd8 | 2018-02-13 14:49:07 +0100 | [diff] [blame] | 103 | [/[/*]/, 'comment'] |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 104 | ], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 105 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 106 | string: [ |
| 107 | [/[^\\"]+/, 'string'], |
| 108 | [/@escapes/, 'string.escape'], |
| 109 | [/\\./, 'string.escape.invalid'], |
| 110 | [/"/, {token: 'string.quote', bracket: '@close', next: '@pop'}] |
| 111 | ], |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 112 | |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 113 | whitespace: [ |
| 114 | [/[ \t\r\n]+/, 'white'], |
| 115 | [/\/\*/, 'comment', '@comment'], |
| 116 | [/\/\/.*$/, 'comment'], |
| 117 | [/[#;\\@].*$/, 'comment'] |
| 118 | ] |
| 119 | } |
| 120 | }; |
| 121 | } |
Matt Godbolt | 29ccf88 | 2017-01-15 10:00:42 -0600 | [diff] [blame] | 122 | |
Sebastian Staffa | a36e23c | 2019-03-11 16:32:26 +0100 | [diff] [blame] | 123 | var def = definition(); |
jaredwy | 70556a6 | 2017-11-04 16:07:29 +1100 | [diff] [blame] | 124 | monaco.languages.register({id: 'asm'}); |
Sebastian Staffa | a36e23c | 2019-03-11 16:32:26 +0100 | [diff] [blame] | 125 | monaco.languages.setMonarchTokensProvider('asm', def); |
| 126 | |
| 127 | module.exports = def; |