Skip to main content

Crate sound_law

Crate sound_law 

Source
Expand description

A small, dependency-free engine for writing and applying sound laws.

sound_laws! declares both the segment classes and an ordered rule list. The same notation handles simple letter maps, contextual rewrites, deletion, insertion, assimilation, capture, and metathesis:

use sound_law::{RuleSet, sound_laws};

const LAWS: RuleSet<'static> = sound_laws! {
    classes {
        V = ["a", "e", "i", "o", "u"];
        C = ["p", "t", "k", "s"];
        K = ["k"];
    }
    maps {
        PALATAL = ["k" => "tʲ"];
    }
    rules {
        // A plain letter map and a contextual rewrite use the same syntax.
        palatalize_k: "{c:K}" => "{c|PALATAL}" / "" _ "{V}";
        // Capture a consonant and vowel, then emit them in reverse order.
        metathesis: "{c:C}{v:V}" => "{v}{c}";
    }
};

let program = LAWS.compile()?;
assert_eq!(program.apply("pa"), "ap");

§Pattern notation

Patterns are strings, keeping the macro grammar compact and allowing rules to be assembled as ordinary const data.

  • literal text matches itself;
  • {V} matches one member of class V;
  • {v:V} captures one member of V as v;
  • {[a|e|ai]} matches one inline literal alternative, longest first;
  • {v:[a|e|ai]} captures an inline alternative;
  • {C|V}, {C&SONORANT}, and {C-[p|t]} form finite class unions, intersections, and differences;
  • {x:.} captures any one Unicode scalar value;
  • {cluster:C+}, {maybe:C?}, and {run:C*} use greedy quantifiers with backtracking and capture the complete matched span;
  • {x:!V} matches and captures one scalar value that does not begin a member of V;
  • {=v} matches the exact text captured earlier as v;
  • ^ and $ anchor a pattern to the start and end of the complete form;
  • # matches a zero-width edge of a whitespace-delimited word.

Replacements contain literal text and capture references such as {v}. {v|MAP} emits a capture through a declared correspondence map. Captures may be omitted, duplicated, transformed, or emitted in any order. A backslash escapes a metacharacter in either a pattern or replacement. An empty target inserts at every boundary satisfying its context: "" => "ə" / "{C}" _ "{C}".

Rules apply globally and left-to-right by default, to the result of the preceding rule. [rtl] changes overlap selection and [once] selects only the first directional match. Matches made by one rule are simultaneous: its own output is not reconsidered until another pass is explicitly requested with Program::apply_until_stable.

Macros§

sound_laws
Declare segment classes and ordered sound laws in one expression.

Structs§

Class
A named set of possible segments.
CompileError
A problem in a class, pattern, backreference, or replacement template.
Map
A named, source-keyed correspondence used by mapped capture replacements.
MatchLimitError
Matcher search exceeded the caller’s deterministic work budget.
Program
A validated, executable sequence of sound laws.
ProgramCompositionError
Two compiled programs cannot be composed without ambiguous rule names.
Rule
One declarative sound law.
RuleSet
An uncompiled collection of classes and ordered rules.
TraceStep
One changed intermediate form from Program::apply_with_trace.

Enums§

Application
How many matches one rule applies to in a word.
Direction
The direction in which a rule selects non-overlapping matches.
IterationError
Failure to reach a fixed point in Program::apply_until_stable.
RuleRangeError
An invalid index or name passed to a partial-execution API.