Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
93 changes: 93 additions & 0 deletions assets/highlighting-tests/c.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Comments
// Single-line comment

/*
* Multi-line
* comment
*/

#include <stdio.h>
#include <stdbool.h>
#define MAX_BUFFER 1024
#ifndef MY_HEADER_H
#define MY_HEADER_H
#endif

// Numbers
42;
3.14;
.5;
1e10;
1.5e-3;
0xff;
0xFF;
0b1010;
0o77;
42ul;
3.14f;

// Constants
true;
false;
NULL;

// Strings and characters
'a';
'\n';
"double quotes with escape: \" \n \t \\";

// Control flow keywords
int main(int argc, char *argv[]) {
if (argc > 1) {
// ...
} else if (argc == 0) {
// ...
} else {
// ...
}

for (int i = 0; i < 10; i++) {
if (i == 5) continue;
if (i == 8) break;
}

while (false) { }
do { } while (false);

switch (argc) {
case 1:
break;
default:
goto end;
}

end:
return 0;
}

// Other keywords and structures
struct Point {
int x;
int y;
};

typedef union {
char c;
double d;
} DataUnion;

enum Color { RED, GREEN, BLUE };

extern int global_var;
static const int MAX_RETRIES = 5;
volatile int hardware_register;
register int fast_loop_counter;

// C99/C11 specific keywords
_Atomic int atomic_counter;
_Bool is_active;
_Noreturn void die(void);

// Function calls
printf("hello\n");
sizeof(struct Point);
112 changes: 112 additions & 0 deletions assets/highlighting-tests/cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Comments
// Single-line comment

/*
* Multi-line
* comment
*/

#include <iostream>
#include <memory>
#define CONST_VAL 100

// Numbers (including C++14 digit separators)
42;
3.14;
.5;
1e10;
1.5e-3;
0xff;
0xFF;
0b1010'1100;
1'000'000;
42ull;
3.14f;

// Constants
true;
false;
NULL;
nullptr;

// Strings and Characters
'c';
'\t';
"double quotes with escape: \" \n \t \\";

// Control flow keywords
int main() {
if (true) {
} else if (false) {
} else {
}

for (int i = 0; i < 10; ++i) {
if (i == 5) continue;
if (i == 8) break;
}

while (false) { }
do { } while (false);

switch (1) {
case 1: break;
default: break;
}

try {
throw std::runtime_error("oops");
} catch (const std::exception& e) {
// handle error
}

return 0;
}

// Modern C++ keywords and features
template <typename T>
concept Addable = requires(T a, T b) {
a + b;
};

class Animal {
private:
int age;
protected:
bool is_alive;
public:
Animal() : age(0), is_alive(true) {}
virtual ~Animal() = default;
virtual void speak() const = 0;
};

class Dog final : public Animal {
public:
void speak() const override {
std::cout << "Woof!" << std::endl;
}
};

constexpr int get_magic_number() {
return 42;
}

// Other keywords
namespace my_space {
enum class State { START, STOP };

inline void helper() {
auto val = get_magic_number();
decltype(val) val_copy = val;

Dog* dog = new Dog();
delete dog;
}
}

using namespace my_space;
export module my_module;

// Function calls
std::cout << "hello\n";
sizeof(int);
18 changes: 18 additions & 0 deletions assets/highlighting-tests/markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ Reference: ![Logo][logo-ref]
echo "Hello, world" | tr a-z A-Z
```

```c
#include <stdio.h>

int main() {
printf("Hello, world\n");
return 0;
}
```

```cpp
#include <iostream>

int main() {
std::cout << "Hello, world" << std::endl;
return 0;
}
```

```javascript
export function greet(name) {
return `hello ${name}`;
Expand Down
63 changes: 63 additions & 0 deletions crates/lsh/definitions/c.lsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#[display_name = "C"]
#[path = "**/*.c"]
#[path = "**/*.h"]
pub fn c() {
until /$/ {
yield other;

if /\/\/.*/ {
yield comment;
} else if /\/\*/ {
loop {
yield comment;
await input;
if /\*\// {
yield comment;
break;
}
}
} else if /"/ {
until /$/ {
yield string;
if /\\./ {
// Skip escaped characters
} else if /"/ {
yield string;
break;
}
await input;
}
} else if /'/ {
until /$/ {
yield string;
if /\\./ {
// Skip escaped characters
} else if /'/ {
yield string;
break;
}
await input;
}
} else if /#\s*(?:include|define|undef|if|ifdef|ifndef|elif|else|endif|error|pragma|warning|line)\>/ {
yield keyword.control;
} else if /(?:break|case|continue|default|do|else|for|goto|if|return|switch|while)\>/ {
yield keyword.control;
} else if /(?:auto|char|const|double|enum|extern|float|inline|int|long|register|restrict|short|signed|sizeof|static|struct|typedef|union|unsigned|void|volatile|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local)\>/ {
yield keyword.other;
} else if /(?:true|false|NULL)\>/ {
yield constant.language;
} else if /(?i:-?(?:0x[\da-fA-F']+|0b[01']+|[\d']+\.?[\d']*|\.[\d']+)(?:p[+-]?[\d']+|e[+-]?[\d']+)?[ulfz]*)/ {
if /\w+/ {
// Invalid numeric literal
} else {
yield constant.numeric;
}
} else if /(\w+)\s*\(/ {
yield $1 as method;
} else if /\w+/ {
// Gobble word chars to align the next iteration on a word boundary.
}

yield other;
}
}
66 changes: 66 additions & 0 deletions crates/lsh/definitions/cpp.lsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#[display_name = "C++"]
#[path = "**/*.cpp"]
#[path = "**/*.hpp"]
#[path = "**/*.cc"]
#[path = "**/*.cxx"]
#[path = "**/*.hxx"]
pub fn cpp() {
until /$/ {
yield other;

if /\/\/.*/ {
yield comment;
} else if /\/\*/ {
loop {
yield comment;
await input;
if /\*\// {
yield comment;
break;
}
}
} else if /"/ {
until /$/ {
yield string;
if /\\./ {
// Skip escaped characters
} else if /"/ {
yield string;
break;
}
await input;
}
} else if /'/ {
until /$/ {
yield string;
if /\\./ {
// Skip escaped characters
} else if /'/ {
yield string;
break;
}
await input;
}
} else if /#\s*(?:include|define|undef|if|ifdef|ifndef|elif|else|endif|error|pragma|warning|line)\>/ {
yield keyword.control;
} else if /(?:break|case|catch|continue|default|do|else|for|goto|if|return|switch|throw|try|while)\>/ {
yield keyword.control;
} else if /(?:alignas|alignof|asm|auto|bool|char|char16_t|char32_t|char8_t|class|concept|const|const_cast|consteval|constexpr|constinit|decltype|delete|double|dynamic_cast|enum|explicit|export|extern|float|friend|inline|int|long|mutable|namespace|new|noexcept|operator|private|protected|public|register|reinterpret_cast|requires|short|signed|sizeof|static|static_assert|static_cast|struct|template|this|thread_local|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t)\>/ {
yield keyword.other;
} else if /(?:true|false|NULL|nullptr)\>/ {
yield constant.language;
} else if /(?i:-?(?:0x[\da-fA-F']+|0b[01']+|[\d']+\.?[\d']*|\.[\d']+)(?:p[+-]?[\d']+|e[+-]?[\d']+)?[ulfz]*)/ {
if /\w+/ {
// Invalid numeric literal
} else {
yield constant.numeric;
}
} else if /(\w+)\s*\(/ {
yield $1 as method;
} else if /\w+/ {
// Gobble word chars to align the next iteration on a word boundary.
}

yield other;
}
}
20 changes: 20 additions & 0 deletions crates/lsh/definitions/markdown.lsh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ pub fn markdown() {
if /.*/ {}
}
}
} else if /(?i:c\+\+|cpp|cc|cxx)/ {
loop {
await input;
if /\s*```/ {
return;
} else {
cpp();
if /.*/ {}
}
}
} else if /(?i:c)/ {
loop {
await input;
if /\s*```/ {
return;
} else {
c();
if /.*/ {}
}
}
} else if /(?i:diff)/ {
loop {
await input;
Expand Down