Skip to content

Commit 0f0da93

Browse files
author
Zibi Braniecki
committed
Switch to new comments system and remove sections
1 parent dd692ce commit 0f0da93

File tree

7 files changed

+802
-110
lines changed

7 files changed

+802
-110
lines changed

src/resolve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl ResolveValue for ast::Number {
7474
}
7575
}
7676

77-
impl ResolveValue for ast::Symbol {
77+
impl ResolveValue for ast::VariantName {
7878
fn to_value(&self, _env: &Env) -> Option<FluentValue> {
7979
Some(FluentValue::from(self.name.clone()))
8080
}
@@ -107,7 +107,7 @@ impl ResolveValue for ast::Expression {
107107
if let Some(ref selector) = selector {
108108
for variant in variants {
109109
match variant.key {
110-
ast::VarKey::Symbol(ref symbol) => {
110+
ast::VarKey::VariantName(ref symbol) => {
111111
let key = FluentValue::from(symbol.name.clone());
112112
if key.matches(env.ctx, selector) {
113113
return variant.value.to_value(env);

src/syntax/ast.rs

+7-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
#[derive(Debug, PartialEq)]
22
pub struct Resource {
33
pub body: Vec<Entry>,
4-
pub comment: Option<Comment>,
54
}
65

76
#[derive(Debug, PartialEq)]
87
pub enum Entry {
98
Message(Message),
10-
Section {
11-
name: Symbol,
12-
comment: Option<Comment>,
13-
},
149
Comment(Comment),
15-
Junk {
16-
content: String,
17-
},
10+
Junk { content: String },
1811
}
1912

2013
#[derive(Debug, PartialEq)]
@@ -88,7 +81,7 @@ pub struct Variant {
8881

8982
#[derive(Debug, PartialEq)]
9083
pub enum VarKey {
91-
Symbol(Symbol),
84+
VariantName(VariantName),
9285
Number(Number),
9386
}
9487

@@ -115,19 +108,15 @@ pub struct Number {
115108
}
116109

117110
#[derive(Debug, PartialEq)]
118-
pub struct Symbol {
111+
pub struct VariantName {
119112
pub name: String,
120113
}
121114

122115
#[derive(Debug, PartialEq)]
123-
pub struct Comment {
124-
pub content: String,
125-
}
126-
127-
#[derive(Debug, PartialEq)]
128-
pub struct Section {
129-
pub name: String,
130-
pub comment: Option<Comment>,
116+
pub enum Comment {
117+
Comment { content: String },
118+
GroupComment { content: String },
119+
ResourceComment { content: String },
131120
}
132121

133122
#[derive(Debug, PartialEq)]

src/syntax/parser.rs

+62-68
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub type Result<T> = result::Result<T, ParserError>;
1414

1515
pub fn parse(source: &str) -> result::Result<ast::Resource, (ast::Resource, Vec<ParserError>)> {
1616
let mut errors = vec![];
17-
let mut comment = None;
1817

1918
let mut ps = ParserStream::new(source.chars());
2019

@@ -26,18 +25,9 @@ pub fn parse(source: &str) -> result::Result<ast::Resource, (ast::Resource, Vec<
2625
let entry_start_pos = ps.get_index();
2726

2827
match get_entry(&mut ps) {
29-
Ok(entry) => if entry_start_pos == 0 {
30-
match entry {
31-
ast::Entry::Comment(c) => {
32-
comment = Some(c);
33-
}
34-
_ => {
35-
entries.push(entry);
36-
}
37-
}
38-
} else {
28+
Ok(entry) => {
3929
entries.push(entry);
40-
},
30+
}
4131
Err(mut e) => {
4232
let error_pos = ps.get_index();
4333
entries.push(get_junk_entry(&mut ps, source, entry_start_pos));
@@ -51,35 +41,22 @@ pub fn parse(source: &str) -> result::Result<ast::Resource, (ast::Resource, Vec<
5141
}
5242

5343
if errors.is_empty() {
54-
Ok(ast::Resource {
55-
body: entries,
56-
comment,
57-
})
44+
Ok(ast::Resource { body: entries })
5845
} else {
59-
Err((
60-
ast::Resource {
61-
body: entries,
62-
comment,
63-
},
64-
errors,
65-
))
46+
Err((ast::Resource { body: entries }, errors))
6647
}
6748
}
6849

6950
fn get_entry<I>(ps: &mut ParserStream<I>) -> Result<ast::Entry>
7051
where
7152
I: Iterator<Item = char>,
7253
{
73-
let comment = if ps.current_is('/') {
54+
let comment = if ps.current_is('#') {
7455
Some(get_comment(ps)?)
7556
} else {
7657
None
7758
};
7859

79-
if ps.current_is('[') {
80-
return Ok(get_section(ps, comment)?);
81-
}
82-
8360
if ps.is_message_id_start() {
8461
return Ok(get_message(ps, comment)?);
8562
}
@@ -90,60 +67,72 @@ where
9067
}
9168
}
9269

93-
fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
70+
#[derive(PartialEq, Copy, Clone)]
71+
enum CommentLevel {
72+
Comment = 0,
73+
GroupComment = 1,
74+
ResourceComment = 2,
75+
}
76+
77+
fn get_comment_start<I>(ps: &mut ParserStream<I>, level: &CommentLevel) -> Result<()>
9478
where
9579
I: Iterator<Item = char>,
9680
{
97-
ps.expect_char('/')?;
98-
ps.expect_char('/')?;
99-
ps.take_char_if(' ');
100-
101-
let mut content = String::new();
102-
103-
loop {
104-
while let Some(ch) = ps.take_char(|x| x != '\n') {
105-
content.push(ch);
106-
}
81+
let depth = *level as u8;
82+
for _ in 0..(depth + 1) {
83+
ps.expect_char('#')?;
84+
}
10785

108-
ps.next();
86+
ps.expect_char(' ')?;
87+
Ok(())
88+
}
10989

110-
if ps.current_is('/') {
111-
content.push('\n');
112-
ps.next();
113-
ps.expect_char('/')?;
114-
ps.take_char_if(' ');
90+
fn get_comment_level<I>(ps: &mut ParserStream<I>) -> Result<CommentLevel>
91+
where
92+
I: Iterator<Item = char>,
93+
{
94+
let mut level = CommentLevel::Comment;
95+
ps.peek();
96+
if ps.current_peek_is('#') {
97+
ps.peek();
98+
if ps.current_peek_is('#') {
99+
level = CommentLevel::ResourceComment;
115100
} else {
116-
break;
101+
level = CommentLevel::GroupComment;
117102
}
118103
}
119-
120-
Ok(ast::Comment { content })
104+
ps.reset_peek();
105+
Ok(level)
121106
}
122107

123-
fn get_section<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
108+
fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
124109
where
125110
I: Iterator<Item = char>,
126111
{
127-
ps.expect_char('[')?;
128-
ps.expect_char('[')?;
112+
let level = get_comment_level(ps)?;
113+
get_comment_start(ps, &level)?;
129114

130-
ps.skip_line_ws();
131-
132-
let symb = get_symbol(ps)?;
133-
134-
ps.skip_line_ws();
115+
let mut content = String::new();
135116

136-
ps.expect_char(']')?;
137-
ps.expect_char(']')?;
117+
loop {
118+
while let Some(ch) = ps.take_char(|x| x != '\n') {
119+
content.push(ch);
120+
}
138121

139-
ps.skip_line_ws();
122+
ps.next();
140123

141-
ps.expect_char('\n')?;
124+
if !ps.current_is('#') || level != get_comment_level(ps)? {
125+
break;
126+
} else {
127+
get_comment_start(ps, &level)?;
128+
}
129+
}
142130

143-
Ok(ast::Entry::Section {
144-
name: symb,
145-
comment,
146-
})
131+
match level {
132+
CommentLevel::Comment => Ok(ast::Comment::Comment { content }),
133+
CommentLevel::GroupComment => Ok(ast::Comment::GroupComment { content }),
134+
CommentLevel::ResourceComment => Ok(ast::Comment::ResourceComment { content }),
135+
}
147136
}
148137

149138
fn get_message<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
@@ -152,6 +141,11 @@ where
152141
{
153142
let id = get_private_identifier(ps)?;
154143

144+
if let Some(ast::Comment::Comment { .. }) = comment {
145+
} else if comment.is_some() {
146+
return error!(ErrorKind::Generic);
147+
}
148+
155149
ps.skip_line_ws();
156150

157151
let pattern = if ps.current_is('=') {
@@ -261,12 +255,12 @@ where
261255
return Ok(ast::VarKey::Number(get_number(ps)?));
262256
}
263257
_ => {
264-
return Ok(ast::VarKey::Symbol(get_symbol(ps)?));
258+
return Ok(ast::VarKey::VariantName(get_variant_name(ps)?));
265259
}
266260
}
267261
} else {
268262
return error!(ErrorKind::ExpectedField {
269-
field: "Symbol | Number".to_owned(),
263+
field: "VariantName | Number".to_owned(),
270264
});
271265
}
272266
}
@@ -323,7 +317,7 @@ where
323317
Ok(variants)
324318
}
325319

326-
fn get_symbol<I>(ps: &mut ParserStream<I>) -> Result<ast::Symbol>
320+
fn get_variant_name<I>(ps: &mut ParserStream<I>) -> Result<ast::VariantName>
327321
where
328322
I: Iterator<Item = char>,
329323
{
@@ -339,7 +333,7 @@ where
339333
name.pop();
340334
}
341335

342-
Ok(ast::Symbol { name })
336+
Ok(ast::VariantName { name })
343337
}
344338

345339
fn get_digits<I>(ps: &mut ParserStream<I>) -> Result<String>

0 commit comments

Comments
 (0)