Files
carbon-lang/highlighting/highlightjs_example.html
T
Chandler Carruthandjosh11b 1f075a1348 Add a language definition for highlight.js. (#1365)
There are still quite a few parts of the language that need support
here, but I've tried to piece together a decent foundation and address
some of the particular challenges with building a good framework that
handles some of the complex parts of the grammar such as successfully
identifying the declared names in even reasonably complex patterns.

I've included a pretty ad-hoc test file I've been using to make sure it
works. If folks want a particular testing strategy for this, happy to
explore building one although I don't really know what it should look
like.

Also happy to have suggestions about where this should live in the
repository. No strong opinions there. Hoping to eventually add
a `tmLanguage` file and any other syntax highlighting systems that are
useful.

I'll next be finishing off other parts of the language, and then I plan
to use this as a component of our presentation material, which is why
I'm focused on Highlight.js -- that's the system used most widely in
HTML-based presentation infrastructure like Reveal.js.

Co-authored-by: josh11b <josh11b@users.noreply.github.com>
2022-07-07 23:20:29 -07:00

85 lines
1.8 KiB
HTML

<!--
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
-->
<!-- Grab a released highlight.js from one of its CDNs. -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/ir-black.min.css"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
<script type="module">
import Carbon from './highlightjs_carbon_lang.js';
hljs.registerLanguage('Carbon', Carbon);
hljs.highlightAll();
</script>
<pre><code class="language-carbon">
package Sorting api;
import Carbon library "Printing";
fn Partition[T:! Comparable & Movable](s: Slice(T))
-> i64 {
var i: i64 = -1;
for (e: T in s) {
if (e <= s.Last()) {
++i;
Swap(&s[i], &e);
}
}
return i;
}
fn QuickSort[T:! Comparable & Movable](s: Slice(T)) {
if (s.Size() <= 1) {
return;
}
let p: i64 = Partition(s);
QuickSort(s[:p - 1]));
QuickSort(s[p + 1:]));
}
interface WidgetI {
}
constraint WidgetC {
}
base class Widget {
fn Print[addr me: Self*](var i: i64, x: f32, _: bool);
}
fn Widget.Print[addr me: Self*](var i: i64, x: f32, _: bool, 47) {
Carbon.Print("test" + "t\u{FFFF}his");
Carbon.Print(
"""
testing more
with weird stuff \
that does stuff
""");
Carbon.Print(
"""c++
template &lt;typename T&gt; class C {};
""");
}
class FancyWidget(T:! Type) extends Widget {
fn Print[me: Self]();
impl as Add(T) {
fn Op[me: Self]();
}
}
fn FancyWidget(T:! Type).Print[me: Self]() {
Carbon.Print("more test");
}
impl forall [T:! Type] FancyWidget(T) as Printable {
alias Print = FancyWidget(T).Print;
}
</code></pre>