@@ -14,7 +14,6 @@ pub type Result<T> = result::Result<T, ParserError>;
1414
1515pub 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
6950fn get_entry < I > ( ps : & mut ParserStream < I > ) -> Result < ast:: Entry >
7051where
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 < ( ) >
9483where
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 >
124116where
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
149145fn 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 >
327323where
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
345341fn get_digits < I > ( ps : & mut ParserStream < I > ) -> Result < String >
0 commit comments