ypbody.rl 4.85 KB
Newer Older
Daniel Salzman's avatar
Daniel Salzman committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>

Daniel Salzman's avatar
Daniel Salzman committed
19
#include "libknot/internal/yparser/yparser.h"
Daniel Salzman's avatar
Daniel Salzman committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include "libknot/errcode.h"

%%{
	machine yparser;

	access parser->;

	# Newline processing.
	action _newline_init {
		// Return if key without value.
		if (parser->event != YP_ENULL && !parser->processed) {
			parser->processed = true;
			found = true;
			fbreak;
		}
	}
	action _newline {
		parser->line_count++;
		parser->event = YP_ENULL;
		parser->processed = false;
	}
	newline_char = '\n' >_newline_init %_newline;

	# Comment processing.
	comment_char = '#';
	comment      = comment_char . (^newline_char)*;

	# White space processing.
48
	sep_char = ' ';
Daniel Salzman's avatar
Daniel Salzman committed
49 50 51
	sep = sep_char+;

	blank = (sep? :> comment?). newline_char;
52
	rest  = ((sep :> comment) | sep?). newline_char;
Daniel Salzman's avatar
Daniel Salzman committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

	# Data processing.
	action _item_data_init {
		parser->data_len = 0;
	}
	action _item_data {
		if (parser->data_len >= sizeof(parser->data)) {
			return KNOT_ESPACE;
		}
		parser->data[parser->data_len++] = fc;
	}
	action _item_data_exit {
		// Return if a value parsed.
		parser->data[parser->data_len] = '\0';
		parser->processed = true;
		found = true;
		fbreak;
	}
	quote_char = '\"';
	list_char  = [\[,\]];
	data_char  =
		(ascii - space - cntrl - quote_char - sep_char -
		 comment_char - list_char
		) $_item_data;
	data_str_char =
		(data_char | sep_char | comment_char | list_char
		) $_item_data;
	data_str = (quote_char . data_str_char* . quote_char);
	item_data = (data_char+ | data_str) >_item_data_init %_item_data_exit;
82 83
	item_data_plus = item_data . ((sep? . ',' . sep?) . item_data)*;
	item_data_list = '\[' . sep? . item_data_plus . sep? . '\]';
Daniel Salzman's avatar
Daniel Salzman committed
84 85 86

	# Key processing.
	action _key_init {
87 88 89 90
		if (indent > 0 && parser->indent > 0 &&
		    indent != parser->indent) {
			return KNOT_EPARSEFAIL;
		}
Daniel Salzman's avatar
Daniel Salzman committed
91 92 93 94 95 96 97 98 99 100 101 102 103
		parser->processed = false;
		parser->key_len = 0;
		parser->data_len = 0;
		parser->event = YP_ENULL;
	}
	action _key {
		if (parser->key_len >= sizeof(parser->key)) {
			return KNOT_ESPACE;
		}
		parser->key[parser->key_len++] = fc;
	}
	action _key0_exit {
		parser->key[parser->key_len] = '\0';
104
		parser->indent = 0;
Daniel Salzman's avatar
Daniel Salzman committed
105 106 107 108
		parser->event = YP_EKEY0;
	}
	action _key1_exit {
		parser->key[parser->key_len] = '\0';
109
		parser->indent = indent;
Daniel Salzman's avatar
Daniel Salzman committed
110 111 112 113
		parser->event = YP_EKEY1;
	}
	action _id_exit {
		parser->key[parser->key_len] = '\0';
114 115
		parser->indent = indent;
		parser->id_pos = id_pos;
Daniel Salzman's avatar
Daniel Salzman committed
116 117
		parser->event = YP_EID;
	}
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	action _indent {
		indent++;
	}
	action _id {
		id_pos++;
	}
	action _dash_init {
		if (id_pos > 0 && parser->id_pos > 0 &&
		    id_pos != parser->id_pos) {
			return KNOT_EPARSEFAIL;
		}
		parser->indent = 0;
	}
	key_name = (alnum . ("-" | alnum)*) >_key_init $_key;
	key0 =                                                  key_name %_key0_exit;
	key1 =   sep $_indent .                                 key_name %_key1_exit;
	id   = ((sep $_id)? . "-" >_dash_init . sep) $_indent . key_name %_id_exit;
	item = (((key0 . sep? . ":" . (sep . (item_data_list | item_data))?)
	        |(key1 . sep? . ":" .  sep . (item_data_list | item_data))
	        |(id   . sep? . ":" .  sep . item_data)
	        ) . rest
Daniel Salzman's avatar
Daniel Salzman committed
139 140 141 142 143 144 145 146 147 148 149 150 151
	       );

	# Main processing loop.
	action _error {
		return KNOT_EPARSEFAIL;
	}

	main := (blank | item)* $!_error;
}%%

// Include parser static data (Ragel internals).
%% write data;

Daniel Salzman's avatar
Daniel Salzman committed
152 153 154
int _yp_start_state = %%{ write start; }%%;

int _yp_parse(
Daniel Salzman's avatar
Daniel Salzman committed
155 156 157 158 159
	yp_parser_t *parser)
{
	// Parser input limits (Ragel internals).
	const char *p, *pe, *eof;

160 161 162 163
	// Current item indent.
	size_t indent = 0;
	// Current id dash position.
	size_t id_pos = 0;
Daniel Salzman's avatar
Daniel Salzman committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	// Indicates if the current parsing step contains an item.
	bool found = false;

	if (!parser->input.eof) { // Restore parser input limits.
		p = parser->input.current;
		pe = parser->input.end;
		eof = NULL;
	} else { // Set the last artifical block with just one new line char.
		p = "\n";
		pe = p + 1;
		eof = pe;
	}

	// Include parser body.
	%% write exec;

	// Store the current parser position.
	if (!parser->input.eof) {
		parser->input.current = p;
	} else {
		parser->input.current = parser->input.end;
	}

	// Check for general parser error.
	if (parser->cs == %%{ write error; }%%) {
		return KNOT_EPARSEFAIL;
	}

	// Check if parsed an item.
	if (found) {
		return KNOT_EOK;
	} else {
		return KNOT_EFEWDATA;
	}
}