|
| 1 | +# ============================================================================= |
| 2 | +# _____ ____ _ _ ____ _ _ __ __ _ _ |
| 3 | +# _ __ _ _| ____| _ \ / \ / \ | _ \ _ __ ___ (_) ___ ___| |_| \/ | ___ __| | ___| | |
| 4 | +# | '_ \| | | | _| | | | |/ _ \ / _ \ | |_) | '__/ _ \| |/ _ \/ __| __| |\/| |/ _ \ / _` |/ _ \ | |
| 5 | +# | |_) | |_| | |___| |_| / ___ \ / ___ \ _| __/| | | (_) | | __/ (__| |_| | | | (_) | (_| | __/ | |
| 6 | +# | .__/ \__, |_____|____/_/ \_\/_/ \_(_)_| |_| \___// |\___|\___|\__|_| |_|\___/ \__,_|\___|_| |
| 7 | +# |_| |___/ |__/ |
| 8 | +# ============================================================================= |
| 9 | +# Authors: Patrick Lehmann |
| 10 | +# |
| 11 | +# Package installer: An abstract model of EDA tool projects. |
| 12 | +# |
| 13 | +# License: |
| 14 | +# ============================================================================ |
| 15 | +# Copyright 2017-2021 Patrick Lehmann - Boetzingen, Germany |
| 16 | +# |
| 17 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 18 | +# you may not use this file except in compliance with the License. |
| 19 | +# You may obtain a copy of the License at |
| 20 | +# |
| 21 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 22 | +# |
| 23 | +# Unless required by applicable law or agreed to in writing, software |
| 24 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 25 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 26 | +# See the License for the specific language governing permissions and |
| 27 | +# limitations under the License. |
| 28 | +# |
| 29 | +# SPDX-License-Identifier: Apache-2.0 |
| 30 | +# ============================================================================ |
| 31 | +# |
| 32 | +from enum import Enum, unique |
| 33 | +from pathlib import Path |
| 34 | + |
| 35 | +from pydecor import export |
| 36 | +from typing import Optional as Nullable, List |
| 37 | + |
| 38 | +from pyEDAA.ProjectModel import ProjectFile, TCLContent, Project, Design, FileSet, VHDLLibrary, VHDLSourceFile |
| 39 | + |
| 40 | + |
| 41 | +@export |
| 42 | +class OSVVMProjectFile(ProjectFile, TCLContent): |
| 43 | + """An OSVVM project file (``*.pro``).""" |
| 44 | + |
| 45 | + _osvvmProject: Nullable[Project] |
| 46 | + |
| 47 | + def __init__( |
| 48 | + self, |
| 49 | + path: Path, |
| 50 | + project: Project = None, |
| 51 | + design: Design = None, |
| 52 | + fileSet: FileSet = None |
| 53 | + ): |
| 54 | + super().__init__(path, project, design, fileSet) |
| 55 | + |
| 56 | + self._osvvmProject = None |
| 57 | + |
| 58 | + @property |
| 59 | + def ProjectModel(self) -> Project: |
| 60 | + return self._osvvmProject |
| 61 | + |
| 62 | + class Instruction: |
| 63 | + _line: int |
| 64 | + |
| 65 | + def __init__(self, line: int): |
| 66 | + self._line = line |
| 67 | + |
| 68 | + class Empty(Instruction): |
| 69 | + def __init__(self, line: int): |
| 70 | + super().__init__(line) |
| 71 | + |
| 72 | + class Comment(Instruction): |
| 73 | + _commentText: str |
| 74 | + |
| 75 | + def __init__(self, line: int, commentText: str): |
| 76 | + super().__init__(line) |
| 77 | + self._commentText = commentText.rstrip() |
| 78 | + |
| 79 | + @property |
| 80 | + def CommentText(self) -> str: |
| 81 | + return self._commentText |
| 82 | + |
| 83 | + class Analyze(Instruction): |
| 84 | + _vhdlSourceFile: VHDLSourceFile |
| 85 | + |
| 86 | + def __init__(self, line: int, parameterText: str): |
| 87 | + super().__init__(line) |
| 88 | + self._vhdlSourceFile = VHDLSourceFile(Path(parameterText.strip())) |
| 89 | + |
| 90 | + @property |
| 91 | + def VHDLSourceFile(self) -> VHDLSourceFile: |
| 92 | + return self._vhdlSourceFile |
| 93 | + |
| 94 | + class Library(Instruction): |
| 95 | + _vhdlLibrary: VHDLLibrary |
| 96 | + |
| 97 | + def __init__(self, line: int, parameterText: str): |
| 98 | + super().__init__(line) |
| 99 | + self._vhdlLibrary = VHDLLibrary(parameterText.strip()) |
| 100 | + |
| 101 | + @property |
| 102 | + def VHDLLibrary(self) -> VHDLLibrary: |
| 103 | + return self._vhdlLibrary |
| 104 | + |
| 105 | + class Include(Instruction): |
| 106 | + _osvvmProjectFile: 'OSVVMProjectFile' |
| 107 | + _fileSet: FileSet |
| 108 | + |
| 109 | + def __init__(self, line: int, workingDirectory: Path, parameterText: str): |
| 110 | + super().__init__(line) |
| 111 | + |
| 112 | + includeFile = Path(parameterText.strip()) |
| 113 | + includePath = (workingDirectory / includeFile).resolve() |
| 114 | + |
| 115 | + self._fileSet = FileSet(includeFile.name, directory=includeFile.parent) |
| 116 | + self._osvvmProjectFile = OSVVMProjectFile(includePath) |
| 117 | + |
| 118 | + @property |
| 119 | + def OSVVMProjectFile(self) -> 'OSVVMProjectFile': |
| 120 | + return self._osvvmProjectFile |
| 121 | + |
| 122 | + def Parse(self, fileSet: FileSet): |
| 123 | + self._fileSet.Parent = fileSet |
| 124 | + |
| 125 | + for instruction in self._osvvmProjectFile._Parse(): |
| 126 | + if isinstance(instruction, OSVVMProjectFile.Include): |
| 127 | + instruction.Parse(self._fileSet) |
| 128 | + elif isinstance(instruction, OSVVMProjectFile.Analyze): |
| 129 | + self._fileSet.AddFile(instruction.VHDLSourceFile) |
| 130 | + elif isinstance(instruction, OSVVMProjectFile.Library): |
| 131 | + self._fileSet.Design.AddVHDLLibrary(instruction.VHDLLibrary) |
| 132 | +# elif isinstance(instruction, OSVVMProjectFile.Build): |
| 133 | + |
| 134 | + elif not isinstance(instruction, (OSVVMProjectFile.Empty, OSVVMProjectFile.Comment)): |
| 135 | + raise Exception(f"Unknown instruction '{instruction.__class__.__name__}' in OSVVM project file '{self._osvvmProjectFile.ResolvedPath}'") |
| 136 | + |
| 137 | + def Parse(self): |
| 138 | + projectName = self._path.name |
| 139 | + self._osvvmProject = Project(projectName, rootDirectory=self._path.parent) |
| 140 | + |
| 141 | + fileSet = self._osvvmProject.DefaultDesign.DefaultFileSet |
| 142 | + |
| 143 | + for instruction in self._Parse(): |
| 144 | + if isinstance(instruction, OSVVMProjectFile.Include): |
| 145 | + instruction.Parse(fileSet) |
| 146 | + elif isinstance(instruction, OSVVMProjectFile.Analyze): |
| 147 | + fileSet.AddFile(instruction.VHDLSourceFile) |
| 148 | + elif not isinstance(instruction, (OSVVMProjectFile.Empty, OSVVMProjectFile.Comment)): |
| 149 | + raise Exception(f"Unknown instruction '{instruction.__class__.__name__}' in OSVVM project file '{self.ResolvedPath}'") |
| 150 | + |
| 151 | + def _Parse(self): |
| 152 | + path = self.ResolvedPath |
| 153 | + if not path.exists(): |
| 154 | + raise Exception(f"OSVVM project file '{path}' not found.") from FileNotFoundError(f"File '{path}' not found.") |
| 155 | + |
| 156 | + instructions: List = [] |
| 157 | + print() |
| 158 | + with path.open("r") as file: |
| 159 | + i = 1 |
| 160 | + for line in file: |
| 161 | + line = line.lstrip() |
| 162 | + |
| 163 | + if line.startswith("#"): |
| 164 | + comment = OSVVMProjectFile.Comment(i, line[1:]) |
| 165 | + instructions.append(comment) |
| 166 | + |
| 167 | + elif line.startswith("analyze"): |
| 168 | + vhdlFile = OSVVMProjectFile.Analyze(i, line[8:]) |
| 169 | + instructions.append(vhdlFile) |
| 170 | + |
| 171 | + elif line.startswith("library"): |
| 172 | + vhdlLibrary = OSVVMProjectFile.Library(i, line[8:]) |
| 173 | + instructions.append(vhdlLibrary) |
| 174 | + |
| 175 | + elif line.startswith("include"): |
| 176 | + include = OSVVMProjectFile.Include(i, path.parent, line[8:]) |
| 177 | + instructions.append(include) |
| 178 | + |
| 179 | + elif line.startswith("build"): |
| 180 | + parameter = line[6:] |
| 181 | + print(f"BUILD: {parameter}") |
| 182 | + elif line.startswith("if"): |
| 183 | + print(f"IF (line={i}): {line[3:].rstrip()}") |
| 184 | + elif line.startswith("}"): |
| 185 | + print(f"}} (line={i}): {line[2:].rstrip()}") |
| 186 | + elif len(line) == 0: |
| 187 | + instructions.append(OSVVMProjectFile.Empty(i)) |
| 188 | + else: |
| 189 | + print(f"UNKNOWN (line={i}): '{line.rstrip()}'") |
| 190 | + |
| 191 | + i += 1 |
| 192 | + |
| 193 | + return instructions |
0 commit comments