Skip to content

Commit

Permalink
Format code and apply some clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
fergcb committed Jul 9, 2023
1 parent 38cb966 commit fd9ddee
Show file tree
Hide file tree
Showing 15 changed files with 1,052 additions and 989 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Lint
run: cargo fmt --check
- name: Build
run: cargo build --verbose
- name: Test
Expand Down
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"editor.tabSize": 4,
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"editor.insertSpaces": true
}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
lint:
cargo clippy

release-patch:
cargo release patch --no-publish --execute

release-minor:
cargo release minor --no-publish --execute

release-major:
cargo release major --no-publish --execute
cargo release major --no-publish --execute
135 changes: 67 additions & 68 deletions src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,128 +1,127 @@
use std::ops::Range;

use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

pub type Span = Range<usize>;
pub type Spanned<T> = (Span, T);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Stmt {
SoundChange {
source: Spanned<Source>,
target: Spanned<Target>,
environment: Option<Spanned<Environment>>,
description: Option<Spanned<String>>,
},
Import {
path: Vec<Spanned<String>>,
absolute: bool,
names: Vec<Spanned<String>>,
},
Language {
id: Spanned<String>,
parent: Option<Spanned<String>>,
name: Option<Spanned<String>>,
},
Word {
gloss: Spanned<String>,
pronunciation: Spanned<Vec<String>>,
definitions: Vec<Definition>,
},
Class {
label: Spanned<String>,
encodes: Vec<Spanned<String>>,
annotates: Vec<Spanned<String>>,
phonemes: Vec<Spanned<PhonemeDef>>,
},
Series {
label: Spanned<String>,
series: Spanned<Series>,
},
Trait {
label: Spanned<String>,
members: Vec<Spanned<TraitMember>>,
},
Milestone {
time: Option<Spanned<Time>>,
language: Option<Spanned<String>>,
}
SoundChange {
source: Spanned<Source>,
target: Spanned<Target>,
environment: Option<Spanned<Environment>>,
description: Option<Spanned<String>>,
},
Import {
path: Vec<Spanned<String>>,
absolute: bool,
names: Vec<Spanned<String>>,
},
Language {
id: Spanned<String>,
parent: Option<Spanned<String>>,
name: Option<Spanned<String>>,
},
Word {
gloss: Spanned<String>,
pronunciation: Spanned<Vec<String>>,
definitions: Vec<Definition>,
},
Class {
label: Spanned<String>,
encodes: Vec<Spanned<String>>,
annotates: Vec<Spanned<String>>,
phonemes: Vec<Spanned<PhonemeDef>>,
},
Series {
label: Spanned<String>,
series: Spanned<Series>,
},
Trait {
label: Spanned<String>,
members: Vec<Spanned<TraitMember>>,
},
Milestone {
time: Option<Spanned<Time>>,
language: Option<Spanned<String>>,
},
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Source {
Pattern(Pattern),
Empty,
Pattern(Pattern),
Empty,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Target {
Modification(Vec<Spanned<Feature>>),
Pattern(Pattern),
Empty,
Modification(Vec<Spanned<Feature>>),
Pattern(Pattern),
Empty,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Feature {
Positive(String),
Negative(String),
Positive(String),
Negative(String),
}

pub type Pattern = Vec<Segment>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Segment {
Category(Category),
Phonemes(String),
Category(Category),
Phonemes(String),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Category {
pub base_class: Option<Spanned<char>>,
pub features: Vec<Spanned<Feature>>,
pub base_class: Option<Spanned<char>>,
pub features: Vec<Spanned<Feature>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Environment {
pub before: Option<EnvPattern>,
pub after: Option<EnvPattern>,
pub before: Option<EnvPattern>,
pub after: Option<EnvPattern>,
}

pub type EnvPattern = Vec<EnvElement>;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum EnvElement {
Segment(Segment),
SyllableBoundary,
WordBoundary,
Segment(Segment),
SyllableBoundary,
WordBoundary,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Definition {
pub pos: Option<Spanned<String>>,
pub definition: Spanned<String>,
pub pos: Option<Spanned<String>>,
pub definition: Spanned<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Series {
Category(Category),
List(Vec<Spanned<String>>),
Category(Category),
List(Vec<Spanned<String>>),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PhonemeDef {
pub label: Spanned<String>,
pub traits: Vec<Spanned<String>>,
pub label: Spanned<String>,
pub traits: Vec<Spanned<String>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TraitMember {
pub labels: Vec<Spanned<String>>,
pub notation: Option<Spanned<String>>,
pub default: bool,
pub labels: Vec<Spanned<String>>,
pub notation: Option<Spanned<String>>,
pub default: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Time {
Instant(i64),
Range(i64, i64),
Instant(i64),
Range(i64, i64),
}

94 changes: 47 additions & 47 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,56 @@ pub mod ast;

#[cfg(test)]
mod test {
use super::*;
use super::*;

use crate::ast::{Stmt, Spanned};
use crate::ast::{Spanned, Stmt};

use ariadne::{
Report,
Label,
Source as SourceCode,
ReportKind, Color,
};

use chumsky::{error::SimpleReason, prelude::Simple};
use ariadne::{Color, Label, Report, ReportKind, Source as SourceCode};

fn display_errs(src: &str, errs: &Vec<Simple<char>>) {
let start = errs.iter()
.map(|err| err.span())
.fold(src.len(), |min, cur| if cur.start < min { cur.start } else { min });
use chumsky::{error::SimpleReason, prelude::Simple};

Report::build(ReportKind::Error, (), start)
.with_labels(
errs.iter()
.map(|err| {
Label::new(err.span())
.with_message(match err.reason() {
SimpleReason::Unexpected => err.to_string(),
SimpleReason::Unclosed { span: _, delimiter } => format!("Unmatched delimited {}", delimiter),
SimpleReason::Custom(msg) => msg.clone(),
})
.with_color(Color::Red)
})
)
.finish()
.eprint(SourceCode::from(src.clone()))
.unwrap();
}
fn display_errs(src: &str, errs: &Vec<Simple<char>>) {
let start = errs
.iter()
.map(|err| err.span())
.fold(
src.len(),
|min, cur| if cur.start < min { cur.start } else { min },
);

fn _parse(src: &str) -> Result<Vec<Spanned<Stmt>>, Vec<Simple<char>>> {
let res = parse(src);
Report::build(ReportKind::Error, (), start)
.with_labels(errs.iter().map(|err| {
Label::new(err.span())
.with_message(match err.reason() {
SimpleReason::Unexpected => err.to_string(),
SimpleReason::Unclosed { span: _, delimiter } => {
format!("Unmatched delimited {}", delimiter)
}
SimpleReason::Custom(msg) => msg.clone(),
})
.with_color(Color::Red)
}))
.finish()
.eprint(SourceCode::from(src.clone()))
.unwrap();
}

fn _parse(src: &str) -> Result<Vec<Spanned<Stmt>>, Vec<Simple<char>>> {
let res = parse(src);

match res {
Ok(ast) => Ok(ast),
Err(errs) => {
display_errs(&src, &errs);
Err(errs)
},
match res {
Ok(ast) => Ok(ast),
Err(errs) => {
display_errs(&src, &errs);
Err(errs)
}
}
}
}

#[test]
fn it_works() {
let res = _parse("
#[test]
fn it_works() {
let res = _parse(
"
import * from @core/ipa
series F = { i, e, ε, æ }
Expand All @@ -79,10 +78,11 @@ mod test {
@ 1940, AmEng
$ [C+alveolar+stop] > [+flap] / V_V : Alveolar stops lenite to flaps intervocallically
");
",
);

println!("{:#?}", res);
println!("{:#?}", res);

assert!(res.is_ok())
}
assert!(res.is_ok())
}
}
Loading

0 comments on commit fd9ddee

Please sign in to comment.