Skip to content

Instantly share code, notes, and snippets.

# create this file at <roc-repo-root>/examples/parser/repro.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/repro.roc
#
app "lazy-parser-repro"
packages { pf: "platform/main.roc" }
imports [
Parser.Core.{ Parser, map, lazy},
Parser.Str.{ parseStr, RawStr }
# create this file at <roc-repo-root>/examples/parser/parse-json.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/parse-json.roc
#
app "parse-json"
packages { pf: "platform/main.roc" }
imports [
Parser.Core.{ Parser, oneOf, flatten, map, apply, many, const, maybe, between, alt, sepBy, ignore, lazy},
Parser.Str.{ string, strFromRaw, parseStr, RawStr, codeunitSatisfies, codeunit }
@travisstaloch
travisstaloch / float-parsing-repro.roc
Created October 7, 2022 08:53
roc compiler linking error reproduction
# create this file at <roc-repo-root>/examples/parser/float-parsing-repro.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/float-parsing-repro.roc
#
app "parse-json-repro"
packages { pf: "platform/main.roc" }
imports [
Parser.Core.{ Parser, flatten, map, apply, many, const, maybe},
Parser.Str.{ strFromRaw, parseStr, RawStr, codeunitSatisfies, codeunit }
@travisstaloch
travisstaloch / parse-json.roc
Last active October 7, 2022 06:10
Toy json parser in roc
# create this file at <roc-repo-root>/examples/parser/parse-json.roc
# run
# $ cd <roc-repo-root> && roc examples/parser/parse-json.roc
#
# one-liner: clone roc repo to /tmp + wget gist + run gist
# $ cd /tmp && git clone [email protected]:roc-lang/roc.git && cd roc && wget https://gist.github.com/travisstaloch/7add1274a085f1e6cba59ca93d991d9c/raw/parse-json.roc -O examples/parser/parse-json.roc && roc examples/parser/parse-json.roc
app "parse-json"
packages { pf: "platform/main.roc" }
imports [
//! Shows how to manually convert a group of enums / ints into a packed struct and then into an integer
//! which can be used in a switch statement. Useful for eliminting complicated nested switches / ifs.
//!
//! Then creates helpers switchable() and switchableAny() for doing the same.
//!
//! Note: the second part only works with stage1 as of 9/25/22 (zig version
//! 0.10.0-dev.4115+75e9a8c7f). stage2 currently crashes on this but will likely work soon.
const std = @import("std");
@travisstaloch
travisstaloch / print_compile_log.zig
Created June 13, 2022 04:00
translates garbled @compilelog output into a readable form
//! this script translates garbled @compileLog output into a readable form.
//! reads from stdin so you may need to redirect stderr to stdin like this:
//! $ zig run file.zig |& zig run print_compile_log.zig
//!
//! works by splitting lines on "u8{" delimiter and then skipping `delim_prefixes`
//! in `skipDelimPrefix()`. this makes things work when the type of the garbled string
//! is either `[]const u8{...}` or `*[N:0]u8{...}`
//!
const std = @import("std");
@travisstaloch
travisstaloch / parser.zig
Last active March 12, 2021 05:39
Parser combinators in zig
// adapted from https://gist.github.com/slimsag/db2dd2c49aa038e23b654120e70c9b00
const std = @import("std");
const Allocator = std.mem.Allocator;
pub const Error = error{
EndOfStream,
Utf8InvalidStartByte,
} || std.fs.File.ReadError || std.fs.File.SeekError || std.mem.Allocator.Error;
extern const unsigned int choose[53][10];
static int hash_binary(int k)
{
int sum = 0;
sum += choose[0][k];
return sum;
}
// copy all .c and .h files from antirez/rax to a folder named 'raxc'
// mkdir raxc && cp path/to/antirez/rax/*{.h,.c} raxc
// compile the rax c libs
// cd raxc && zig build-lib -lc --c-source rc4rand.c && zig build-lib -lc --c-source rax.c
// or compile with RAX_DEBUG_MSG (you might have to delete zig-cache)
// zig build-lib -lc --c-source rax.c -DRAX_DEBUG_MSG
// run this file's tests, linking rax c libs and including c headers
// cd .. && zig test -lc -lrax -lrc4rand -Iraxc -Lraxc test-rax.zig
@travisstaloch
travisstaloch / parse_psf.zig
Last active January 25, 2020 21:01
parse psf font file, getting the header at comptime
const std = @import("std");
const warn = std.debug.warn;
test "parse psf font file" {
// https://github.com/monarrk/trOS/raw/master/src/vga/font.psf
// const font_raw = @embedFile("font.psf");
// https://github.com/powerline/fonts/blob/master/Terminus/PSF/ter-powerline-v16b.psf.gz
const font_raw = @embedFile("ter-powerline-v16b.psf");
const PSFHeader = extern struct {