-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dot.h
119 lines (90 loc) · 3.6 KB
/
dot.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Peppa PEG - Ultra lightweight PEG Parser in ANSI C.
*
* MIT License
*
* Copyright (c) 2021 Ju
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Example: Write a DOT Parser using Peppa PEG.
*
* https://graphviz.org/doc/info/lang.html
*
*/
# ifndef P4_LANG_DOT_H
# define P4_LANG_DOT_H
#ifdef __cplusplus
extern "C"
{
#endif
#include "../peppa.h"
P4_Grammar* P4_CreateDotGrammar() {
return P4_LoadGrammar(
"@lifted\n"
"entry = &. graph+ !.;\n"
"graph = keyword_strict? (keyword_graph / keyword_digraph) ID? \"{\" stmts \"}\";\n"
"@squashed\n"
"ID = identifier / number / html_string / string;\n"
"@squashed @tight\n"
"identifier = ([a-z]/[A-Z]/\"_\") ([a-z]/[A-Z]/[0-9]/\"_\")*;\n"
"@squashed @tight\n"
"number = \"-\"? ((\".\" [0-9]+) / [0-9]+ (\".\" [0-9]*)?);\n"
"@squashed @tight\n"
"string = \"\\\"\" (!\"\\\"\" . / \"\\\\\\\"\")+ \"\\\"\";\n"
"@nonterminal\n"
"node_id = ID port?;\n"
"node_stmt = node_id attr_list?;\n"
"port = \":\" compass_pt / \":\" ID (\":\" compass_pt)?;\n"
"compass_pt = \"ne\" / \"nw\" / \"n\" / \"se\" / \"sw\" / \"s\" / \"e\" / \"w\" / \"c\" / \"_\";\n"
"attribute = ID \"=\" ID;\n"
"@lifted\n"
"a_list = attribute ((\";\" / \",\") attribute)*;\n"
"attr_list = (\"[\" a_list \"]\")+;\n"
"attr_stmt = (keyword_graph / keyword_node / keyword_edge) attr_list;\n"
"keyword_graph = \"graph\";\n"
"keyword_node= \"node\";\n"
"keyword_edge= \"edge\";\n"
"@lifted\n"
"stmt = edge_stmt / subgraph / attr_stmt / attribute / node_stmt;\n"
"@spaced @lifted\n"
"ws = \" \" / \"\\t\" / \"\\n\" / \"\\r\";"
"directed = \"->\";"
"undirected = \"--\";"
"@lifted\n"
"edgeop = directed / undirected;\n"
"keyword_subgraph = \"subgraph\";\n"
"subgraph = (keyword_subgraph ID?)? \"{\" stmts? \"}\";\n"
"stmts = stmt (\";\"? stmt)*;\n"
"edge_stmt = (node_id / subgraph) (edgeop (node_id / subgraph))+ attr_list?;"
"keyword_digraph = \"digraph\";\n"
"keyword_strict = \"strict\";\n"
"@spaced @squashed @lifted\n"
"comment = macro_comment / c_comment / block_comment;\n"
"macro_comment = \"#\" (!\"\\n\" .)* \"\\n\"?;\n"
"c_comment = \"//\" (!\"\\n\" .)* \"\\n\"?;\n"
"block_comment = \"/*\" (!\"*/\" .)* \"*/\";\n"
"@squashed\n"
"html_string = \"<\" ((!(\"<\" / \">\") .)+/ html_string)* \">\";\n"
);
}
#ifdef __cplusplus
}
#endif
# endif