summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2011-06-26 17:12:24 +0000
committerptmcg <ptmcg@9bf210a0-9d2d-494c-87cf-cfb32e7dff7b>2011-06-26 17:12:24 +0000
commit455bd8b0db19817dc70792602411c1ebeca63cd3 (patch)
tree1ef6b756ef7d3731c990e19986b9461795d65cff
parentbd971fed1889934c708879a826b47f13a8b95fab (diff)
downloadpyparsing_1.5.6@211.tar.gz
Add ungroup; add excludeChars arg to Word; package for 1.5.6 releasepyparsing_1.5.6@211
git-svn-id: svn://svn.code.sf.net/p/pyparsing/code/src@211 9bf210a0-9d2d-494c-87cf-cfb32e7dff7b
-rw-r--r--CHANGES21
-rw-r--r--HowToUsePyparsing.html571
-rw-r--r--HowToUsePyparsing.txt64
-rw-r--r--pyparsing.py53
-rw-r--r--pyparsing_py2.py53
-rw-r--r--pyparsing_py3.py44
6 files changed, 396 insertions, 410 deletions
diff --git a/CHANGES b/CHANGES
index d3f20a5..78573a2 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,7 +2,7 @@
Change Log
==========
-Version 1.5.6 - May, 2011
+Version 1.5.6 - June, 2011
----------------------------
- Cleanup of parse action normalizing code, to be more version-tolerant,
and robust in the face of future Python versions - much thanks to
@@ -16,6 +16,11 @@ Version 1.5.6 - May, 2011
expression would duplicate subsequent tokens - reported by Frankie
Ribery on stackoverflow, thanks!
+- Added 'ungroup' helper method, to address token grouping done
+ implicitly by And expressions, even if only one expression in the
+ And actually returns any text - also inspired by stackoverflow
+ discussion with Frankie Ribery!
+
- Fixed bug in srange, which accepted escaped hex characters of the
form '\0x##', but should be '\x##'. Both forms will be supported
for backwards compatibility.
@@ -29,10 +34,9 @@ Version 1.5.6 - May, 2011
yourself using it in a commercial purpose, please consider making a
charitable donation as described in the parser's header.
-- Added other new examples:
- . protobuf parser - parses Google's protobuf language
- . btpyparse - a BibTex parser contributed by Matthew Brett,
- with test suite test_bibparse.py (thanks, Matthew!)
+- Added the excludeChars argument to the Word class, to simplify defining
+ a word composed of all characters in a large range except for one or
+ two. Suggested by JesterEE on the pyparsing wiki.
- Added optional overlap parameter to scanString, to return overlapping
matches found in the source text.
@@ -52,6 +56,13 @@ Version 1.5.6 - May, 2011
- Fixed up internal list flattener to use iteration instead of recursion,
to avoid stack overflow when transforming large files.
+- Added other new examples:
+ . protobuf parser - parses Google's protobuf language
+ . btpyparse - a BibTex parser contributed by Matthew Brett,
+ with test suite test_bibparse.py (thanks, Matthew!)
+ . groupUsingListAllMatches.py - demo using trailing '*' for results
+ names
+
Version 1.5.5 - August, 2010
----------------------------
diff --git a/HowToUsePyparsing.html b/HowToUsePyparsing.html
index 3f31765..b071fcb 100644
--- a/HowToUsePyparsing.html
+++ b/HowToUsePyparsing.html
@@ -3,16 +3,16 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
+<meta name="generator" content="Docutils 0.8: http://docutils.sourceforge.net/" />
<title>Using the pyparsing module</title>
<meta name="author" content="Paul McGuire" />
-<meta name="date" content="January, 2010" />
-<meta name="copyright" content="Copyright © 2003-2010 Paul McGuire." />
+<meta name="date" content="June, 2011" />
+<meta name="copyright" content="Copyright © 2003-2011 Paul McGuire." />
<style type="text/css">
/*
:Author: David Goodger (goodger@python.org)
-:Id: $Id: html4css1.css 5196 2007-06-03 20:25:28Z wiemann $
+:Id: $Id: html4css1.css 6387 2010-08-13 12:23:41Z milde $
:Copyright: This stylesheet has been placed in the public domain.
Default cascading style sheet for the HTML output of Docutils.
@@ -50,6 +50,10 @@ blockquote.epigraph {
dl.docutils dd {
margin-bottom: 0.5em }
+object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
+ overflow: hidden;
+}
+
/* Uncomment (and remove this text!) to get bold-faced definition list terms
dl.docutils dt {
font-weight: bold }
@@ -160,11 +164,38 @@ h2.subtitle {
hr.docutils {
width: 75% }
-img.align-left {
- clear: left }
+img.align-left, .figure.align-left, object.align-left {
+ clear: left ;
+ float: left ;
+ margin-right: 1em }
-img.align-right {
- clear: right }
+img.align-right, .figure.align-right, object.align-right {
+ clear: right ;
+ float: right ;
+ margin-left: 1em }
+
+img.align-center, .figure.align-center, object.align-center {
+ display: block;
+ margin-left: auto;
+ margin-right: auto;
+}
+
+.align-left {
+ text-align: left }
+
+.align-center {
+ clear: both ;
+ text-align: center }
+
+.align-right {
+ text-align: right }
+
+/* reset inner alignment in figures */
+div.align-right {
+ text-align: left }
+
+/* div.align-center * { */
+/* text-align: left } */
ol.simple, ul.simple {
margin-bottom: 1em }
@@ -219,8 +250,7 @@ p.topic-title {
pre.address {
margin-bottom: 0 ;
margin-top: 0 ;
- font-family: serif ;
- font-size: 100% }
+ font: inherit }
pre.literal-block, pre.doctest-block {
margin-left: 2em ;
@@ -301,11 +331,11 @@ ul.auto-toc {
</pre>
</td></tr>
<tr><th class="docinfo-name">Revision:</th>
-<td>1.5.3</td></tr>
+<td>1.5.6</td></tr>
<tr><th class="docinfo-name">Date:</th>
-<td>January, 2010</td></tr>
+<td>June, 2011</td></tr>
<tr><th class="docinfo-name">Copyright:</th>
-<td>Copyright © 2003-2010 Paul McGuire.</td></tr>
+<td>Copyright © 2003-2011 Paul McGuire.</td></tr>
</tbody>
</table>
<table class="docutils field-list" frame="void" rules="none">
@@ -326,28 +356,25 @@ extracting data from formatted reports.</td>
<ul class="auto-toc simple">
<li><a class="reference internal" href="#steps-to-follow" id="id1">1&nbsp;&nbsp;&nbsp;Steps to follow</a><ul class="auto-toc">
<li><a class="reference internal" href="#hello-world" id="id2">1.1&nbsp;&nbsp;&nbsp;Hello, World!</a></li>
-<li><a class="reference internal" href="#new-features-in-1-5-2" id="id3">1.2&nbsp;&nbsp;&nbsp;New features in 1.5.2</a></li>
-<li><a class="reference internal" href="#new-features-in-1-5-1" id="id4">1.3&nbsp;&nbsp;&nbsp;New features in 1.5.1</a></li>
-<li><a class="reference internal" href="#new-features-in-1-5-0" id="id5">1.4&nbsp;&nbsp;&nbsp;New features in 1.5.0</a></li>
-<li><a class="reference internal" href="#usage-notes" id="id6">1.5&nbsp;&nbsp;&nbsp;Usage notes</a></li>
+<li><a class="reference internal" href="#usage-notes" id="id3">1.2&nbsp;&nbsp;&nbsp;Usage notes</a></li>
</ul>
</li>
-<li><a class="reference internal" href="#classes" id="id7">2&nbsp;&nbsp;&nbsp;Classes</a><ul class="auto-toc">
-<li><a class="reference internal" href="#classes-in-the-pyparsing-module" id="id8">2.1&nbsp;&nbsp;&nbsp;Classes in the pyparsing module</a></li>
-<li><a class="reference internal" href="#basic-parserelement-subclasses" id="id9">2.2&nbsp;&nbsp;&nbsp;Basic ParserElement subclasses</a></li>
-<li><a class="reference internal" href="#expression-subclasses" id="id10">2.3&nbsp;&nbsp;&nbsp;Expression subclasses</a></li>
-<li><a class="reference internal" href="#expression-operators" id="id11">2.4&nbsp;&nbsp;&nbsp;Expression operators</a></li>
-<li><a class="reference internal" href="#positional-subclasses" id="id12">2.5&nbsp;&nbsp;&nbsp;Positional subclasses</a></li>
-<li><a class="reference internal" href="#converter-subclasses" id="id13">2.6&nbsp;&nbsp;&nbsp;Converter subclasses</a></li>
-<li><a class="reference internal" href="#special-subclasses" id="id14">2.7&nbsp;&nbsp;&nbsp;Special subclasses</a></li>
-<li><a class="reference internal" href="#other-classes" id="id15">2.8&nbsp;&nbsp;&nbsp;Other classes</a></li>
-<li><a class="reference internal" href="#exception-classes-and-troubleshooting" id="id16">2.9&nbsp;&nbsp;&nbsp;Exception classes and Troubleshooting</a></li>
+<li><a class="reference internal" href="#classes" id="id4">2&nbsp;&nbsp;&nbsp;Classes</a><ul class="auto-toc">
+<li><a class="reference internal" href="#classes-in-the-pyparsing-module" id="id5">2.1&nbsp;&nbsp;&nbsp;Classes in the pyparsing module</a></li>
+<li><a class="reference internal" href="#basic-parserelement-subclasses" id="id6">2.2&nbsp;&nbsp;&nbsp;Basic ParserElement subclasses</a></li>
+<li><a class="reference internal" href="#expression-subclasses" id="id7">2.3&nbsp;&nbsp;&nbsp;Expression subclasses</a></li>
+<li><a class="reference internal" href="#expression-operators" id="id8">2.4&nbsp;&nbsp;&nbsp;Expression operators</a></li>
+<li><a class="reference internal" href="#positional-subclasses" id="id9">2.5&nbsp;&nbsp;&nbsp;Positional subclasses</a></li>
+<li><a class="reference internal" href="#converter-subclasses" id="id10">2.6&nbsp;&nbsp;&nbsp;Converter subclasses</a></li>
+<li><a class="reference internal" href="#special-subclasses" id="id11">2.7&nbsp;&nbsp;&nbsp;Special subclasses</a></li>
+<li><a class="reference internal" href="#other-classes" id="id12">2.8&nbsp;&nbsp;&nbsp;Other classes</a></li>
+<li><a class="reference internal" href="#exception-classes-and-troubleshooting" id="id13">2.9&nbsp;&nbsp;&nbsp;Exception classes and Troubleshooting</a></li>
</ul>
</li>
-<li><a class="reference internal" href="#miscellaneous-attributes-and-methods" id="id17">3&nbsp;&nbsp;&nbsp;Miscellaneous attributes and methods</a><ul class="auto-toc">
-<li><a class="reference internal" href="#helper-methods" id="id18">3.1&nbsp;&nbsp;&nbsp;Helper methods</a></li>
-<li><a class="reference internal" href="#helper-parse-actions" id="id19">3.2&nbsp;&nbsp;&nbsp;Helper parse actions</a></li>
-<li><a class="reference internal" href="#common-string-and-token-constants" id="id20">3.3&nbsp;&nbsp;&nbsp;Common string and token constants</a></li>
+<li><a class="reference internal" href="#miscellaneous-attributes-and-methods" id="id14">3&nbsp;&nbsp;&nbsp;Miscellaneous attributes and methods</a><ul class="auto-toc">
+<li><a class="reference internal" href="#helper-methods" id="id15">3.1&nbsp;&nbsp;&nbsp;Helper methods</a></li>
+<li><a class="reference internal" href="#helper-parse-actions" id="id16">3.2&nbsp;&nbsp;&nbsp;Helper parse actions</a></li>
+<li><a class="reference internal" href="#common-string-and-token-constants" id="id17">3.3&nbsp;&nbsp;&nbsp;Common string and token constants</a></li>
</ul>
</li>
</ul>
@@ -359,7 +386,7 @@ extracting data from formatted reports.</td>
<li>First define the tokens and patterns to be matched, and assign
this to a program variable. Optional results names or parsing
actions can also be defined at this time.</li>
-<li>Call <tt class="docutils literal"><span class="pre">parseString()</span></tt> or <tt class="docutils literal"><span class="pre">scanString()</span></tt> on this variable, passing in
+<li>Call <tt class="docutils literal">parseString()</tt> or <tt class="docutils literal">scanString()</tt> on this variable, passing in
the string to
be parsed. During the matching process, whitespace between
tokens is skipped by default (although this can be changed).
@@ -368,7 +395,7 @@ called.</li>
<li>Process the parsed results, returned as a list of strings.
Matching results may also be accessed as named attributes of
the returned results, if names are defined in the definition of
-the token pattern, using <tt class="docutils literal"><span class="pre">setResultsName()</span></tt>.</li>
+the token pattern, using <tt class="docutils literal">setResultsName()</tt>.</li>
</ol>
<div class="section" id="hello-world">
<h2><a class="toc-backref" href="#id2">1.1&nbsp;&nbsp;&nbsp;Hello, World!</a></h2>
@@ -386,49 +413,8 @@ print greeting
['Hello', ',', 'World', '!']
</pre>
</div>
-<div class="section" id="new-features-in-1-5-2">
-<h2><a class="toc-backref" href="#id3">1.2&nbsp;&nbsp;&nbsp;New features in 1.5.2</a></h2>
-<p>There are no new pyparsing features in this release, it is primarily a bug-fixing release.
-This release does include changes to support running pyparsing under IronPython 2.0.1, and
-a pyparsing_py3 module for use with Python 3.</p>
-</div>
-<div class="section" id="new-features-in-1-5-1">
-<h2><a class="toc-backref" href="#id4">1.3&nbsp;&nbsp;&nbsp;New features in 1.5.1</a></h2>
-<p>Some of the significant features added in version 1.5.0 are:</p>
-<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">originalTextFor</span></tt> helper method, to simplify grammar expressions that need to preserve
-the original input text; should be used in place of the <tt class="docutils literal"><span class="pre">keepOriginalText</span></tt> parse action:</p>
-<pre class="literal-block">
-fullName = Word(alphas) + Word(alphas)
-fullName.setParseAction(keepOriginalText)
-</pre>
-<p>should now be written:</p>
-<pre class="literal-block">
-fullName = originalTextFor(Word(alphas) + Word(alphas))
-</pre>
-</li>
-<li><p class="first">added parameter <tt class="docutils literal"><span class="pre">parseAll</span></tt> to <tt class="docutils literal"><span class="pre">ParserElement.parseFile</span></tt> to
-match the arguments for <tt class="docutils literal"><span class="pre">ParserElement.parseString</span></tt></p>
-</li>
-<li><p class="first">new argument <tt class="docutils literal"><span class="pre">failOn</span></tt> for SkipTo expressions, to define literal strings or expressions that
-should not be included in the skipped text</p>
-</li>
-</ul>
-</div>
-<div class="section" id="new-features-in-1-5-0">
-<h2><a class="toc-backref" href="#id5">1.4&nbsp;&nbsp;&nbsp;New features in 1.5.0</a></h2>
-<p>Some of the significant features added in version 1.5.0 are:</p>
-<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">indentedBlock</span></tt> helper method to define grammars using block indentation for grouping (like Python)</li>
-<li>new parameter <tt class="docutils literal"><span class="pre">parseAll</span></tt> in <tt class="docutils literal"><span class="pre">ParserElement.parseString</span></tt></li>
-<li>operator '-' for combining ParserElements; similar to the '+' operator, but raises an immediate
-<tt class="docutils literal"><span class="pre">ParseSyntaxException</span></tt> if an expression after the '-' operator fails to match; using '-' can
-provide error messages that are more useful for application users to find syntax errors in their
-input text</li>
-</ul>
-</div>
<div class="section" id="usage-notes">
-<h2><a class="toc-backref" href="#id6">1.5&nbsp;&nbsp;&nbsp;Usage notes</a></h2>
+<h2><a class="toc-backref" href="#id3">1.2&nbsp;&nbsp;&nbsp;Usage notes</a></h2>
<ul>
<li><p class="first">The pyparsing module can be used to interpret simple command
strings or algebraic expressions, or can be used to extract data
@@ -437,8 +423,8 @@ or report scraping&quot;). However, it is possible that your defined
matching patterns may accept invalid inputs. Use pyparsing to
extract data from strings assumed to be well-formatted.</p>
</li>
-<li><p class="first">To keep up the readability of your code, use <a class="reference internal" href="#operators">operators</a> such as <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">|</span></tt>,
-<tt class="docutils literal"><span class="pre">^</span></tt>, and <tt class="docutils literal"><span class="pre">~</span></tt> to combine expressions. You can also combine
+<li><p class="first">To keep up the readability of your code, use <a class="reference internal" href="#operators">operators</a> such as <tt class="docutils literal">+</tt>, <tt class="docutils literal">|</tt>,
+<tt class="docutils literal">^</tt>, and <tt class="docutils literal">~</tt> to combine expressions. You can also combine
string literals with ParseExpressions - they will be
automatically converted to Literal objects. For example:</p>
<pre class="literal-block">
@@ -447,13 +433,13 @@ variable = Word( alphas, max=1 ) # single letter variable, such as x, z, m, et
arithOp = Word( &quot;+-*/&quot;, max=1 ) # arithmetic operators
equation = variable + &quot;=&quot; + integer + arithOp + integer # will match &quot;x=2+2&quot;, etc.
</pre>
-<p>In the definition of <tt class="docutils literal"><span class="pre">equation</span></tt>, the string <tt class="docutils literal"><span class="pre">&quot;=&quot;</span></tt> will get added as
+<p>In the definition of <tt class="docutils literal">equation</tt>, the string <tt class="docutils literal">&quot;=&quot;</tt> will get added as
a <tt class="docutils literal"><span class="pre">Literal(&quot;=&quot;)</span></tt>, but in a more readable way.</p>
</li>
<li><p class="first">The pyparsing module's default behavior is to ignore whitespace. This is the
case for 99% of all parsers ever written. This allows you to write simple, clean,
-grammars, such as the above <tt class="docutils literal"><span class="pre">equation</span></tt>, without having to clutter it up with
-extraneous <tt class="docutils literal"><span class="pre">ws</span></tt> markers. The <tt class="docutils literal"><span class="pre">equation</span></tt> grammar will successfully parse all of the
+grammars, such as the above <tt class="docutils literal">equation</tt>, without having to clutter it up with
+extraneous <tt class="docutils literal">ws</tt> markers. The <tt class="docutils literal">equation</tt> grammar will successfully parse all of the
following statements:</p>
<pre class="literal-block">
x=2+2
@@ -463,22 +449,22 @@ r= 1234/ 100000
</pre>
<p>Of course, it is quite simple to extend this example to support more elaborate expressions, with
nesting with parentheses, floating point numbers, scientific notation, and named constants
-(such as <tt class="docutils literal"><span class="pre">e</span></tt> or <tt class="docutils literal"><span class="pre">pi</span></tt>). See <tt class="docutils literal"><span class="pre">fourFn.py</span></tt>, included in the examples directory.</p>
+(such as <tt class="docutils literal">e</tt> or <tt class="docutils literal">pi</tt>). See <tt class="docutils literal">fourFn.py</tt>, included in the examples directory.</p>
</li>
<li><p class="first">To modify pyparsing's default whitespace skipping, you can use one or
more of the following methods:</p>
<ul>
-<li><p class="first">use the static method <tt class="docutils literal"><span class="pre">ParserElement.setDefaultWhitespaceChars</span></tt>
+<li><p class="first">use the static method <tt class="docutils literal">ParserElement.setDefaultWhitespaceChars</tt>
to override the normal set of whitespace chars (' tn'). For instance
when defining a grammar in which newlines are significant, you should
-call <tt class="docutils literal"><span class="pre">ParserElement.setDefaultWhitespaceChars('</span> <span class="pre">\t')</span></tt> to remove
+call <tt class="docutils literal">ParserElement.setDefaultWhitespaceChars(' \t')</tt> to remove
newline from the set of skippable whitespace characters. Calling
this method will affect all pyparsing expressions defined afterward.</p>
</li>
-<li><p class="first">call <tt class="docutils literal"><span class="pre">leaveWhitespace()</span></tt> on individual expressions, to suppress the
+<li><p class="first">call <tt class="docutils literal">leaveWhitespace()</tt> on individual expressions, to suppress the
skipping of whitespace before trying to match the expression</p>
</li>
-<li><p class="first">use <tt class="docutils literal"><span class="pre">Combine</span></tt> to require that successive expressions must be
+<li><p class="first">use <tt class="docutils literal">Combine</tt> to require that successive expressions must be
adjacent in the input string. For instance, this expression:</p>
<pre class="literal-block">
real = Word(nums) + '.' + Word(nums)
@@ -502,41 +488,41 @@ two integers, or None and an integer, representing min and max repetitions
second tuple element). See the following examples, where n is used to
indicate an integer value:</p>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">expr*3</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">expr</span> <span class="pre">+</span> <span class="pre">expr</span> <span class="pre">+</span> <span class="pre">expr</span></tt></li>
-<li><tt class="docutils literal"><span class="pre">expr*(2,3)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">expr</span> <span class="pre">+</span> <span class="pre">expr</span> <span class="pre">+</span> <span class="pre">Optional(expr)</span></tt></li>
+<li><tt class="docutils literal">expr*3</tt> is equivalent to <tt class="docutils literal">expr + expr + expr</tt></li>
+<li><tt class="docutils literal"><span class="pre">expr*(2,3)</span></tt> is equivalent to <tt class="docutils literal">expr + expr + Optional(expr)</tt></li>
<li><tt class="docutils literal"><span class="pre">expr*(n,None)</span></tt> or <tt class="docutils literal"><span class="pre">expr*(n,)</span></tt> is equivalent
-to <tt class="docutils literal"><span class="pre">expr*n</span> <span class="pre">+</span> <span class="pre">ZeroOrMore(expr)</span></tt> (read as &quot;at least n instances of expr&quot;)</li>
+to <tt class="docutils literal">expr*n + ZeroOrMore(expr)</tt> (read as &quot;at least n instances of expr&quot;)</li>
<li><tt class="docutils literal"><span class="pre">expr*(None,n)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">expr*(0,n)</span></tt>
(read as &quot;0 to n instances of expr&quot;)</li>
-<li><tt class="docutils literal"><span class="pre">expr*(None,None)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">ZeroOrMore(expr)</span></tt></li>
-<li><tt class="docutils literal"><span class="pre">expr*(1,None)</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">OneOrMore(expr)</span></tt></li>
+<li><tt class="docutils literal"><span class="pre">expr*(None,None)</span></tt> is equivalent to <tt class="docutils literal">ZeroOrMore(expr)</tt></li>
+<li><tt class="docutils literal"><span class="pre">expr*(1,None)</span></tt> is equivalent to <tt class="docutils literal">OneOrMore(expr)</tt></li>
</ul>
<p>Note that <tt class="docutils literal"><span class="pre">expr*(None,n)</span></tt> does not raise an exception if
more than n exprs exist in the input stream; that is,
<tt class="docutils literal"><span class="pre">expr*(None,n)</span></tt> does not enforce a maximum number of expr
occurrences. If this behavior is desired, then write
-<tt class="docutils literal"><span class="pre">expr*(None,n)</span> <span class="pre">+</span> <span class="pre">~expr</span></tt>.</p>
+<tt class="docutils literal"><span class="pre">expr*(None,n)</span> + ~expr</tt>.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">MatchFirst</span></tt> expressions are matched left-to-right, and the first
+<li><p class="first"><tt class="docutils literal">MatchFirst</tt> expressions are matched left-to-right, and the first
match found will skip all later expressions within, so be sure
to define less-specific patterns after more-specific patterns.
If you are not sure which expressions are most specific, use Or
-expressions (defined using the <tt class="docutils literal"><span class="pre">^</span></tt> operator) - they will always
+expressions (defined using the <tt class="docutils literal">^</tt> operator) - they will always
match the longest expression, although they are more
compute-intensive.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">Or</span></tt> expressions will evaluate all of the specified subexpressions
+<li><p class="first"><tt class="docutils literal">Or</tt> expressions will evaluate all of the specified subexpressions
to determine which is the &quot;best&quot; match, that is, which matches
the longest string in the input data. In case of a tie, the
-left-most expression in the <tt class="docutils literal"><span class="pre">Or</span></tt> list will win.</p>
+left-most expression in the <tt class="docutils literal">Or</tt> list will win.</p>
</li>
<li><p class="first">If parsing the contents of an entire file, pass it to the
-<tt class="docutils literal"><span class="pre">parseFile</span></tt> method using:</p>
+<tt class="docutils literal">parseFile</tt> method using:</p>
<pre class="literal-block">
expr.parseFile( sourceFile )
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">ParseExceptions</span></tt> will report the location where an expected token
+<li><p class="first"><tt class="docutils literal">ParseExceptions</tt> will report the location where an expected token
or expression failed to match. For example, if we tried to use our
&quot;Hello, World!&quot; parser to parse &quot;Hello World!&quot; (leaving out the separating
comma), we would get an exception, with the message:</p>
@@ -547,15 +533,15 @@ pyparsing.ParseException: Expected &quot;,&quot; (6), (1,7)
expressions, the reported location may not be exactly where you
would expect. See more information under <a class="reference internal" href="#parseexception">ParseException</a> .</p>
</li>
-<li><p class="first">Use the <tt class="docutils literal"><span class="pre">Group</span></tt> class to enclose logical groups of tokens within a
+<li><p class="first">Use the <tt class="docutils literal">Group</tt> class to enclose logical groups of tokens within a
sublist. This will help organize your results into more
hierarchical form (the default behavior is to return matching
tokens as a flat list of matching input strings).</p>
</li>
<li><p class="first">Punctuation may be significant for matching, but is rarely of
-much interest in the parsed results. Use the <tt class="docutils literal"><span class="pre">suppress()</span></tt> method
+much interest in the parsed results. Use the <tt class="docutils literal">suppress()</tt> method
to keep these tokens from cluttering up your returned lists of
-tokens. For example, <tt class="docutils literal"><span class="pre">delimitedList()</span></tt> matches a succession of
+tokens. For example, <tt class="docutils literal">delimitedList()</tt> matches a succession of
one or more expressions, separated by delimiters (commas by
default), but only returns a list of the actual expressions -
the delimiters are used for parsing, but are suppressed from the
@@ -568,7 +554,7 @@ other data types (ints, floats, booleans, etc.).</p>
expressions. It is much easier to access a token using its field
name than using a positional index, especially if the expression
contains optional elements. You can also shortcut
-the <tt class="docutils literal"><span class="pre">setResultsName</span></tt> call:</p>
+the <tt class="docutils literal">setResultsName</tt> call:</p>
<pre class="literal-block">
stats = &quot;AVE:&quot; + realNum.setResultsName(&quot;average&quot;) + \
&quot;MIN:&quot; + realNum.setResultsName(&quot;min&quot;) + \
@@ -582,9 +568,9 @@ stats = &quot;AVE:&quot; + realNum(&quot;average&quot;) + \
</pre>
</li>
<li><p class="first">Be careful when defining parse actions that modify global variables or
-data structures (as in <tt class="docutils literal"><span class="pre">fourFn.py</span></tt>), especially for low level tokens
-or expressions that may occur within an <tt class="docutils literal"><span class="pre">And</span></tt> expression; an early element
-of an <tt class="docutils literal"><span class="pre">And</span></tt> may match, but the overall expression may fail.</p>
+data structures (as in <tt class="docutils literal">fourFn.py</tt>), especially for low level tokens
+or expressions that may occur within an <tt class="docutils literal">And</tt> expression; an early element
+of an <tt class="docutils literal">And</tt> may match, but the overall expression may fail.</p>
</li>
<li><p class="first">Performance of pyparsing may be slow for complex grammars and/or large
input strings. The <a class="reference external" href="http://psyco.sourceforge.net/">psyco</a> package can be used to improve the speed of the
@@ -595,24 +581,24 @@ improvments have been in the 20-50% range.</p>
</div>
</div>
<div class="section" id="classes">
-<h1><a class="toc-backref" href="#id7">2&nbsp;&nbsp;&nbsp;Classes</a></h1>
+<h1><a class="toc-backref" href="#id4">2&nbsp;&nbsp;&nbsp;Classes</a></h1>
<div class="section" id="classes-in-the-pyparsing-module">
-<h2><a class="toc-backref" href="#id8">2.1&nbsp;&nbsp;&nbsp;Classes in the pyparsing module</a></h2>
-<p><tt class="docutils literal"><span class="pre">ParserElement</span></tt> - abstract base class for all pyparsing classes;
+<h2><a class="toc-backref" href="#id5">2.1&nbsp;&nbsp;&nbsp;Classes in the pyparsing module</a></h2>
+<p><tt class="docutils literal">ParserElement</tt> - abstract base class for all pyparsing classes;
methods for code to use are:</p>
<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">parseString(</span> <span class="pre">sourceString,</span> <span class="pre">parseAll=False</span> <span class="pre">)</span></tt> - only called once, on the overall
+<li><p class="first"><tt class="docutils literal">parseString( sourceString, parseAll=False )</tt> - only called once, on the overall
matching pattern; returns a <a class="reference internal" href="#parseresults">ParseResults</a> object that makes the
matched tokens available as a list, and optionally as a dictionary,
or as an object with named attributes; if parseAll is set to True, then
parseString will raise a ParseException if the grammar does not process
the complete input string.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">parseFile(</span> <span class="pre">sourceFile</span> <span class="pre">)</span></tt> - a convenience function, that accepts an
+<li><p class="first"><tt class="docutils literal">parseFile( sourceFile )</tt> - a convenience function, that accepts an
input file object or filename. The file contents are passed as a
-string to <tt class="docutils literal"><span class="pre">parseString()</span></tt>. <tt class="docutils literal"><span class="pre">parseFile</span></tt> also supports the <tt class="docutils literal"><span class="pre">parseAll</span></tt> argument.</p>
+string to <tt class="docutils literal">parseString()</tt>. <tt class="docutils literal">parseFile</tt> also supports the <tt class="docutils literal">parseAll</tt> argument.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">scanString(</span> <span class="pre">sourceString</span> <span class="pre">)</span></tt> - generator function, used to find and
+<li><p class="first"><tt class="docutils literal">scanString( sourceString )</tt> - generator function, used to find and
extract matching text in the given source string; for each matched text,
returns a tuple of:</p>
<ul class="simple">
@@ -620,95 +606,98 @@ returns a tuple of:</p>
<li>start location of the matched text in the given source string</li>
<li>end location in the given source string</li>
</ul>
-<p><tt class="docutils literal"><span class="pre">scanString</span></tt> allows you to scan through the input source string for
+<p><tt class="docutils literal">scanString</tt> allows you to scan through the input source string for
random matches, instead of exhaustively defining the grammar for the entire
-source text (as would be required with <tt class="docutils literal"><span class="pre">parseString</span></tt>).</p>
+source text (as would be required with <tt class="docutils literal">parseString</tt>).</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">transformString(</span> <span class="pre">sourceString</span> <span class="pre">)</span></tt> - convenience wrapper function for
-<tt class="docutils literal"><span class="pre">scanString</span></tt>, to process the input source string, and replace matching
+<li><p class="first"><tt class="docutils literal">transformString( sourceString )</tt> - convenience wrapper function for
+<tt class="docutils literal">scanString</tt>, to process the input source string, and replace matching
text with the tokens returned from parse actions defined in the grammar
(see <a class="reference internal" href="#setparseaction">setParseAction</a>).</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">searchString(</span> <span class="pre">sourceString</span> <span class="pre">)</span></tt> - another convenience wrapper function for
-<tt class="docutils literal"><span class="pre">scanString</span></tt>, returns a list of the matching tokens returned from each
-call to <tt class="docutils literal"><span class="pre">scanString</span></tt>.</p>
+<li><p class="first"><tt class="docutils literal">searchString( sourceString )</tt> - another convenience wrapper function for
+<tt class="docutils literal">scanString</tt>, returns a list of the matching tokens returned from each
+call to <tt class="docutils literal">scanString</tt>.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setName(</span> <span class="pre">name</span> <span class="pre">)</span></tt> - associate a short descriptive name for this
+<li><p class="first"><tt class="docutils literal">setName( name )</tt> - associate a short descriptive name for this
element, useful in displaying exceptions and trace information</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setResultsName(</span> <span class="pre">string,</span> <span class="pre">listAllMatches=False</span> <span class="pre">)</span></tt> - name to be given
+<li><p class="first"><tt class="docutils literal">setResultsName( string, listAllMatches=False )</tt> - name to be given
to tokens matching
the element; if multiple tokens within
-a repetition group (such as <tt class="docutils literal"><span class="pre">ZeroOrMore</span></tt> or <tt class="docutils literal"><span class="pre">delimitedList</span></tt>) the
+a repetition group (such as <tt class="docutils literal">ZeroOrMore</tt> or <tt class="docutils literal">delimitedList</tt>) the
default is to return only the last matching token - if listAllMatches
-is set to True, then a list of matching tokens is returned. Note:
-<tt class="docutils literal"><span class="pre">setResultsName</span></tt> returns a <em>copy</em> of the element so that a single
+is set to True, then a list of all the matching tokens is returned.
+(New in 1.5.6 - a results name with a trailing '*' character will be
+interpreted as setting listAllMatches to True.)
+Note:
+<tt class="docutils literal">setResultsName</tt> returns a <em>copy</em> of the element so that a single
basic element can be referenced multiple times and given
different names within a complex grammar.</p>
</li>
</ul>
<ul id="setparseaction">
-<li><p class="first"><tt class="docutils literal"><span class="pre">setParseAction(</span> <span class="pre">*fn</span> <span class="pre">)</span></tt> - specify one or more functions to call after successful
-matching of the element; each function is defined as <tt class="docutils literal"><span class="pre">fn(</span> <span class="pre">s,</span>
-<span class="pre">loc,</span> <span class="pre">toks</span> <span class="pre">)</span></tt>, where:</p>
+<li><p class="first"><tt class="docutils literal">setParseAction( *fn )</tt> - specify one or more functions to call after successful
+matching of the element; each function is defined as <tt class="docutils literal">fn( s,
+loc, toks )</tt>, where:</p>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">s</span></tt> is the original parse string</li>
-<li><tt class="docutils literal"><span class="pre">loc</span></tt> is the location in the string where matching started</li>
-<li><tt class="docutils literal"><span class="pre">toks</span></tt> is the list of the matched tokens, packaged as a <a class="reference internal" href="#parseresults">ParseResults</a> object</li>
+<li><tt class="docutils literal">s</tt> is the original parse string</li>
+<li><tt class="docutils literal">loc</tt> is the location in the string where matching started</li>
+<li><tt class="docutils literal">toks</tt> is the list of the matched tokens, packaged as a <a class="reference internal" href="#parseresults">ParseResults</a> object</li>
</ul>
<p>Multiple functions can be attached to a ParserElement by specifying multiple
arguments to setParseAction, or by calling setParseAction multiple times.</p>
-<p>Each parse action function can return a modified <tt class="docutils literal"><span class="pre">toks</span></tt> list, to perform conversion, or
-string modifications. For brevity, <tt class="docutils literal"><span class="pre">fn</span></tt> may also be a
+<p>Each parse action function can return a modified <tt class="docutils literal">toks</tt> list, to perform conversion, or
+string modifications. For brevity, <tt class="docutils literal">fn</tt> may also be a
lambda - here is an example of using a parse action to convert matched
integer tokens from strings to integers:</p>
<pre class="literal-block">
intNumber = Word(nums).setParseAction( lambda s,l,t: [ int(t[0]) ] )
</pre>
-<p>If <tt class="docutils literal"><span class="pre">fn</span></tt> does not modify the <tt class="docutils literal"><span class="pre">toks</span></tt> list, it does not need to return
+<p>If <tt class="docutils literal">fn</tt> does not modify the <tt class="docutils literal">toks</tt> list, it does not need to return
anything at all.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setBreak(</span> <span class="pre">breakFlag=True</span> <span class="pre">)</span></tt> - if breakFlag is True, calls pdb.set_break()
+<li><p class="first"><tt class="docutils literal">setBreak( breakFlag=True )</tt> - if breakFlag is True, calls pdb.set_break()
as this expression is about to be parsed</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">copy()</span></tt> - returns a copy of a ParserElement; can be used to use the same
+<li><p class="first"><tt class="docutils literal">copy()</tt> - returns a copy of a ParserElement; can be used to use the same
parse expression in different places in a grammar, with different parse actions
attached to each</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">leaveWhitespace()</span></tt> - change default behavior of skipping
+<li><p class="first"><tt class="docutils literal">leaveWhitespace()</tt> - change default behavior of skipping
whitespace before starting matching (mostly used internally to the
pyparsing module, rarely used by client code)</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setWhitespaceChars(</span> <span class="pre">chars</span> <span class="pre">)</span></tt> - define the set of chars to be ignored
+<li><p class="first"><tt class="docutils literal">setWhitespaceChars( chars )</tt> - define the set of chars to be ignored
as whitespace before trying to match a specific ParserElement, in place of the
default set of whitespace (space, tab, newline, and return)</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setDefaultWhitespaceChars(</span> <span class="pre">chars</span> <span class="pre">)</span></tt> - class-level method to override
+<li><p class="first"><tt class="docutils literal">setDefaultWhitespaceChars( chars )</tt> - class-level method to override
the default set of whitespace chars for all subsequently created ParserElements
(including copies); useful when defining grammars that treat one or more of the
default whitespace characters as significant (such as a line-sensitive grammar, to
omit newline from the list of ignorable whitespace)</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">suppress()</span></tt> - convenience function to suppress the output of the
+<li><p class="first"><tt class="docutils literal">suppress()</tt> - convenience function to suppress the output of the
given element, instead of wrapping it with a Suppress object.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">ignore(</span> <span class="pre">expr</span> <span class="pre">)</span></tt> - function to specify parse expression to be
+<li><p class="first"><tt class="docutils literal">ignore( expr )</tt> - function to specify parse expression to be
ignored while matching defined patterns; can be called
repeatedly to specify multiple expressions; useful to specify
patterns of comment syntax, for example</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">setDebug(</span> <span class="pre">dbgFlag=True</span> <span class="pre">)</span></tt> - function to enable/disable tracing output
+<li><p class="first"><tt class="docutils literal">setDebug( dbgFlag=True )</tt> - function to enable/disable tracing output
when trying to match this element</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">validate()</span></tt> - function to verify that the defined grammar does not
+<li><p class="first"><tt class="docutils literal">validate()</tt> - function to verify that the defined grammar does not
contain infinitely recursive constructs</p>
</li>
</ul>
<ul class="simple" id="parsewithtabs">
-<li><tt class="docutils literal"><span class="pre">parseWithTabs()</span></tt> - function to override default behavior of converting
+<li><tt class="docutils literal">parseWithTabs()</tt> - function to override default behavior of converting
tabs to spaces before parsing the input string; rarely used, except when
specifying whitespace-significant grammars using the <a class="reference internal" href="#white">White</a> class.</li>
-<li><tt class="docutils literal"><span class="pre">enablePackrat()</span></tt> - a class-level static method to enable a memoizing
+<li><tt class="docutils literal">enablePackrat()</tt> - a class-level static method to enable a memoizing
performance enhancement, known as &quot;packrat parsing&quot;. packrat parsing is
disabled by default, since it may conflict with some user programs that use
parse actions. To activate the packrat feature, your
@@ -720,29 +709,29 @@ after importing pyparsing.</li>
</ul>
</div>
<div class="section" id="basic-parserelement-subclasses">
-<h2><a class="toc-backref" href="#id9">2.2&nbsp;&nbsp;&nbsp;Basic ParserElement subclasses</a></h2>
+<h2><a class="toc-backref" href="#id6">2.2&nbsp;&nbsp;&nbsp;Basic ParserElement subclasses</a></h2>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">Literal</span></tt> - construct with a string to be matched exactly</li>
-<li><tt class="docutils literal"><span class="pre">CaselessLiteral</span></tt> - construct with a string to be matched, but
+<li><tt class="docutils literal">Literal</tt> - construct with a string to be matched exactly</li>
+<li><tt class="docutils literal">CaselessLiteral</tt> - construct with a string to be matched, but
without case checking; results are always returned as the
defining literal, NOT as they are found in the input string</li>
-<li><tt class="docutils literal"><span class="pre">Keyword</span></tt> - similar to Literal, but must be immediately followed by
+<li><tt class="docutils literal">Keyword</tt> - similar to Literal, but must be immediately followed by
whitespace, punctuation, or other non-keyword characters; prevents
accidental matching of a non-keyword that happens to begin with a
defined keyword</li>
-<li><tt class="docutils literal"><span class="pre">CaselessKeyword</span></tt> - similar to Keyword, but with caseless matching
+<li><tt class="docutils literal">CaselessKeyword</tt> - similar to Keyword, but with caseless matching
behavior</li>
</ul>
<ul id="word">
-<li><p class="first"><tt class="docutils literal"><span class="pre">Word</span></tt> - one or more contiguous characters; construct with a
+<li><p class="first"><tt class="docutils literal">Word</tt> - one or more contiguous characters; construct with a
string containing the set of allowed initial characters, and an
optional second string of allowed body characters; for instance,
a common Word construct is to match a code identifier - in C, a
valid identifier must start with an alphabetic character or an
underscore ('_'), followed by a body that can also include numeric
-digits. That is, <tt class="docutils literal"><span class="pre">a</span></tt>, <tt class="docutils literal"><span class="pre">i</span></tt>, <tt class="docutils literal"><span class="pre">MAX_LENGTH</span></tt>, <tt class="docutils literal"><span class="pre">_a1</span></tt>, <tt class="docutils literal"><span class="pre">b_109_</span></tt>, and
-<tt class="docutils literal"><span class="pre">plan9FromOuterSpace</span></tt>
-are all valid identifiers; <tt class="docutils literal"><span class="pre">9b7z</span></tt>, <tt class="docutils literal"><span class="pre">$a</span></tt>, <tt class="docutils literal"><span class="pre">.section</span></tt>, and <tt class="docutils literal"><span class="pre">0debug</span></tt>
+digits. That is, <tt class="docutils literal">a</tt>, <tt class="docutils literal">i</tt>, <tt class="docutils literal">MAX_LENGTH</tt>, <tt class="docutils literal">_a1</tt>, <tt class="docutils literal">b_109_</tt>, and
+<tt class="docutils literal">plan9FromOuterSpace</tt>
+are all valid identifiers; <tt class="docutils literal">9b7z</tt>, <tt class="docutils literal">$a</tt>, <tt class="docutils literal">.section</tt>, and <tt class="docutils literal">0debug</tt>
are not. To
define an identifier using a Word, use either of the following:</p>
<pre class="literal-block">
@@ -766,19 +755,25 @@ also be constructed with any of the following optional parameters:</p>
<li>exact - indicating an exact length of matching characters</li>
</ul>
<p>If exact is specified, it will override any values for min or max.</p>
-</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">CharsNotIn</span></tt> - similar to <a class="reference internal" href="#word">Word</a>, but matches characters not
+<p>New in 1.5.6 - Sometimes you want to define a word using all
+characters in a range except for one or two of them; you can do this
+with the new excludeChars argument. This is helpful if you want to define
+a word with all printables except for a single delimiter character, such
+as '.'. Previously, you would have to create a custom string to pass to Word.
+With this change, you can just create <tt class="docutils literal">Word(printables, <span class="pre">excludeChars='.')</span></tt>.</p>
+</li>
+<li><p class="first"><tt class="docutils literal">CharsNotIn</tt> - similar to <a class="reference internal" href="#word">Word</a>, but matches characters not
in the given constructor string (accepts only one string for both
initial and body characters); also supports min, max, and exact
optional parameters.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">Regex</span></tt> - a powerful construct, that accepts a regular expression
+<li><p class="first"><tt class="docutils literal">Regex</tt> - a powerful construct, that accepts a regular expression
to be matched at the current parse position; accepts an optional
flags parameter, corresponding to the flags parameter in the re.compile
method; if the expression includes named sub-fields, they will be
represented in the returned <a class="reference internal" href="#parseresults">ParseResults</a></p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">QuotedString</span></tt> - supports the definition of custom quoted string
+<li><p class="first"><tt class="docutils literal">QuotedString</tt> - supports the definition of custom quoted string
formats, in addition to pyparsing's built-in dblQuotedString and
sglQuotedString. QuotedString allows you to specify the following
parameters:</p>
@@ -791,7 +786,7 @@ parameters:</p>
<li>endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=None =&gt; same as quoteChar)</li>
</ul>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">SkipTo</span></tt> - skips ahead in the input string, accepting any
+<li><p class="first"><tt class="docutils literal">SkipTo</tt> - skips ahead in the input string, accepting any
characters up to the specified pattern; may be constructed with
the following optional parameters:</p>
<ul class="simple">
@@ -800,27 +795,27 @@ the following optional parameters:</p>
<li>ignore - allows the user to specify patterns to not be matched,
to prevent false matches</li>
<li>failOn - if a literal string or expression is given for this argument, it defines an expression that
-should cause the <tt class="docutils literal"><span class="pre">SkipTo</span></tt> expression to fail, and not skip over that expression</li>
+should cause the <tt class="docutils literal">SkipTo</tt> expression to fail, and not skip over that expression</li>
</ul>
</li>
</ul>
<ul class="simple" id="white">
-<li><tt class="docutils literal"><span class="pre">White</span></tt> - also similar to <a class="reference internal" href="#word">Word</a>, but matches whitespace
+<li><tt class="docutils literal">White</tt> - also similar to <a class="reference internal" href="#word">Word</a>, but matches whitespace
characters. Not usually needed, as whitespace is implicitly
ignored by pyparsing. However, some grammars are whitespace-sensitive,
such as those that use leading tabs or spaces to indicating grouping
or hierarchy. (If matching on tab characters, be sure to call
<a class="reference internal" href="#parsewithtabs">parseWithTabs</a> on the top-level parse element.)</li>
-<li><tt class="docutils literal"><span class="pre">Empty</span></tt> - a null expression, requiring no characters - will always
+<li><tt class="docutils literal">Empty</tt> - a null expression, requiring no characters - will always
match; useful for debugging and for specialized grammars</li>
-<li><tt class="docutils literal"><span class="pre">NoMatch</span></tt> - opposite of Empty, will never match; useful for debugging
+<li><tt class="docutils literal">NoMatch</tt> - opposite of Empty, will never match; useful for debugging
and for specialized grammars</li>
</ul>
</div>
<div class="section" id="expression-subclasses">
-<h2><a class="toc-backref" href="#id10">2.3&nbsp;&nbsp;&nbsp;Expression subclasses</a></h2>
+<h2><a class="toc-backref" href="#id7">2.3&nbsp;&nbsp;&nbsp;Expression subclasses</a></h2>
<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">And</span></tt> - construct with a list of ParserElements, all of which must
+<li><p class="first"><tt class="docutils literal">And</tt> - construct with a list of ParserElements, all of which must
match for And to match; can also be created using the '+'
operator; multiple expressions can be Anded together using the '*'
operator as in:</p>
@@ -831,7 +826,7 @@ ipAddress = Word(nums) + ('.'+Word(nums))*3
<pre class="literal-block">
usPhoneNumber = Word(nums) + ('-'+Word(nums))*(1,2)
</pre>
-<p>A special form of <tt class="docutils literal"><span class="pre">And</span></tt> is created if the '-' operator is used
+<p>A special form of <tt class="docutils literal">And</tt> is created if the '-' operator is used
instead of the '+' operator. In the ipAddress example above, if
no trailing '.' and Word(nums) are found after matching the initial
Word(nums), then pyparsing will back up in the grammar and try other
@@ -846,136 +841,136 @@ which will halt the parsing process immediately. By careful use of the
the location where the incoming text does not match the specified
grammar.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">Or</span></tt> - construct with a list of ParserElements, any of which must
+<li><p class="first"><tt class="docutils literal">Or</tt> - construct with a list of ParserElements, any of which must
match for Or to match; if more than one expression matches, the
expression that makes the longest match will be used; can also
be created using the '^' operator</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">MatchFirst</span></tt> - construct with a list of ParserElements, any of
+<li><p class="first"><tt class="docutils literal">MatchFirst</tt> - construct with a list of ParserElements, any of
which must match for MatchFirst to match; matching is done
left-to-right, taking the first expression that matches; can
also be created using the '|' operator</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">Each</span></tt> - similar to And, in that all of the provided expressions
+<li><p class="first"><tt class="docutils literal">Each</tt> - similar to And, in that all of the provided expressions
must match; however, Each permits matching to be done in any order;
can also be created using the '&amp;' operator</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">Optional</span></tt> - construct with a ParserElement, but this element is
-not required to match; can be constructed with an optional <tt class="docutils literal"><span class="pre">default</span></tt> argument,
+<li><p class="first"><tt class="docutils literal">Optional</tt> - construct with a ParserElement, but this element is
+not required to match; can be constructed with an optional <tt class="docutils literal">default</tt> argument,
containing a default string or object to be supplied if the given optional
parse element is not found in the input string; parse action will only
be called if a match is found, or if a default is specified</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">ZeroOrMore</span></tt> - similar to Optional, but can be repeated</p>
+<li><p class="first"><tt class="docutils literal">ZeroOrMore</tt> - similar to Optional, but can be repeated</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">OneOrMore</span></tt> - similar to ZeroOrMore, but at least one match must
+<li><p class="first"><tt class="docutils literal">OneOrMore</tt> - similar to ZeroOrMore, but at least one match must
be present</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">FollowedBy</span></tt> - a lookahead expression, requires matching of the given
+<li><p class="first"><tt class="docutils literal">FollowedBy</tt> - a lookahead expression, requires matching of the given
expressions, but does not advance the parsing position within the input string</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">NotAny</span></tt> - a negative lookahead expression, prevents matching of named
+<li><p class="first"><tt class="docutils literal">NotAny</tt> - a negative lookahead expression, prevents matching of named
expressions, does not advance the parsing position within the input string;
can also be created using the unary '~' operator</p>
</li>
</ul>
</div>
<div class="section" id="expression-operators">
-<span id="operators"></span><h2><a class="toc-backref" href="#id11">2.4&nbsp;&nbsp;&nbsp;Expression operators</a></h2>
+<span id="operators"></span><h2><a class="toc-backref" href="#id8">2.4&nbsp;&nbsp;&nbsp;Expression operators</a></h2>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">~</span></tt> - creates NotAny using the expression after the operator</li>
-<li><tt class="docutils literal"><span class="pre">+</span></tt> - creates And using the expressions before and after the operator</li>
-<li><tt class="docutils literal"><span class="pre">|</span></tt> - creates MatchFirst (first left-to-right match) using the expressions before and after the operator</li>
-<li><tt class="docutils literal"><span class="pre">^</span></tt> - creates Or (longest match) using the expressions before and after the operator</li>
-<li><tt class="docutils literal"><span class="pre">&amp;</span></tt> - creates Each using the expressions before and after the operator</li>
-<li><tt class="docutils literal"><span class="pre">*</span></tt> - creates And by multiplying the expression by the integer operand; if
+<li><tt class="docutils literal">~</tt> - creates NotAny using the expression after the operator</li>
+<li><tt class="docutils literal">+</tt> - creates And using the expressions before and after the operator</li>
+<li><tt class="docutils literal">|</tt> - creates MatchFirst (first left-to-right match) using the expressions before and after the operator</li>
+<li><tt class="docutils literal">^</tt> - creates Or (longest match) using the expressions before and after the operator</li>
+<li><tt class="docutils literal">&amp;</tt> - creates Each using the expressions before and after the operator</li>
+<li><tt class="docutils literal">*</tt> - creates And by multiplying the expression by the integer operand; if
expression is multiplied by a 2-tuple, creates an And of (min,max)
expressions (similar to &quot;{min,max}&quot; form in regular expressions); if
min is None, intepret as (0,max); if max is None, interpret as
expr*min + ZeroOrMore(expr)</li>
-<li><tt class="docutils literal"><span class="pre">-</span></tt> - like <tt class="docutils literal"><span class="pre">+</span></tt> but with no backup and retry of alternatives</li>
-<li><tt class="docutils literal"><span class="pre">*</span></tt> - repetition of expression</li>
-<li><tt class="docutils literal"><span class="pre">==</span></tt> - matching expression to string; returns True if the string matches the given expression</li>
-<li><tt class="docutils literal"><span class="pre">&lt;&lt;</span></tt> - inserts the expression following the operator as the body of the
+<li><tt class="docutils literal">-</tt> - like <tt class="docutils literal">+</tt> but with no backup and retry of alternatives</li>
+<li><tt class="docutils literal">*</tt> - repetition of expression</li>
+<li><tt class="docutils literal">==</tt> - matching expression to string; returns True if the string matches the given expression</li>
+<li><tt class="docutils literal">&lt;&lt;</tt> - inserts the expression following the operator as the body of the
Forward expression before the operator</li>
</ul>
</div>
<div class="section" id="positional-subclasses">
-<h2><a class="toc-backref" href="#id12">2.5&nbsp;&nbsp;&nbsp;Positional subclasses</a></h2>
+<h2><a class="toc-backref" href="#id9">2.5&nbsp;&nbsp;&nbsp;Positional subclasses</a></h2>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">StringStart</span></tt> - matches beginning of the text</li>
-<li><tt class="docutils literal"><span class="pre">StringEnd</span></tt> - matches the end of the text</li>
-<li><tt class="docutils literal"><span class="pre">LineStart</span></tt> - matches beginning of a line (lines delimited by <tt class="docutils literal"><span class="pre">\n</span></tt> characters)</li>
-<li><tt class="docutils literal"><span class="pre">LineEnd</span></tt> - matches the end of a line</li>
-<li><tt class="docutils literal"><span class="pre">WordStart</span></tt> - matches a leading word boundary</li>
-<li><tt class="docutils literal"><span class="pre">WordEnd</span></tt> - matches a trailing word boundary</li>
+<li><tt class="docutils literal">StringStart</tt> - matches beginning of the text</li>
+<li><tt class="docutils literal">StringEnd</tt> - matches the end of the text</li>
+<li><tt class="docutils literal">LineStart</tt> - matches beginning of a line (lines delimited by <tt class="docutils literal">\n</tt> characters)</li>
+<li><tt class="docutils literal">LineEnd</tt> - matches the end of a line</li>
+<li><tt class="docutils literal">WordStart</tt> - matches a leading word boundary</li>
+<li><tt class="docutils literal">WordEnd</tt> - matches a trailing word boundary</li>
</ul>
</div>
<div class="section" id="converter-subclasses">
-<h2><a class="toc-backref" href="#id13">2.6&nbsp;&nbsp;&nbsp;Converter subclasses</a></h2>
+<h2><a class="toc-backref" href="#id10">2.6&nbsp;&nbsp;&nbsp;Converter subclasses</a></h2>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">Upcase</span></tt> - converts matched tokens to uppercase (deprecated -
-use <tt class="docutils literal"><span class="pre">upcaseTokens</span></tt> parse action instead)</li>
-<li><tt class="docutils literal"><span class="pre">Combine</span></tt> - joins all matched tokens into a single string, using
+<li><tt class="docutils literal">Upcase</tt> - converts matched tokens to uppercase (deprecated -
+use <tt class="docutils literal">upcaseTokens</tt> parse action instead)</li>
+<li><tt class="docutils literal">Combine</tt> - joins all matched tokens into a single string, using
specified joinString (default <tt class="docutils literal"><span class="pre">joinString=&quot;&quot;</span></tt>); expects
all matching tokens to be adjacent, with no intervening
-whitespace (can be overridden by specifying <tt class="docutils literal"><span class="pre">adjacent=False</span></tt> in constructor)</li>
-<li><tt class="docutils literal"><span class="pre">Suppress</span></tt> - clears matched tokens; useful to keep returned
+whitespace (can be overridden by specifying <tt class="docutils literal">adjacent=False</tt> in constructor)</li>
+<li><tt class="docutils literal">Suppress</tt> - clears matched tokens; useful to keep returned
results from being cluttered with required but uninteresting
tokens (such as list delimiters)</li>
</ul>
</div>
<div class="section" id="special-subclasses">
-<h2><a class="toc-backref" href="#id14">2.7&nbsp;&nbsp;&nbsp;Special subclasses</a></h2>
+<h2><a class="toc-backref" href="#id11">2.7&nbsp;&nbsp;&nbsp;Special subclasses</a></h2>
<ul class="simple">
-<li><tt class="docutils literal"><span class="pre">Group</span></tt> - causes the matched tokens to be enclosed in a list;
-useful in repeated elements like <tt class="docutils literal"><span class="pre">ZeroOrMore</span></tt> and <tt class="docutils literal"><span class="pre">OneOrMore</span></tt> to
+<li><tt class="docutils literal">Group</tt> - causes the matched tokens to be enclosed in a list;
+useful in repeated elements like <tt class="docutils literal">ZeroOrMore</tt> and <tt class="docutils literal">OneOrMore</tt> to
break up matched tokens into groups for each repeated pattern</li>
-<li><tt class="docutils literal"><span class="pre">Dict</span></tt> - like <tt class="docutils literal"><span class="pre">Group</span></tt>, but also constructs a dictionary, using the
+<li><tt class="docutils literal">Dict</tt> - like <tt class="docutils literal">Group</tt>, but also constructs a dictionary, using the
[0]'th elements of all enclosed token lists as the keys, and
each token list as the value</li>
-<li><tt class="docutils literal"><span class="pre">SkipTo</span></tt> - catch-all matching expression that accepts all characters
+<li><tt class="docutils literal">SkipTo</tt> - catch-all matching expression that accepts all characters
up until the given pattern is found to match; useful for specifying
incomplete grammars</li>
-<li><tt class="docutils literal"><span class="pre">Forward</span></tt> - placeholder token used to define recursive token
+<li><tt class="docutils literal">Forward</tt> - placeholder token used to define recursive token
patterns; when defining the actual expression later in the
-program, insert it into the <tt class="docutils literal"><span class="pre">Forward</span></tt> object using the <tt class="docutils literal"><span class="pre">&lt;&lt;</span></tt>
-operator (see <tt class="docutils literal"><span class="pre">fourFn.py</span></tt> for an example).</li>
+program, insert it into the <tt class="docutils literal">Forward</tt> object using the <tt class="docutils literal">&lt;&lt;</tt>
+operator (see <tt class="docutils literal">fourFn.py</tt> for an example).</li>
</ul>
</div>
<div class="section" id="other-classes">
-<h2><a class="toc-backref" href="#id15">2.8&nbsp;&nbsp;&nbsp;Other classes</a></h2>
+<h2><a class="toc-backref" href="#id12">2.8&nbsp;&nbsp;&nbsp;Other classes</a></h2>
<ul id="parseresults">
-<li><p class="first"><tt class="docutils literal"><span class="pre">ParseResults</span></tt> - class used to contain and manage the lists of tokens
+<li><p class="first"><tt class="docutils literal">ParseResults</tt> - class used to contain and manage the lists of tokens
created from parsing the input using the user-defined parse
expression. ParseResults can be accessed in a number of ways:</p>
<ul class="simple">
<li>as a list<ul>
<li>total list of elements can be found using len()</li>
<li>individual elements can be found using [0], [1], [-1], etc.</li>
-<li>elements can be deleted using <tt class="docutils literal"><span class="pre">del</span></tt></li>
+<li>elements can be deleted using <tt class="docutils literal">del</tt></li>
<li>the -1th element can be extracted and removed in a single operation
-using <tt class="docutils literal"><span class="pre">pop()</span></tt>, or any element can be extracted and removed
-using <tt class="docutils literal"><span class="pre">pop(n)</span></tt></li>
+using <tt class="docutils literal">pop()</tt>, or any element can be extracted and removed
+using <tt class="docutils literal">pop(n)</tt></li>
</ul>
</li>
<li>as a dictionary<ul>
-<li>if <tt class="docutils literal"><span class="pre">setResultsName()</span></tt> is used to name elements within the
+<li>if <tt class="docutils literal">setResultsName()</tt> is used to name elements within the
overall parse expression, then these fields can be referenced
as dictionary elements or as attributes</li>
<li>the Dict class generates dictionary entries using the data of the
-input text - in addition to ParseResults listed as <tt class="docutils literal"><span class="pre">[</span> <span class="pre">[</span> <span class="pre">a1,</span> <span class="pre">b1,</span> <span class="pre">c1,</span> <span class="pre">...],</span> <span class="pre">[</span> <span class="pre">a2,</span> <span class="pre">b2,</span> <span class="pre">c2,</span> <span class="pre">...]</span>&nbsp; <span class="pre">]</span></tt>
-it also acts as a dictionary with entries defined as <tt class="docutils literal"><span class="pre">{</span> <span class="pre">a1</span> <span class="pre">:</span> <span class="pre">[</span> <span class="pre">b1,</span> <span class="pre">c1,</span> <span class="pre">...</span> <span class="pre">]</span> <span class="pre">},</span> <span class="pre">{</span> <span class="pre">a2</span> <span class="pre">:</span> <span class="pre">[</span> <span class="pre">b2,</span> <span class="pre">c2,</span> <span class="pre">...</span> <span class="pre">]</span> <span class="pre">}</span></tt>;
+input text - in addition to ParseResults listed as <tt class="docutils literal">[ [ a1, b1, c1, <span class="pre">...],</span> [ a2, b2, c2, <span class="pre">...]</span>&nbsp; ]</tt>
+it also acts as a dictionary with entries defined as <tt class="docutils literal">{ a1 : [ b1, c1, ... ] }, { a2 : [ b2, c2, ... ] }</tt>;
this is especially useful when processing tabular data where the first column contains a key
value for that line of data</li>
-<li>list elements that are deleted using <tt class="docutils literal"><span class="pre">del</span></tt> will still be accessible by their
+<li>list elements that are deleted using <tt class="docutils literal">del</tt> will still be accessible by their
dictionary keys</li>
-<li>supports <tt class="docutils literal"><span class="pre">get()</span></tt>, <tt class="docutils literal"><span class="pre">items()</span></tt> and <tt class="docutils literal"><span class="pre">keys()</span></tt> methods, similar to a dictionary</li>
-<li>a keyed item can be extracted and removed using <tt class="docutils literal"><span class="pre">pop(key)</span></tt>. Here
+<li>supports <tt class="docutils literal">get()</tt>, <tt class="docutils literal">items()</tt> and <tt class="docutils literal">keys()</tt> methods, similar to a dictionary</li>
+<li>a keyed item can be extracted and removed using <tt class="docutils literal">pop(key)</tt>. Here
key must be non-numeric (such as a string), in order to use dict
extraction instead of list extraction.</li>
<li>new named elements can be added (in a parse action, for instance), using the same
-syntax as adding an item to a dict (<tt class="docutils literal"><span class="pre">parseResults[&quot;X&quot;]=&quot;new</span> <span class="pre">item&quot;</span></tt>); named elements can be removed using <tt class="docutils literal"><span class="pre">del</span> <span class="pre">parseResults[&quot;X&quot;]</span></tt></li>
+syntax as adding an item to a dict (<tt class="docutils literal"><span class="pre">parseResults[&quot;X&quot;]=&quot;new</span> item&quot;</tt>); named elements can be removed using <tt class="docutils literal">del <span class="pre">parseResults[&quot;X&quot;]</span></tt></li>
</ul>
</li>
<li>as a nested list<ul>
@@ -986,17 +981,17 @@ tree</li>
</li>
</ul>
<p>ParseResults can also be converted to an ordinary list of strings
-by calling <tt class="docutils literal"><span class="pre">asList()</span></tt>. Note that this will strip the results of any
+by calling <tt class="docutils literal">asList()</tt>. Note that this will strip the results of any
field names that have been defined for any embedded parse elements.
-(The <tt class="docutils literal"><span class="pre">pprint</span></tt> module is especially good at printing out the nested contents
-given by <tt class="docutils literal"><span class="pre">asList()</span></tt>.)</p>
-<p>Finally, ParseResults can be converted to an XML string by calling <tt class="docutils literal"><span class="pre">asXML()</span></tt>. Where
+(The <tt class="docutils literal">pprint</tt> module is especially good at printing out the nested contents
+given by <tt class="docutils literal">asList()</tt>.)</p>
+<p>Finally, ParseResults can be converted to an XML string by calling <tt class="docutils literal">asXML()</tt>. Where
possible, results will be tagged using the results names defined for the respective
-ParseExpressions. <tt class="docutils literal"><span class="pre">asXML()</span></tt> takes two optional arguments:</p>
+ParseExpressions. <tt class="docutils literal">asXML()</tt> takes two optional arguments:</p>
<ul class="simple">
<li>doctagname - for ParseResults that do not have a defined name, this argument
-will wrap the resulting XML in a set of opening and closing tags <tt class="docutils literal"><span class="pre">&lt;doctagname&gt;</span></tt>
-and <tt class="docutils literal"><span class="pre">&lt;/doctagname&gt;</span></tt>.</li>
+will wrap the resulting XML in a set of opening and closing tags <tt class="docutils literal">&lt;doctagname&gt;</tt>
+and <tt class="docutils literal">&lt;/doctagname&gt;</tt>.</li>
<li>namedItemsOnly (default=False) - flag to indicate if the generated XML should
skip items that do not have defined names. If a nested group item is named, then all
embedded items will be included, whether they have names or not.</li>
@@ -1005,9 +1000,9 @@ embedded items will be included, whether they have names or not.</li>
</ul>
</div>
<div class="section" id="exception-classes-and-troubleshooting">
-<h2><a class="toc-backref" href="#id16">2.9&nbsp;&nbsp;&nbsp;Exception classes and Troubleshooting</a></h2>
+<h2><a class="toc-backref" href="#id13">2.9&nbsp;&nbsp;&nbsp;Exception classes and Troubleshooting</a></h2>
<ul id="parseexception">
-<li><p class="first"><tt class="docutils literal"><span class="pre">ParseException</span></tt> - exception returned when a grammar parse fails;
+<li><p class="first"><tt class="docutils literal">ParseException</tt> - exception returned when a grammar parse fails;
ParseExceptions have attributes loc, msg, line, lineno, and column; to view the
text line and location where the reported ParseException occurs, use:</p>
<pre class="literal-block">
@@ -1017,7 +1012,7 @@ except ParseException, err:
print err
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">RecursiveGrammarException</span></tt> - exception returned by <tt class="docutils literal"><span class="pre">validate()</span></tt> if
+<li><p class="first"><tt class="docutils literal">RecursiveGrammarException</tt> - exception returned by <tt class="docutils literal">validate()</tt> if
the grammar contains a recursive infinite loop, such as:</p>
<pre class="literal-block">
badGrammar = Forward()
@@ -1025,13 +1020,13 @@ goodToken = Literal(&quot;A&quot;)
badGrammar &lt;&lt; Optional(goodToken) + badGrammar
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">ParseFatalException</span></tt> - exception that parse actions can raise to stop parsing
+<li><p class="first"><tt class="docutils literal">ParseFatalException</tt> - exception that parse actions can raise to stop parsing
immediately. Should be used when a semantic error is found in the input text, such
as a mismatched XML tag.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">ParseSyntaxException</span></tt> - subclass of <tt class="docutils literal"><span class="pre">ParseFatalException</span></tt> raised when a
+<li><p class="first"><tt class="docutils literal">ParseSyntaxException</tt> - subclass of <tt class="docutils literal">ParseFatalException</tt> raised when a
syntax error is found, based on the use of the '-' operator when defining
-a sequence of expressions in an <tt class="docutils literal"><span class="pre">And</span></tt> expression.</p>
+a sequence of expressions in an <tt class="docutils literal">And</tt> expression.</p>
</li>
</ul>
<p>You can also get some insights into the parsing logic using diagnostic parse actions,
@@ -1040,43 +1035,43 @@ scanString().</p>
</div>
</div>
<div class="section" id="miscellaneous-attributes-and-methods">
-<h1><a class="toc-backref" href="#id17">3&nbsp;&nbsp;&nbsp;Miscellaneous attributes and methods</a></h1>
+<h1><a class="toc-backref" href="#id14">3&nbsp;&nbsp;&nbsp;Miscellaneous attributes and methods</a></h1>
<div class="section" id="helper-methods">
-<h2><a class="toc-backref" href="#id18">3.1&nbsp;&nbsp;&nbsp;Helper methods</a></h2>
+<h2><a class="toc-backref" href="#id15">3.1&nbsp;&nbsp;&nbsp;Helper methods</a></h2>
<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">delimitedList(</span> <span class="pre">expr,</span> <span class="pre">delim=',')</span></tt> - convenience function for
+<li><p class="first"><tt class="docutils literal">delimitedList( expr, <span class="pre">delim=',')</span></tt> - convenience function for
matching one or more occurrences of expr, separated by delim.
By default, the delimiters are suppressed, so the returned results contain
-only the separate list elements. Can optionally specify <tt class="docutils literal"><span class="pre">combine=True</span></tt>,
+only the separate list elements. Can optionally specify <tt class="docutils literal">combine=True</tt>,
indicating that the expressions and delimiters should be returned as one
combined value (useful for scoped variables, such as &quot;a.b.c&quot;, or
&quot;a::b::c&quot;, or paths such as &quot;a/b/c&quot;).</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">countedArray(</span> <span class="pre">expr</span> <span class="pre">)</span></tt> - convenience function for a pattern where an list of
+<li><p class="first"><tt class="docutils literal">countedArray( expr )</tt> - convenience function for a pattern where an list of
instances of the given expression are preceded by an integer giving the count of
elements in the list. Returns an expression that parses the leading integer,
reads exactly that many expressions, and returns the array of expressions in the
parse results - the leading integer is suppressed from the results (although it
is easily reconstructed by using len on the returned array).</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">oneOf(</span> <span class="pre">string,</span> <span class="pre">caseless=False</span> <span class="pre">)</span></tt> - convenience function for quickly declaring an
-alternative set of <tt class="docutils literal"><span class="pre">Literal</span></tt> tokens, by splitting the given string on
+<li><p class="first"><tt class="docutils literal">oneOf( string, caseless=False )</tt> - convenience function for quickly declaring an
+alternative set of <tt class="docutils literal">Literal</tt> tokens, by splitting the given string on
whitespace boundaries. The tokens are sorted so that longer
matches are attempted first; this ensures that a short token does
-not mask a longer one that starts with the same characters. If <tt class="docutils literal"><span class="pre">caseless=True</span></tt>,
+not mask a longer one that starts with the same characters. If <tt class="docutils literal">caseless=True</tt>,
will create an alternative set of CaselessLiteral tokens.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">dictOf(</span> <span class="pre">key,</span> <span class="pre">value</span> <span class="pre">)</span></tt> - convenience function for quickly declaring a
-dictionary pattern of <tt class="docutils literal"><span class="pre">Dict(</span> <span class="pre">ZeroOrMore(</span> <span class="pre">Group(</span> <span class="pre">key</span> <span class="pre">+</span> <span class="pre">value</span> <span class="pre">)</span> <span class="pre">)</span> <span class="pre">)</span></tt>.</p>
+<li><p class="first"><tt class="docutils literal">dictOf( key, value )</tt> - convenience function for quickly declaring a
+dictionary pattern of <tt class="docutils literal">Dict( ZeroOrMore( Group( key + value ) ) )</tt>.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">makeHTMLTags(</span> <span class="pre">tagName</span> <span class="pre">)</span></tt> and <tt class="docutils literal"><span class="pre">makeXMLTags(</span> <span class="pre">tagName</span> <span class="pre">)</span></tt> - convenience
+<li><p class="first"><tt class="docutils literal">makeHTMLTags( tagName )</tt> and <tt class="docutils literal">makeXMLTags( tagName )</tt> - convenience
functions to create definitions of opening and closing tag expressions. Returns
a pair of expressions, for the corresponding &lt;tag&gt; and &lt;/tag&gt; strings. Includes
support for attributes in the opening tag, such as &lt;tag attr1=&quot;abc&quot;&gt; - attributes
-are returned as keyed tokens in the returned ParseResults. <tt class="docutils literal"><span class="pre">makeHTMLTags</span></tt> is less
-restrictive than <tt class="docutils literal"><span class="pre">makeXMLTags</span></tt>, especially with respect to case sensitivity.</p>
+are returned as keyed tokens in the returned ParseResults. <tt class="docutils literal">makeHTMLTags</tt> is less
+restrictive than <tt class="docutils literal">makeXMLTags</tt>, especially with respect to case sensitivity.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">operatorPrecedence(baseOperand,</span> <span class="pre">operatorList)</span></tt> - convenience function to define a
+<li><p class="first"><tt class="docutils literal">operatorPrecedence(baseOperand, operatorList)</tt> - convenience function to define a
grammar for parsing
expressions with a hierarchical precedence of operators. To use the operatorPrecedence
helper:</p>
@@ -1087,7 +1082,7 @@ and integer or a variable. This will be the first argument
to the operatorPrecedence method.</li>
<li>Define a list of tuples for each level of operator
precendence. Each tuple is of the form
-<tt class="docutils literal"><span class="pre">(opExpr,</span> <span class="pre">numTerms,</span> <span class="pre">rightLeftAssoc,</span> <span class="pre">parseAction)</span></tt>, where:<ul>
+<tt class="docutils literal">(opExpr, numTerms, rightLeftAssoc, parseAction)</tt>, where:<ul>
<li>opExpr is the pyparsing expression for the operator;
may also be a string, which will be converted to a Literal; if
None, indicates an empty operator, such as the implied
@@ -1096,7 +1091,7 @@ multiplication operation between 'm' and 'x' in &quot;y = mx + b&quot;.</li>
be 1 or 2)</li>
<li>rightLeftAssoc is the indicator whether the operator is
right or left associative, using the pyparsing-defined
-constants <tt class="docutils literal"><span class="pre">opAssoc.RIGHT</span></tt> and <tt class="docutils literal"><span class="pre">opAssoc.LEFT</span></tt>.</li>
+constants <tt class="docutils literal">opAssoc.RIGHT</tt> and <tt class="docutils literal">opAssoc.LEFT</tt>.</li>
<li>parseAction is the parse action to be associated with
expressions matching this operator expression (the
parse action tuple member may be omitted)</li>
@@ -1109,7 +1104,7 @@ this expression to parse input strings, or incorporate it
into a larger, more complex grammar.</li>
</ol>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">matchPreviousLiteral</span></tt> and <tt class="docutils literal"><span class="pre">matchPreviousExpr</span></tt> - function to define and
+<li><p class="first"><tt class="docutils literal">matchPreviousLiteral</tt> and <tt class="docutils literal">matchPreviousExpr</tt> - function to define and
expression that matches the same content
as was parsed in a previous parse expression. For instance:</p>
<pre class="literal-block">
@@ -1126,7 +1121,7 @@ matchExpr = first + &quot;:&quot; + matchPreviousExpr(first)
<p>will <em>not</em> match the leading &quot;1:1&quot; in &quot;1:10&quot;; the expressions are
evaluated first, and then compared, so &quot;1&quot; is compared with &quot;10&quot;.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">nestedExpr(opener,</span> <span class="pre">closer,</span> <span class="pre">content=None,</span> <span class="pre">ignoreExpr=quotedString)</span></tt> - method for defining nested
+<li><p class="first"><tt class="docutils literal">nestedExpr(opener, closer, content=None, ignoreExpr=quotedString)</tt> - method for defining nested
lists enclosed in opening and closing delimiters.</p>
<ul class="simple">
<li>opener - opening character for a nested list (default=&quot;(&quot;); can also be a pyparsing expression</li>
@@ -1144,7 +1139,7 @@ expression. Specify multiple expressions using an Or or MatchFirst.
The default is quotedString, but if no expressions are to be ignored,
then pass None for this argument.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">indentedBlock(</span> <span class="pre">statementExpr,</span> <span class="pre">indentationStackVar,</span> <span class="pre">indent=True)</span></tt> -
+<li><p class="first"><tt class="docutils literal">indentedBlock( statementExpr, indentationStackVar, indent=True)</tt> -
function to define an indented block of statements, similar to
indentation-based blocking in Python source code:</p>
<ul class="simple">
@@ -1152,7 +1147,7 @@ indentation-based blocking in Python source code:</p>
will be found in the indented block; a valid indentedBlock
must contain at least 1 matching statementExpr</li>
<li>indentationStackVar is a Python list variable; this variable
-should be common to all <tt class="docutils literal"><span class="pre">indentedBlock</span></tt> expressions defined
+should be common to all <tt class="docutils literal">indentedBlock</tt> expressions defined
within the same grammar, and should be reinitialized to [1]
each time the grammar is to be used</li>
<li>indent is a boolean flag indicating whether the expressions
@@ -1163,79 +1158,83 @@ statements (all starting in column 1), set indent to False</li>
</li>
</ul>
<ul id="originaltextfor">
-<li><p class="first"><tt class="docutils literal"><span class="pre">originalTextFor(</span> <span class="pre">expr</span> <span class="pre">)</span></tt> - helper function to preserve the originally parsed text, regardless of any
+<li><p class="first"><tt class="docutils literal">originalTextFor( expr )</tt> - helper function to preserve the originally parsed text, regardless of any
token processing or conversion done by the contained expression. For instance, the following expression:</p>
<pre class="literal-block">
fullName = Word(alphas) + Word(alphas)
</pre>
<p>will return the parse of &quot;John Smith&quot; as ['John', 'Smith']. In some applications, the actual name as it
-was given in the input string is what is desired. To do this, use <tt class="docutils literal"><span class="pre">originalTextFor</span></tt>:</p>
+was given in the input string is what is desired. To do this, use <tt class="docutils literal">originalTextFor</tt>:</p>
<pre class="literal-block">
fullName = originalTextFor(Word(alphas) + Word(alphas))
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">lineno(</span> <span class="pre">loc,</span> <span class="pre">string</span> <span class="pre">)</span></tt> - function to give the line number of the
+<li><p class="first"><tt class="docutils literal">ungroup( expr )</tt> - function to &quot;ungroup&quot; returned tokens; useful
+to undo the default behavior of And to always group the returned tokens, even
+if there is only one in the list. (New in 1.5.6)</p>
+</li>
+<li><p class="first"><tt class="docutils literal">lineno( loc, string )</tt> - function to give the line number of the
location within the string; the first line is line 1, newlines
start new rows</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">col(</span> <span class="pre">loc,</span> <span class="pre">string</span> <span class="pre">)</span></tt> - function to give the column number of the
+<li><p class="first"><tt class="docutils literal">col( loc, string )</tt> - function to give the column number of the
location within the string; the first column is column 1,
newlines reset the column number to 1</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">line(</span> <span class="pre">loc,</span> <span class="pre">string</span> <span class="pre">)</span></tt> - function to retrieve the line of text
-representing <tt class="docutils literal"><span class="pre">lineno(</span> <span class="pre">loc,</span> <span class="pre">string</span> <span class="pre">)</span></tt>; useful when printing out diagnostic
+<li><p class="first"><tt class="docutils literal">line( loc, string )</tt> - function to retrieve the line of text
+representing <tt class="docutils literal">lineno( loc, string )</tt>; useful when printing out diagnostic
messages for exceptions</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">srange(</span> <span class="pre">rangeSpec</span> <span class="pre">)</span></tt> - function to define a string of characters,
+<li><p class="first"><tt class="docutils literal">srange( rangeSpec )</tt> - function to define a string of characters,
given a string of the form used by regexp string ranges, such as <tt class="docutils literal"><span class="pre">&quot;[0-9]&quot;</span></tt> for
all numeric digits, <tt class="docutils literal"><span class="pre">&quot;[A-Z_]&quot;</span></tt> for uppercase characters plus underscore, and
so on (note that rangeSpec does not include support for generic regular
expressions, just string range specs)</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">getTokensEndLoc()</span></tt> - function to call from within a parse action to get
+<li><p class="first"><tt class="docutils literal">getTokensEndLoc()</tt> - function to call from within a parse action to get
the ending location for the matched tokens</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">traceParseAction(fn)</span></tt> - decorator function to debug parse actions. Lists
+<li><p class="first"><tt class="docutils literal">traceParseAction(fn)</tt> - decorator function to debug parse actions. Lists
each call, called arguments, and return value or exception</p>
</li>
</ul>
</div>
<div class="section" id="helper-parse-actions">
-<h2><a class="toc-backref" href="#id19">3.2&nbsp;&nbsp;&nbsp;Helper parse actions</a></h2>
+<h2><a class="toc-backref" href="#id16">3.2&nbsp;&nbsp;&nbsp;Helper parse actions</a></h2>
<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">removeQuotes</span></tt> - removes the first and last characters of a quoted string;
+<li><p class="first"><tt class="docutils literal">removeQuotes</tt> - removes the first and last characters of a quoted string;
useful to remove the delimiting quotes from quoted strings</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">replaceWith(replString)</span></tt> - returns a parse action that simply returns the
+<li><p class="first"><tt class="docutils literal">replaceWith(replString)</tt> - returns a parse action that simply returns the
replString; useful when using transformString, or converting HTML entities, as in:</p>
<pre class="literal-block">
nbsp = Literal(&quot;&amp;nbsp;&quot;).setParseAction( replaceWith(&quot;&lt;BLANK&gt;&quot;) )
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">keepOriginalText</span></tt>- (deprecated, use <a class="reference internal" href="#originaltextfor">originalTextFor</a> instead) restores any internal whitespace or suppressed
+<li><p class="first"><tt class="docutils literal">keepOriginalText</tt>- (deprecated, use <a class="reference internal" href="#originaltextfor">originalTextFor</a> instead) restores any internal whitespace or suppressed
text within the tokens for a matched parse
expression. This is especially useful when defining expressions
for scanString or transformString applications.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">withAttribute(</span> <span class="pre">*args,</span> <span class="pre">**kwargs</span> <span class="pre">)</span></tt> - helper to create a validating parse action to be used with start tags created
-with <tt class="docutils literal"><span class="pre">makeXMLTags</span></tt> or <tt class="docutils literal"><span class="pre">makeHTMLTags</span></tt>. Use <tt class="docutils literal"><span class="pre">withAttribute</span></tt> to qualify a starting tag
+<li><p class="first"><tt class="docutils literal">withAttribute( *args, **kwargs )</tt> - helper to create a validating parse action to be used with start tags created
+with <tt class="docutils literal">makeXMLTags</tt> or <tt class="docutils literal">makeHTMLTags</tt>. Use <tt class="docutils literal">withAttribute</tt> to qualify a starting tag
with a required attribute value, to avoid false matches on common tags such as
-<tt class="docutils literal"><span class="pre">&lt;TD&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;DIV&gt;</span></tt>.</p>
-<p><tt class="docutils literal"><span class="pre">withAttribute</span></tt> can be called with:</p>
+<tt class="docutils literal">&lt;TD&gt;</tt> or <tt class="docutils literal">&lt;DIV&gt;</tt>.</p>
+<p><tt class="docutils literal">withAttribute</tt> can be called with:</p>
<ul class="simple">
<li>keyword arguments, as in <tt class="docutils literal"><span class="pre">(class=&quot;Customer&quot;,align=&quot;right&quot;)</span></tt>, or</li>
-<li>a list of name-value tuples, as in <tt class="docutils literal"><span class="pre">(</span> <span class="pre">(&quot;ns1:class&quot;,</span> <span class="pre">&quot;Customer&quot;),</span> <span class="pre">(&quot;ns2:align&quot;,&quot;right&quot;)</span> <span class="pre">)</span></tt></li>
+<li>a list of name-value tuples, as in <tt class="docutils literal">( (&quot;ns1:class&quot;, <span class="pre">&quot;Customer&quot;),</span> <span class="pre">(&quot;ns2:align&quot;,&quot;right&quot;)</span> )</tt></li>
</ul>
<p>An attribute can be specified to have the special value
-<tt class="docutils literal"><span class="pre">withAttribute.ANY_VALUE</span></tt>, which will match any value - use this to
+<tt class="docutils literal">withAttribute.ANY_VALUE</tt>, which will match any value - use this to
ensure that an attribute is present but any attribute value is
acceptable.</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">downcaseTokens</span></tt> - converts all matched tokens to lowercase</p>
+<li><p class="first"><tt class="docutils literal">downcaseTokens</tt> - converts all matched tokens to lowercase</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">upcaseTokens</span></tt> - converts all matched tokens to uppercase</p>
+<li><p class="first"><tt class="docutils literal">upcaseTokens</tt> - converts all matched tokens to uppercase</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">matchOnlyAtCol(</span> <span class="pre">columnNumber</span> <span class="pre">)</span></tt> - a parse action that verifies that
+<li><p class="first"><tt class="docutils literal">matchOnlyAtCol( columnNumber )</tt> - a parse action that verifies that
an expression was matched at a particular column, raising a
ParseException if matching at a different column number; useful when parsing
tabular data</p>
@@ -1243,42 +1242,42 @@ tabular data</p>
</ul>
</div>
<div class="section" id="common-string-and-token-constants">
-<h2><a class="toc-backref" href="#id20">3.3&nbsp;&nbsp;&nbsp;Common string and token constants</a></h2>
+<h2><a class="toc-backref" href="#id17">3.3&nbsp;&nbsp;&nbsp;Common string and token constants</a></h2>
<ul>
-<li><p class="first"><tt class="docutils literal"><span class="pre">alphas</span></tt> - same as <tt class="docutils literal"><span class="pre">string.letters</span></tt></p>
+<li><p class="first"><tt class="docutils literal">alphas</tt> - same as <tt class="docutils literal">string.letters</tt></p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">nums</span></tt> - same as <tt class="docutils literal"><span class="pre">string.digits</span></tt></p>
+<li><p class="first"><tt class="docutils literal">nums</tt> - same as <tt class="docutils literal">string.digits</tt></p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">alphanums</span></tt> - a string containing <tt class="docutils literal"><span class="pre">alphas</span> <span class="pre">+</span> <span class="pre">nums</span></tt></p>
+<li><p class="first"><tt class="docutils literal">alphanums</tt> - a string containing <tt class="docutils literal">alphas + nums</tt></p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">alphas8bit</span></tt> - a string containing alphabetic 8-bit characters:</p>
+<li><p class="first"><tt class="docutils literal">alphas8bit</tt> - a string containing alphabetic 8-bit characters:</p>
<pre class="literal-block">
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ
</pre>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">printables</span></tt> - same as <tt class="docutils literal"><span class="pre">string.printable</span></tt>, minus the space (<tt class="docutils literal"><span class="pre">'</span> <span class="pre">'</span></tt>) character</p>
+<li><p class="first"><tt class="docutils literal">printables</tt> - same as <tt class="docutils literal">string.printable</tt>, minus the space (<tt class="docutils literal">' '</tt>) character</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">empty</span></tt> - a global <tt class="docutils literal"><span class="pre">Empty()</span></tt>; will always match</p>
+<li><p class="first"><tt class="docutils literal">empty</tt> - a global <tt class="docutils literal">Empty()</tt>; will always match</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">sglQuotedString</span></tt> - a string of characters enclosed in 's; may
+<li><p class="first"><tt class="docutils literal">sglQuotedString</tt> - a string of characters enclosed in 's; may
include whitespace, but not newlines</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">dblQuotedString</span></tt> - a string of characters enclosed in &quot;s; may
+<li><p class="first"><tt class="docutils literal">dblQuotedString</tt> - a string of characters enclosed in &quot;s; may
include whitespace, but not newlines</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">quotedString</span></tt> - <tt class="docutils literal"><span class="pre">sglQuotedString</span> <span class="pre">|</span> <span class="pre">dblQuotedString</span></tt></p>
+<li><p class="first"><tt class="docutils literal">quotedString</tt> - <tt class="docutils literal">sglQuotedString | dblQuotedString</tt></p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">cStyleComment</span></tt> - a comment block delimited by <tt class="docutils literal"><span class="pre">'/*'</span></tt> and <tt class="docutils literal"><span class="pre">'*/'</span></tt> sequences; can span
+<li><p class="first"><tt class="docutils literal">cStyleComment</tt> - a comment block delimited by <tt class="docutils literal"><span class="pre">'/*'</span></tt> and <tt class="docutils literal"><span class="pre">'*/'</span></tt> sequences; can span
multiple lines, but does not support nesting of comments</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">htmlComment</span></tt> - a comment block delimited by <tt class="docutils literal"><span class="pre">'&lt;!--'</span></tt> and <tt class="docutils literal"><span class="pre">'--&gt;'</span></tt> sequences; can span
+<li><p class="first"><tt class="docutils literal">htmlComment</tt> - a comment block delimited by <tt class="docutils literal"><span class="pre">'&lt;!--'</span></tt> and <tt class="docutils literal"><span class="pre">'--&gt;'</span></tt> sequences; can span
multiple lines, but does not support nesting of comments</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">commaSeparatedList</span></tt> - similar to <tt class="docutils literal"><span class="pre">delimitedList</span></tt>, except that the
+<li><p class="first"><tt class="docutils literal">commaSeparatedList</tt> - similar to <tt class="docutils literal">delimitedList</tt>, except that the
list expressions can be any text value, or a quoted string; quoted strings can
safely include commas without incorrectly breaking the string into two tokens</p>
</li>
-<li><p class="first"><tt class="docutils literal"><span class="pre">restOfLine</span></tt> - all remaining printable characters up to but not including the next
+<li><p class="first"><tt class="docutils literal">restOfLine</tt> - all remaining printable characters up to but not including the next
newline</p>
</li>
</ul>
diff --git a/HowToUsePyparsing.txt b/HowToUsePyparsing.txt
index 167266d..2c7e89f 100644
--- a/HowToUsePyparsing.txt
+++ b/HowToUsePyparsing.txt
@@ -5,10 +5,10 @@ Using the pyparsing module
:author: Paul McGuire
:address: ptmcg@users.sourceforge.net
-:revision: 1.5.3
-:date: January, 2010
+:revision: 1.5.6
+:date: June, 2011
-:copyright: Copyright |copy| 2003-2010 Paul McGuire.
+:copyright: Copyright |copy| 2003-2011 Paul McGuire.
.. |copy| unicode:: 0xA9
@@ -63,48 +63,6 @@ The parsed tokens are returned in the following form::
['Hello', ',', 'World', '!']
-New features in 1.5.2
----------------------
-There are no new pyparsing features in this release, it is primarily a bug-fixing release.
-This release does include changes to support running pyparsing under IronPython 2.0.1, and
-a pyparsing_py3 module for use with Python 3.
-
-
-New features in 1.5.1
----------------------
-Some of the significant features added in version 1.5.0 are:
-
-- ``originalTextFor`` helper method, to simplify grammar expressions that need to preserve
- the original input text; should be used in place of the ``keepOriginalText`` parse action::
-
- fullName = Word(alphas) + Word(alphas)
- fullName.setParseAction(keepOriginalText)
-
- should now be written::
-
- fullName = originalTextFor(Word(alphas) + Word(alphas))
-
-- added parameter ``parseAll`` to ``ParserElement.parseFile`` to
- match the arguments for ``ParserElement.parseString``
-
-- new argument ``failOn`` for SkipTo expressions, to define literal strings or expressions that
- should not be included in the skipped text
-
-
-New features in 1.5.0
----------------------
-Some of the significant features added in version 1.5.0 are:
-
-- ``indentedBlock`` helper method to define grammars using block indentation for grouping (like Python)
-
-- new parameter ``parseAll`` in ``ParserElement.parseString``
-
-- operator '-' for combining ParserElements; similar to the '+' operator, but raises an immediate
- ``ParseSyntaxException`` if an expression after the '-' operator fails to match; using '-' can
- provide error messages that are more useful for application users to find syntax errors in their
- input text
-
-
Usage notes
-----------
@@ -324,7 +282,10 @@ methods for code to use are:
the element; if multiple tokens within
a repetition group (such as ``ZeroOrMore`` or ``delimitedList``) the
default is to return only the last matching token - if listAllMatches
- is set to True, then a list of matching tokens is returned. Note:
+ is set to True, then a list of all the matching tokens is returned.
+ (New in 1.5.6 - a results name with a trailing '*' character will be
+ interpreted as setting listAllMatches to True.)
+ Note:
``setResultsName`` returns a *copy* of the element so that a single
basic element can be referenced multiple times and given
different names within a complex grammar.
@@ -459,6 +420,13 @@ Basic ParserElement subclasses
- exact - indicating an exact length of matching characters
If exact is specified, it will override any values for min or max.
+
+ New in 1.5.6 - Sometimes you want to define a word using all
+ characters in a range except for one or two of them; you can do this
+ with the new excludeChars argument. This is helpful if you want to define
+ a word with all printables except for a single delimiter character, such
+ as '.'. Previously, you would have to create a custom string to pass to Word.
+ With this change, you can just create ``Word(printables, excludeChars='.')``.
- ``CharsNotIn`` - similar to Word_, but matches characters not
in the given constructor string (accepts only one string for both
@@ -913,6 +881,10 @@ Helper methods
fullName = originalTextFor(Word(alphas) + Word(alphas))
+- ``ungroup( expr )`` - function to "ungroup" returned tokens; useful
+ to undo the default behavior of And to always group the returned tokens, even
+ if there is only one in the list. (New in 1.5.6)
+
- ``lineno( loc, string )`` - function to give the line number of the
location within the string; the first line is line 1, newlines
start new rows
diff --git a/pyparsing.py b/pyparsing.py
index dec506e..9be97dc 100644
--- a/pyparsing.py
+++ b/pyparsing.py
@@ -59,7 +59,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "1.5.6"
-__versionTime__ = "1 May 2011 23:41"
+__versionTime__ = "26 June 2011 10:53"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -604,10 +604,10 @@ class ParseResults(object):
def __setstate__(self,state):
self.__toklist = state[0]
- self.__tokdict, \
- par, \
- inAccumNames, \
- self.__name = state[1]
+ (self.__tokdict,
+ par,
+ inAccumNames,
+ self.__name) = state[1]
self.__accumNames = {}
self.__accumNames.update(inAccumNames)
if par is not None:
@@ -758,6 +758,9 @@ class ParserElement(object):
see L{I{__call__}<__call__>}.
"""
newself = self.copy()
+ if name.endswith("*"):
+ name = name[:-1]
+ listAllMatches=True
newself.resultsName = name
newself.modalResults = not listAllMatches
return newself
@@ -1314,10 +1317,7 @@ class ParserElement(object):
If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be
passed as C{True}.
"""
- if not name.endswith("*"):
- return self.setResultsName(name)
- else:
- return self.setResultsName(name[:-1], listAllMatches=True)
+ return self.setResultsName(name)
def suppress( self ):
"""Suppresses the output of this C{ParserElement}; useful to keep punctuation from
@@ -1452,12 +1452,10 @@ class Token(ParserElement):
"""Abstract C{ParserElement} subclass, for defining atomic matching patterns."""
def __init__( self ):
super(Token,self).__init__( savelist=False )
- #self.myException = ParseException("",0,"",self)
def setName(self, name):
s = super(Token,self).setName(name)
self.errmsg = "Expected " + self.name
- #s.myException.msg = self.errmsg
return s
@@ -1478,7 +1476,6 @@ class NoMatch(Token):
self.mayReturnEmpty = True
self.mayIndexError = False
self.errmsg = "Unmatchable token"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
exc = self.myException
@@ -1502,7 +1499,6 @@ class Literal(Token):
self.name = '"%s"' % _ustr(self.match)
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = False
- #self.myException.msg = self.errmsg
self.mayIndexError = False
# Performance tuning: this routine gets called a *lot*
@@ -1544,7 +1540,6 @@ class Keyword(Token):
self.name = '"%s"' % self.match
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = False
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.caseless = caseless
if caseless:
@@ -1592,7 +1587,6 @@ class CaselessLiteral(Literal):
self.returnString = matchString
self.name = "'%s'" % self.returnString
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if instring[ loc:loc+self.matchLen ].upper() == self.match:
@@ -1624,10 +1618,17 @@ class Word(Token):
defaults to the initial character set), and an optional minimum,
maximum, and/or exact length. The default value for C{min} is 1 (a
minimum value < 1 is not valid); the default values for C{max} and C{exact}
- are 0, meaning no maximum or exact length restriction.
+ are 0, meaning no maximum or exact length restriction. An optional
+ C{exclude} parameter can list characters that might be found in
+ the input C{bodyChars} string; useful to define a word of all printables
+ except for one or two characters, for instance.
"""
- def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False ):
+ def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False, excludeChars=None ):
super(Word,self).__init__()
+ if excludeChars:
+ initChars = ''.join([c for c in initChars if c not in excludeChars])
+ if bodyChars:
+ bodyChars = ''.join([c for c in bodyChars if c not in excludeChars])
self.initCharsOrig = initChars
self.initChars = set(initChars)
if bodyChars :
@@ -1655,7 +1656,6 @@ class Word(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.asKeyword = asKeyword
@@ -1781,7 +1781,6 @@ class Regex(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.mayReturnEmpty = True
@@ -1887,7 +1886,6 @@ class QuotedString(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.mayReturnEmpty = True
@@ -1959,7 +1957,6 @@ class CharsNotIn(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = ( self.minLen == 0 )
- #self.myException.msg = self.errmsg
self.mayIndexError = False
def parseImpl( self, instring, loc, doActions=True ):
@@ -2022,7 +2019,6 @@ class White(Token):
self.name = ("".join([White.whiteStrs[c] for c in self.matchWhite]))
self.mayReturnEmpty = True
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.minLen = min
@@ -2095,7 +2091,6 @@ class LineStart(_PositionToken):
super(LineStart,self).__init__()
self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
self.errmsg = "Expected start of line"
- #self.myException.msg = self.errmsg
def preParse( self, instring, loc ):
preloc = super(LineStart,self).preParse(instring,loc)
@@ -2120,7 +2115,6 @@ class LineEnd(_PositionToken):
super(LineEnd,self).__init__()
self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
self.errmsg = "Expected end of line"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc<len(instring):
@@ -2145,7 +2139,6 @@ class StringStart(_PositionToken):
def __init__( self ):
super(StringStart,self).__init__()
self.errmsg = "Expected start of text"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc != 0:
@@ -2163,7 +2156,6 @@ class StringEnd(_PositionToken):
def __init__( self ):
super(StringEnd,self).__init__()
self.errmsg = "Expected end of text"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc < len(instring):
@@ -2707,7 +2699,6 @@ class NotAny(ParseElementEnhance):
self.skipWhitespace = False # do NOT use self.leaveWhitespace(), don't want to propagate to exprs
self.mayReturnEmpty = True
self.errmsg = "Found unwanted token, "+_ustr(self.expr)
- #self.myException = ParseException("",0,self.errmsg,self)
def parseImpl( self, instring, loc, doActions=True ):
try:
@@ -2866,7 +2857,6 @@ class SkipTo(ParseElementEnhance):
else:
self.failOn = failOn
self.errmsg = "No match found for "+_ustr(self.expr)
- #self.myException = ParseException("",0,self.errmsg,self)
def parseImpl( self, instring, loc, doActions=True ):
startLoc = loc
@@ -3330,7 +3320,12 @@ def originalTextFor(expr, asString=True):
del t["_original_end"]
matchExpr.setParseAction(extractText)
return matchExpr
-
+
+def ungroup(expr):
+ """Helper to undo pyparsing's default grouping of And expressions, even
+ if all but one are non-empty."""
+ return TokenConverter(expr).setParseAction(lambda t:t[0])
+
# convenience constants for positional expressions
empty = Empty().setName("empty")
lineStart = LineStart().setName("lineStart")
diff --git a/pyparsing_py2.py b/pyparsing_py2.py
index dec506e..9be97dc 100644
--- a/pyparsing_py2.py
+++ b/pyparsing_py2.py
@@ -59,7 +59,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "1.5.6"
-__versionTime__ = "1 May 2011 23:41"
+__versionTime__ = "26 June 2011 10:53"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -604,10 +604,10 @@ class ParseResults(object):
def __setstate__(self,state):
self.__toklist = state[0]
- self.__tokdict, \
- par, \
- inAccumNames, \
- self.__name = state[1]
+ (self.__tokdict,
+ par,
+ inAccumNames,
+ self.__name) = state[1]
self.__accumNames = {}
self.__accumNames.update(inAccumNames)
if par is not None:
@@ -758,6 +758,9 @@ class ParserElement(object):
see L{I{__call__}<__call__>}.
"""
newself = self.copy()
+ if name.endswith("*"):
+ name = name[:-1]
+ listAllMatches=True
newself.resultsName = name
newself.modalResults = not listAllMatches
return newself
@@ -1314,10 +1317,7 @@ class ParserElement(object):
If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be
passed as C{True}.
"""
- if not name.endswith("*"):
- return self.setResultsName(name)
- else:
- return self.setResultsName(name[:-1], listAllMatches=True)
+ return self.setResultsName(name)
def suppress( self ):
"""Suppresses the output of this C{ParserElement}; useful to keep punctuation from
@@ -1452,12 +1452,10 @@ class Token(ParserElement):
"""Abstract C{ParserElement} subclass, for defining atomic matching patterns."""
def __init__( self ):
super(Token,self).__init__( savelist=False )
- #self.myException = ParseException("",0,"",self)
def setName(self, name):
s = super(Token,self).setName(name)
self.errmsg = "Expected " + self.name
- #s.myException.msg = self.errmsg
return s
@@ -1478,7 +1476,6 @@ class NoMatch(Token):
self.mayReturnEmpty = True
self.mayIndexError = False
self.errmsg = "Unmatchable token"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
exc = self.myException
@@ -1502,7 +1499,6 @@ class Literal(Token):
self.name = '"%s"' % _ustr(self.match)
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = False
- #self.myException.msg = self.errmsg
self.mayIndexError = False
# Performance tuning: this routine gets called a *lot*
@@ -1544,7 +1540,6 @@ class Keyword(Token):
self.name = '"%s"' % self.match
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = False
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.caseless = caseless
if caseless:
@@ -1592,7 +1587,6 @@ class CaselessLiteral(Literal):
self.returnString = matchString
self.name = "'%s'" % self.returnString
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if instring[ loc:loc+self.matchLen ].upper() == self.match:
@@ -1624,10 +1618,17 @@ class Word(Token):
defaults to the initial character set), and an optional minimum,
maximum, and/or exact length. The default value for C{min} is 1 (a
minimum value < 1 is not valid); the default values for C{max} and C{exact}
- are 0, meaning no maximum or exact length restriction.
+ are 0, meaning no maximum or exact length restriction. An optional
+ C{exclude} parameter can list characters that might be found in
+ the input C{bodyChars} string; useful to define a word of all printables
+ except for one or two characters, for instance.
"""
- def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False ):
+ def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False, excludeChars=None ):
super(Word,self).__init__()
+ if excludeChars:
+ initChars = ''.join([c for c in initChars if c not in excludeChars])
+ if bodyChars:
+ bodyChars = ''.join([c for c in bodyChars if c not in excludeChars])
self.initCharsOrig = initChars
self.initChars = set(initChars)
if bodyChars :
@@ -1655,7 +1656,6 @@ class Word(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.asKeyword = asKeyword
@@ -1781,7 +1781,6 @@ class Regex(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.mayReturnEmpty = True
@@ -1887,7 +1886,6 @@ class QuotedString(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.mayIndexError = False
self.mayReturnEmpty = True
@@ -1959,7 +1957,6 @@ class CharsNotIn(Token):
self.name = _ustr(self)
self.errmsg = "Expected " + self.name
self.mayReturnEmpty = ( self.minLen == 0 )
- #self.myException.msg = self.errmsg
self.mayIndexError = False
def parseImpl( self, instring, loc, doActions=True ):
@@ -2022,7 +2019,6 @@ class White(Token):
self.name = ("".join([White.whiteStrs[c] for c in self.matchWhite]))
self.mayReturnEmpty = True
self.errmsg = "Expected " + self.name
- #self.myException.msg = self.errmsg
self.minLen = min
@@ -2095,7 +2091,6 @@ class LineStart(_PositionToken):
super(LineStart,self).__init__()
self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
self.errmsg = "Expected start of line"
- #self.myException.msg = self.errmsg
def preParse( self, instring, loc ):
preloc = super(LineStart,self).preParse(instring,loc)
@@ -2120,7 +2115,6 @@ class LineEnd(_PositionToken):
super(LineEnd,self).__init__()
self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
self.errmsg = "Expected end of line"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc<len(instring):
@@ -2145,7 +2139,6 @@ class StringStart(_PositionToken):
def __init__( self ):
super(StringStart,self).__init__()
self.errmsg = "Expected start of text"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc != 0:
@@ -2163,7 +2156,6 @@ class StringEnd(_PositionToken):
def __init__( self ):
super(StringEnd,self).__init__()
self.errmsg = "Expected end of text"
- #self.myException.msg = self.errmsg
def parseImpl( self, instring, loc, doActions=True ):
if loc < len(instring):
@@ -2707,7 +2699,6 @@ class NotAny(ParseElementEnhance):
self.skipWhitespace = False # do NOT use self.leaveWhitespace(), don't want to propagate to exprs
self.mayReturnEmpty = True
self.errmsg = "Found unwanted token, "+_ustr(self.expr)
- #self.myException = ParseException("",0,self.errmsg,self)
def parseImpl( self, instring, loc, doActions=True ):
try:
@@ -2866,7 +2857,6 @@ class SkipTo(ParseElementEnhance):
else:
self.failOn = failOn
self.errmsg = "No match found for "+_ustr(self.expr)
- #self.myException = ParseException("",0,self.errmsg,self)
def parseImpl( self, instring, loc, doActions=True ):
startLoc = loc
@@ -3330,7 +3320,12 @@ def originalTextFor(expr, asString=True):
del t["_original_end"]
matchExpr.setParseAction(extractText)
return matchExpr
-
+
+def ungroup(expr):
+ """Helper to undo pyparsing's default grouping of And expressions, even
+ if all but one are non-empty."""
+ return TokenConverter(expr).setParseAction(lambda t:t[0])
+
# convenience constants for positional expressions
empty = Empty().setName("empty")
lineStart = LineStart().setName("lineStart")
diff --git a/pyparsing_py3.py b/pyparsing_py3.py
index 5f11b1d..88cdf64 100644
--- a/pyparsing_py3.py
+++ b/pyparsing_py3.py
@@ -59,7 +59,7 @@ The pyparsing module handles some of the problems that are typically vexing when
"""
__version__ = "1.5.6"
-__versionTime__ = "1 May 2011 23:41"
+__versionTime__ = "26 June 2011 10:53"
__author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
import string
@@ -105,8 +105,10 @@ def _xml_escape(data):
"""Escape &, <, >, ", ', etc. in a string of data."""
# ampersand must be replaced first
- for from_,to_ in zip('&><"\'', "amp gt lt quot apos".split()):
- data = data.replace(from_, '&'+to_+';')
+ from_symbols = '&><"\''
+ to_symbols = ['&'+s+';' for s in "amp gt lt quot apos".split()]
+ for from_,to_ in zip(from_symbols, to_symbols):
+ data = data.replace(from_, to_)
return data
class _Constants(object):
@@ -557,10 +559,10 @@ class ParseResults(object):
def __setstate__(self,state):
self.__toklist = state[0]
- self.__tokdict, \
- par, \
- inAccumNames, \
- self.__name = state[1]
+ (self.__tokdict,
+ par,
+ inAccumNames,
+ self.__name) = state[1]
self.__accumNames = {}
self.__accumNames.update(inAccumNames)
if par is not None:
@@ -699,6 +701,9 @@ class ParserElement(object):
see L{I{__call__}<__call__>}.
"""
newself = self.copy()
+ if name.endswith("*"):
+ name = name[:-1]
+ listAllMatches=True
newself.resultsName = name
newself.modalResults = not listAllMatches
return newself
@@ -1246,10 +1251,7 @@ class ParserElement(object):
If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be
passed as C{True}.
"""
- if not name.endswith("*"):
- return self.setResultsName(name)
- else:
- return self.setResultsName(name[:-1], listAllMatches=True)
+ return self.setResultsName(name)
def suppress( self ):
"""Suppresses the output of this C{ParserElement}; useful to keep punctuation from
@@ -1523,10 +1525,17 @@ class Word(Token):
defaults to the initial character set), and an optional minimum,
maximum, and/or exact length. The default value for C{min} is 1 (a
minimum value < 1 is not valid); the default values for C{max} and C{exact}
- are 0, meaning no maximum or exact length restriction.
+ are 0, meaning no maximum or exact length restriction. An optional
+ C{exclude} parameter can list characters that might be found in
+ the input C{bodyChars} string; useful to define a word of all printables
+ except for one or two characters, for instance.
"""
- def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False ):
+ def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False, excludeChars=None ):
super(Word,self).__init__()
+ if excludeChars:
+ initChars = ''.join([c for c in initChars if c not in excludeChars])
+ if bodyChars:
+ bodyChars = ''.join([c for c in bodyChars if c not in excludeChars])
self.initCharsOrig = initChars
self.initChars = set(initChars)
if bodyChars :
@@ -3149,7 +3158,12 @@ def originalTextFor(expr, asString=True):
del t["_original_end"]
matchExpr.setParseAction(extractText)
return matchExpr
-
+
+def ungroup(expr):
+ """Helper to undo pyparsing's default grouping of And expressions, even
+ if all but one are non-empty."""
+ return TokenConverter(expr).setParseAction(lambda t:t[0])
+
# convenience constants for positional expressions
empty = Empty().setName("empty")
lineStart = LineStart().setName("lineStart")
@@ -3266,7 +3280,7 @@ def _makeTags(tagStr, xml):
else:
printablesLessRAbrack = "".join( [ c for c in printables if c not in ">" ] )
tagAttrValue = quotedString.copy().setParseAction( removeQuotes ) | Word(printablesLessRAbrack)
- openTag = Suppress("<") + tagStr + \
+ openTag = Suppress("<") + tagStr("tag") + \
Dict(ZeroOrMore(Group( tagAttrName.setParseAction(downcaseTokens) + \
Optional( Suppress("=") + tagAttrValue ) ))) + \
Optional("/",default=[False]).setResultsName("empty").setParseAction(lambda s,l,t:t[0]=='/') + Suppress(">")