# -*- coding: utf-8 -*- 

# lp4all: literate programming embedded in source code as wiki comments
# Copyright (C) 2006 Jean-Marie Favreau, Frédéric Lehobey, David Mentré
#                    and Thomas Petazzoni
# 
# This program 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 2 of the License, or
# (at your option) any later version.
# 
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from singleton import Singleton

class Label:
    def __init__(self, file, linestart, lineend):
        self.file      = file
        self.linestart = linestart
        self.lineend   = lineend

    def __str__(self):
        return "%s:%d-%d" % (self.file, self.linestart, self.lineend)

# Dictionary of all labels, stored in the form of Label objects
class LabelDict(Singleton):
    def __init__(self):
        self.labels = {}

    def add(self, label, file, linestart, lineend):
        if self.labels.has_key(label):
            print "WARNING: Label '%s' at %s:%d-%d already defined at %s" \
                  % (label, file, linestart, lineend, self.labels[label])
            return
        self.labels[label] = Label(file, linestart, lineend)

    def find(self, label):
        return self.labels[label]

    def dump(self):
        for (label, labelObj) in self.labels.iteritems():
            print "%s => %s" % (label, labelObj)