UNPKG

4.55 kBMarkdownView Raw
1RRDiagram-JS
2---
3
4Generate railroad diagrams from code or BNF. Generate BNF from code.
5
6RR Diagram is a Javascript library that generates railroad diagrams (also called syntax diagrams) from code or from BNF notation. The output format is a very compact SVG image where rules can contain links.
7
8RR Diagram can also be used to generate BNF notation from a model.
9
10This is a Javascript port of the [Java-based version](https://github.com/Chrriis/RRDiagram). This version adds the capability of converting BNF present in an HTML page as well as relying on CSS styles from the page to style the SVG content.
11
12Example
13=======
14
15This is the kind of diagrams that can get generated:
16![H2 Select](http://rrdiagram.sourceforge.net/H2Select.svg)
17
18The above is generated using the right conversion options on this BNF:
19<pre>
20H2_SELECT =
21'SELECT' [ 'TOP' term ] [ 'DISTINCT' | 'ALL' ] selectExpression {',' selectExpression} \
22'FROM' tableExpression {',' tableExpression} [ 'WHERE' expression ] \
23[ 'GROUP BY' expression {',' expression} ] [ 'HAVING' expression ] \
24[ ( 'UNION' [ 'ALL' ] | 'MINUS' | 'EXCEPT' | 'INTERSECT' ) select ] [ 'ORDER BY' order {',' order} ] \
25[ 'LIMIT' expression [ 'OFFSET' expression ] [ 'SAMPLE_SIZE' rowCountInt ] ] \
26[ 'FOR UPDATE' ];
27</pre>
28
29Usage
30=====
31
32To convert BNF text to a nice diagram, place the text in a `pre` tag and give it a class like `BNF`. Then include rrdiagram.js in your webpage. At the end of your page, add the following script to replace all those `pre` tags using the `BNF` class with a div that uses the `BNFSVG` class:
33```Javascript
34var bnfDisplay = new rrdiagram.bnfdisplay.BNFDisplay();
35bnfDisplay.replaceBNF('BNF', 'BNFSVG');
36```
37
38Styles used by the produced diagrams must be defined in the page. Here is an example of those definitions:
39```CSS
40.rrConnector {fill:none;stroke:#222222;}
41.rrRule {fill:#d3f0ff;stroke:#222222;}
42.rrRuleText {fill:#000000;font-family:Verdana,Sans-serif;font-size:12px;}
43.rrLiteral {fill:#90d9ff;stroke:#222222;}
44.rrLiteralText {fill:#000000;font-family:Verdana,Sans-serif;font-size:12px;}
45.rrSpecialSequence {fill:#e4f4ff;stroke:#222222;}
46.rrSpecialSequenceText {fill:#000000;font-family:Verdana,Sans-serif;font-size:12px;}
47.rrLoopCardinalities {fill:#000000;font-family:Verdana,Sans-serif;font-size:10px;}
48```
49
50The whole API is available too.
51
52The diagram model represents the actual constructs visible on the diagram.
53To convert a diagram model to SVG:
54```Javascript
55var rrDiagram = new rrdiagram.ui.RRDiagram(rrElement);
56var rrDiagramToSVG = new rrdiagram.ui.RRDiagramToSVG();
57var svg = rrDiagramToSVG.convert(rrDiagram);
58```
59
60The grammar model represents a BNF-like grammar.
61It can be converted to a diagram model:
62```Javascript
63var grammar = new rrdiagram.model.Grammar(rules);
64var grammarToRRDiagram = new rrdiagram.model.GrammarToRRDiagram();
65var rules = grammar.getRules();
66for(var i=0; i<rules.length; i++) {
67 var rrDiagram = grammarToRRDiagram.convert(rules[i]);
68 // Do something with diagram, like get the SVG.
69}
70```
71
72The grammar model can be created from code, or can read BNF syntax:
73```Javascript
74var bnfToGrammar = new rrdiagram.model.BNFToGrammar();
75var grammar = bnfToGrammar.convert(reader);
76// Do something with grammar, like get the diagram for SVG output.
77```
78
79The grammar model can also be saved to BNF syntax:
80```Javascript
81var grammarToBNF = new rrdiagram.model.GrammarToBNF();
82// Set options on the grammarToBNF object
83var bnf = grammarToBNF.convert(grammar);
84```
85
86BNF Syntax
87==========
88
89The supported BNF subset when reading is the following:
90<pre>
91- definition
92 =
93 :=
94 ::=
95- concatenation
96 ,
97 &lt;whitespace&gt;
98- termination
99 ;
100- alternation
101 |
102- option
103 [ ... ]
104 ?
105- repetition
106 { ... } =&gt; 0..N
107 expression* =&gt; 0..N
108 expression+ =&gt; 1..N
109 &lt;digits&gt; * expression => &lt;digits&gt;...&lt;digits&gt;
110 &lt;digits&gt; * [expression] => &lt;0&gt;...&lt;digits&gt;
111 &lt;digits&gt; * expression? => &lt;0&gt;...&lt;digits&gt;
112- grouping
113 ( ... )
114- literal
115 " ... " or ' ... '
116- special characters
117 (? ... ?)
118- comments
119 (* ... *)
120</pre>
121
122When getting the BNF syntax from the grammar model, it is possible to tweak the kind of BNF to get by changing some options on the converter.
123
124License
125=======
126This library is provided under the ASL, version 2.0 or later.
127
128
129
130Setup
131---
132
133```
134npm install
135```
136
137
138
139Compile
140---
141
142```
143npm run compile
144```