Skip to content

Commit 9f854e9

Browse files
codeforth:0.1.0 (#3990)
1 parent a8340a1 commit 9f854e9

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Alexander Brevig
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# codeforth
2+
3+
Forth syntax highlighting for [Typst](https://typst.app/).
4+
5+
## Installation
6+
7+
````typst
8+
#import "@preview/codeforth:0.1.0": forth-syntax
9+
#set raw(syntaxes: forth-syntax)
10+
11+
```forth
12+
: fun 1 2 + ;
13+
```
14+
````
15+
16+
## Credits
17+
18+
- Syntax patterns informed by [mitranim/sublime-forth](https://github.com/mitranim/sublime-forth)
19+
20+
## License
21+
22+
MIT
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
%YAML 1.2
2+
---
3+
# Forth syntax highlighting for Typst
4+
# Based on: https://github.com/mitranim/sublime-forth
5+
# Adapted for Typst's regex engine compatibility
6+
name: Forth
7+
file_extensions: [f, fs, fth, 4th, forth]
8+
scope: source.forth
9+
10+
contexts:
11+
prototype:
12+
- include: comments
13+
14+
main:
15+
- include: strings
16+
- include: numbers
17+
- include: definitions
18+
- include: control-flow
19+
- include: stack-manipulation
20+
- include: arithmetic
21+
- include: memory
22+
- include: variables-constants
23+
- include: io
24+
- include: comparison
25+
26+
comments:
27+
# Line comment with backslash
28+
- match: '\\.*$'
29+
scope: comment.line.forth
30+
# Block comment
31+
- match: '\(\s'
32+
push:
33+
- meta_scope: comment.block.forth
34+
- match: '\)'
35+
pop: true
36+
37+
strings:
38+
# Print string ."
39+
- match: '\.\"\s'
40+
push:
41+
- meta_scope: string.quoted.double.forth
42+
- match: '"'
43+
pop: true
44+
# String literal s"
45+
- match: 's\"\s'
46+
scope: support.function.forth
47+
push:
48+
- meta_scope: string.quoted.double.forth
49+
- match: '"'
50+
pop: true
51+
# Character literals
52+
- match: '\bchar\b'
53+
scope: keyword.other.forth
54+
- match: '\bchars\b'
55+
scope: support.function.memory.forth
56+
57+
numbers:
58+
# Hexadecimal with $ prefix
59+
- match: '\$[0-9a-fA-F]+'
60+
scope: constant.numeric.hex.forth
61+
# Hexadecimal with 0x prefix
62+
- match: '\b0x[0-9a-fA-F]+\b'
63+
scope: constant.numeric.hex.forth
64+
# Binary with % prefix
65+
- match: '%[01]+'
66+
scope: constant.numeric.binary.forth
67+
# Decimal (negative or positive)
68+
- match: '-?\b[0-9]+\b'
69+
scope: constant.numeric.decimal.forth
70+
71+
definitions:
72+
# Colon definition - word name follows
73+
- match: ':\s+(\S+)'
74+
captures:
75+
0: keyword.control.definition.forth
76+
1: entity.name.function.forth
77+
# Semicolon definition end
78+
- match: '\s;\s'
79+
scope: keyword.control.definition.forth
80+
- match: '\s;$'
81+
scope: keyword.control.definition.forth
82+
# Immediate
83+
- match: '\bimmediate\b'
84+
scope: keyword.other.forth
85+
# Postpone and compile
86+
- match: '\b(postpone|compile|literal)\b'
87+
scope: keyword.other.forth
88+
89+
control-flow:
90+
- match: '\b(if|else|then|endif)\b'
91+
scope: keyword.control.conditional.forth
92+
- match: '\b(begin|until|while|repeat|again)\b'
93+
scope: keyword.control.loop.forth
94+
- match: '\b(do|loop|leave|unloop)\b'
95+
scope: keyword.control.loop.forth
96+
- match: '\b(case|of|endof|endcase)\b'
97+
scope: keyword.control.switch.forth
98+
- match: '\b(exit|quit|abort)\b'
99+
scope: keyword.control.flow.forth
100+
- match: '\b(recurse|execute|evaluate)\b'
101+
scope: keyword.control.forth
102+
103+
stack-manipulation:
104+
- match: '\b(dup|drop|swap|over|rot|nip|tuck|pick|roll|depth)\b'
105+
scope: support.function.stack.forth
106+
- match: '\b(2dup|2drop|2swap|2over|2rot)\b'
107+
scope: support.function.stack.forth
108+
- match: '\b-rot\b'
109+
scope: support.function.stack.forth
110+
111+
arithmetic:
112+
- match: '\b(mod|negate|abs|min|max)\b'
113+
scope: keyword.operator.arithmetic.forth
114+
- match: '\b(and|or|xor|invert|lshift|rshift)\b'
115+
scope: keyword.operator.bitwise.forth
116+
117+
memory:
118+
- match: '\b(here|allot|cells|align|aligned)\b'
119+
scope: support.function.memory.forth
120+
- match: '\b(move|fill|erase|cmove)\b'
121+
scope: support.function.memory.forth
122+
123+
variables-constants:
124+
- match: '\b(variable|constant|value|2variable|2constant)\b'
125+
scope: storage.type.forth
126+
- match: '\b(create)\b'
127+
scope: storage.type.forth
128+
- match: '\bto\b'
129+
scope: keyword.other.forth
130+
- match: '\b(true|false)\b'
131+
scope: constant.language.forth
132+
133+
io:
134+
- match: '\b(emit|type|cr|space|spaces)\b'
135+
scope: support.function.io.forth
136+
- match: '\b(key|accept|expect)\b'
137+
scope: support.function.io.forth
138+
- match: '\b(open-file|close-file|read-file|write-file|read-line|write-line)\b'
139+
scope: support.function.io.forth
140+
- match: '\b(include|require|included)\b'
141+
scope: keyword.control.import.forth
142+
143+
comparison:
144+
- match: '\b(within)\b'
145+
scope: keyword.operator.comparison.forth
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#let forth-syntax = read("forth.sublime-syntax", encoding: none)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "codeforth"
3+
version = "0.1.0"
4+
entrypoint = "lib.typ"
5+
authors = ["Alexander Brevig"]
6+
license = "MIT"
7+
description = "Syntax highlighting support for the Forth programming language."
8+
repository = "https://github.com/AlexanderBrevig/typst-forth"
9+
keywords = ["syntax-highlighting", "forth"]
10+
categories = ["text"]
11+
exclude = ["example.*"]
12+
compiler = "0.13.0"

0 commit comments

Comments
 (0)