Skip to content

Commit b2cddd2

Browse files
committed
Initial
1 parent 58269de commit b2cddd2

File tree

10 files changed

+633
-0
lines changed

10 files changed

+633
-0
lines changed

Cargo.lock

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ members = [
2323
"utils/databake",
2424
"utils/databake/derive",
2525
"experimental/segmenter",
26+
"experimental/message",
2627
"ffi/capi_cdylib",
2728
"ffi/diplomat",
2829
"ffi/capi_staticlib",

experimental/message/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "icu_message"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
smallvec = "1.6"
10+
11+
[dev-dependencies]
12+
iai = "0.1"
13+
criterion = "0.3.4"
14+
15+
[[bench]]
16+
name = "parser_iai"
17+
harness = false
18+
19+
[[bench]]
20+
name = "parser"
21+
harness = false
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
6+
use icu_message::parser::Parser;
7+
8+
fn overview_bench(c: &mut Criterion) {
9+
let source = "{Hello World}";
10+
c.bench_function("message/parse", |b| {
11+
b.iter(|| {
12+
let parser = Parser::new(black_box(source));
13+
let _ = parser.parse();
14+
})
15+
});
16+
17+
let source = "{Today is {$today}} a good day.";
18+
c.bench_function("message/parse_placeholder", |b| {
19+
b.iter(|| {
20+
let parser = Parser::new(black_box(source));
21+
let _ = parser.parse();
22+
})
23+
});
24+
}
25+
26+
fn compare_bench(c: &mut Criterion) {
27+
let mut messages = vec![];
28+
29+
for i in 0..99 {
30+
messages.push(format!("{{Value {i}}}"));
31+
}
32+
33+
c.bench_function("message/compare/simple", |b| {
34+
b.iter(|| {
35+
for msg in &messages {
36+
let parser = Parser::new(black_box(msg.as_str()));
37+
let _ = parser.parse();
38+
}
39+
})
40+
});
41+
}
42+
43+
criterion_group!(benches, overview_bench, compare_bench);
44+
criterion_main!(benches);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This file is part of ICU4X. For terms of use, please see the file
2+
// called LICENSE at the top level of the ICU4X source tree
3+
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4+
5+
use icu_message::parser::Parser;
6+
7+
fn iai_parse_message() {
8+
let source = "{Hello World}";
9+
let parser = Parser::new(source);
10+
let _ = parser.parse();
11+
}
12+
13+
iai::main!(iai_parse_message,);

experimental/message/src/ast.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use smallvec::SmallVec;
2+
3+
#[derive(Debug, PartialEq)]
4+
pub struct Message<S> {
5+
pub declarations: SmallVec<[Declaration<S>; 1]>,
6+
pub value: MessageValue<S>,
7+
}
8+
9+
#[derive(Debug, PartialEq)]
10+
pub struct Declaration<S> {
11+
pub variable: S,
12+
pub expression: Expression<S>,
13+
}
14+
15+
#[derive(Debug, PartialEq)]
16+
pub enum MessageValue<S> {
17+
Pattern(Pattern<S>),
18+
Select(Box<Select<S>>),
19+
}
20+
21+
#[derive(Debug, PartialEq)]
22+
pub struct Select<S> {
23+
pub selector: SmallVec<[Expression<S>; 1]>,
24+
pub variants: SmallVec<[Variant<S>; 3]>,
25+
}
26+
27+
#[derive(Debug, PartialEq)]
28+
pub struct Variant<S> {
29+
pub key: SmallVec<[VariantKey<S>; 1]>,
30+
pub pattern: Pattern<S>,
31+
}
32+
33+
#[derive(Debug, PartialEq)]
34+
pub struct Pattern<S> {
35+
pub body: SmallVec<[PatternElement<S>; 3]>,
36+
}
37+
38+
#[derive(Debug, PartialEq)]
39+
pub enum PatternElement<S> {
40+
Text(S),
41+
Placeholder(Placeholder<S>),
42+
}
43+
44+
#[derive(Debug, PartialEq)]
45+
pub enum Placeholder<S> {
46+
Markup {
47+
name: S,
48+
options: SmallVec<[Option<S>; 1]>,
49+
},
50+
MarkupEnd {
51+
name: S,
52+
},
53+
Expression(Expression<S>),
54+
}
55+
56+
#[derive(Debug, PartialEq)]
57+
pub enum Expression<S> {
58+
Operand {
59+
operand: Operand<S>,
60+
annotation: std::option::Option<Annotation<S>>,
61+
},
62+
Annotation(Annotation<S>),
63+
}
64+
65+
#[derive(Debug, PartialEq)]
66+
pub enum Operand<S> {
67+
Literal(Literal<S>),
68+
Variable(S),
69+
}
70+
71+
#[derive(Debug, PartialEq)]
72+
pub struct Annotation<S> {
73+
function: S,
74+
options: SmallVec<[Option<S>; 1]>,
75+
}
76+
77+
#[derive(Debug, PartialEq)]
78+
pub struct Literal<S> {
79+
pub value: S,
80+
}
81+
82+
#[derive(Debug, PartialEq)]
83+
pub enum VariantKey<S> {
84+
Literal(Literal<S>),
85+
Asterisk,
86+
}
87+
88+
#[derive(Debug, PartialEq)]
89+
pub struct Option<S> {
90+
name: S,
91+
value: OptionValue<S>,
92+
}
93+
94+
#[derive(Debug, PartialEq)]
95+
pub enum OptionValue<S> {
96+
Literal(Literal<S>),
97+
Variable(S),
98+
}

experimental/message/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
pub mod ast;
2+
pub mod parser;
3+
4+
pub struct MessageFormat {}
5+
6+
impl MessageFormat {
7+
pub fn new() -> Self {
8+
Self {}
9+
}
10+
11+
pub fn format<'s>(&self, _msg: ast::Message<&'s str>) -> String {
12+
panic!();
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// macro_rules! get_byte {
2+
// ($s:expr, $idx:expr) => {
3+
// $s.source.as_ref().as_bytes().get($idx)
4+
// };
5+
// }
6+
7+
macro_rules! get_current_byte {
8+
($s:expr) => {
9+
$s.source.as_ref().as_bytes().get($s.ptr)
10+
};
11+
}

0 commit comments

Comments
 (0)