Просмотр исходного кода

Implement minimal textmate language definition (#1595)

TextMate is the go-to definition language for syntax highlighting for editors lack dedicated language support for a given programming language. This patch contains a very minimal, yet functional TextMate bundle definition for Carbon.

This allows Carbon users to import the TextMate bundle into their editors to get basic syntax highlighting for Carbon code until we have a more solid specification for the language (that's when the fun with dedicated plugins/extensions begins!).

**Support**
- String literals with escape codes (currently using C highlighting rules)
- Numeric literals in decimal, hexadecimal and binary (lacks _ separator support though)
- Single and multi-line comments
- Highlighting of all? (let me know if some are missing) keywords in the language

Here is an image of the highlighting in action in IntelliJ Dracula mode
![image](https://user-images.githubusercontent.com/42585241/180587912-23b5ae3e-2bdb-49a1-83e8-24e6e0b7cfb5.png)
Mats Larsen 3 лет назад
Родитель
Сommit
8ba4916ab4

+ 5 - 0
.pre-commit-config.yaml

@@ -163,6 +163,11 @@ repos:
           - '/*'
           - ''
           - '*/'
+          - --custom_format
+          - '\.(plist)$'
+          - '<!--'
+          - ''
+          - ' -->'
         exclude: |
           (?x)^(
               .bazelversion|

+ 29 - 0
utils/textmate/README.md

@@ -0,0 +1,29 @@
+# Textmate Language Definition
+
+<!--
+Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+Exceptions. See /LICENSE for license information.
+SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+-->
+
+This directory contains a [TextMate](https://macromates.com/) bundle which can
+be used in various editors such as TextMate, Atom, or the IntelliJ family for
+syntax highlighting of Carbon source files.
+
+If you are using TextMate, see the documentation on
+[how to install TextMate bundles](https://macromates.com/manual/en/bundles#getting_more_bundles).
+Clone the repository with Git and symlink/copy the `utils/textmate` directory to
+any of the paths TextMate will search through (you can find these paths in the
+TextMate documentation above).
+
+If you are using IntelliJ or a IntelliJ Platform product, you can find
+documentation on
+[how to install TextMate bundles in IntelliJ](https://www.jetbrains.com/help/idea/textmate.html#import-textmate-bundles).
+Clone the repository with Git and open the `utils/textmate` directory inside the
+IntelliJ TextMate Bundle window.
+
+If you are using Atom, you can convert the bundle to an Atom-compatible one. See
+[the Atom documentation on how to do that.](https://flight-manual.atom.io/hacking-atom/sections/converting-from-textmate/)
+
+For other editors that support TextMate bundles you can consult your editors
+documentation to see how to use the bundle.

+ 46 - 0
utils/textmate/Samples/main.carbon

@@ -0,0 +1,46 @@
+// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+// Exceptions. See /LICENSE for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+// More single line comments
+
+package Carbon api;
+
+interface HasValueParam(T:! Type, V:! T) {
+  fn Go[me: Self]() -> T;
+}
+
+impl () as HasValueParam(i32, 5) {
+  fn Go[me: Self]() -> i32 { return 42; }
+}
+
+class Point {
+  fn Origin() -> Self {
+    return {.x = 0, .y = 0};
+  }
+
+  var x: i32;
+  var y: i32;
+}
+
+fn Procedure() -> i32 {
+  returned var zoop: i32 = 0;
+  while (DoSomeJob() {
+    zoop += 1;
+  }
+
+  return var;
+}
+
+fn Main() -> i32 {
+  let str = "Hello world";
+  let hex = 0xABCDEF1234567890;
+  let bin = 0b0000111001010010;
+  let dec = 123456789012345678;
+
+  let big: Carbon.Int(1024) = 1234;
+  let aaa: auto = "Carbon";
+  let view: StringView = "Carbon";
+
+  return Procedure();
+}

+ 150 - 0
utils/textmate/Syntaxes/Carbon.plist

@@ -0,0 +1,150 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+Exceptions. See /LICENSE for license information.
+SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ -->
+
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
+        "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+    <dict>
+        <!-- Section metadata -->
+        <key>name</key>
+        <string>carbon</string>
+        <key>uuid</key>
+        <string>04F7F7E3-D5EE-40A4-9899-53CD34D28276</string>
+        <key>scopeName</key>
+        <string>source.carbon</string>
+        <key>fileTypes</key>
+        <array>
+            <string>carbon</string>
+        </array>
+
+        <!-- Folding markers -->
+        <key>foldingStartMarker</key>
+        <string>\{\s*$</string>
+        <key>foldingStopMarker</key>
+        <string>^\s*\}</string>
+
+        <!-- Active patterns from repository -->
+        <key>patterns</key>
+        <array>
+            <!-- Rule: comments -->
+            <dict>
+                <key>begin</key>
+                <string>(^[ \t]+)?(?=//)</string>
+                <key>end</key>
+                <string>(?!\G)</string>
+                <key>patterns</key>
+                <array>
+                    <dict>
+                        <key>name</key>
+                        <string>comment.line.carbon</string>
+                        <key>begin</key>
+                        <string>//</string>
+                        <key>end</key>
+                        <string>$</string>
+                    </dict>
+                </array>
+            </dict>
+            <!-- Rule: control flow keywords -->
+            <dict>
+                <key>name</key>
+                <string>keyword.control.carbon</string>
+                <key>match</key>
+                <string>\b(break|case|continue|default|else|if|for|match|return|then|while)\b</string>
+            </dict>
+            <!-- Rule: other keywords -->
+            <dict>
+                <key>name</key>
+                <string>keyword.other.carbon</string>
+                <key>match</key>
+                <string>\b(abstract|addr|alias|and|api|as|auto|base|choice|class|constraint|destructor|extends|external|final|forall|fn|friend|impl|import|interface|let|library|like|namespace|not|observe|or|override|package|partial|private|protected|returned|template|var|virtual|where|while|_)\b</string>
+            </dict>
+            <!-- Rule: builtin types -->
+            <dict>
+                <key>name</key>
+                <string>storage.type.carbon</string>
+                <key>match</key>
+                <string>\b(As|bool|Carbon\.Int|Carbon\.UInt|f16|f32|f64|f128|i8|i16|i32|i64|i128|i256|Slice|String|StringView|Type|u8|u16|u32|u64|u128|u256)\b</string>
+            </dict>
+            <!-- Rule: boolean literals - has keyword coloring -->
+            <dict>
+                <key>name</key>
+                <string>keyword.other.carbon</string>
+                <key>match</key>
+                <string>\b(true|false)\b</string>
+            </dict>
+            <!-- Rule: block string literals -->
+            <dict>
+                <key>name</key>
+                <string>string.quoted.triple.carbon</string>
+                <key>begin</key>
+                <string>"""</string>
+                <key>end</key>
+                <string>"""</string>
+
+                <key>patterns</key>
+                <array>
+                    <dict>
+                        <key>include</key>
+                        <string>#string_escapes</string>
+                    </dict>
+                </array>
+            </dict>
+            <!-- Rule: basic string literals -->
+            <dict>
+                <key>name</key>
+                <string>string.quoted.double.carbon</string>
+                <key>begin</key>
+                <string>"</string>
+                <key>end</key>
+                <string>"</string>
+
+                <key>patterns</key>
+                <array>
+                    <dict>
+                        <key>include</key>
+                        <string>#string_escapes</string>
+                    </dict>
+                </array>
+            </dict>
+            <!-- Rule: hexadecimal number literals -->
+            <dict>
+                <key>name</key>
+                <string>constant.numeric.carbon</string>
+                <key>match</key>
+                <string>0x[_0-9A-F]+(\.[_0-9A-F]+(p[-+]?[1-9][0-9]*)?)?</string>
+            </dict>
+            <!-- Rule: binary number literals -->
+            <dict>
+                <key>name</key>
+                <string>constant.numeric.carbon</string>
+                <key>match</key>
+                <string>0b[_01]+</string>
+            </dict>
+            <!-- Rule: decimal number literals -->
+            <dict>
+                <key>name</key>
+                <string>constant.numeric.carbon</string>
+                <key>match</key>
+                <string>[1-9][_0-9]*(\.[_0-9]+(e[-+]?[1-9][0-9]*)?)?</string>
+            </dict>
+        </array>
+
+        <!-- Rule repository -->
+        <key>repository</key>
+        <dict>
+            <!-- Rule: string escape sequences -->
+            <key>string_escapes</key>
+            <dict>
+                <key>name</key>
+                <string>constant.character.escape.carbon</string>
+                <key>match</key>
+                <string>\([tnr'"0\0]|x[0-9A-F]{2}|u\{[0-9A-F]{4,}\})</string>
+            </dict>
+        </dict>
+    </dict>
+</plist>

+ 27 - 0
utils/textmate/info.plist

@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+Part of the Carbon Language project, under the Apache License v2.0 with LLVM
+Exceptions. See /LICENSE for license information.
+SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ -->
+
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+    <key>contactName</key>
+    <string>Carbon Developers</string>
+
+    <key>description</key>
+    <string>Support for Carbon Language</string>
+
+    <key>name</key>
+    <string>carbon</string>
+
+    <key>ordering</key>
+    <array>
+        <string>04F7F7E3-D5EE-40A4-9899-53CD34D28276</string>
+    </array>
+
+    <key>uuid</key>
+    <string>6152A817-84AE-47A8-96DB-0B50DF2EEC10</string>
+</plist>