Skip to content

Commit 6c373e2

Browse files
Parse directives in comments in-place
1 parent 638eb32 commit 6c373e2

File tree

3 files changed

+191
-22
lines changed

3 files changed

+191
-22
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ clean:
44

55
test:
66
./vhdlproc/vhdlproc.py --self-test
7+
./vhdlproc/vhdlproc.py vhdlproc/tests/and.vhdl
8+
./vhdlproc/vhdlproc.py vhdlproc/tests/and.proc.vhdl --parse-comments
9+
./vhdlproc/vhdlproc.py vhdlproc/tests/include.vhdl
10+
./vhdlproc/vhdlproc.py vhdlproc/tests/include.proc.vhdl --parse-comments
11+
./vhdlproc/vhdlproc.py vhdlproc/tests/nest.vhdl
12+
./vhdlproc/vhdlproc.py vhdlproc/tests/nest.proc.vhdl --parse-comments
713

814
format:
915
black .

README.md

Lines changed: 128 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ $ ./vhdlproc/vhdlproc.py --help
3030
### Command Line
3131

3232
```
33-
usage: vhdlproc.py [-h] [-D IDENTIFIER=value] [-o DIRECTORY] [-e EXTENSION] [--self-test]
34-
[--log-level LEVEL]
33+
usage: vhdlproc.py [-h] [-D IDENTIFIER=value] [-o DIRECTORY] [-e EXTENSION] [--parse-comments]
34+
[--self-test] [--log-level LEVEL]
3535
[input ...]
3636
37-
VHDLproc v2.2 - VHDL Preprocessor
37+
VHDLproc v2.3 - VHDL Preprocessor
3838
3939
positional arguments:
4040
input Input files (will skip over files with the output extension)
@@ -44,6 +44,8 @@ options:
4444
-D IDENTIFIER=value Specify identifiers for conditional compilation, ex. DEBUG_LEVEL=2
4545
-o DIRECTORY Directory to store parsed files
4646
-e EXTENSION Output extension for processed files (defaults to '.proc.vhdl')
47+
--parse-comments Parse commented directives as though they aren't commented, overwrite original
48+
file. Disables skipping based on file extension
4749
--self-test Run a self-test to ensure functionality
4850
--log-level LEVEL Configure the logging level
4951
```
@@ -69,6 +71,14 @@ ghdl -a --std=08 build/*.vhdl # pass processed files in build/ to ghdl
6971
ghdl -r --std=08 testbench # run simulation
7072
```
7173

74+
Commented directives can also be parsed in-place, including replacing `include` directives:
75+
76+
```bash
77+
vhdlproc *.vhdl -e .vhdl --parse-comments # parse commented directives and overwrite original file
78+
ghdl -a --std=08 *.vhdl # same exact files that were passed to ghdl
79+
ghdl -r --std=08 testbench # run simulation
80+
```
81+
7282
### Python Library
7383

7484
Parse files (will automatically set the include path):
@@ -130,18 +140,22 @@ parsed_text = processor.parse(code, identifiers=identifiers, include_path="path/
130140
131141
`end [if]
132142
133-
`warning "STRING" - Print STRING to standard error output stream
143+
`warning "STRING" -- Print STRING to standard error output stream
134144
135-
`error "STRING" - Print STRING to standard error output stream
136-
Will force close VHDLproc without saving
145+
`error "STRING" -- Print STRING to standard error output stream
146+
-- Will force close VHDLproc without saving
137147
138148
-- Additional extensions not part of VHDL-2019
139149
140-
`define LABEL "STRING" - Gives LABEL the value of STRING for
141-
conditional statements
150+
`define LABEL "STRING" -- Gives LABEL the value of STRING for
151+
-- conditional statements
152+
153+
`include "FILENAME" -- Include another file relative to
154+
-- the location of the source
142155
143-
`include "FILENAME" - Include another file relative to
144-
the location of the source
156+
`end include "FILENAME" -- This is a counterpart to `include for parsing commented directives
157+
-- in-place, should not be used directly (added automatically)
158+
-- Sets the bound of where to replace when re-including a file
145159
```
146160

147161
### Identifiers (or Labels)
@@ -154,6 +168,8 @@ By default, `TOOL_NAME` is set to `VHDLproc` and `TOOL_VERSION` is set to the cu
154168
- [ ] Prevent a file from including itself (to prevent infinite loops)
155169
- [ ] Modify text and file operations to work on Windows (if they don't already)
156170
- [ ] Throw an error if a `` `warning `` or `` `error `` string isn't wrapped in quotes
171+
- [x] Parse comments / files in-place
172+
- [x] Fix precedence of operators
157173
- [x] Add the option to the CLI to take in a series of file inputs, process them, save the individual results to temporary files (i.e. in `/tmp/` or a local path), then return all of the filepaths. This would be useful for doing this with GHDL: `ghdl -a $(vhdlproc *.vhdl)`.
158174

159175
## Examples
@@ -191,6 +207,7 @@ component pll is
191207
clk_locked : out std_logic
192208
);
193209
end component;
210+
-- `end include "include-to.vhdl"
194211
```
195212

196213
### Define, Repeated If/Elsif
@@ -283,4 +300,105 @@ constant enable_features : bool := true
283300
-- `warning "Certain features disabled!"
284301
-- constant enable_features : bool := false
285302
-- `end
303+
```
304+
305+
### Parsing Comments
306+
307+
With the flag `--parse-comments`, directives are executed in-place as if they weren't commented. Code added by an `include` directive is replaced with an updated version, bounded by a corresponding `end include`.
308+
309+
Input:
310+
```vhdl
311+
-- `warning "== Including file =="
312+
-- `define Include_file "TRUE"
313+
314+
-- `if INCLUDE_FILE = "TRUE" then
315+
-- `include "../tests/include-to.vhdl"
316+
component OLD_CODE is
317+
port(
318+
a : in unsigned(3 downto 0);
319+
b : in unsigned(3 downto 0);
320+
s : in std_logic_vector(1 downto 0);
321+
y : out unsigned(3 downto 0)
322+
);
323+
end component;
324+
-- `end include "../tests/include-to.vhdl"
325+
-- `else
326+
-- `error "Not including thing"
327+
-- `end if
328+
329+
-- `warning "== Not including file =="
330+
-- `define Include_file "false"
331+
-- `define passed ""
332+
333+
-- `if INCLUDE_FILE = "TRUE" then
334+
-- `include "../tests/include-to.vhdl"
335+
-- `else
336+
-- `end if
337+
338+
-- `if passed /= "" then
339+
-- `Warning "Failed"
340+
-- `else
341+
-- `Warning "Passed"
342+
-- `end
343+
```
344+
345+
include-to.vhdl:
346+
347+
```vhdl
348+
component pll is
349+
port (
350+
clk_in : in std_logic;
351+
clk_out : out std_logic;
352+
clk_locked : out std_logic
353+
);
354+
end component;
355+
`if include_file = "false" then
356+
`error "Failed"
357+
`else
358+
`warning "Passed"
359+
`end
360+
361+
`define passed "failed"
362+
```
363+
364+
Output:
365+
```vhdl
366+
-- `warning "== Including file =="
367+
-- `define Include_file "TRUE"
368+
369+
-- `if INCLUDE_FILE = "TRUE" then
370+
-- `include "../tests/include-to.vhdl"
371+
component pll is
372+
port (
373+
clk_in : in std_logic;
374+
clk_out : out std_logic;
375+
clk_locked : out std_logic
376+
);
377+
end component;
378+
-- `if include_file = "false" then
379+
-- `error "Failed"
380+
-- `else
381+
-- `warning "Passed"
382+
-- `end
383+
384+
-- `define passed "failed"
385+
-- `end include "../tests/include-to.vhdl"
386+
-- `else
387+
-- `error "Not including thing"
388+
-- `end if
389+
390+
-- `warning "== Not including file =="
391+
-- `define Include_file "false"
392+
-- `define passed ""
393+
394+
-- `if INCLUDE_FILE = "TRUE" then
395+
-- `include "../tests/include-to.vhdl"
396+
-- `else
397+
-- `end if
398+
399+
-- `if passed /= "" then
400+
-- `Warning "Failed"
401+
-- `else
402+
-- `Warning "Passed"
403+
-- `end
286404
```

0 commit comments

Comments
 (0)