source: libyaml/trunk/src/parser.c @ 201

Revision 201, 7.4 KB checked in by xi, 8 years ago (diff)

Start working on the parser.

Line 
1
2/*
3 * The parser implements the following grammar:
4 *
5 * stream               ::= STREAM-START implicit_document? explicit_document* STREAM-END
6 * implicit_document    ::= block_node DOCUMENT-END*
7 * explicit_document    ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
8 * block_node_or_indentless_sequence    ::=
9 *                          ALIAS
10 *                          | properties (block_content | indentless_block_sequence)?
11 *                          | block_content
12 *                          | indentless_block_sequence
13 * block_node           ::= ALIAS
14 *                          | properties block_content?
15 *                          | block_content
16 * flow_node            ::= ALIAS
17 *                          | properties flow_content?
18 *                          | flow_content
19 * properties           ::= TAG ANCHOR? | ANCHOR TAG?
20 * block_content        ::= block_collection | flow_collection | SCALAR
21 * flow_content         ::= flow_collection | SCALAR
22 * block_collection     ::= block_sequence | block_mapping
23 * flow_collection      ::= flow_sequence | flow_mapping
24 * block_sequence       ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
25 * indentless_sequence  ::= (BLOCK-ENTRY block_node?)+
26 * block_mapping        ::= BLOCK-MAPPING_START
27 *                          ((KEY block_node_or_indentless_sequence?)?
28 *                          (VALUE block_node_or_indentless_sequence?)?)*
29 *                          BLOCK-END
30 * flow_sequence        ::= FLOW-SEQUENCE-START
31 *                          (flow_sequence_entry FLOW-ENTRY)*
32 *                          flow_sequence_entry?
33 *                          FLOW-SEQUENCE-END
34 * flow_sequence_entry  ::= flow_node | KEY flow_node? (VALUE flow_node?)?
35 * flow_mapping         ::= FLOW-MAPPING-START
36 *                          (flow_mapping_entry FLOW-ENTRY)*
37 *                          flow_mapping_entry?
38 *                          FLOW-MAPPING-END
39 * flow_mapping_entry   ::= flow_node | KEY flow_node? (VALUE flow_node?)?
40 */
41
42#if HAVE_CONFIG_H
43#include <config.h>
44#endif
45
46#include <yaml.h>
47
48#include <assert.h>
49
50/*
51 * Public API declarations.
52 */
53
54YAML_DECLARE(yaml_event_t *)
55yaml_parser_get_event(yaml_parser_t *parser);
56
57YAML_DECLARE(yaml_event_t *)
58yaml_parser_peek_event(yaml_parser_t *parser);
59
60/*
61 * State functions.
62 */
63
64static yaml_event_t *
65yaml_parser_state_machine(yaml_parser_t *parser);
66
67static yaml_event_t *
68yaml_parser_parse_stream_start(yaml_parser_t *parser);
69
70static yaml_event_t *
71yaml_parser_parse_document_start(yaml_parser_t *parser, int implicit);
72
73static yaml_event_t *
74yaml_parser_parse_document_content(yaml_parser_t *parser);
75
76static yaml_event_t *
77yaml_parser_parse_document_end(yaml_parser_t *parser);
78
79static yaml_event_t *
80yaml_parser_parse_node(yaml_parser_t *parser,
81        int block, int indentless_sequence);
82
83static yaml_event_t *
84yaml_parser_parse_block_sequence_entry(yaml_parser_t *parser, int first);
85
86static yaml_event_t *
87yaml_parser_parse_indentless_sequence_entry(yaml_parser_t *parser);
88
89static yaml_event_t *
90yaml_parser_parse_block_mapping_key(yaml_parser_t *parser, int first);
91
92static yaml_event_t *
93yaml_parser_parse_block_mapping_value(yaml_parser_t *parser);
94
95static yaml_event_t *
96yaml_parser_parse_flow_sequence_entry(yaml_parser_t *parser, int first);
97
98static yaml_event_t *
99yaml_parser_parse_flow_sequence_entry_mapping_key(yaml_parser_t *parser);
100
101static yaml_event_t *
102yaml_parser_parse_flow_sequence_entry_mapping_value(yaml_parser_t *parser);
103
104static yaml_event_t *
105yaml_parser_parse_flow_sequence_entry_mapping_end(yaml_parser_t *parser);
106
107static yaml_event_t *
108yaml_parser_parse_flow_mapping_key(yaml_parser_t *parser, int first);
109
110static yaml_event_t *
111yaml_parser_parse_flow_mapping_value(yaml_parser_t *parser, int empty);
112
113/*
114 * Get the next event and advance the parser.
115 */
116
117YAML_DECLARE(yaml_event_t *)
118yaml_parser_get_event(yaml_parser_t *parser)
119{
120    yaml_event_t *value;
121
122    /* Update the current event if needed. */
123   
124    if (!parser->current_event) {
125        parser->current_event = yaml_parser_state_machine(parser);
126    }
127
128    /* Return and clear the current event. */
129
130    value = parser->current_event;
131    parser->current_event = NULL;
132    return value;
133}
134
135/*
136 * Peek the next event.
137 */
138
139YAML_DECLARE(yaml_event_t *)
140yaml_parser_peek_event(yaml_parser_t *parser)
141{
142    yaml_event_t *value;
143
144    /* Update the current event if needed. */
145   
146    if (!parser->current_event) {
147        parser->current_event = yaml_parser_state_machine(parser);
148    }
149
150    /* Return the current event. */
151
152    return parser->current_event;
153}
154
155/*
156 * State dispatcher.
157 */
158
159static yaml_event_t *
160yaml_parser_state_machine(yaml_parser_t *parser)
161{
162    assert (parser->state != YAML_PARSE_END_STATE);
163
164    switch (parser->state)
165    {
166        case YAML_PARSE_STREAM_START_STATE:
167            return yaml_parser_parse_stream_start(parser);
168
169        case YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE:
170            return yaml_parser_parse_document_start(parser, 1);
171
172        case YAML_PARSE_DOCUMENT_START_STATE:
173            return yaml_parser_parse_document_start(parser, 0);
174
175        case YAML_PARSE_DOCUMENT_CONTENT_STATE:
176            return yaml_parser_parse_document_content(parser);
177
178        case YAML_PARSE_DOCUMENT_END_STATE:
179            return yaml_parser_parse_document_end(parser);
180
181        case YAML_PARSE_BLOCK_NODE_STATE:
182            return yaml_parser_parse_node(parser, 1, 0);
183
184        case YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE:
185            return yaml_parser_parse_node(parser, 1, 1);
186
187        case YAML_PARSE_FLOW_NODE_STATE:
188            return yaml_parser_parse_node(parser, 0, 0);
189
190        case YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE:
191            return yaml_parser_parse_block_sequence_entry(parser, 1);
192
193        case YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE:
194            return yaml_parser_parse_block_sequence_entry(parser, 0);
195
196        case YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE:
197            return yaml_parser_parse_indentless_sequence_entry(parser);
198
199        case YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE:
200            return yaml_parser_parse_block_mapping_key(parser, 1);
201
202        case YAML_PARSE_BLOCK_MAPPING_KEY_STATE:
203            return yaml_parser_parse_block_mapping_key(parser, 0);
204
205        case YAML_PARSE_BLOCK_MAPPING_VALUE_STATE:
206            return yaml_parser_parse_block_mapping_value(parser);
207
208        case YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE:
209            return yaml_parser_parse_flow_sequence_entry(parser, 1);
210
211        case YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE:
212            return yaml_parser_parse_flow_sequence_entry(parser, 0);
213
214        case YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE:
215            return yaml_parser_parse_flow_sequence_entry_mapping_key(parser);
216
217        case YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE:
218            return yaml_parser_parse_flow_sequence_entry_mapping_value(parser);
219
220        case YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE:
221            return yaml_parser_parse_flow_sequence_entry_mapping_end(parser);
222
223        case YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE:
224            return yaml_parser_parse_flow_mapping_key(parser, 1);
225
226        case YAML_PARSE_FLOW_MAPPING_KEY_STATE:
227            return yaml_parser_parse_flow_mapping_key(parser, 0);
228
229        case YAML_PARSE_FLOW_MAPPING_VALUE_STATE:
230            return yaml_parser_parse_flow_mapping_value(parser, 0);
231
232        case YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE:
233            return yaml_parser_parse_flow_mapping_value(parser, 1);
234    }
235    assert(1);
236}
237
Note: See TracBrowser for help on using the repository browser.