syntax highlighting gallery

  • this is a page demonstrating syntaxes supported by the treehouse

    2024-03-12
    • 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

      2024-03-12
  • design notes

    2024-05-18
    • don’t do magic: stick to standards and community conventions.

      2024-05-18
      • like in C, don’t highlight uppercase identifiers. those are not special in any way.

        2024-05-18
      • in Rust, we highlight identifiers starting with an uppercase letter, because that’s the style convention for user-defined types.

        2024-05-18
    • keep it simple. the highlighting doesn’t have to be perfect. you know what you’re typing. in case of user input, you have a compiler that will highlight the error anyways.

      2024-05-18
  • c

    2024-05-18
    • NOTE: this is C23 so you may see some unfamiliar keywords

      2024-05-18
    • patterns

      #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
      
      . ->
      ++ -- & * + - ~ !
      / % << >> < > <= >= == != ^ | && ||
      ? : :: ; ...
      = *= /= %= += -= <<= >>= &= ^= |=
      , # ##
      <: :> <% %> %: %:%:
      
      2024-05-18
    • keywords

      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
      
      2024-05-18
    • sample

      #include <snug/bump.h>
      
      #include <snug/panic.h>
      
      void bump_init(struct bump* a, void* slab, i32 size)
      {
          a->start = slab;
          a->ptr = slab + size;
      #if SNUGGLES_BUMP_TRACKING
          a->tracker = null;
      #endif
      }
      
      void* bump_try_alloc(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 bump_alloc_or_panic.
          if (a->ptr < a->start) {
              addr = null;
          } else {
              addr = a->ptr;
          }
      
      #if SNUGGLES_BUMP_TRACKING
          if (a->tracker) {
              (a->tracker)(addr, size, what);
          }
      #endif
      
          return addr;
      }
      
      void* bump_alloc_or_panic(struct bump* a, i32 size, const char* what)
      {
          (void)what; // Currently unused, may use for panic message in the future.
      
          void* p = bump_try_alloc(a, size, what);
          b32 allocation_succeeded = p != 0;
          ASSERT(allocation_succeeded, "out of memory");
          return p;
      }
      
      2024-05-18
    • .types

      2024-05-18
      • patterns

        x /*: int */
        
        2024-05-18
  • javascript

    2024-03-12
    • patterns

      // 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`
      + - * / == != <= >= ! ~ || && . ? :
      , ;
      
      2024-03-12
    • keywords

      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
      
      2024-03-12
    • sample code

      // 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;
      }
      
      2024-03-12
    • edge cases

      2024-03-12
      • "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.

        2024-03-12
  • json

    2024-03-12
    • patterns

      abcd
      0.912392198e+2113
      "abcd":
      "abcd"
      ,
      
      2024-03-12
    • keywords

      null true false
      
      2024-03-12
    • sample

      {
          "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" }
          }
      }
      
      2024-03-12
  • lua

    2024-03-31
    • patterns

      -- 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
      
      2024-03-31
    • keywords

      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>
      
      2024-03-31
    • sample

      -- 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
      
      2024-03-31
  • rust

    2024-04-07
    • patterns

      // this is a comment
      /* this is a multiline comment */
      
      "string" 'c' 'ł'
      b"string" b'ł'
      r"string" r#"string"# r##"string"## r###"string"###
      0b11001100 0b11001100_u8
      0o1234567_f32
      0xDEADBEEF 0xDEADBEEF_i16
      2137
      3.14159265 2.3e-32
      + - * / % == ~= <= >= & .
      #![doc = ""]
      identifier macro! function() 'static
      T Vec<i32>
      union Example
      europeanunion A
      
      2024-04-07
    • keywords

      _ 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
      
      2024-04-07
    • sample

      use chrono::{Datelike, Utc};
      use serde::{Deserialize, Serialize};
      
      #[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
      #[serde(rename_all = "snake_case")]
      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())
          }
      }
      
      2024-04-07
  • toml

    2025-01-13
    • this one is not very good… it was written very hastily for page:design/freehouse

      2025-01-13
      • it’ll probably be tweaked later

        2025-01-13
    • sample

      # This string is prepended before each link address.
      # Replace this with a different root path if you need to host the site in a subdirectory (eg. "/treehouse").
      # Note this does not affect the behavior of `serve`, as the treehouse is always served under /.
      # Therefore this is most useful for reverse proxies.
      # This variable can also be set using the TREEHOUSE_SITE environment variable.
      site = ""
      
      # This is used to generate a link in the footer that links to the page's source commit.
      # The final URL is `{commit_base_url}/{commit}/content/{tree_path}.tree`.
      commit_base_url = "https://src.liquidev.net/liquidex/treehouse/src/commit"
      
      [user]
      title = "riki's house"
      author = "riki"
      description = "a fluffy ragdoll's fluffy house = -w- ="
      canonical_url = "https://riki.house"
      
      # URI prefix to use for entry IDs in feeds.
      # This is kept at the old domain so as not to wreak havoc in people's feed readers.
      # (changing this would mark all existing entries as unread, which would be really annoying.)
      feed_id_prefix = "https://liquidex.house"
      
      [defs]
      
      # Social garbage
      "social/github" = "https://github.com/liquidev"
      "social/soundcloud" = "https://soundcloud.com/daknus"
      "social/listenbrainz" = "https://listenbrainz.org/user/liquidev/"
      
      # treehouse management facilities
      "treehouse/issues" = "https://src.liquidev.net/liquidex/treehouse/issues"
      
      # My own repositories
      "stitchkit/repo" = "https://github.com/abyteintime/stitchkit"
      "dawd3/repo" = "https://github.com/liquidev/dawd3"
      "treehouse/repo" = "https://src.liquidev.net/liquidex/treehouse"
      "dispatchers/repo" = "https://github.com/liquidev/dispatchers"
      "abit/repo" = "https://github.com/abyteintime/abit"
      "rokugo/repo" = "https://github.com/rokugo-lang/rokugo"
      "mica/repo" = "https://github.com/mica-lang/mica"
      "planet_overgamma/repo" = "https://github.com/liquidev/planet-overgamma"
      "rkgk/repo" = "https://github.com/liquidev/rkgk"
      "netcanv/repo" = "https://github.com/netcanv/netcanv"
      
      # Blog posts I like to reference
      "article/function_coloring" = "https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/"
      
      "word/hyperlink" = "https://en.wiktionary.org/wiki/hyperlink"
      "word/iff" = "https://en.wiktionary.org/wiki/iff"
      
      # People
      "person/areox" = "https://areox.boo"
      "person/ezioleq" = "https://ezioleq.com"
      "person/firstbober" = "https://firstbober.com"
      "person/olekolek1000" = "https://oo8.dev"
      "person/vixenka" = "https://vixenka.com"
      
      [feed]
      tags = [
          # Hobby corners
          "meow",
          "programming",
          "design",
          "music",
          "games",
          "philosophy",
      
          # Programming fields
          "graphics",
          "plt",
      
          # Programming languages
          "c",
          "cxx",
          "lua",
          "javascript",
      
          # Projects
          "treehouse",
          "haku",
      ]
      
      [redirects.page]
      "programming/cxx" = "programming/languages/cxx"
      "programming/unreal-engine" = "programming/technologies/unreal-engine"
      
      [emoji]
      
      [pics]
      
      [build.javascript]
      import_roots = [
          { name = "treehouse", path = "" },
          { name = "tairu", path = "components/tairu" },
          { name = "haku", path = "components/haku" },
      ]
      
      2025-01-13