Skip to content

Commit

Permalink
Lezer syntax highlighting (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdinosaur committed Sep 20, 2023
1 parent 3a47beb commit ce6066e
Show file tree
Hide file tree
Showing 25 changed files with 3,677 additions and 238 deletions.
6 changes: 3 additions & 3 deletions ast/src/expression.rs
Expand Up @@ -19,6 +19,9 @@ pub enum Expression {
/// Literal number.
Number(Decimal),

/// A named local variable.
Identifier(String),

/// Literal list.
List(Vec<SpannedExpression>),

Expand All @@ -31,9 +34,6 @@ pub enum Expression {
body: Box<SpannedExpression>,
},

/// A named local variable.
Identifier(String),

/// An operation on a single [`Expression`] operand with an [`Operator`]
Unary {
right: Box<SpannedExpression>,
Expand Down
43 changes: 24 additions & 19 deletions docs/next.config.mjs
@@ -1,35 +1,40 @@
/* eslint-env node */

import { getHighlighter, BUNDLED_LANGUAGES } from 'shiki'
import Nextra from 'nextra'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import rehypeHighlightCodeBlock from '@mapbox/rehype-highlight-code-block'
import { fromLezer } from 'hast-util-from-lezer'
import { parser as rimuParser } from 'rimu-lezer'
import { toHtml } from 'hast-util-to-html'

const __dirname = dirname(fileURLToPath(import.meta.url))
const lezerParsers = {
rimu: rimuParser,
}

const withNextra = Nextra({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.js',
unstable_staticImage: true,
codeHighlight: false,
mdxOptions: {
rehypePrettyCodeOptions: {
getHighlighter: (options) =>
getHighlighter({
...options,
langs: [
...BUNDLED_LANGUAGES,
{
id: 'rimu',
scopeName: 'source.rimu',
aliases: [],
path: join(__dirname, '../syntax/textmate/rimu.tmLanguage.json'),
},
],
}),
},
rehypePlugins: [
[
rehypeHighlightCodeBlock,
{
highlight,
},
],
],
},
})

const config = withNextra()

export default config

function highlight(code, lang) {
const parser = lezerParsers[lang]
if (parser == null) return code
const tree = parser.parse(code)
const element = fromLezer(code, tree)
return toHtml(element)
}
6 changes: 5 additions & 1 deletion docs/package.json
Expand Up @@ -9,10 +9,14 @@
"start": "next start"
},
"dependencies": {
"@mapbox/rehype-highlight-code-block": "^1.0.0",
"hast-util-from-lezer": "^0.5.2",
"hast-util-to-html": "^9.0.0",
"next": "13.4.19",
"nextra": "2.11.1",
"nextra-theme-docs": "2.11.1",
"react": "18.2.0",
"react-dom": "18.2.0"
"react-dom": "18.2.0",
"rimu-lezer": "file:../syntax/lezer"
}
}
5 changes: 5 additions & 0 deletions docs/pages/_app.js
@@ -0,0 +1,5 @@
import '../styles.css'

export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
35 changes: 23 additions & 12 deletions docs/pages/language/blocks.mdx
Expand Up @@ -93,18 +93,29 @@ TODO

### `if`

<iframe
width="100%"
height="256"
style={{ marginTop: "2rem" }}
src="https://play.rimu.dev/?i=bRYxBCoQwDEWv8skJnFkKepJsKkRUtJE2Za5v28C4-3mPPAr3fcrIEVjUTK8-gX3Fd8CMz-C3bRLBdEiImcmZnFkqs6QlS-p4UbXcEzXAFJgwTT4abBX__e22jVWsJblqsVdpsb-lBw"
/>
```rimu
apple:
bottom:
if 20 > 10
then "jeans"
else "trousers"
boots:
if "a" == "a"
then
with: "fur"
else
without: "fur"
```

### `let`

<iframe
width="100%"
height="256"
style={{ marginTop: "2rem" }}
src="https://play.rimu.dev/?i=bU8pJLYnJU1BITEmxUtBI1FFI0lSwtVNIVNBWSAKJF5cmlRQlJpcgSYKEgRoUdEEqMvOQVUGkdEGmaRga6CgYGWjChIwNlAA"
/>
```rimu
let
add: (a, b) => a + b
subtract: (a, b) =>
a - b
in
subtract
- add(10, 20)
- 30
```

1 comment on commit ce6066e

@vercel
Copy link

@vercel vercel bot commented on ce6066e Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.