1 | RRDiagram-JS
|
2 | ---
|
3 |
|
4 | Generate railroad diagrams from code or BNF. Generate BNF from code.
|
5 |
|
6 | RR 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 |
|
8 | RR Diagram can also be used to generate BNF notation from a model.
|
9 |
|
10 | This 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 |
|
12 | Example
|
13 | =======
|
14 |
|
15 | This is the kind of diagrams that can get generated:
|
16 | ![H2 Select](http://rrdiagram.sourceforge.net/H2Select.svg)
|
17 |
|
18 | The above is generated using the right conversion options on this BNF:
|
19 | <pre>
|
20 | H2_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 |
|
29 | Usage
|
30 | =====
|
31 |
|
32 | To 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
|
34 | var bnfDisplay = new rrdiagram.bnfdisplay.BNFDisplay();
|
35 | bnfDisplay.replaceBNF('BNF', 'BNFSVG');
|
36 | ```
|
37 |
|
38 | Styles 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 |
|
50 | The whole API is available too.
|
51 |
|
52 | The diagram model represents the actual constructs visible on the diagram.
|
53 | To convert a diagram model to SVG:
|
54 | ```Javascript
|
55 | var rrDiagram = new rrdiagram.ui.RRDiagram(rrElement);
|
56 | var rrDiagramToSVG = new rrdiagram.ui.RRDiagramToSVG();
|
57 | var svg = rrDiagramToSVG.convert(rrDiagram);
|
58 | ```
|
59 |
|
60 | The grammar model represents a BNF-like grammar.
|
61 | It can be converted to a diagram model:
|
62 | ```Javascript
|
63 | var grammar = new rrdiagram.model.Grammar(rules);
|
64 | var grammarToRRDiagram = new rrdiagram.model.GrammarToRRDiagram();
|
65 | var rules = grammar.getRules();
|
66 | for(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 |
|
72 | The grammar model can be created from code, or can read BNF syntax:
|
73 | ```Javascript
|
74 | var bnfToGrammar = new rrdiagram.model.BNFToGrammar();
|
75 | var grammar = bnfToGrammar.convert(reader);
|
76 | // Do something with grammar, like get the diagram for SVG output.
|
77 | ```
|
78 |
|
79 | The grammar model can also be saved to BNF syntax:
|
80 | ```Javascript
|
81 | var grammarToBNF = new rrdiagram.model.GrammarToBNF();
|
82 | // Set options on the grammarToBNF object
|
83 | var bnf = grammarToBNF.convert(grammar);
|
84 | ```
|
85 |
|
86 | BNF Syntax
|
87 | ==========
|
88 |
|
89 | The supported BNF subset when reading is the following:
|
90 | <pre>
|
91 | - definition
|
92 | =
|
93 | :=
|
94 | ::=
|
95 | - concatenation
|
96 | ,
|
97 | <whitespace>
|
98 | - termination
|
99 | ;
|
100 | - alternation
|
101 | |
|
102 | - option
|
103 | [ ... ]
|
104 | ?
|
105 | - repetition
|
106 | { ... } => 0..N
|
107 | expression* => 0..N
|
108 | expression+ => 1..N
|
109 | <digits> * expression => <digits>...<digits>
|
110 | <digits> * [expression] => <0>...<digits>
|
111 | <digits> * expression? => <0>...<digits>
|
112 | - grouping
|
113 | ( ... )
|
114 | - literal
|
115 | " ... " or ' ... '
|
116 | - special characters
|
117 | (? ... ?)
|
118 | - comments
|
119 | (* ... *)
|
120 | </pre>
|
121 |
|
122 | When 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 |
|
124 | License
|
125 | =======
|
126 | This library is provided under the ASL, version 2.0 or later.
|
127 |
|
128 |
|
129 |
|
130 | Setup
|
131 | ---
|
132 |
|
133 | ```
|
134 | npm install
|
135 | ```
|
136 |
|
137 |
|
138 |
|
139 | Compile
|
140 | ---
|
141 |
|
142 | ```
|
143 | npm run compile
|
144 | ```
|