Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lezer syntax highlighting #59

Merged
merged 25 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4771522
start writing a syntax highlighter with lezer
ahdinosaur Sep 19, 2023
c5d01e7
so close but feels so far
ahdinosaur Sep 19, 2023
937b67a
okay so that works
ahdinosaur Sep 19, 2023
4fbf510
hmm
ahdinosaur Sep 19, 2023
d0227c1
use Python lezer parser code and wow this works now
ahdinosaur Sep 19, 2023
421f3aa
kinda hacky but passed the nested list mania test
ahdinosaur Sep 19, 2023
7dce1e3
oh wow this might actually work
ahdinosaur Sep 19, 2023
ead8a8b
sweet progress
ahdinosaur Sep 19, 2023
402b63d
doo dah dooooo
ahdinosaur Sep 19, 2023
19fafc6
not tested, but is that the whole language?
ahdinosaur Sep 19, 2023
83ee773
highlights
ahdinosaur Sep 19, 2023
759d7ba
hot damn
ahdinosaur Sep 19, 2023
e57c43f
better docs highlighting with lezer
ahdinosaur Sep 20, 2023
155fee5
do dah dooooo
ahdinosaur Sep 20, 2023
46f2eb8
automatic "Open in Playground" for every code block
ahdinosaur Sep 20, 2023
22cca09
heyo
ahdinosaur Sep 20, 2023
8453593
do da doo
ahdinosaur Sep 20, 2023
b641773
why are the tests failing? when did that happen
ahdinosaur Sep 20, 2023
4e465ff
fix precedence. i still do not really understand that
ahdinosaur Sep 20, 2023
b1fab5e
write some lezer tests, fix a bug i think?
ahdinosaur Sep 20, 2023
2c40bba
fix open to playground
ahdinosaur Sep 20, 2023
9211fc3
learn something new every day: the block call function is an expression
ahdinosaur Sep 20, 2023
5a81b2a
remove unnecessary lock
ahdinosaur Sep 20, 2023
5c1cf2d
is this what i should do?
ahdinosaur Sep 20, 2023
ada25fc
remove leftovers
ahdinosaur Sep 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ast/src/expression.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
```
Loading