def strconst()

in assets/lambda_helper_neptune/python/rdflib/plugins/parsers/notation3.py [0:0]


    def strconst(self, argstr, i, delim):
        """parse an N3 string constant delimited by delim.
        return index, val
        """
        delim1 = delim[0]
        delim2, delim3, delim4, delim5 = delim1 * 2, delim1 * 3, delim1 * 4, delim1 * 5

        j = i
        ustr = ""    # Empty unicode string
        startline = self.lines  # Remember where for error messages
        while j < len(argstr):
            if argstr[j] == delim1:
                if delim == delim1:  # done when delim is " or '
                    i = j + 1
                    return i, ustr
                if delim == delim3:  # done when delim is """ or ''' and, respectively ...
                    if argstr[j:j + 5] == delim5:  # ... we have "" or '' before
                        i = j + 5
                        ustr = ustr + delim2
                        return i, ustr
                    if argstr[j:j + 4] == delim4:  # ... we have " or ' before
                        i = j + 4
                        ustr = ustr + delim1
                        return i, ustr
                    if argstr[j:j + 3] == delim3:  # current " or ' is part of delim
                        i = j + 3
                        return i, ustr

                     # we are inside of the string and current char is " or '
                    j = j + 1
                    ustr = ustr + delim1
                    continue

            m = interesting.search(argstr, j)   # was argstr[j:].
             # Note for pos param to work, MUST be compiled  ... re bug?
            assert m, "Quote expected in string at ^ in %s^%s" % (
                argstr[j - 20:j], argstr[j:j + 20])  # at least need a quote

            i = m.start()
            try:
                ustr = ustr + argstr[j:i]
            except UnicodeError:
                err = ""
                for c in argstr[j:i]:
                    err = err + (" %02x" % ord(c))
                streason = sys.exc_info()[1].__str__()
                raise BadSyntax(
                    self._thisDoc, startline, argstr, j,
                    "Unicode error appending characters" +
                    " %s to string, because\n\t%s"
                    % (err, streason))

             # print "@@@ i = ",i, " j=",j, "m.end=", m.end()

            ch = argstr[i]
            if ch == delim1:
                j = i
                continue
            elif ch in ('"', "'") and ch != delim1:
                ustr = ustr + ch
                j = i + 1
                continue
            elif ch in "\r\n":
                if delim == delim1:
                    raise BadSyntax(
                        self._thisDoc, startline, argstr, i,
                        "newline found in string literal")
                self.lines = self.lines + 1
                ustr = ustr + ch
                j = i + 1
                self.startOfLine = j

            elif ch == "\\":
                j = i + 1
                ch = argstr[j:j + 1]   # Will be empty if string ends
                if not ch:
                    raise BadSyntax(
                        self._thisDoc, startline, argstr, i,
                        "unterminated string literal (2)")
                k = 'abfrtvn\\"'.find(ch)
                if k >= 0:
                    uch = '\a\b\f\r\t\v\n\\"'[k]
                    ustr = ustr + uch
                    j = j + 1
                elif ch == "u":
                    j, ch = self.uEscape(argstr, j + 1, startline)
                    ustr = ustr + ch
                elif ch == "U":
                    j, ch = self.UEscape(argstr, j + 1, startline)
                    ustr = ustr + ch
                else:
                    self.BadSyntax(argstr, i,
                        "bad escape")

        self.BadSyntax(argstr, i,
                        "unterminated string literal")