Newer
Older
# -*- coding: utf-8 -*-
#
# Copyright (c) 2013 CERN
# Author: Tomasz Wlostowski (tomasz.wlostowski@cern.ch)
#
# This file is part of Hdlmake.
#
# Hdlmake is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Hdlmake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Hdlmake. If not, see <http://www.gnu.org/licenses/>.
def _remove_gaps(buf, delims, gap_chars, lower_strings=False):
da = {}
for c in buf:
for d in delims:
within_string = any(da.values()) and not (c in delims)
if(c in gap_chars):
if(not prev_is_gap):
prev_is_gap = True
else:
prev_is_gap = False
buf2 += c
if c == ";" or c == "\n":
lines.append(buf2)
buf2 = ""
def parse(self, dep_file):
from dep_file import DepRelation
if dep_file.is_parsed:
return
logging.info("Parsing %s" % dep_file.path)
buf = ""
# stage 1: strip comments
while ci > 0:
quotes = l[:ci].count('"') # ignore comments in strings
if quotes % 2 == 0:
l = l[:ci-1]
# stage 2: remove spaces, crs, lfs, strip strings (we don't need them)
string_literal = char_literal = False
prev_is_gap = False
gap_chars = " \r\n\t"
string_literal = not string_literal
char_literal = not char_literal
within_string = (string_literal or char_literal) and (c != '"') and (c != "'")
if(not within_string):
if(c in gap_chars):
if(not prev_is_gap):
prev_is_gap = True
else:
prev_is_gap = False
if ( (c == ";") or (buf2[-8:] == "generate") or (buf2[-5:] == "begin")) :
else:
prev_is_gap = False
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
def use() :
if ( g.group(1).lower() == "work" ) : #work is the current library in VHDL
logging.debug("use package %s.%s" % (dep_file.library, g.group(2)) )
dep_file.add_relation(DepRelation("%s.%s" % (dep_file.library, g.group(2)) , DepRelation.USE, DepRelation.PACKAGE))
else :
logging.debug("use package %s.%s" % (g.group(1), g.group(2)) )
dep_file.add_relation(DepRelation("%s.%s" % (g.group(1), g.group(2)), DepRelation.USE, DepRelation.PACKAGE))
def entity() :
logging.debug("found entity %s.%s" % ( dep_file.library, g.group(1) ) )
dep_file.add_relation(DepRelation("%s.%s" % (dep_file.library, g.group(1)),
DepRelation.PROVIDE,
DepRelation.ENTITY))
def package() :
logging.debug("found package %s.%s" % (dep_file.library, g.group(1) ))
dep_file.add_relation(DepRelation("%s.%s" % (dep_file.library, g.group(1)),
DepRelation.PROVIDE,
DepRelation.PACKAGE))
def arch_begin() :
arch_name = g.group(1)
within_architecture = True
def arch_end() :
within_architecture = False
def instance() :
for lib in libraries :
logging.debug("-> instantiates %s.%s as %s" % (lib, g.group(2), g.group(1)) )
dep_file.add_relation(DepRelation("%s.%s" % (lib, g.group(2)),
DepRelation.USE,
DepRelation.ENTITY))
def instance_from_library() :
if ( g.group(2).lower() == "work" ) : #work is the current library in VHDL
logging.debug("-> instantiates %s.%s as %s" % (dep_file.library, g.group(3), g.group(1)) )
dep_file.add_relation(DepRelation("%s.%s" % (dep_file.library, g.group(3)),
DepRelation.USE,
DepRelation.ENTITY))
else :
logging.debug("-> instantiates %s.%s as %s" % (g.group(2), g.group(3), g.group(1)) )
dep_file.add_relation(DepRelation("%s.%s" % (g.group(2), g.group(3)),
DepRelation.USE,
DepRelation.ENTITY))
def library() :
logging.debug("use library %s" % g.group(1) )
libraries.add(g.group(1))
use: "^ *use +(\w+) *\. *(\w+) *\. *\w+ *;",
entity: "^ *entity +(\w+) +is +(?:port|generic|end)",
package: "^ *package +(\w+) +is",
arch_begin: "^ *architecture +(\w+) +of +(\w+) +is +",
arch_end: "^ *end +(\w+) +;",
instance: "^ *(\w+) *\: *(\w+) *(?:port +map|generic +map| *;)",
instance_from_library: "^ *(\w+) *\: *entity *(\w+) *\. *(\w+) *(?:port +map|generic +map| *;)",
library: "^ *library *(\w+) *;"
compiled_patterns = map(lambda p: (p, re.compile(patterns[p])), patterns)
within_architecture = False
matches = filter(lambda (k, v): v is not None, map(lambda (k, v): (k, re.match(v, l.lower())), compiled_patterns))
if(not len(matches)):
continue