Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
zbraniecki committed Jul 28, 2022
1 parent 58269de commit 3d01a91
Show file tree
Hide file tree
Showing 10 changed files with 621 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ members = [
"utils/databake",
"utils/databake/derive",
"experimental/segmenter",
"experimental/message",
"ffi/capi_cdylib",
"ffi/diplomat",
"ffi/capi_staticlib",
Expand Down
21 changes: 21 additions & 0 deletions experimental/message/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "icu_message"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
smallvec = "1.6"

[dev-dependencies]
iai = "0.1"
criterion = "0.3.4"

[[bench]]
name = "parser_iai"
harness = false

[[bench]]
name = "parser"
harness = false
44 changes: 44 additions & 0 deletions experimental/message/benches/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use icu_message::parser::Parser;

fn overview_bench(c: &mut Criterion) {
let source = "{Hello World}";
c.bench_function("message/parse", |b| {
b.iter(|| {
let parser = Parser::new(black_box(source));
let _ = parser.parse();
})
});

let source = "{Today is {$today}} a good day.";
c.bench_function("message/parse_placeholder", |b| {
b.iter(|| {
let parser = Parser::new(black_box(source));
let _ = parser.parse();
})
});
}

fn compare_bench(c: &mut Criterion) {
let mut messages = vec![];

for i in 0..99 {
messages.push(format!("{{Value {i}}}"));
}

c.bench_function("message/compare/simple", |b| {
b.iter(|| {
for msg in &messages {
let parser = Parser::new(black_box(msg.as_str()));
let _ = parser.parse();
}
})
});
}

criterion_group!(benches, overview_bench, compare_bench);
criterion_main!(benches);
13 changes: 13 additions & 0 deletions experimental/message/benches/parser_iai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use icu_message::parser::Parser;

fn iai_parse_message() {
let source = "{Hello World}";
let parser = Parser::new(source);
let _ = parser.parse();
}

iai::main!(iai_parse_message,);
98 changes: 98 additions & 0 deletions experimental/message/src/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use smallvec::SmallVec;

#[derive(Debug, PartialEq)]
pub struct Message<S> {
pub declarations: SmallVec<[Declaration<S>; 1]>,
pub value: MessageValue<S>,
}

#[derive(Debug, PartialEq)]
pub struct Declaration<S> {
pub variable: S,
pub expression: Expression<S>,
}

#[derive(Debug, PartialEq)]
pub enum MessageValue<S> {
Pattern(Pattern<S>),
Select(Box<Select<S>>),
}

#[derive(Debug, PartialEq)]
pub struct Select<S> {
pub selector: SmallVec<[Expression<S>; 1]>,
pub variants: SmallVec<[Variant<S>; 3]>,
}

#[derive(Debug, PartialEq)]
pub struct Variant<S> {
pub key: SmallVec<[VariantKey<S>; 1]>,
pub pattern: Pattern<S>,
}

#[derive(Debug, PartialEq)]
pub struct Pattern<S> {
pub body: SmallVec<[PatternElement<S>; 3]>,
}

#[derive(Debug, PartialEq)]
pub enum PatternElement<S> {
Text(S),
Placeholder(Placeholder<S>),
}

#[derive(Debug, PartialEq)]
pub enum Placeholder<S> {
Markup {
name: S,
options: SmallVec<[Option<S>; 1]>,
},
MarkupEnd {
name: S,
},
Expression(Expression<S>),
}

#[derive(Debug, PartialEq)]
pub enum Expression<S> {
Operand {
operand: Operand<S>,
annotation: std::option::Option<Annotation<S>>,
},
Annotation(Annotation<S>),
}

#[derive(Debug, PartialEq)]
pub enum Operand<S> {
Literal(Literal<S>),
Variable(S),
}

#[derive(Debug, PartialEq)]
pub struct Annotation<S> {
function: S,
options: SmallVec<[Option<S>; 1]>,
}

#[derive(Debug, PartialEq)]
pub struct Literal<S> {
pub value: S,
}

#[derive(Debug, PartialEq)]
pub enum VariantKey<S> {
Literal(Literal<S>),
Asterisk,
}

#[derive(Debug, PartialEq)]
pub struct Option<S> {
name: S,
value: OptionValue<S>,
}

#[derive(Debug, PartialEq)]
pub enum OptionValue<S> {
Literal(Literal<S>),
Variable(S),
}
2 changes: 2 additions & 0 deletions experimental/message/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod ast;
pub mod parser;
11 changes: 11 additions & 0 deletions experimental/message/src/parser/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// macro_rules! get_byte {
// ($s:expr, $idx:expr) => {
// $s.source.as_ref().as_bytes().get($idx)
// };
// }

macro_rules! get_current_byte {
($s:expr) => {
$s.source.as_ref().as_bytes().get($s.ptr)
};
}
Loading

0 comments on commit 3d01a91

Please sign in to comment.