Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_escapables = "tnrfbacdeghijklmopqsuvwxyz " + _bslash + "'" + '"'
_octDigits = "01234567"
_escapedChar = (Word(_bslash, _escapables, exact=2) |
Word(_bslash, _octDigits, min=2, max=4))
_sglQuote = Literal("'")
_dblQuote = Literal('"')
dblQuotedString = Combine(_dblQuote + ZeroOrMore(CharsNotIn('\\"\n\r') | _escapedChar | '""') + _dblQuote).streamline().setName("string enclosed in double quotes")
sglQuotedString = Combine(_sglQuote + ZeroOrMore(CharsNotIn("\\'\n\r") | _escapedChar | "''") + _sglQuote).streamline().setName("string enclosed in single quotes")
quotedString = (dblQuotedString | sglQuotedString).setName("quotedString using single or double quotes")
# it's easy to get these comment structures wrong - they're very common, so may as well make them available
cStyleComment = Combine(Literal("/*") +
ZeroOrMore(CharsNotIn("*") | ("*" + ~Literal("/"))) +
Literal("*/")).streamline().setName("cStyleComment enclosed in /* ... */")
htmlComment = Combine(Literal("") + Literal("-").leaveWhitespace())) +
Literal("-->")).streamline().setName("htmlComment enclosed in ")
restOfLine = Optional(CharsNotIn("\n\r"), default="").setName("rest of line up to \\n").leaveWhitespace()
dblSlashComment = "//" + restOfLine
cppStyleComment = FollowedBy("/") + (dblSlashComment | cStyleComment)
javaStyleComment = cppStyleComment
pythonStyleComment = "#" + restOfLine
_noncomma = "".join([c for c in printables if c != ","])
_commasepitem = Combine(OneOrMore(Word(_noncomma) +
Optional(Word(" \t") +
~Literal(",") + ~LineEnd()))).streamline().setName("commaItem")
commaSeparatedList = delimitedList(Optional(quotedString | _commasepitem, default="")).setName("commaSeparatedList")
if __name__ == "__main__":
def test(teststring):
def makeHTMLTags(tagStr):
"""Helper to construct opening and closing tag expressions for HTML, given a tag name"""
return _makeTags(tagStr, False)
def makeXMLTags(tagStr):
"""Helper to construct opening and closing tag expressions for XML, given a tag name"""
return _makeTags(tagStr, True)
alphas8bit = srange(r"[\0xc0-\0xd6\0xd8-\0xf6\0xf8-\0xfe]")
_escapables = "tnrfbacdeghijklmopqsuvwxyz " + _bslash + "'" + '"'
_octDigits = "01234567"
_escapedChar = (Word(_bslash, _escapables, exact=2) |
Word(_bslash, _octDigits, min=2, max=4))
_sglQuote = Literal("'")
_dblQuote = Literal('"')
dblQuotedString = Combine(_dblQuote + ZeroOrMore(CharsNotIn('\\"\n\r') | _escapedChar | '""') + _dblQuote).streamline().setName("string enclosed in double quotes")
sglQuotedString = Combine(_sglQuote + ZeroOrMore(CharsNotIn("\\'\n\r") | _escapedChar | "''") + _sglQuote).streamline().setName("string enclosed in single quotes")
quotedString = (dblQuotedString | sglQuotedString).setName("quotedString using single or double quotes")
# it's easy to get these comment structures wrong - they're very common, so may as well make them available
cStyleComment = Combine(Literal("/*") +
ZeroOrMore(CharsNotIn("*") | ("*" + ~Literal("/"))) +
Literal("*/")).streamline().setName("cStyleComment enclosed in /* ... */")
htmlComment = Combine(Literal("") + Literal("-").leaveWhitespace())) +
Literal("-->")).streamline().setName("htmlComment enclosed in ")
restOfLine = Optional(CharsNotIn("\n\r"), default="").setName("rest of line up to \\n").leaveWhitespace()
dblSlashComment = "//" + restOfLine
cppStyleComment = FollowedBy("/") + (dblSlashComment | cStyleComment)
javaStyleComment = cppStyleComment
return _makeTags(tagStr, True)
alphas8bit = srange(r"[\0xc0-\0xd6\0xd8-\0xf6\0xf8-\0xfe]")
_escapables = "tnrfbacdeghijklmopqsuvwxyz " + _bslash + "'" + '"'
_octDigits = "01234567"
_escapedChar = (Word(_bslash, _escapables, exact=2) |
Word(_bslash, _octDigits, min=2, max=4))
_sglQuote = Literal("'")
_dblQuote = Literal('"')
dblQuotedString = Combine(_dblQuote + ZeroOrMore(CharsNotIn('\\"\n\r') | _escapedChar | '""') + _dblQuote).streamline().setName("string enclosed in double quotes")
sglQuotedString = Combine(_sglQuote + ZeroOrMore(CharsNotIn("\\'\n\r") | _escapedChar | "''") + _sglQuote).streamline().setName("string enclosed in single quotes")
quotedString = (dblQuotedString | sglQuotedString).setName("quotedString using single or double quotes")
# it's easy to get these comment structures wrong - they're very common, so may as well make them available
cStyleComment = Combine(Literal("/*") +
ZeroOrMore(CharsNotIn("*") | ("*" + ~Literal("/"))) +
Literal("*/")).streamline().setName("cStyleComment enclosed in /* ... */")
htmlComment = Combine(Literal("") + Literal("-").leaveWhitespace())) +
Literal("-->")).streamline().setName("htmlComment enclosed in ")
restOfLine = Optional(CharsNotIn("\n\r"), default="").setName("rest of line up to \\n").leaveWhitespace()
dblSlashComment = "//" + restOfLine
cppStyleComment = FollowedBy("/") + (dblSlashComment | cStyleComment)
javaStyleComment = cppStyleComment
pythonStyleComment = "#" + restOfLine
_noncomma = "".join([c for c in printables if c != ","])
_commasepitem = Combine(OneOrMore(Word(_noncomma) +
Optional(Word(" \t") +
~Literal(",") + ~LineEnd()))).streamline().setName("commaItem")
commaSeparatedList = delimitedList(Optional(quotedString | _commasepitem, default="")).setName("commaSeparatedList")
_escapables = "tnrfbacdeghijklmopqsuvwxyz " + _bslash + "'" + '"'
_octDigits = "01234567"
_escapedChar = (Word(_bslash, _escapables, exact=2) |
Word(_bslash, _octDigits, min=2, max=4))
_sglQuote = Literal("'")
_dblQuote = Literal('"')
dblQuotedString = Combine(_dblQuote + ZeroOrMore(CharsNotIn('\\"\n\r') | _escapedChar | '""') + _dblQuote).streamline().setName("string enclosed in double quotes")
sglQuotedString = Combine(_sglQuote + ZeroOrMore(CharsNotIn("\\'\n\r") | _escapedChar | "''") + _sglQuote).streamline().setName("string enclosed in single quotes")
quotedString = (dblQuotedString | sglQuotedString).setName("quotedString using single or double quotes")
# it's easy to get these comment structures wrong - they're very common, so may as well make them available
cStyleComment = Combine(Literal("/*") +
ZeroOrMore(CharsNotIn("*") | ("*" + ~Literal("/"))) +
Literal("*/")).streamline().setName("cStyleComment enclosed in /* ... */")
htmlComment = Combine(Literal("") + Literal("-").leaveWhitespace())) +
Literal("-->")).streamline().setName("htmlComment enclosed in ")
restOfLine = Optional(CharsNotIn("\n\r"), default="").setName("rest of line up to \\n").leaveWhitespace()
dblSlashComment = "//" + restOfLine
cppStyleComment = FollowedBy("/") + (dblSlashComment | cStyleComment)
javaStyleComment = cppStyleComment
pythonStyleComment = "#" + restOfLine
_noncomma = "".join([c for c in printables if c != ","])
_commasepitem = Combine(OneOrMore(Word(_noncomma) +
Optional(Word(" \t") +
~Literal(",") + ~LineEnd()))).streamline().setName("commaItem")
commaSeparatedList = delimitedList(Optional(quotedString | _commasepitem, default="")).setName("commaSeparatedList")
if __name__ == "__main__":
def __rxor__(self, other):
"""Implementation of ^= operator"""
if isinstance(other, basestring):
other = Literal(other)
return other ^ self
def __and__(self, other):
"""Implementation of & operator - returns Each"""
if isinstance(other, basestring):
other = Literal(other)
return Each([self, other])
def __init__(self, expr, savelist=False):
super(ParseElementEnhance, self).__init__(savelist)
if isinstance(expr, basestring):
expr = Literal(expr)
self.expr = expr
self.strRepr = None
if expr is not None:
self.mayIndexError = expr.mayIndexError
self.skipWhitespace = expr.skipWhitespace
self.whiteChars = expr.whiteChars
def __ror__(self, other):
"""Implementation of |= operator"""
if isinstance(other, basestring):
other = Literal(other)
return other | self
def __add__(self, other):
"""Implementation of + operator - returns And"""
if isinstance(other, basestring):
other = Literal(other)
return And([self, other])
def __or__(self, other):
"""Implementation of | operator - returns MatchFirst"""
if isinstance(other, basestring):
other = Literal(other)
return MatchFirst([self, other])