Skip to content

Instantly share code, notes, and snippets.

View eignnx's full-sized avatar

eignnx eignnx

View GitHub Profile
@eignnx
eignnx / semantic_nom_whitespace.rs
Last active June 22, 2021 22:42
An idea for how to handle whitespace in Rust's `nom` parser-combinator library.
/// A module for parsing whitespace. Takes into account comments too.
///
/// # Module Outline
/// - mod space
/// - fn comment
/// - mod allowed
/// - fn here
/// - fn after
/// - fn before
/// - fn around
@eignnx
eignnx / lesson3.affix
Last active April 2, 2020 00:06
Lesson 3 of the `affix-grammar` beginner guide.
-- # Beginner Tutorial: Lesson 3
-- ### Data Variants
-- A rule can be parameterized by what's called a "data variant." Let's define one to illustrate.
data Gender = nonbinary | feminine | neutral
-- An instance of `Gender` can be one of the three possibilities listed.
--
-- *Aside: this isn't an exhaustive list of genders (that would probably be infinite), just some of the most common in English. See the exercise at the end!*
@eignnx
eignnx / lesson2.affix
Last active April 2, 2020 00:07
Lesson 2 of the `affix-grammar` beginner guide.
-- # Affix Grammar Tutorial: Lesson 2
-- ### Joining Words Together
-- Check out this grammar 👇
rule start = "These" "are" "a" "few" things + "!"
rule things = "words" | "sentence elements" | "lexemes"
-- When two sentence elements (like `"quoted text"` or `rule_names`) are placed next to each other, they are implicitly joined together and separated by a space (" "). So writing:
-- > `"Stop" "thief!"`
@eignnx
eignnx / lesson1.affix
Last active April 1, 2020 22:40
Lesson 1 of the `affix-grammar` beginner guide.
-- # Affix Grammar Tutorial: Lesson 1
-- Hi! Welcome to Affix Grammar!
-- ### What is it?
-- Affix Grammar lets you generate sentences, stories, textual patterns and more! It's kinda like Mad Libs.
-- Lets generate a simple sentence that has a few variations. An example of the kind of sentence we'll generate is:
-- > "Last Wednesday, Val went for a walk in the park after supper."
--
-- First, we'll define a "rule" called `start`. This will be the entry point to our grammar.
@eignnx
eignnx / hello-world.affix
Last active May 1, 2020 19:30
A "Hello world" example for the `affix-grammar` sentence generator.
-- # Welcome to Affix Grammar!
-- Affix Grammar is a tool which lets you write grammars, and randomly generates sentences based on those grammars.
-- You can use it to:
-- - generate stories (kinda like Mad Libs),
-- - test out context-free grammars,
-- - test your knowledge of the grammar of a human language you're learning!
-- Here's a simple grammar which generates "hello world" sentences from different languages.
--
-- Click the `Generate` button a few times to see the sentences that are produced!
fn slice_of_slice<'pool, 'slice, 'ret>(
pool: &'pool [usize],
slice: &'slice [usize],
) -> &'ret [usize]
where
'pool: 'slice, // `pool` outlives `slice`
'slice: 'ret, // `slice` outlives the return value
{
// Most of this is bullshit so that both `slice` and `pool` are used.