aboutsummaryrefslogtreecommitdiffstats
path: root/libparser.h
blob: d8b13d985ba167995c45fac08d4f02524eba50d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
48
49
50
51
52
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
82
83
84
/* See LICENSE file for copyright and license details. */
#ifndef LIBPARSER_H
#define LIBPARSER_H

#include <stddef.h>


/* This is mostly internal (unless you want to programmatically create a grammar) { */

union libparser_sentence;

enum libparser_sentence_type {
	LIBPARSER_SENTENCE_TYPE_CONCATENATION, /* .binary */
	LIBPARSER_SENTENCE_TYPE_ALTERNATION,   /* .binary */
	LIBPARSER_SENTENCE_TYPE_REJECTION,     /* .unary */
	LIBPARSER_SENTENCE_TYPE_OPTIONAL,      /* .unary */
	LIBPARSER_SENTENCE_TYPE_REPEATED,      /* .unary */
	LIBPARSER_SENTENCE_TYPE_STRING,        /* .string */
	LIBPARSER_SENTENCE_TYPE_CHAR_RANGE,    /* .char_range */
	LIBPARSER_SENTENCE_TYPE_RULE,          /* .rule */
	LIBPARSER_SENTENCE_TYPE_EXCEPTION,     /* (none) */
	LIBPARSER_SENTENCE_TYPE_EOF            /* (none) */
};

struct libparser_sentence_binary {
	enum libparser_sentence_type type;
	const union libparser_sentence *left;
	const union libparser_sentence *right;
};

struct libparser_sentence_unary {
	enum libparser_sentence_type type;
	const union libparser_sentence *sentence;
};

struct libparser_sentence_string {
	enum libparser_sentence_type type;
	const char *string;
	size_t length;
};

struct libparser_sentence_char_range {
	enum libparser_sentence_type type;
	unsigned char low;
	unsigned char high;
};

struct libparser_sentence_rule {
	enum libparser_sentence_type type;
	const char *rule;
};

union libparser_sentence { 
	enum libparser_sentence_type type;
	struct libparser_sentence_binary binary;
	struct libparser_sentence_unary unary;
	struct libparser_sentence_string string;
	struct libparser_sentence_char_range char_range;
	struct libparser_sentence_rule rule;
};

struct libparser_rule {
	const char *name;
	union libparser_sentence *sentence;
};

/* } */


struct libparser_unit {
	const char *rule;
	struct libparser_unit *in;
	struct libparser_unit *next;
	size_t start;
	size_t end;
};


extern const struct libparser_rule *const libparser_rule_table[];


int libparser_parse_file(const struct libparser_rule *const rules[], const char *data, size_t length, struct libparser_unit **rootp);

#endif