| 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 | |
|---|
| 54 | YAML_DECLARE(yaml_event_t *) |
|---|
| 55 | yaml_parser_get_event(yaml_parser_t *parser); |
|---|
| 56 | |
|---|
| 57 | YAML_DECLARE(yaml_event_t *) |
|---|
| 58 | yaml_parser_peek_event(yaml_parser_t *parser); |
|---|
| 59 | |
|---|
| 60 | /* |
|---|
| 61 | * State functions. |
|---|
| 62 | */ |
|---|
| 63 | |
|---|
| 64 | static yaml_event_t * |
|---|
| 65 | yaml_parser_state_machine(yaml_parser_t *parser); |
|---|
| 66 | |
|---|
| 67 | static yaml_event_t * |
|---|
| 68 | yaml_parser_parse_stream_start(yaml_parser_t *parser); |
|---|
| 69 | |
|---|
| 70 | static yaml_event_t * |
|---|
| 71 | yaml_parser_parse_document_start(yaml_parser_t *parser, int implicit); |
|---|
| 72 | |
|---|
| 73 | static yaml_event_t * |
|---|
| 74 | yaml_parser_parse_document_content(yaml_parser_t *parser); |
|---|
| 75 | |
|---|
| 76 | static yaml_event_t * |
|---|
| 77 | yaml_parser_parse_document_end(yaml_parser_t *parser); |
|---|
| 78 | |
|---|
| 79 | static yaml_event_t * |
|---|
| 80 | yaml_parser_parse_node(yaml_parser_t *parser, |
|---|
| 81 | int block, int indentless_sequence); |
|---|
| 82 | |
|---|
| 83 | static yaml_event_t * |
|---|
| 84 | yaml_parser_parse_block_sequence_entry(yaml_parser_t *parser, int first); |
|---|
| 85 | |
|---|
| 86 | static yaml_event_t * |
|---|
| 87 | yaml_parser_parse_indentless_sequence_entry(yaml_parser_t *parser); |
|---|
| 88 | |
|---|
| 89 | static yaml_event_t * |
|---|
| 90 | yaml_parser_parse_block_mapping_key(yaml_parser_t *parser, int first); |
|---|
| 91 | |
|---|
| 92 | static yaml_event_t * |
|---|
| 93 | yaml_parser_parse_block_mapping_value(yaml_parser_t *parser); |
|---|
| 94 | |
|---|
| 95 | static yaml_event_t * |
|---|
| 96 | yaml_parser_parse_flow_sequence_entry(yaml_parser_t *parser, int first); |
|---|
| 97 | |
|---|
| 98 | static yaml_event_t * |
|---|
| 99 | yaml_parser_parse_flow_sequence_entry_mapping_key(yaml_parser_t *parser); |
|---|
| 100 | |
|---|
| 101 | static yaml_event_t * |
|---|
| 102 | yaml_parser_parse_flow_sequence_entry_mapping_value(yaml_parser_t *parser); |
|---|
| 103 | |
|---|
| 104 | static yaml_event_t * |
|---|
| 105 | yaml_parser_parse_flow_sequence_entry_mapping_end(yaml_parser_t *parser); |
|---|
| 106 | |
|---|
| 107 | static yaml_event_t * |
|---|
| 108 | yaml_parser_parse_flow_mapping_key(yaml_parser_t *parser, int first); |
|---|
| 109 | |
|---|
| 110 | static yaml_event_t * |
|---|
| 111 | yaml_parser_parse_flow_mapping_value(yaml_parser_t *parser, int empty); |
|---|
| 112 | |
|---|
| 113 | /* |
|---|
| 114 | * Get the next event and advance the parser. |
|---|
| 115 | */ |
|---|
| 116 | |
|---|
| 117 | YAML_DECLARE(yaml_event_t *) |
|---|
| 118 | yaml_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 | |
|---|
| 139 | YAML_DECLARE(yaml_event_t *) |
|---|
| 140 | yaml_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 | |
|---|
| 159 | static yaml_event_t * |
|---|
| 160 | yaml_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 | |
|---|