@@ -14,7 +14,6 @@ pub type Result<T> = result::Result<T, ParserError>;
14
14
15
15
pub fn parse ( source : & str ) -> result:: Result < ast:: Resource , ( ast:: Resource , Vec < ParserError > ) > {
16
16
let mut errors = vec ! [ ] ;
17
- let mut comment = None ;
18
17
19
18
let mut ps = ParserStream :: new ( source. chars ( ) ) ;
20
19
@@ -26,18 +25,9 @@ pub fn parse(source: &str) -> result::Result<ast::Resource, (ast::Resource, Vec<
26
25
let entry_start_pos = ps. get_index ( ) ;
27
26
28
27
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) => {
39
29
entries. push ( entry) ;
40
- } ,
30
+ }
41
31
Err ( mut e) => {
42
32
let error_pos = ps. get_index ( ) ;
43
33
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<
51
41
}
52
42
53
43
if errors. is_empty ( ) {
54
- Ok ( ast:: Resource {
55
- body : entries,
56
- comment,
57
- } )
44
+ Ok ( ast:: Resource { body : entries } )
58
45
} else {
59
- Err ( (
60
- ast:: Resource {
61
- body : entries,
62
- comment,
63
- } ,
64
- errors,
65
- ) )
46
+ Err ( ( ast:: Resource { body : entries } , errors) )
66
47
}
67
48
}
68
49
69
50
fn get_entry < I > ( ps : & mut ParserStream < I > ) -> Result < ast:: Entry >
70
51
where
71
52
I : Iterator < Item = char > ,
72
53
{
73
- let comment = if ps. current_is ( '/ ' ) {
54
+ let comment = if ps. current_is ( '# ' ) {
74
55
Some ( get_comment ( ps) ?)
75
56
} else {
76
57
None
77
58
} ;
78
59
79
- if ps. current_is ( '[' ) {
80
- return Ok ( get_section ( ps, comment) ?) ;
81
- }
82
-
83
60
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
+ } ;
85
67
}
86
68
87
69
match comment {
@@ -90,60 +72,74 @@ where
90
72
}
91
73
}
92
74
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 < ( ) >
94
83
where
95
84
I : Iterator < Item = char > ,
96
85
{
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
+ }
107
90
108
- ps. next ( ) ;
91
+ if !ps. current_is ( '\n' ) {
92
+ ps. expect_char ( ' ' ) ?;
93
+ }
94
+ Ok ( ( ) )
95
+ }
109
96
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 ;
115
107
} else {
116
- break ;
108
+ level = CommentLevel :: GroupComment ;
117
109
}
118
110
}
119
-
120
- Ok ( ast :: Comment { content } )
111
+ ps . reset_peek ( ) ;
112
+ Ok ( level )
121
113
}
122
114
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 >
124
116
where
125
117
I : Iterator < Item = char > ,
126
118
{
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) ?;
133
121
134
- ps . skip_line_ws ( ) ;
122
+ let mut content = String :: new ( ) ;
135
123
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
+ }
138
128
139
- ps. skip_line_ws ( ) ;
129
+ ps. next ( ) ;
140
130
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
+ }
142
137
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
+ }
147
143
}
148
144
149
145
fn get_message < I > ( ps : & mut ParserStream < I > , comment : Option < ast:: Comment > ) -> Result < ast:: Entry >
@@ -261,12 +257,12 @@ where
261
257
return Ok ( ast:: VarKey :: Number ( get_number ( ps) ?) ) ;
262
258
}
263
259
_ => {
264
- return Ok ( ast:: VarKey :: Symbol ( get_symbol ( ps) ?) ) ;
260
+ return Ok ( ast:: VarKey :: VariantName ( get_variant_name ( ps) ?) ) ;
265
261
}
266
262
}
267
263
} else {
268
264
return error ! ( ErrorKind :: ExpectedField {
269
- field: "Symbol | Number" . to_owned( ) ,
265
+ field: "VariantName | Number" . to_owned( ) ,
270
266
} ) ;
271
267
}
272
268
}
@@ -323,7 +319,7 @@ where
323
319
Ok ( variants)
324
320
}
325
321
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 >
327
323
where
328
324
I : Iterator < Item = char > ,
329
325
{
@@ -339,7 +335,7 @@ where
339
335
name. pop ( ) ;
340
336
}
341
337
342
- Ok ( ast:: Symbol { name } )
338
+ Ok ( ast:: VariantName { name } )
343
339
}
344
340
345
341
fn get_digits < I > ( ps : & mut ParserStream < I > ) -> Result < String >
0 commit comments