├── requirements.txt
├── try-statement.png
├── with-statement.png
├── README.md
├── .gitattributes
├── LICENSE
├── main.py
├── .gitignore
├── with-statement.svg
├── try-statement.svg
└── railroad.py
/requirements.txt:
--------------------------------------------------------------------------------
1 | CairoSVG==2.4.2
--------------------------------------------------------------------------------
/try-statement.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tonybaloney/python-railroads/HEAD/try-statement.png
--------------------------------------------------------------------------------
/with-statement.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tonybaloney/python-railroads/HEAD/with-statement.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Python grammar railroad diagrams
2 | ================================
3 |
4 | Requirements:
5 | - `cairo`, e.g. `brew install cairo`
6 |
7 | Run:
8 | ```console
9 | $ python main.py
10 | ```
11 |
12 | # The try statement
13 |
14 | 
15 |
16 | # The with statement
17 |
18 | 
19 |
20 | Credit
21 | ------
22 |
23 | railroad.py is adapted from https://github.com/tabatkins/railroad-diagrams.git
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2020, Anthony Shaw
2 |
3 | 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:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | 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.
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | import cairosvg
2 | from railroad import *
3 |
4 | TRY_STATEMENT = Diagram(
5 | Terminal('try', 'skip'),
6 | Terminal(':'),
7 | NonTerminal('suite'),
8 | Choice(0,
9 | Sequence(
10 | OneOrMore(
11 | Sequence(
12 | Terminal('except'),
13 | Optional(
14 | Sequence(
15 | NonTerminal('test'),
16 | Optional(
17 | Sequence(
18 | Terminal('as'),
19 | NonTerminal('NAME')
20 | )
21 | )
22 | )
23 | )
24 | ), Arrow('<')
25 | ),
26 | Optional(
27 | Sequence(
28 | Terminal('else'),
29 | Terminal(':'),
30 | NonTerminal('suite')
31 | )
32 | ),
33 | Optional(
34 | Sequence(
35 | Terminal('finally'),
36 | Terminal(':'),
37 | NonTerminal('suite')
38 | )
39 | ),
40 | ),
41 | Sequence(
42 | Terminal('finally'),
43 | Terminal(':'),
44 | NonTerminal('suite')
45 | )
46 | )
47 | )
48 |
49 | WITH_STATEMENT = Diagram(
50 | Terminal('with', 'skip'),
51 | NonTerminal('test'),
52 | Optional(
53 | Sequence(
54 | Terminal('as'),
55 | NonTerminal('expr')
56 | )),
57 | Optional(OneOrMore(Sequence(
58 | Terminal(', as'),
59 | NonTerminal('expr')
60 | ), Arrow('<'))),
61 | Terminal(':'),
62 | NonTerminal('suite')
63 | )
64 |
65 | statements = {
66 | 'try-statement': TRY_STATEMENT,
67 | 'with-statement': WITH_STATEMENT
68 | }
69 |
70 |
71 | def main():
72 | for key, value in statements.items():
73 | with open('{0}.svg'.format(key), 'w') as out_svg:
74 | diagram = value
75 | diagram.writeSvg(out_svg.write)
76 | cairosvg.svg2png(url='{0}.svg'.format(key), write_to='{0}.png'.format(key), output_width=3000, dpi=300)
77 |
78 |
79 | if __name__ == "__main__":
80 | main()
81 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #################
2 | ## Eclipse
3 | #################
4 |
5 | *.pydevproject
6 | .project
7 | .metadata
8 | bin/
9 | tmp/
10 | *.tmp
11 | *.bak
12 | *.swp
13 | *~.nib
14 | local.properties
15 | .classpath
16 | .settings/
17 | .loadpath
18 |
19 | # External tool builders
20 | .externalToolBuilders/
21 |
22 | # Locally stored "Eclipse launch configurations"
23 | *.launch
24 |
25 | # CDT-specific
26 | .cproject
27 |
28 | # PDT-specific
29 | .buildpath
30 |
31 |
32 | #################
33 | ## Visual Studio
34 | #################
35 |
36 | ## Ignore Visual Studio temporary files, build results, and
37 | ## files generated by popular Visual Studio add-ons.
38 |
39 | # User-specific files
40 | *.suo
41 | *.user
42 | *.sln.docstates
43 |
44 | # Build results
45 | [Dd]ebug/
46 | [Rr]elease/
47 | *_i.c
48 | *_p.c
49 | *.ilk
50 | *.meta
51 | *.obj
52 | *.pch
53 | *.pdb
54 | *.pgc
55 | *.pgd
56 | *.rsp
57 | *.sbr
58 | *.tlb
59 | *.tli
60 | *.tlh
61 | *.tmp
62 | *.vspscc
63 | .builds
64 | *.dotCover
65 |
66 | ## TODO: If you have NuGet Package Restore enabled, uncomment this
67 | #packages/
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 |
76 | # Visual Studio profiler
77 | *.psess
78 | *.vsp
79 |
80 | # ReSharper is a .NET coding add-in
81 | _ReSharper*
82 |
83 | # Installshield output folder
84 | [Ee]xpress
85 |
86 | # DocProject is a documentation generator add-in
87 | DocProject/buildhelp/
88 | DocProject/Help/*.HxT
89 | DocProject/Help/*.HxC
90 | DocProject/Help/*.hhc
91 | DocProject/Help/*.hhk
92 | DocProject/Help/*.hhp
93 | DocProject/Help/Html2
94 | DocProject/Help/html
95 |
96 | # Click-Once directory
97 | publish
98 |
99 | # Others
100 | [Bb]in
101 | [Oo]bj
102 | sql
103 | TestResults
104 | *.Cache
105 | ClientBin
106 | stylecop.*
107 | ~$*
108 | *.dbmdl
109 | Generated_Code #added for RIA/Silverlight projects
110 |
111 | # Backup & report files from converting an old project file to a newer
112 | # Visual Studio version. Backup files are not needed, because we have git ;-)
113 | _UpgradeReport_Files/
114 | Backup*/
115 | UpgradeLog*.XML
116 |
117 |
118 |
119 | ############
120 | ## Windows
121 | ############
122 |
123 | # Windows image file caches
124 | Thumbs.db
125 |
126 | # Folder config file
127 | Desktop.ini
128 |
129 |
130 | #############
131 | ## Python
132 | #############
133 |
134 | *.py[co]
135 |
136 | # Packages
137 | *.egg
138 | *.egg-info
139 | dist
140 | build
141 | eggs
142 | parts
143 | bin
144 | var
145 | sdist
146 | develop-eggs
147 | .installed.cfg
148 | MANIFEST
149 |
150 | # Installer logs
151 | pip-log.txt
152 |
153 | # Unit test / coverage reports
154 | .coverage
155 | .tox
156 |
157 | #Translations
158 | *.mo
159 |
160 | #Mr Developer
161 | .mr.developer.cfg
162 |
163 | # Mac crap
164 | .DS_Store
165 |
166 | testpy.html
167 | /.venv/
168 | /.idea/
169 |
--------------------------------------------------------------------------------
/with-statement.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/try-statement.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/railroad.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | from __future__ import division, unicode_literals
3 | import sys
4 | import math as Math
5 |
6 | if sys.version_info >= (3, ):
7 | unicode = str
8 |
9 | # Display constants
10 | DEBUG = False # if true, writes some debug information into attributes
11 | VS = 8 # minimum vertical separation between things. For a 3px stroke, must be at least 4
12 | AR = 10 # radius of arcs
13 | DIAGRAM_CLASS = 'railroad-diagram' # class to put on the root