Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!bcyclone02.am1.xlned.com!bcyclone02.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'pop': 0.05; 'definitions': 0.07; 'nested': 0.07; 'parser': 0.07; 'problem:': 0.07; 'strict': 0.07; 'string': 0.09; 'definition,': 0.09; 'lines.': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '\'"\'),': 0.16; "'\\n')": 0.16; 'definitions,': 0.16; 'delimiter': 0.16; 'empty,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hashes': 0.16; 'line).': 0.16; 'list)': 0.16; 'markers,': 0.16; 'nest': 0.16; 'nesting': 0.16; 'pairs': 0.16; 'stack;': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'stack': 0.19; '(the': 0.22; 'appears': 0.22; 'otherwise,': 0.22; 'cc:addr:python.org': 0.22; 'instance,': 0.24; 'text,': 0.24; 'cc:2**0': 0.24; 'possibly': 0.26; 'this:': 0.26; 'header:In- Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'tim': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'usually': 0.31; 'though.': 0.31; 'extend': 0.32; 'another': 0.32; 'quite': 0.32; 'not.': 0.33; 'comment': 0.34; 'problem': 0.35; 'something': 0.35; 'definition': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'building': 0.35; 'data,': 0.36; 'programming,': 0.36; 'step': 0.37; 'sometimes': 0.38; 'nov': 0.38; 'that,': 0.38; 'little': 0.38; 'quote': 0.39; 'according': 0.40; 'skip:u 10': 0.60; 'worry': 0.60; 'simple': 0.61; "you're": 0.61; 'such': 0.63; 'talking': 0.65; 'determine': 0.67; '26,': 0.68; '(any': 0.84; '(probably': 0.84; 'characters,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=PVyFjXvLJPfT9ccS3JRxZhi2IUuZiMhh6L1mnxHHEjo=; b=ozk8AxNZ1hB2lrtVwbV14f+OZyJAeAoRmuPmEtLMIb+fOHD9pWdwUmh8wHFY8GQWRN VwVLvRgyuJY/ZK9xi2CNdA2xHgEktzh9LE16hO3dyzc+vYTQYdTUJXmp5FFW0U6bXIgt nx3G/4yWzAg9yxn7J1DU8X9PAoLCOzYM8omCi8NdbhBHnWvTXbsUZPiroDRp3FSQkyoM cbfTfVfEdr9uIrhlBMhwQTWmEW+6ffMk901jMPt4p9VCKXADEGcA8L9+HgKkpORzkzhK 9XlH7HfH8TUF9x+nSj+UThb49Abhp2NGnnhbJ6IKq2jZeG5XRx2xcCENnV7/gyYzDR67 YYow== MIME-Version: 1.0 X-Received: by 10.50.171.193 with SMTP id aw1mr20687209igc.2.1416962699326; Tue, 25 Nov 2014 16:44:59 -0800 (PST) In-Reply-To: References: Date: Wed, 26 Nov 2014 11:44:59 +1100 Subject: Re: Quotation Ugliness From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1416962702 news.xs4all.nl 2941 [2001:888:2000:d::a6]:36200 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 5639 X-Received-Body-CRC: 891886849 Xref: csiph.com comp.lang.python:81531 On Wed, Nov 26, 2014 at 11:18 AM, Tim Daneliuk wrote: > A problem for your consideration: > > We are given a tuple of delimiter string pairs to quote or comment text, > possibly over multiple lines. Something like this: > > delims = (('"', '"'), ("'", "'"), ('#', '\n'), ("\*", "*\), ('\\', '\n') > ...) > > These may be nested. > > Here's the problem: Determine is the string S appears *outside* or *inside* > any such quotation. Be aware that, according to the usual definitions, some of those don't nest... and others may or may not. If you're talking about comment characters, for instance, /* /* */ */ sometimes nests and sometimes doesn't; but # # \n \n definitely doesn't nest (two hashes on a line doesn't extend the comment to another line). Also, they're usually slashes, not backslashes. But if your definitions are simple and strict (any of these may be nested and to any level) and correct (the tuple is exactly the pairs you're after), then your best bet would be to step through the data, building up a stack (probably in a Python list) of currently-open delimiters. As you find open-quote markers, you add to the stack; any time you find the top-of-stack closing delimiter, you pop it off. When you find the string S, if the stack is empty, it's outside; otherwise, it's inside. You may have issues with your definition of nesting, though. For instance, what's it mean if you have double-quotes, then a hash? In normal programming, the hash isn't significant. If that's your definition, then the only nesting you need worry about is /* and */, so your parser is quite simple: when you find any opener, you seek its corresponding closer, and then special-case /* to count any additional /* and look for a */ for each one */ . At the moment, the problem's a little underspecified. ChrisA