Skip to content

Commit 1d07e93

Browse files
author
Zibi Braniecki
committed
Switch to new comments system and remove sections
1 parent 5b3382e commit 1d07e93

File tree

6 files changed

+88
-111
lines changed

6 files changed

+88
-111
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

+65-69
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,37 +41,29 @@ 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() {
84-
return Ok(get_message(ps, comment)?);
61+
match comment {
62+
None | Some(ast::Comment::Comment { .. }) => {
63+
return Ok(get_message(ps, comment)?);
64+
}
65+
_ => {}
66+
};
8567
}
8668

8769
match comment {
@@ -90,60 +72,74 @@ where
9072
}
9173
}
9274

93-
fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
75+
#[derive(PartialEq, Copy, Clone)]
76+
enum CommentLevel {
77+
Comment = 0,
78+
GroupComment = 1,
79+
ResourceComment = 2,
80+
}
81+
82+
fn get_comment_start<I>(ps: &mut ParserStream<I>, level: &CommentLevel) -> Result<()>
9483
where
9584
I: Iterator<Item = char>,
9685
{
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-
}
86+
let depth = *level as u8;
87+
for _ in 0..(depth + 1) {
88+
ps.expect_char('#')?;
89+
}
10790

108-
ps.next();
91+
if !ps.current_is('\n') {
92+
ps.expect_char(' ')?;
93+
}
94+
Ok(())
95+
}
10996

110-
if ps.current_is('/') {
111-
content.push('\n');
112-
ps.next();
113-
ps.expect_char('/')?;
114-
ps.take_char_if(' ');
97+
fn get_comment_level<I>(ps: &mut ParserStream<I>) -> Result<CommentLevel>
98+
where
99+
I: Iterator<Item = char>,
100+
{
101+
let mut level = CommentLevel::Comment;
102+
ps.peek();
103+
if ps.current_peek_is('#') {
104+
ps.peek();
105+
if ps.current_peek_is('#') {
106+
level = CommentLevel::ResourceComment;
115107
} else {
116-
break;
108+
level = CommentLevel::GroupComment;
117109
}
118110
}
119-
120-
Ok(ast::Comment { content })
111+
ps.reset_peek();
112+
Ok(level)
121113
}
122114

123-
fn get_section<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
115+
fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
124116
where
125117
I: Iterator<Item = char>,
126118
{
127-
ps.expect_char('[')?;
128-
ps.expect_char('[')?;
129-
130-
ps.skip_line_ws();
131-
132-
let symb = get_symbol(ps)?;
119+
let level = get_comment_level(ps)?;
120+
get_comment_start(ps, &level)?;
133121

134-
ps.skip_line_ws();
122+
let mut content = String::new();
135123

136-
ps.expect_char(']')?;
137-
ps.expect_char(']')?;
124+
loop {
125+
while let Some(ch) = ps.take_char(|x| x != '\n') {
126+
content.push(ch);
127+
}
138128

139-
ps.skip_line_ws();
129+
ps.next();
140130

141-
ps.expect_char('\n')?;
131+
if !ps.current_is('#') || level != get_comment_level(ps)? {
132+
break;
133+
} else {
134+
get_comment_start(ps, &level)?;
135+
}
136+
}
142137

143-
Ok(ast::Entry::Section {
144-
name: symb,
145-
comment,
146-
})
138+
match level {
139+
CommentLevel::Comment => Ok(ast::Comment::Comment { content }),
140+
CommentLevel::GroupComment => Ok(ast::Comment::GroupComment { content }),
141+
CommentLevel::ResourceComment => Ok(ast::Comment::ResourceComment { content }),
142+
}
147143
}
148144

149145
fn get_message<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
@@ -261,12 +257,12 @@ where
261257
return Ok(ast::VarKey::Number(get_number(ps)?));
262258
}
263259
_ => {
264-
return Ok(ast::VarKey::Symbol(get_symbol(ps)?));
260+
return Ok(ast::VarKey::VariantName(get_variant_name(ps)?));
265261
}
266262
}
267263
} else {
268264
return error!(ErrorKind::ExpectedField {
269-
field: "Symbol | Number".to_owned(),
265+
field: "VariantName | Number".to_owned(),
270266
});
271267
}
272268
}
@@ -323,7 +319,7 @@ where
323319
Ok(variants)
324320
}
325321

326-
fn get_symbol<I>(ps: &mut ParserStream<I>) -> Result<ast::Symbol>
322+
fn get_variant_name<I>(ps: &mut ParserStream<I>) -> Result<ast::VariantName>
327323
where
328324
I: Iterator<Item = char>,
329325
{
@@ -339,7 +335,7 @@ where
339335
name.pop();
340336
}
341337

342-
Ok(ast::Symbol { name })
338+
Ok(ast::VariantName { name })
343339
}
344340

345341
fn get_digits<I>(ps: &mut ParserStream<I>) -> Result<String>
+13-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
// File comment
1+
### File comment
22

3-
// Standalone comment
3+
# Standalone comment
44

5-
// Another standalone comment
5+
# Another standalone comment
66

7-
//Comment with no leading space
7+
# Comment with a leading space
88

9-
// Comment with a leading space
9+
## Multi
10+
## Line Section
11+
##
12+
## Comment
1013

11-
//Multi
12-
//Line
13-
//Comment
14-
15-
//Comment for entity key1
14+
# Comment for entity key1
1615
key1 = New entity
1716
18-
//Comment for entity key2
17+
# Comment for entity key2
1918
key2=
2019
| Multi line message
20+
21+
## Group comment
22+
key3 = Message

tests/fixtures/parser/ftl/04-sections.ftl

-10
This file was deleted.

tests/fixtures/parser/ftl/05-variants.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ key5
1919
key5
2020
.m = Foo
2121

22-
[[ section ]]
22+
## section
2323

2424

2525
key6 = {

0 commit comments

Comments
 (0)