c
patterns
c #include <stdio.h> #define SOMETHING_SOMETHING
// a comment /* a multiline comment */
function() struct S enum E union U u8’ą’ u’g’ U’g’ L’g’ u8”UTF-8” u”UTF-16” U”UTF-32” L”wchar_t” ident
0b1010’1010 0b1010’1010u 0b1010’1010llu 0b1010’1010wb 0xDeadBeef 012345l 123ull 127wb 3.14159265 3.141592f 1.0e-4d 0xa.dp+2
. -> ++ – & * + - ~ ! / % << >> < > <= >= == != ^ | && || ? : :: ; … = *= /= %= += -= <<= >>= &= ^= |= , # ## <: :> <% %> %: %:%:
keywords
c alignas alignof auto break case const constexpr continue default do else extern for goto if inline register restrict return sizeof static static_assert switch thread_local typedef typeof typeof_unqual volatile while _Generic _Noreturn
bool char double float int long short signed struct unsigned union void _Atomic _BitInt _Complex _Decimal128 _Decimal32 _Decimal64 _Imaginary
nullptr false true
sample
c #include <snug/bump.h>
#include <snug/panic.h>
void bumpinit(struct bump* a, void* slab, i32 size) { a->start = slab; a->ptr = slab + size; #if SNUGGLESBUMP_TRACKING a->tracker = null; #endif }
void* bumptryalloc(struct bump* a, i32 size, const char* what) { // Allocate n bytes and align the pointer to 16 bytes. a->ptr = (void*)((long long)(a->ptr - size) & ~0xF);
void* addr; // TODO: Because this check is done after the allocation, this will eventually start // overflowing. Not good, but not important either because most allocations // use bumpallocor_panic. if (a->ptr < a->start) { addr = null; } else { addr = a->ptr; }
#if SNUGGLESBUMPTRACKING if (a->tracker) { (a->tracker)(addr, size, what); } #endif
return addr; }
void* bumpallocor_panic(struct bump* a, i32 size, const char* what) { (void)what; // Currently unused, may use for panic message in the future.
void* p = bumptryalloc(a, size, what); b32 allocationsucceeded = p != 0; ASSERT(allocationsucceeded, “out of memory”); return p; }
javascript
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; }
edge cases
"cause i don't need" many_words "many words" 'how can we connect this way' when_i_dont_understand 'the words you say'
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.
json
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
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
rust
patterns
rust // this is a comment /* this is a multiline comment */
“string” ‘c’ ‘ł’ b”string” b’ł’ r”string” r#”string”# r##”string”## r###”string”### 0b11001100 0b11001100u8 0o1234567f32 0xDEADBEEF 0xDEADBEEF_i16 2137 3.14159265 2.3e-32 + - * / % == ~= <= >= & . #![doc = ““] identifier macro! function() ’static T Vec<i32> union Example europeanunion A
keywords
rust _ as async await break const continue dyn else enum extern fn for if impl in let loop macro_rules! match mod move mut pub ref return static struct trait type unsafe use where while
crate self Self super
true false
abstract become box do final macro override priv try typeof unsized virtual yield
sample
rust use chrono::{Datelike, Utc}; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)] #[serde(renameall = “snakecase”)] pub enum Season { Spring, Summer, Autumn, Winter, }
impl Season { pub fn on(month: u32, day: u32) -> Option<Season> { let md = (month, day); Some(match () { _ if ((1, 1)..=(3, 20)).contains(&md) => Season::Winter, _ if ((3, 21)..=(6, 21)).contains(&md) => Season::Spring, _ if ((6, 22)..=(9, 22)).contains(&md) => Season::Summer, _ if ((9, 23)..=(12, 21)).contains(&md) => Season::Autumn, _ if ((12, 22)..=(12, 31)).contains(&md) => Season::Winter, // Just in case something really darn weird happens to the calendar. _ => return None, }) }
pub fn current() -> Option<Season> { let now = Utc::now(); Self::on(now.month(), now.day()) } }