treehouse/content/treehouse/dev/syntax-highlighting.tree
2024-03-25 22:07:52 +01:00

195 lines
5.4 KiB
Text

%% title = "syntax highlighting gallery"
% id = "01HRT0DG7VSMZCX4PPCKE3ZCKN"
- this is a page demonstrating syntaxes supported by the treehouse
% id = "01HRT0DG7VF31P185J898QQH85"
- really there's not much more to it, but I use it for debugging + with it you can get a general feel for how I highlight things in the treehouse
% id = "01HRT0DG7VN5TH971H7W8AT8YY"
- `javascript`
% id = "01HRT0DG7V9PAY44NMYVMF7B63"
- patterns
```javascript
// This is a single-line comment.
/* This is
a multiline comment. */
ident Class CONSTANT funciton()
0b1010 0o01234567 0x0123456789ABCDEF
01234567
1.0 1.41e-3 1.42E+4 1.43e1
'string' "string" `string`
+ - * / == != <= >= ! ~ || && . ? :
, ;
```
% id = "01HRT0DG7VM2MV9YA5D694WA8Y"
- keywords
```javascript
as async await break case catch class const continue debugger default delete do else export
extends finally for from function get if import in instanceof let new of return set static
switch throw try typeof var void while with yield
super this
false true undefined null
```
% id = "01HRT0DG7VA7MA87JR7MGQW294"
- sample code
```javascript
// t is an existing tile index; variable name is short for brevity
export function removeRedundancies(t) {
if (isSet(t, SE) && (!isSet(t, S) || !isSet(t, E))) {
t &= ~SE;
}
if (isSet(t, SW) && (!isSet(t, S) || !isSet(t, W))) {
t &= ~SW;
}
if (isSet(t, NW) && (!isSet(t, N) || !isSet(t, W))) {
t &= ~NW;
}
if (isSet(t, NE) && (!isSet(t, N) || !isSet(t, E))) {
t &= ~NE;
}
return t;
}
```
% id = "01HRT0DG7V51ENS54V2QFEAAF0"
+ edge cases
% id = "01HRT0DG7VHX3VJ861W5VF0FNP"
- ```javascript
"cause i don't need"
many_words "many words"
'how can we connect this way'
when_i_dont_understand 'the words you say'
```
```javascript
hello "string\"escape" world
```
this one is tricky because it requires a whopping five backslashes in the JSON syntax definition, and I initially had the pattern defined as:
```json
{ "regex": "\"(\\\"|[^\"])*\"", "is": "string" }
```
which produced the following regex:
```regex
"(\"|[^"])*"
```
escaping the `"` in the process, therefore producing the following regex:
```regex
"("|[^"])*"
```
which can be reduced to:
```regex
"([^])*"
```
note that `[^]` is not the same as `.`, because the latter omits newlines.
% id = "01HRT0W4AKQJEAMJ8XHJVMY4ZV"
- `json`
% id = "01HRT0W4AKPFENNFWMA7AAGN5Z"
- patterns
```json
abcd
0.912392198e+2113
"abcd":
"abcd"
,
```
% id = "01HRT0W4AK8D1W9ZN9HJTHC85Q"
- keywords
```json
null true false
```
% id = "01HRT0W4AK4F2S65M1NT75PCGQ"
- sample
```json
{
"patterns": [
{ "regex": "[a-zA-Z_][a-zA-Z0-9_]*", "is": "error" },
{ "regex": "[0-9]+(\\.[0-9]*([eE][-+]?[0-9]+)?)?", "is": "literal" },
{
"regex": "\"(\\\\\"|[^\"])*\"(:)",
"is": { "default": "keyword2", "captures": ["keyword2", "punct"] }
},
{ "regex": "\"(\\\\\"|[^\"])*\"", "is": "string" },
{ "regex": "[,]", "is": "punct" }
],
"keywords": {
"null": { "into": "literal" },
"true": { "into": "literal" },
"false": { "into": "literal" }
}
}
```
- `lua`
- patterns
```lua
-- single-line comment
--[[
multi-line comment
NOTE: comments with [==[ ]==] are not supported due to a lack of backreference support
in Rust regex
]]
'string' "string" [[multiline
string]]
0xABCD 0xA.B 0xAp+3 0xCE.DE5p-2
123 1.0 1.41e-3 1.42E+4 1.43e1
<bye_egg>
...
+ - * / % ^ == ~= <= >= #
funciton() ident
```
- keywords
```lua
if then else elseif end do function repeat until while for break return local in not and or goto
self
true false nil
<close> <const>
```
- sample
```lua
-- Ticks the scheduler: executes every active fiber, removes inactive fibers,
-- and ignores sleeping fibers.
--
-- Extra arguments passed to this function are passed to all running fibers.
function Scheduler:tick(...)
local time = timer.getTime()
local i = 1
while i <= #self.fibers do
local fiber = self.fibers[i]
local coro = fiber.coro
if time >= fiber.wakeAt then
local ok, result = coroutine.resume(coro, ...)
if not ok then
error("scheduler for '"..self.name.."': "..
"ticking '"..fiber.name.."' failed with an error\n"..result)
else
if coroutine.status(coro) == "dead" then
self.fibers[i] = self.fibers[#self.fibers]
self.fibers[#self.fibers] = nil
i = i - 1
elseif result ~= nil and result > 0 then
fiber.wakeAt = time + result
end
end
end
i = i + 1
end
end
```