Skip to content

0.5 comments #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ResolveValue for ast::Number {
}
}

impl ResolveValue for ast::Symbol {
impl ResolveValue for ast::VariantName {
fn to_value(&self, _env: &Env) -> Option<FluentValue> {
Some(FluentValue::from(self.name.clone()))
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl ResolveValue for ast::Expression {
if let Some(ref selector) = selector {
for variant in variants {
match variant.key {
ast::VarKey::Symbol(ref symbol) => {
ast::VarKey::VariantName(ref symbol) => {
let key = FluentValue::from(symbol.name.clone());
if key.matches(env.ctx, selector) {
return variant.value.to_value(env);
Expand Down
25 changes: 7 additions & 18 deletions src/syntax/ast.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#[derive(Debug, PartialEq)]
pub struct Resource {
pub body: Vec<Entry>,
pub comment: Option<Comment>,
}

#[derive(Debug, PartialEq)]
pub enum Entry {
Message(Message),
Section {
name: Symbol,
comment: Option<Comment>,
},
Comment(Comment),
Junk {
content: String,
},
Junk { content: String },
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -88,7 +81,7 @@ pub struct Variant {

#[derive(Debug, PartialEq)]
pub enum VarKey {
Symbol(Symbol),
VariantName(VariantName),
Number(Number),
}

Expand All @@ -115,19 +108,15 @@ pub struct Number {
}

#[derive(Debug, PartialEq)]
pub struct Symbol {
pub struct VariantName {
pub name: String,
}

#[derive(Debug, PartialEq)]
pub struct Comment {
pub content: String,
}

#[derive(Debug, PartialEq)]
pub struct Section {
pub name: String,
pub comment: Option<Comment>,
pub enum Comment {
Comment { content: String },
GroupComment { content: String },
ResourceComment { content: String },
}

#[derive(Debug, PartialEq)]
Expand Down
134 changes: 65 additions & 69 deletions src/syntax/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub type Result<T> = result::Result<T, ParserError>;

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

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

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

match get_entry(&mut ps) {
Ok(entry) => if entry_start_pos == 0 {
match entry {
ast::Entry::Comment(c) => {
comment = Some(c);
}
_ => {
entries.push(entry);
}
}
} else {
Ok(entry) => {
entries.push(entry);
},
}
Err(mut e) => {
let error_pos = ps.get_index();
entries.push(get_junk_entry(&mut ps, source, entry_start_pos));
Expand All @@ -51,37 +41,29 @@ pub fn parse(source: &str) -> result::Result<ast::Resource, (ast::Resource, Vec<
}

if errors.is_empty() {
Ok(ast::Resource {
body: entries,
comment,
})
Ok(ast::Resource { body: entries })
} else {
Err((
ast::Resource {
body: entries,
comment,
},
errors,
))
Err((ast::Resource { body: entries }, errors))
}
}

fn get_entry<I>(ps: &mut ParserStream<I>) -> Result<ast::Entry>
where
I: Iterator<Item = char>,
{
let comment = if ps.current_is('/') {
let comment = if ps.current_is('#') {
Some(get_comment(ps)?)
} else {
None
};

if ps.current_is('[') {
return Ok(get_section(ps, comment)?);
}

if ps.is_message_id_start() {
return Ok(get_message(ps, comment)?);
match comment {
None | Some(ast::Comment::Comment { .. }) => {
return Ok(get_message(ps, comment)?);
}
_ => {}
};
}

match comment {
Expand All @@ -90,60 +72,74 @@ where
}
}

fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
#[derive(PartialEq, Copy, Clone)]
enum CommentLevel {
Comment = 0,
GroupComment = 1,
ResourceComment = 2,
}

fn get_comment_start<I>(ps: &mut ParserStream<I>, level: &CommentLevel) -> Result<()>
where
I: Iterator<Item = char>,
{
ps.expect_char('/')?;
ps.expect_char('/')?;
ps.take_char_if(' ');

let mut content = String::new();

loop {
while let Some(ch) = ps.take_char(|x| x != '\n') {
content.push(ch);
}
let depth = *level as u8;
for _ in 0..(depth + 1) {
ps.expect_char('#')?;
}

ps.next();
if !ps.current_is('\n') {
ps.expect_char(' ')?;
}
Ok(())
}

if ps.current_is('/') {
content.push('\n');
ps.next();
ps.expect_char('/')?;
ps.take_char_if(' ');
fn get_comment_level<I>(ps: &mut ParserStream<I>) -> Result<CommentLevel>
where
I: Iterator<Item = char>,
{
let mut level = CommentLevel::Comment;
ps.peek();
if ps.current_peek_is('#') {
ps.peek();
if ps.current_peek_is('#') {
level = CommentLevel::ResourceComment;
} else {
break;
level = CommentLevel::GroupComment;
}
}

Ok(ast::Comment { content })
ps.reset_peek();
Ok(level)
}

fn get_section<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
fn get_comment<I>(ps: &mut ParserStream<I>) -> Result<ast::Comment>
where
I: Iterator<Item = char>,
{
ps.expect_char('[')?;
ps.expect_char('[')?;

ps.skip_line_ws();

let symb = get_symbol(ps)?;
let level = get_comment_level(ps)?;
get_comment_start(ps, &level)?;

ps.skip_line_ws();
let mut content = String::new();

ps.expect_char(']')?;
ps.expect_char(']')?;
loop {
while let Some(ch) = ps.take_char(|x| x != '\n') {
content.push(ch);
}

ps.skip_line_ws();
ps.next();

ps.expect_char('\n')?;
if !ps.current_is('#') || level != get_comment_level(ps)? {
break;
} else {
get_comment_start(ps, &level)?;
}
}

Ok(ast::Entry::Section {
name: symb,
comment,
})
match level {
CommentLevel::Comment => Ok(ast::Comment::Comment { content }),
CommentLevel::GroupComment => Ok(ast::Comment::GroupComment { content }),
CommentLevel::ResourceComment => Ok(ast::Comment::ResourceComment { content }),
}
}

fn get_message<I>(ps: &mut ParserStream<I>, comment: Option<ast::Comment>) -> Result<ast::Entry>
Expand Down Expand Up @@ -261,12 +257,12 @@ where
return Ok(ast::VarKey::Number(get_number(ps)?));
}
_ => {
return Ok(ast::VarKey::Symbol(get_symbol(ps)?));
return Ok(ast::VarKey::VariantName(get_variant_name(ps)?));
}
}
} else {
return error!(ErrorKind::ExpectedField {
field: "Symbol | Number".to_owned(),
field: "VariantName | Number".to_owned(),
});
}
}
Expand Down Expand Up @@ -323,7 +319,7 @@ where
Ok(variants)
}

fn get_symbol<I>(ps: &mut ParserStream<I>) -> Result<ast::Symbol>
fn get_variant_name<I>(ps: &mut ParserStream<I>) -> Result<ast::VariantName>
where
I: Iterator<Item = char>,
{
Expand All @@ -339,7 +335,7 @@ where
name.pop();
}

Ok(ast::Symbol { name })
Ok(ast::VariantName { name })
}

fn get_digits<I>(ps: &mut ParserStream<I>) -> Result<String>
Expand Down
24 changes: 13 additions & 11 deletions tests/fixtures/parser/ftl/03-comments.ftl
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// File comment
### File comment

// Standalone comment
# Standalone comment

// Another standalone comment
# Another standalone comment

//Comment with no leading space
# Comment with a leading space

// Comment with a leading space
## Multi
## Line Section
##
## Comment

//Multi
//Line
//Comment

//Comment for entity key1
# Comment for entity key1
key1 = New entity

//Comment for entity key2
# Comment for entity key2
key2=
| Multi line message

## Group comment
key3 = Message
10 changes: 0 additions & 10 deletions tests/fixtures/parser/ftl/04-sections.ftl

This file was deleted.

2 changes: 1 addition & 1 deletion tests/fixtures/parser/ftl/05-variants.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ key5
key5
.m = Foo

[[ section ]]
## section


key6 = {
Expand Down