annotate abc2xml/pyparsing.py @ 1066:cd82291954e3 build-default-387

Added Lilliburleo.
author Jim Hague <jim.hague@acm.org>
date Thu, 07 Oct 2021 20:02:25 +0100
parents 4fab69a1027d
children b1dbb76f4eb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1 # module pyparsing.py
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3 # Copyright (c) 2003-2011 Paul T. McGuire
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
4 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
5 # Permission is hereby granted, free of charge, to any person obtaining
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
6 # a copy of this software and associated documentation files (the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
7 # "Software"), to deal in the Software without restriction, including
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
8 # without limitation the rights to use, copy, modify, merge, publish,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
9 # distribute, sublicense, and/or sell copies of the Software, and to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
10 # permit persons to whom the Software is furnished to do so, subject to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
11 # the following conditions:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
12 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
13 # The above copyright notice and this permission notice shall be
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
14 # included in all copies or substantial portions of the Software.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
15 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
23 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
24 #from __future__ import generators
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
25
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
26 __doc__ = \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
27 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
28 pyparsing module - Classes and methods to define and execute parsing grammars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
29
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
30 The pyparsing module is an alternative approach to creating and executing simple grammars,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
31 vs. the traditional lex/yacc approach, or the use of regular expressions. With pyparsing, you
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
32 don't need to learn a new syntax for defining grammars or matching expressions - the parsing module
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
33 provides a library of classes that you use to construct the grammar directly in Python.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
35 Here is a program to parse "Hello, World!" (or any greeting of the form C{"<salutation>, <addressee>!"})::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
37 from pyparsing import Word, alphas
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
38
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
39 # define grammar of a greeting
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
40 greet = Word( alphas ) + "," + Word( alphas ) + "!"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
41
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
42 hello = "Hello, World!"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
43 print hello, "->", greet.parseString( hello )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
44
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
45 The program outputs the following::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
46
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
47 Hello, World! -> ['Hello', ',', 'World', '!']
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
48
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
49 The Python representation of the grammar is quite readable, owing to the self-explanatory
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
50 class names, and the use of '+', '|' and '^' operators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
51
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
52 The parsed results returned from C{parseString()} can be accessed as a nested list, a dictionary, or an
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
53 object with named attributes.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
54
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
55 The pyparsing module handles some of the problems that are typically vexing when writing text parsers:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
56 - extra or missing whitespace (the above program will also handle "Hello,World!", "Hello , World !", etc.)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
57 - quoted strings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
58 - embedded comments
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
59 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
60
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
61 __version__ = "1.5.6"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62 __versionTime__ = "26 June 2011 10:53"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
63 __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
65 import string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
66 from weakref import ref as wkref
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
67 import copy
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
68 import sys
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
69 import warnings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
70 import re
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
71 import sre_constants
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
72 #~ sys.stderr.write( "testing pyparsing module, version %s, %s\n" % (__version__,__versionTime__ ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
73
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
74 __all__ = [
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
75 'And', 'CaselessKeyword', 'CaselessLiteral', 'CharsNotIn', 'Combine', 'Dict', 'Each', 'Empty',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
76 'FollowedBy', 'Forward', 'GoToColumn', 'Group', 'Keyword', 'LineEnd', 'LineStart', 'Literal',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
77 'MatchFirst', 'NoMatch', 'NotAny', 'OneOrMore', 'OnlyOnce', 'Optional', 'Or',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
78 'ParseBaseException', 'ParseElementEnhance', 'ParseException', 'ParseExpression', 'ParseFatalException',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
79 'ParseResults', 'ParseSyntaxException', 'ParserElement', 'QuotedString', 'RecursiveGrammarException',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
80 'Regex', 'SkipTo', 'StringEnd', 'StringStart', 'Suppress', 'Token', 'TokenConverter', 'Upcase',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
81 'White', 'Word', 'WordEnd', 'WordStart', 'ZeroOrMore',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
82 'alphanums', 'alphas', 'alphas8bit', 'anyCloseTag', 'anyOpenTag', 'cStyleComment', 'col',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
83 'commaSeparatedList', 'commonHTMLEntity', 'countedArray', 'cppStyleComment', 'dblQuotedString',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
84 'dblSlashComment', 'delimitedList', 'dictOf', 'downcaseTokens', 'empty', 'getTokensEndLoc', 'hexnums',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
85 'htmlComment', 'javaStyleComment', 'keepOriginalText', 'line', 'lineEnd', 'lineStart', 'lineno',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
86 'makeHTMLTags', 'makeXMLTags', 'matchOnlyAtCol', 'matchPreviousExpr', 'matchPreviousLiteral',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
87 'nestedExpr', 'nullDebugAction', 'nums', 'oneOf', 'opAssoc', 'operatorPrecedence', 'printables',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
88 'punc8bit', 'pythonStyleComment', 'quotedString', 'removeQuotes', 'replaceHTMLEntity',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
89 'replaceWith', 'restOfLine', 'sglQuotedString', 'srange', 'stringEnd',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
90 'stringStart', 'traceParseAction', 'unicodeString', 'upcaseTokens', 'withAttribute',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
91 'indentedBlock', 'originalTextFor',
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
92 ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
93
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
94 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
95 Detect if we are running version 3.X and make appropriate changes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
96 Robert A. Clark
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
97 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
98 _PY3K = sys.version_info[0] > 2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
99 if _PY3K:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
100 _MAX_INT = sys.maxsize
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
101 basestring = str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
102 unichr = chr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
103 _ustr = str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
104 alphas = string.ascii_lowercase + string.ascii_uppercase
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
105 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
106 _MAX_INT = sys.maxint
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
107 range = xrange
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
108 set = lambda s : dict( [(c,0) for c in s] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
109 alphas = string.lowercase + string.uppercase
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
110
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
111 def _ustr(obj):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
112 """Drop-in replacement for str(obj) that tries to be Unicode friendly. It first tries
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
113 str(obj). If that fails with a UnicodeEncodeError, then it tries unicode(obj). It
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
114 then < returns the unicode object | encodes it with the default encoding | ... >.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
115 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
116 if isinstance(obj,unicode):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
117 return obj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
118
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
119 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
120 # If this works, then _ustr(obj) has the same behaviour as str(obj), so
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
121 # it won't break any existing code.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
122 return str(obj)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
123
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
124 except UnicodeEncodeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
125 # The Python docs (http://docs.python.org/ref/customization.html#l2h-182)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
126 # state that "The return value must be a string object". However, does a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
127 # unicode object (being a subclass of basestring) count as a "string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
128 # object"?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
129 # If so, then return a unicode object:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
130 return unicode(obj)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
131 # Else encode it... but how? There are many choices... :)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
132 # Replace unprintables with escape codes?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
133 #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
134 # Replace unprintables with question marks?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
135 #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
136 # ...
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
137
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
138 alphas = string.lowercase + string.uppercase
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
139
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
140 # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
141 singleArgBuiltins = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
142 import __builtin__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
143 for fname in "sum len enumerate sorted reversed list tuple set any all".split():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
144 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
145 singleArgBuiltins.append(getattr(__builtin__,fname))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
146 except AttributeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
147 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
148
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
149 def _xml_escape(data):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
150 """Escape &, <, >, ", ', etc. in a string of data."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
151
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
152 # ampersand must be replaced first
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
153 from_symbols = '&><"\''
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
154 to_symbols = ['&'+s+';' for s in "amp gt lt quot apos".split()]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
155 for from_,to_ in zip(from_symbols, to_symbols):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
156 data = data.replace(from_, to_)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
157 return data
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
158
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
159 class _Constants(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
160 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
161
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
162 nums = string.digits
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
163 hexnums = nums + "ABCDEFabcdef"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
164 alphanums = alphas + nums
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
165 _bslash = chr(92)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
166 printables = "".join( [ c for c in string.printable if c not in string.whitespace ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
167
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
168 class ParseBaseException(Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
169 """base exception class for all parsing runtime exceptions"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
170 # Performance tuning: we construct a *lot* of these, so keep this
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
171 # constructor as small and fast as possible
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
172 def __init__( self, pstr, loc=0, msg=None, elem=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
173 self.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
174 if msg is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
175 self.msg = pstr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
176 self.pstr = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
177 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
178 self.msg = msg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
179 self.pstr = pstr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
180 self.parserElement = elem
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
181
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
182 def __getattr__( self, aname ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
183 """supported attributes by name are:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
184 - lineno - returns the line number of the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
185 - col - returns the column number of the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
186 - line - returns the line containing the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
187 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
188 if( aname == "lineno" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
189 return lineno( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
190 elif( aname in ("col", "column") ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
191 return col( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
192 elif( aname == "line" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
193 return line( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
194 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
195 raise AttributeError(aname)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
196
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
197 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
198 return "%s (at char %d), (line:%d, col:%d)" % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
199 ( self.msg, self.loc, self.lineno, self.column )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
200 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
201 return _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
202 def markInputline( self, markerString = ">!<" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
203 """Extracts the exception line from the input string, and marks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
204 the location of the exception with a special symbol.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
205 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
206 line_str = self.line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
207 line_column = self.column - 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
208 if markerString:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
209 line_str = "".join( [line_str[:line_column],
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
210 markerString, line_str[line_column:]])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
211 return line_str.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
212 def __dir__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
213 return "loc msg pstr parserElement lineno col line " \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
214 "markInputLine __str__ __repr__".split()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
215
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
216 class ParseException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
217 """exception thrown when parse expressions don't match class;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
218 supported attributes by name are:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
219 - lineno - returns the line number of the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
220 - col - returns the column number of the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
221 - line - returns the line containing the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
222 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
223 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
224
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
225 class ParseFatalException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
226 """user-throwable exception thrown when inconsistent parse content
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
227 is found; stops all parsing immediately"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
228 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
229
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
230 class ParseSyntaxException(ParseFatalException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
231 """just like C{ParseFatalException}, but thrown internally when an
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
232 C{ErrorStop} ('-' operator) indicates that parsing is to stop immediately because
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
233 an unbacktrackable syntax error has been found"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
234 def __init__(self, pe):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
235 super(ParseSyntaxException, self).__init__(
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
236 pe.pstr, pe.loc, pe.msg, pe.parserElement)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
237
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
238 #~ class ReparseException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
239 #~ """Experimental class - parse actions can raise this exception to cause
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
240 #~ pyparsing to reparse the input string:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
241 #~ - with a modified input string, and/or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
242 #~ - with a modified start location
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
243 #~ Set the values of the ReparseException in the constructor, and raise the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
244 #~ exception in a parse action to cause pyparsing to use the new string/location.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
245 #~ Setting the values as None causes no change to be made.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
246 #~ """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
247 #~ def __init_( self, newstring, restartLoc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
248 #~ self.newParseText = newstring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
249 #~ self.reparseLoc = restartLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
250
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
251 class RecursiveGrammarException(Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
252 """exception thrown by C{validate()} if the grammar could be improperly recursive"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
253 def __init__( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
254 self.parseElementTrace = parseElementList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
255
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
256 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
257 return "RecursiveGrammarException: %s" % self.parseElementTrace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
258
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
259 class _ParseResultsWithOffset(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
260 def __init__(self,p1,p2):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
261 self.tup = (p1,p2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
262 def __getitem__(self,i):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
263 return self.tup[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
264 def __repr__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
265 return repr(self.tup)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
266 def setOffset(self,i):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
267 self.tup = (self.tup[0],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
268
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
269 class ParseResults(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
270 """Structured parse results, to provide multiple means of access to the parsed data:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
271 - as a list (C{len(results)})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
272 - by list index (C{results[0], results[1]}, etc.)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
273 - by attribute (C{results.<resultsName>})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
274 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
275 #~ __slots__ = ( "__toklist", "__tokdict", "__doinit", "__name", "__parent", "__accumNames", "__weakref__" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
276 def __new__(cls, toklist, name=None, asList=True, modal=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
277 if isinstance(toklist, cls):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
278 return toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
279 retobj = object.__new__(cls)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
280 retobj.__doinit = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
281 return retobj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
282
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
283 # Performance tuning: we construct a *lot* of these, so keep this
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
284 # constructor as small and fast as possible
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
285 def __init__( self, toklist, name=None, asList=True, modal=True, isinstance=isinstance ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
286 if self.__doinit:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
287 self.__doinit = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
288 self.__name = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
289 self.__parent = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
290 self.__accumNames = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
291 if isinstance(toklist, list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
292 self.__toklist = toklist[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
293 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
294 self.__toklist = [toklist]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
295 self.__tokdict = dict()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
296
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
297 if name is not None and name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
298 if not modal:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
299 self.__accumNames[name] = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
300 if isinstance(name,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
301 name = _ustr(name) # will always return a str, but use _ustr for consistency
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
302 self.__name = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
303 if not toklist in (None,'',[]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
304 if isinstance(toklist,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
305 toklist = [ toklist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
306 if asList:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
307 if isinstance(toklist,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
308 self[name] = _ParseResultsWithOffset(toklist.copy(),0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
309 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
310 self[name] = _ParseResultsWithOffset(ParseResults(toklist[0]),0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
311 self[name].__name = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
312 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
313 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
314 self[name] = toklist[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
315 except (KeyError,TypeError,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
316 self[name] = toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
317
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
318 def __getitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
319 if isinstance( i, (int,slice) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
320 return self.__toklist[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
321 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
322 if i not in self.__accumNames:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
323 return self.__tokdict[i][-1][0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
324 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
325 return ParseResults([ v[0] for v in self.__tokdict[i] ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
326
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
327 def __setitem__( self, k, v, isinstance=isinstance ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
328 if isinstance(v,_ParseResultsWithOffset):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
329 self.__tokdict[k] = self.__tokdict.get(k,list()) + [v]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
330 sub = v[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
331 elif isinstance(k,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
332 self.__toklist[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
333 sub = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
334 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
335 self.__tokdict[k] = self.__tokdict.get(k,list()) + [_ParseResultsWithOffset(v,0)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
336 sub = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
337 if isinstance(sub,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
338 sub.__parent = wkref(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
339
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
340 def __delitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
341 if isinstance(i,(int,slice)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
342 mylen = len( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
343 del self.__toklist[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
344
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
345 # convert int to slice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
346 if isinstance(i, int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
347 if i < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
348 i += mylen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
349 i = slice(i, i+1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
350 # get removed indices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
351 removed = list(range(*i.indices(mylen)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
352 removed.reverse()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
353 # fixup indices in token dictionary
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
354 for name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
355 occurrences = self.__tokdict[name]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
356 for j in removed:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
357 for k, (value, position) in enumerate(occurrences):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
358 occurrences[k] = _ParseResultsWithOffset(value, position - (position > j))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
359 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
360 del self.__tokdict[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
361
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
362 def __contains__( self, k ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
363 return k in self.__tokdict
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
364
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
365 def __len__( self ): return len( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
366 def __bool__(self): return len( self.__toklist ) > 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
367 __nonzero__ = __bool__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
368 def __iter__( self ): return iter( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
369 def __reversed__( self ): return iter( self.__toklist[::-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
370 def keys( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
371 """Returns all named result keys."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
372 return self.__tokdict.keys()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
373
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
374 def pop( self, index=-1 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
375 """Removes and returns item at specified index (default=last).
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
376 Will work with either numeric indices or dict-key indicies."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
377 ret = self[index]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
378 del self[index]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
379 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
380
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
381 def get(self, key, defaultValue=None):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
382 """Returns named result matching the given key, or if there is no
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
383 such name, then returns the given C{defaultValue} or C{None} if no
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
384 C{defaultValue} is specified."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
385 if key in self:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
386 return self[key]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
387 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
388 return defaultValue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
389
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
390 def insert( self, index, insStr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
391 """Inserts new element at location index in the list of parsed tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
392 self.__toklist.insert(index, insStr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
393 # fixup indices in token dictionary
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
394 for name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
395 occurrences = self.__tokdict[name]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
396 for k, (value, position) in enumerate(occurrences):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
397 occurrences[k] = _ParseResultsWithOffset(value, position + (position > index))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
398
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
399 def items( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
400 """Returns all named result keys and values as a list of tuples."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
401 return [(k,self[k]) for k in self.__tokdict]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
402
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
403 def values( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
404 """Returns all named result values."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
405 return [ v[-1][0] for v in self.__tokdict.values() ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
406
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
407 def __getattr__( self, name ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
408 if True: #name not in self.__slots__:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
409 if name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
410 if name not in self.__accumNames:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
411 return self.__tokdict[name][-1][0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
412 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
413 return ParseResults([ v[0] for v in self.__tokdict[name] ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
414 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
415 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
416 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
417
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
418 def __add__( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
419 ret = self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
420 ret += other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
421 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
422
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
423 def __iadd__( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
424 if other.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
425 offset = len(self.__toklist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
426 addoffset = ( lambda a: (a<0 and offset) or (a+offset) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
427 otheritems = other.__tokdict.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
428 otherdictitems = [(k, _ParseResultsWithOffset(v[0],addoffset(v[1])) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
429 for (k,vlist) in otheritems for v in vlist]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
430 for k,v in otherdictitems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
431 self[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
432 if isinstance(v[0],ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
433 v[0].__parent = wkref(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
434
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
435 self.__toklist += other.__toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
436 self.__accumNames.update( other.__accumNames )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
437 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
438
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
439 def __radd__(self, other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
440 if isinstance(other,int) and other == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
441 return self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
442
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
443 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
444 return "(%s, %s)" % ( repr( self.__toklist ), repr( self.__tokdict ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
445
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
446 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
447 out = "["
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
448 sep = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
449 for i in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
450 if isinstance(i, ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
451 out += sep + _ustr(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
452 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
453 out += sep + repr(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
454 sep = ", "
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
455 out += "]"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
456 return out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
457
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
458 def _asStringList( self, sep='' ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
459 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
460 for item in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
461 if out and sep:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
462 out.append(sep)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
463 if isinstance( item, ParseResults ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
464 out += item._asStringList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
465 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
466 out.append( _ustr(item) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
467 return out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
468
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
469 def asList( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
470 """Returns the parse results as a nested list of matching tokens, all converted to strings."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
471 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
472 for res in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
473 if isinstance(res,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
474 out.append( res.asList() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
475 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
476 out.append( res )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
477 return out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
478
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
479 def asDict( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
480 """Returns the named parse results as dictionary."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
481 return dict( self.items() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
482
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
483 def copy( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
484 """Returns a new copy of a C{ParseResults} object."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
485 ret = ParseResults( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
486 ret.__tokdict = self.__tokdict.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
487 ret.__parent = self.__parent
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
488 ret.__accumNames.update( self.__accumNames )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
489 ret.__name = self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
490 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
491
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
492 def asXML( self, doctag=None, namedItemsOnly=False, indent="", formatted=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
493 """Returns the parse results as XML. Tags are created for tokens and lists that have defined results names."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
494 nl = "\n"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
495 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
496 namedItems = dict( [ (v[1],k) for (k,vlist) in self.__tokdict.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
497 for v in vlist ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
498 nextLevelIndent = indent + " "
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
499
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
500 # collapse out indents if formatting is not desired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
501 if not formatted:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
502 indent = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
503 nextLevelIndent = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
504 nl = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
505
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
506 selfTag = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
507 if doctag is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
508 selfTag = doctag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
509 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
510 if self.__name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
511 selfTag = self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
512
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
513 if not selfTag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
514 if namedItemsOnly:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
515 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
516 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
517 selfTag = "ITEM"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
518
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
519 out += [ nl, indent, "<", selfTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
520
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
521 worklist = self.__toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
522 for i,res in enumerate(worklist):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
523 if isinstance(res,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
524 if i in namedItems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
525 out += [ res.asXML(namedItems[i],
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
526 namedItemsOnly and doctag is None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
527 nextLevelIndent,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
528 formatted)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
529 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
530 out += [ res.asXML(None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
531 namedItemsOnly and doctag is None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
532 nextLevelIndent,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
533 formatted)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
534 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
535 # individual token, see if there is a name for it
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
536 resTag = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
537 if i in namedItems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
538 resTag = namedItems[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
539 if not resTag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
540 if namedItemsOnly:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
541 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
542 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
543 resTag = "ITEM"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
544 xmlBodyText = _xml_escape(_ustr(res))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
545 out += [ nl, nextLevelIndent, "<", resTag, ">",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
546 xmlBodyText,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
547 "</", resTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
548
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
549 out += [ nl, indent, "</", selfTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
550 return "".join(out)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
551
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
552 def __lookup(self,sub):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
553 for k,vlist in self.__tokdict.items():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
554 for v,loc in vlist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
555 if sub is v:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
556 return k
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
557 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
558
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
559 def getName(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
560 """Returns the results name for this token expression."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
561 if self.__name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
562 return self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
563 elif self.__parent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
564 par = self.__parent()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
565 if par:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
566 return par.__lookup(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
567 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
568 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
569 elif (len(self) == 1 and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
570 len(self.__tokdict) == 1 and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
571 self.__tokdict.values()[0][0][1] in (0,-1)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
572 return self.__tokdict.keys()[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
573 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
574 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
575
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
576 def dump(self,indent='',depth=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
577 """Diagnostic method for listing out the contents of a C{ParseResults}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
578 Accepts an optional C{indent} argument so that this string can be embedded
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
579 in a nested display of other data."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
580 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
581 out.append( indent+_ustr(self.asList()) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
582 keys = self.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
583 keys.sort()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
584 for k,v in keys:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
585 if out:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
586 out.append('\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
587 out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
588 if isinstance(v,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
589 if v.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
590 out.append( v.dump(indent,depth+1) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
591 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
592 out.append(_ustr(v))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
593 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
594 out.append(_ustr(v))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
595 return "".join(out)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
596
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
597 # add support for pickle protocol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
598 def __getstate__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
599 return ( self.__toklist,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
600 ( self.__tokdict.copy(),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
601 self.__parent is not None and self.__parent() or None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
602 self.__accumNames,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
603 self.__name ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
604
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
605 def __setstate__(self,state):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
606 self.__toklist = state[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
607 (self.__tokdict,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
608 par,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
609 inAccumNames,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
610 self.__name) = state[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
611 self.__accumNames = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
612 self.__accumNames.update(inAccumNames)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
613 if par is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
614 self.__parent = wkref(par)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
615 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
616 self.__parent = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
617
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
618 def __dir__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
619 return dir(super(ParseResults,self)) + self.keys()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
620
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
621 def col (loc,strg):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
622 """Returns current column within a string, counting newlines as line separators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
623 The first column is number 1.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
624
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
625 Note: the default parsing behavior is to expand tabs in the input string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
626 before starting the parsing process. See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
627 on parsing strings containing <TAB>s, and suggested methods to maintain a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
628 consistent view of the parsed string, the parse location, and line and column
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
629 positions within the parsed string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
630 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
631 return (loc<len(strg) and strg[loc] == '\n') and 1 or loc - strg.rfind("\n", 0, loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
632
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
633 def lineno(loc,strg):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
634 """Returns current line number within a string, counting newlines as line separators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
635 The first line is number 1.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
636
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
637 Note: the default parsing behavior is to expand tabs in the input string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
638 before starting the parsing process. See L{I{ParserElement.parseString}<ParserElement.parseString>} for more information
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
639 on parsing strings containing <TAB>s, and suggested methods to maintain a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
640 consistent view of the parsed string, the parse location, and line and column
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
641 positions within the parsed string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
642 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
643 return strg.count("\n",0,loc) + 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
644
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
645 def line( loc, strg ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
646 """Returns the line of text containing loc within a string, counting newlines as line separators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
647 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
648 lastCR = strg.rfind("\n", 0, loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
649 nextCR = strg.find("\n", loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
650 if nextCR >= 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
651 return strg[lastCR+1:nextCR]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
652 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
653 return strg[lastCR+1:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
654
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
655 def _defaultStartDebugAction( instring, loc, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
656 print ("Match " + _ustr(expr) + " at loc " + _ustr(loc) + "(%d,%d)" % ( lineno(loc,instring), col(loc,instring) ))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
657
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
658 def _defaultSuccessDebugAction( instring, startloc, endloc, expr, toks ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
659 print ("Matched " + _ustr(expr) + " -> " + str(toks.asList()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
660
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
661 def _defaultExceptionDebugAction( instring, loc, expr, exc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
662 print ("Exception raised:" + _ustr(exc))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
663
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
664 def nullDebugAction(*args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
665 """'Do-nothing' debug action, to suppress debugging output during parsing."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
666 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
667
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
668 'decorator to trim function calls to match the arity of the target'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
669 if not _PY3K:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
670 def _trim_arity(func, maxargs=2):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
671 limit = [0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
672 def wrapper(*args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
673 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
674 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
675 return func(*args[limit[0]:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
676 except TypeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
677 if limit[0] <= maxargs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
678 limit[0] += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
679 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
680 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
681 return wrapper
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
682 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
683 def _trim_arity(func, maxargs=2):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
684 limit = maxargs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
685 def wrapper(*args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
686 #~ nonlocal limit
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
687 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
688 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
689 return func(*args[limit:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
690 except TypeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
691 if limit:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
692 limit -= 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
693 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
694 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
695 return wrapper
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
696
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
697 class ParserElement(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
698 """Abstract base level parser element class."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
699 DEFAULT_WHITE_CHARS = " \n\t\r"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
700 verbose_stacktrace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
701
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
702 def setDefaultWhitespaceChars( chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
703 """Overrides the default whitespace chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
704 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
705 ParserElement.DEFAULT_WHITE_CHARS = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
706 setDefaultWhitespaceChars = staticmethod(setDefaultWhitespaceChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
707
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
708 def __init__( self, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
709 self.parseAction = list()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
710 self.failAction = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
711 #~ self.name = "<unknown>" # don't define self.name, let subclasses try/except upcall
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
712 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
713 self.resultsName = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
714 self.saveAsList = savelist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
715 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
716 self.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
717 self.copyDefaultWhiteChars = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
718 self.mayReturnEmpty = False # used when checking for left-recursion
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
719 self.keepTabs = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
720 self.ignoreExprs = list()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
721 self.debug = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
722 self.streamlined = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
723 self.mayIndexError = True # used to optimize exception handling for subclasses that don't advance parse index
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
724 self.errmsg = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
725 self.modalResults = True # used to mark results names as modal (report only last) or cumulative (list all)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
726 self.debugActions = ( None, None, None ) #custom debug actions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
727 self.re = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
728 self.callPreparse = True # used to avoid redundant calls to preParse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
729 self.callDuringTry = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
730
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
731 def copy( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
732 """Make a copy of this C{ParserElement}. Useful for defining different parse actions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
733 for the same parsing pattern, using copies of the original parse element."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
734 cpy = copy.copy( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
735 cpy.parseAction = self.parseAction[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
736 cpy.ignoreExprs = self.ignoreExprs[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
737 if self.copyDefaultWhiteChars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
738 cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
739 return cpy
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
740
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
741 def setName( self, name ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
742 """Define name for this expression, for use in debugging."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
743 self.name = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
744 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
745 if hasattr(self,"exception"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
746 self.exception.msg = self.errmsg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
747 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
748
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
749 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
750 """Define name for referencing matching tokens as a nested attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
751 of the returned parse results.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
752 NOTE: this returns a *copy* of the original C{ParserElement} object;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
753 this is so that the client can define a basic element, such as an
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
754 integer, and reference it in multiple places with different names.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
755
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
756 You can also set results names using the abbreviated syntax,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
757 C{expr("name")} in place of C{expr.setResultsName("name")} -
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
758 see L{I{__call__}<__call__>}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
759 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
760 newself = self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
761 if name.endswith("*"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
762 name = name[:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
763 listAllMatches=True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
764 newself.resultsName = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
765 newself.modalResults = not listAllMatches
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
766 return newself
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
767
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
768 def setBreak(self,breakFlag = True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
769 """Method to invoke the Python pdb debugger when this element is
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
770 about to be parsed. Set C{breakFlag} to True to enable, False to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
771 disable.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
772 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
773 if breakFlag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
774 _parseMethod = self._parse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
775 def breaker(instring, loc, doActions=True, callPreParse=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
776 import pdb
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
777 pdb.set_trace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
778 return _parseMethod( instring, loc, doActions, callPreParse )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
779 breaker._originalParseMethod = _parseMethod
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
780 self._parse = breaker
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
781 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
782 if hasattr(self._parse,"_originalParseMethod"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
783 self._parse = self._parse._originalParseMethod
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
784 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
785
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
786 def setParseAction( self, *fns, **kwargs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
787 """Define action to perform when successfully matching parse element definition.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
788 Parse action fn is a callable method with 0-3 arguments, called as C{fn(s,loc,toks)},
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
789 C{fn(loc,toks)}, C{fn(toks)}, or just C{fn()}, where:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
790 - s = the original string being parsed (see note below)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
791 - loc = the location of the matching substring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
792 - toks = a list of the matched tokens, packaged as a ParseResults object
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
793 If the functions in fns modify the tokens, they can return them as the return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
794 value from fn, and the modified list of tokens will replace the original.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
795 Otherwise, fn does not need to return any value.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
796
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
797 Note: the default parsing behavior is to expand tabs in the input string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
798 before starting the parsing process. See L{I{parseString}<parseString>} for more information
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
799 on parsing strings containing <TAB>s, and suggested methods to maintain a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
800 consistent view of the parsed string, the parse location, and line and column
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
801 positions within the parsed string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
802 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
803 self.parseAction = list(map(_trim_arity, list(fns)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
804 self.callDuringTry = ("callDuringTry" in kwargs and kwargs["callDuringTry"])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
805 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
806
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
807 def addParseAction( self, *fns, **kwargs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
808 """Add parse action to expression's list of parse actions. See L{I{setParseAction}<setParseAction>}."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
809 self.parseAction += list(map(_trim_arity, list(fns)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
810 self.callDuringTry = self.callDuringTry or ("callDuringTry" in kwargs and kwargs["callDuringTry"])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
811 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
812
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
813 def setFailAction( self, fn ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
814 """Define action to perform if parsing fails at this expression.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
815 Fail acton fn is a callable function that takes the arguments
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
816 C{fn(s,loc,expr,err)} where:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
817 - s = string being parsed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
818 - loc = location where expression match was attempted and failed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
819 - expr = the parse expression that failed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
820 - err = the exception thrown
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
821 The function returns no value. It may throw C{ParseFatalException}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
822 if it is desired to stop parsing immediately."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
823 self.failAction = fn
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
824 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
825
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
826 def _skipIgnorables( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
827 exprsFound = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
828 while exprsFound:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
829 exprsFound = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
830 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
831 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
832 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
833 loc,dummy = e._parse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
834 exprsFound = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
835 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
836 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
837 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
838
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
839 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
840 if self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
841 loc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
842
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
843 if self.skipWhitespace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
844 wt = self.whiteChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
845 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
846 while loc < instrlen and instring[loc] in wt:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
847 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
848
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
849 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
850
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
851 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
852 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
853
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
854 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
855 return tokenlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
856
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
857 #~ @profile
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
858 def _parseNoCache( self, instring, loc, doActions=True, callPreParse=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
859 debugging = ( self.debug ) #and doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
860
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
861 if debugging or self.failAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
862 #~ print ("Match",self,"at loc",loc,"(%d,%d)" % ( lineno(loc,instring), col(loc,instring) ))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
863 if (self.debugActions[0] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
864 self.debugActions[0]( instring, loc, self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
865 if callPreParse and self.callPreparse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
866 preloc = self.preParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
867 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
868 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
869 tokensStart = preloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
870 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
871 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
872 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
873 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
874 raise ParseException( instring, len(instring), self.errmsg, self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
875 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
876 #~ print ("Exception raised:", err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
877 err = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
878 if self.debugActions[2]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
879 err = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
880 self.debugActions[2]( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
881 if self.failAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
882 if err is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
883 err = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
884 self.failAction( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
885 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
886 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
887 if callPreParse and self.callPreparse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
888 preloc = self.preParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
889 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
890 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
891 tokensStart = preloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
892 if self.mayIndexError or loc >= len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
893 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
894 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
895 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
896 raise ParseException( instring, len(instring), self.errmsg, self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
897 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
898 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
899
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
900 tokens = self.postParse( instring, loc, tokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
901
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
902 retTokens = ParseResults( tokens, self.resultsName, asList=self.saveAsList, modal=self.modalResults )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
903 if self.parseAction and (doActions or self.callDuringTry):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
904 if debugging:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
905 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
906 for fn in self.parseAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
907 tokens = fn( instring, tokensStart, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
908 if tokens is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
909 retTokens = ParseResults( tokens,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
910 self.resultsName,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
911 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
912 modal=self.modalResults )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
913 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
914 #~ print "Exception raised in user parse action:", err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
915 if (self.debugActions[2] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
916 err = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
917 self.debugActions[2]( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
918 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
919 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
920 for fn in self.parseAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
921 tokens = fn( instring, tokensStart, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
922 if tokens is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
923 retTokens = ParseResults( tokens,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
924 self.resultsName,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
925 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
926 modal=self.modalResults )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
927
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
928 if debugging:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
929 #~ print ("Matched",self,"->",retTokens.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
930 if (self.debugActions[1] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
931 self.debugActions[1]( instring, tokensStart, loc, self, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
932
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
933 return loc, retTokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
934
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
935 def tryParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
936 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
937 return self._parse( instring, loc, doActions=False )[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
938 except ParseFatalException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
939 raise ParseException( instring, loc, self.errmsg, self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
940
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
941 # this method gets repeatedly called during backtracking with the same arguments -
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
942 # we can cache these arguments and save ourselves the trouble of re-parsing the contained expression
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
943 def _parseCache( self, instring, loc, doActions=True, callPreParse=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
944 lookup = (self,instring,loc,callPreParse,doActions)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
945 if lookup in ParserElement._exprArgCache:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
946 value = ParserElement._exprArgCache[ lookup ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
947 if isinstance(value, Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
948 raise value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
949 return (value[0],value[1].copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
950 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
951 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
952 value = self._parseNoCache( instring, loc, doActions, callPreParse )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
953 ParserElement._exprArgCache[ lookup ] = (value[0],value[1].copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
954 return value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
955 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
956 pe = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
957 ParserElement._exprArgCache[ lookup ] = pe
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
958 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
959
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
960 _parse = _parseNoCache
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
961
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
962 # argument cache for optimizing repeated calls when backtracking through recursive expressions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
963 _exprArgCache = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
964 def resetCache():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
965 ParserElement._exprArgCache.clear()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
966 resetCache = staticmethod(resetCache)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
967
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
968 _packratEnabled = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
969 def enablePackrat():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
970 """Enables "packrat" parsing, which adds memoizing to the parsing logic.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
971 Repeated parse attempts at the same string location (which happens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
972 often in many complex grammars) can immediately return a cached value,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
973 instead of re-executing parsing/validating code. Memoizing is done of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
974 both valid results and parsing exceptions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
975
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
976 This speedup may break existing programs that use parse actions that
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
977 have side-effects. For this reason, packrat parsing is disabled when
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
978 you first import pyparsing. To activate the packrat feature, your
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
979 program must call the class method C{ParserElement.enablePackrat()}. If
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
980 your program uses C{psyco} to "compile as you go", you must call
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
981 C{enablePackrat} before calling C{psyco.full()}. If you do not do this,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
982 Python will crash. For best results, call C{enablePackrat()} immediately
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
983 after importing pyparsing.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
984 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
985 if not ParserElement._packratEnabled:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
986 ParserElement._packratEnabled = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
987 ParserElement._parse = ParserElement._parseCache
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
988 enablePackrat = staticmethod(enablePackrat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
989
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
990 def parseString( self, instring, parseAll=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
991 """Execute the parse expression with the given string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
992 This is the main interface to the client code, once the complete
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
993 expression has been built.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
994
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
995 If you want the grammar to require that the entire input string be
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
996 successfully parsed, then set C{parseAll} to True (equivalent to ending
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
997 the grammar with C{StringEnd()}).
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
998
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
999 Note: C{parseString} implicitly calls C{expandtabs()} on the input string,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1000 in order to report proper column numbers in parse actions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1001 If the input string contains tabs and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1002 the grammar uses parse actions that use the C{loc} argument to index into the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1003 string being parsed, you can ensure you have a consistent view of the input
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1004 string by:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1005 - calling C{parseWithTabs} on your grammar before calling C{parseString}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1006 (see L{I{parseWithTabs}<parseWithTabs>})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1007 - define your parse action using the full C{(s,loc,toks)} signature, and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1008 reference the input string using the parse action's C{s} argument
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1009 - explictly expand the tabs in your input string before calling
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1010 C{parseString}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1011 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1012 ParserElement.resetCache()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1013 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1014 self.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1015 #~ self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1016 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1017 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1018 if not self.keepTabs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1019 instring = instring.expandtabs()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1020 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1021 loc, tokens = self._parse( instring, 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1022 if parseAll:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1023 loc = self.preParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1024 se = Empty() + StringEnd()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1025 se._parse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1026 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1027 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1028 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1029 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1030 # catch and re-raise exception from here, clears out pyparsing internal stack trace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1031 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1032 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1033 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1034 return tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1035
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1036 def scanString( self, instring, maxMatches=_MAX_INT, overlap=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1037 """Scan the input string for expression matches. Each match will return the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1038 matching tokens, start location, and end location. May be called with optional
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1039 C{maxMatches} argument, to clip scanning after 'n' matches are found. If
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1040 C{overlap} is specified, then overlapping matches will be reported.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1041
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1042 Note that the start and end locations are reported relative to the string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1043 being parsed. See L{I{parseString}<parseString>} for more information on parsing
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1044 strings with embedded tabs."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1045 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1046 self.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1047 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1048 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1049
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1050 if not self.keepTabs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1051 instring = _ustr(instring).expandtabs()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1052 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1053 loc = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1054 preparseFn = self.preParse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1055 parseFn = self._parse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1056 ParserElement.resetCache()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1057 matches = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1058 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1059 while loc <= instrlen and matches < maxMatches:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1060 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1061 preloc = preparseFn( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1062 nextLoc,tokens = parseFn( instring, preloc, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1063 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1064 loc = preloc+1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1065 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1066 if nextLoc > loc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1067 matches += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1068 yield tokens, preloc, nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1069 if overlap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1070 nextloc = preparseFn( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1071 if nextloc > loc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1072 loc = nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1073 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1074 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1075 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1076 loc = nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1077 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1078 loc = preloc+1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1079 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1080 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1081 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1082 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1083 # catch and re-raise exception from here, clears out pyparsing internal stack trace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1084 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1085 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1086
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1087 def transformString( self, instring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1088 """Extension to C{scanString}, to modify matching text with modified tokens that may
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1089 be returned from a parse action. To use C{transformString}, define a grammar and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1090 attach a parse action to it that modifies the returned token list.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1091 Invoking C{transformString()} on a target string will then scan for matches,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1092 and replace the matched text patterns according to the logic in the parse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1093 action. C{transformString()} returns the resulting transformed string."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1094 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1095 lastE = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1096 # force preservation of <TAB>s, to minimize unwanted transformation of string, and to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1097 # keep string locs straight between transformString and scanString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1098 self.keepTabs = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1099 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1100 for t,s,e in self.scanString( instring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1101 out.append( instring[lastE:s] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1102 if t:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1103 if isinstance(t,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1104 out += t.asList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1105 elif isinstance(t,list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1106 out += t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1107 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1108 out.append(t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1109 lastE = e
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1110 out.append(instring[lastE:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1111 out = [o for o in out if o]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1112 return "".join(map(_ustr,_flatten(out)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1113 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1114 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1115 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1116 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1117 # catch and re-raise exception from here, clears out pyparsing internal stack trace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1118 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1119 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1120
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1121 def searchString( self, instring, maxMatches=_MAX_INT ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1122 """Another extension to C{scanString}, simplifying the access to the tokens found
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1123 to match the given parse expression. May be called with optional
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1124 C{maxMatches} argument, to clip searching after 'n' matches are found.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1125 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1126 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1127 return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1128 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1129 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1130 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1131 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1132 # catch and re-raise exception from here, clears out pyparsing internal stack trace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1133 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1134 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1135
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1136 def __add__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1137 """Implementation of + operator - returns And"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1138 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1139 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1140 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1141 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1142 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1143 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1144 return And( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1145
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1146 def __radd__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1147 """Implementation of + operator when left operand is not a C{ParserElement}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1148 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1149 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1150 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1151 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1152 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1153 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1154 return other + self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1155
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1156 def __sub__(self, other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1157 """Implementation of - operator, returns C{And} with error stop"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1158 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1159 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1160 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1161 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1162 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1163 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1164 return And( [ self, And._ErrorStop(), other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1165
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1166 def __rsub__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1167 """Implementation of - operator when left operand is not a C{ParserElement}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1168 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1169 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1170 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1171 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1172 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1173 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1174 return other - self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1175
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1176 def __mul__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1177 """Implementation of * operator, allows use of C{expr * 3} in place of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1178 C{expr + expr + expr}. Expressions may also me multiplied by a 2-integer
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1179 tuple, similar to C{{min,max}} multipliers in regular expressions. Tuples
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1180 may also include C{None} as in:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1181 - C{expr*(n,None)} or C{expr*(n,)} is equivalent
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1182 to C{expr*n + ZeroOrMore(expr)}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1183 (read as "at least n instances of C{expr}")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1184 - C{expr*(None,n)} is equivalent to C{expr*(0,n)}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1185 (read as "0 to n instances of C{expr}")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1186 - C{expr*(None,None)} is equivalent to C{ZeroOrMore(expr)}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1187 - C{expr*(1,None)} is equivalent to C{OneOrMore(expr)}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1188
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1189 Note that C{expr*(None,n)} does not raise an exception if
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1190 more than n exprs exist in the input stream; that is,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1191 C{expr*(None,n)} does not enforce a maximum number of expr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1192 occurrences. If this behavior is desired, then write
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1193 C{expr*(None,n) + ~expr}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1194
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1195 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1196 if isinstance(other,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1197 minElements, optElements = other,0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1198 elif isinstance(other,tuple):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1199 other = (other + (None, None))[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1200 if other[0] is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1201 other = (0, other[1])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1202 if isinstance(other[0],int) and other[1] is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1203 if other[0] == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1204 return ZeroOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1205 if other[0] == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1206 return OneOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1207 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1208 return self*other[0] + ZeroOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1209 elif isinstance(other[0],int) and isinstance(other[1],int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1210 minElements, optElements = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1211 optElements -= minElements
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1212 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1213 raise TypeError("cannot multiply 'ParserElement' and ('%s','%s') objects", type(other[0]),type(other[1]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1214 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1215 raise TypeError("cannot multiply 'ParserElement' and '%s' objects", type(other))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1216
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1217 if minElements < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1218 raise ValueError("cannot multiply ParserElement by negative value")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1219 if optElements < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1220 raise ValueError("second tuple value must be greater or equal to first tuple value")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1221 if minElements == optElements == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1222 raise ValueError("cannot multiply ParserElement by 0 or (0,0)")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1223
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1224 if (optElements):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1225 def makeOptionalList(n):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1226 if n>1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1227 return Optional(self + makeOptionalList(n-1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1228 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1229 return Optional(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1230 if minElements:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1231 if minElements == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1232 ret = self + makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1233 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1234 ret = And([self]*minElements) + makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1235 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1236 ret = makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1237 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1238 if minElements == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1239 ret = self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1240 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1241 ret = And([self]*minElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1242 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1243
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1244 def __rmul__(self, other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1245 return self.__mul__(other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1246
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1247 def __or__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1248 """Implementation of | operator - returns C{MatchFirst}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1249 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1250 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1251 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1252 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1253 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1254 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1255 return MatchFirst( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1256
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1257 def __ror__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1258 """Implementation of | operator when left operand is not a C{ParserElement}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1259 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1260 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1261 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1262 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1263 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1264 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1265 return other | self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1266
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1267 def __xor__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1268 """Implementation of ^ operator - returns C{Or}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1269 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1270 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1271 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1272 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1273 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1274 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1275 return Or( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1276
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1277 def __rxor__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1278 """Implementation of ^ operator when left operand is not a C{ParserElement}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1279 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1280 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1281 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1282 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1283 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1284 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1285 return other ^ self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1286
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1287 def __and__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1288 """Implementation of & operator - returns C{Each}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1289 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1290 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1291 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1292 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1293 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1294 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1295 return Each( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1296
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1297 def __rand__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1298 """Implementation of & operator when left operand is not a C{ParserElement}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1299 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1300 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1301 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1302 warnings.warn("Cannot combine element of type %s with ParserElement" % type(other),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1303 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1304 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1305 return other & self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1306
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1307 def __invert__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1308 """Implementation of ~ operator - returns C{NotAny}"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1309 return NotAny( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1310
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1311 def __call__(self, name):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1312 """Shortcut for C{setResultsName}, with C{listAllMatches=default}::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1313 userdata = Word(alphas).setResultsName("name") + Word(nums+"-").setResultsName("socsecno")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1314 could be written as::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1315 userdata = Word(alphas)("name") + Word(nums+"-")("socsecno")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1316
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1317 If C{name} is given with a trailing C{'*'} character, then C{listAllMatches} will be
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1318 passed as C{True}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1319 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1320 return self.setResultsName(name)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1321
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1322 def suppress( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1323 """Suppresses the output of this C{ParserElement}; useful to keep punctuation from
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1324 cluttering up returned output.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1325 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1326 return Suppress( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1327
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1328 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1329 """Disables the skipping of whitespace before matching the characters in the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1330 C{ParserElement}'s defined pattern. This is normally only used internally by
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1331 the pyparsing module, but may be needed in some whitespace-sensitive grammars.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1332 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1333 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1334 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1335
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1336 def setWhitespaceChars( self, chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1337 """Overrides the default whitespace chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1338 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1339 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1340 self.whiteChars = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1341 self.copyDefaultWhiteChars = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1342 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1343
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1344 def parseWithTabs( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1345 """Overrides default behavior to expand C{<TAB>}s to spaces before parsing the input string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1346 Must be called before C{parseString} when the input grammar contains elements that
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1347 match C{<TAB>} characters."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1348 self.keepTabs = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1349 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1350
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1351 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1352 """Define expression to be ignored (e.g., comments) while doing pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1353 matching; may be called repeatedly, to define multiple comment or other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1354 ignorable patterns.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1355 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1356 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1357 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1358 self.ignoreExprs.append( other.copy() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1359 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1360 self.ignoreExprs.append( Suppress( other.copy() ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1361 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1362
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1363 def setDebugActions( self, startAction, successAction, exceptionAction ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1364 """Enable display of debugging messages while doing pattern matching."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1365 self.debugActions = (startAction or _defaultStartDebugAction,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1366 successAction or _defaultSuccessDebugAction,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1367 exceptionAction or _defaultExceptionDebugAction)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1368 self.debug = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1369 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1370
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1371 def setDebug( self, flag=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1372 """Enable display of debugging messages while doing pattern matching.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1373 Set C{flag} to True to enable, False to disable."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1374 if flag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1375 self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1376 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1377 self.debug = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1378 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1379
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1380 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1381 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1382
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1383 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1384 return _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1385
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1386 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1387 self.streamlined = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1388 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1389 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1390
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1391 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1392 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1393
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1394 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1395 """Check defined expressions for valid structure, check for infinite recursive definitions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1396 self.checkRecursion( [] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1397
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1398 def parseFile( self, file_or_filename, parseAll=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1399 """Execute the parse expression on the given file or filename.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1400 If a filename is specified (instead of a file object),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1401 the entire file is opened, read, and closed before parsing.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1402 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1403 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1404 file_contents = file_or_filename.read()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1405 except AttributeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1406 f = open(file_or_filename, "rb")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1407 file_contents = f.read()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1408 f.close()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1409 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1410 return self.parseString(file_contents, parseAll)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1411 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1412 # catch and re-raise exception from here, clears out pyparsing internal stack trace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1413 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1414 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1415
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1416 def getException(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1417 return ParseException("",0,self.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1418
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1419 def __getattr__(self,aname):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1420 if aname == "myException":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1421 self.myException = ret = self.getException();
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1422 return ret;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1423 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1424 raise AttributeError("no such attribute " + aname)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1425
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1426 def __eq__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1427 if isinstance(other, ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1428 return self is other or self.__dict__ == other.__dict__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1429 elif isinstance(other, basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1430 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1431 self.parseString(_ustr(other), parseAll=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1432 return True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1433 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1434 return False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1435 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1436 return super(ParserElement,self)==other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1437
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1438 def __ne__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1439 return not (self == other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1440
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1441 def __hash__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1442 return hash(id(self))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1443
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1444 def __req__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1445 return self == other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1446
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1447 def __rne__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1448 return not (self == other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1449
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1450
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1451 class Token(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1452 """Abstract C{ParserElement} subclass, for defining atomic matching patterns."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1453 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1454 super(Token,self).__init__( savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1455
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1456 def setName(self, name):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1457 s = super(Token,self).setName(name)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1458 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1459 return s
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1460
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1461
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1462 class Empty(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1463 """An empty token, will always match."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1464 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1465 super(Empty,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1466 self.name = "Empty"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1467 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1468 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1469
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1470
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1471 class NoMatch(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1472 """A token that will never match."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1473 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1474 super(NoMatch,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1475 self.name = "NoMatch"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1476 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1477 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1478 self.errmsg = "Unmatchable token"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1479
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1480 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1481 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1482 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1483 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1484 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1485
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1486
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1487 class Literal(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1488 """Token to exactly match a specified string."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1489 def __init__( self, matchString ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1490 super(Literal,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1491 self.match = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1492 self.matchLen = len(matchString)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1493 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1494 self.firstMatchChar = matchString[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1495 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1496 warnings.warn("null string passed to Literal; use Empty() instead",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1497 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1498 self.__class__ = Empty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1499 self.name = '"%s"' % _ustr(self.match)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1500 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1501 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1502 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1503
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1504 # Performance tuning: this routine gets called a *lot*
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1505 # if this is a single character match string and the first character matches,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1506 # short-circuit as quickly as possible, and avoid calling startswith
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1507 #~ @profile
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1508 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1509 if (instring[loc] == self.firstMatchChar and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1510 (self.matchLen==1 or instring.startswith(self.match,loc)) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1511 return loc+self.matchLen, self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1512 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1513 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1514 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1515 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1516 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1517 _L = Literal
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1518
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1519 class Keyword(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1520 """Token to exactly match a specified string as a keyword, that is, it must be
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1521 immediately followed by a non-keyword character. Compare with C{Literal}::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1522 Literal("if") will match the leading C{'if'} in C{'ifAndOnlyIf'}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1523 Keyword("if") will not; it will only match the leading C{'if'} in C{'if x=1'}, or C{'if(y==2)'}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1524 Accepts two optional constructor arguments in addition to the keyword string:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1525 C{identChars} is a string of characters that would be valid identifier characters,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1526 defaulting to all alphanumerics + "_" and "$"; C{caseless} allows case-insensitive
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1527 matching, default is C{False}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1528 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1529 DEFAULT_KEYWORD_CHARS = alphanums+"_$"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1530
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1531 def __init__( self, matchString, identChars=DEFAULT_KEYWORD_CHARS, caseless=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1532 super(Keyword,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1533 self.match = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1534 self.matchLen = len(matchString)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1535 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1536 self.firstMatchChar = matchString[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1537 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1538 warnings.warn("null string passed to Keyword; use Empty() instead",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1539 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1540 self.name = '"%s"' % self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1541 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1542 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1543 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1544 self.caseless = caseless
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1545 if caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1546 self.caselessmatch = matchString.upper()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1547 identChars = identChars.upper()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1548 self.identChars = set(identChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1549
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1550 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1551 if self.caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1552 if ( (instring[ loc:loc+self.matchLen ].upper() == self.caselessmatch) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1553 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen].upper() not in self.identChars) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1554 (loc == 0 or instring[loc-1].upper() not in self.identChars) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1555 return loc+self.matchLen, self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1556 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1557 if (instring[loc] == self.firstMatchChar and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1558 (self.matchLen==1 or instring.startswith(self.match,loc)) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1559 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen] not in self.identChars) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1560 (loc == 0 or instring[loc-1] not in self.identChars) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1561 return loc+self.matchLen, self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1562 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1563 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1564 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1565 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1566 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1567
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1568 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1569 c = super(Keyword,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1570 c.identChars = Keyword.DEFAULT_KEYWORD_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1571 return c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1572
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1573 def setDefaultKeywordChars( chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1574 """Overrides the default Keyword chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1575 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1576 Keyword.DEFAULT_KEYWORD_CHARS = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1577 setDefaultKeywordChars = staticmethod(setDefaultKeywordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1578
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1579 class CaselessLiteral(Literal):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1580 """Token to match a specified string, ignoring case of letters.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1581 Note: the matched results will always be in the case of the given
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1582 match string, NOT the case of the input text.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1583 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1584 def __init__( self, matchString ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1585 super(CaselessLiteral,self).__init__( matchString.upper() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1586 # Preserve the defining literal.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1587 self.returnString = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1588 self.name = "'%s'" % self.returnString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1589 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1590
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1591 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1592 if instring[ loc:loc+self.matchLen ].upper() == self.match:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1593 return loc+self.matchLen, self.returnString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1594 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1595 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1596 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1597 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1598 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1599
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1600 class CaselessKeyword(Keyword):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1601 def __init__( self, matchString, identChars=Keyword.DEFAULT_KEYWORD_CHARS ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1602 super(CaselessKeyword,self).__init__( matchString, identChars, caseless=True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1603
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1604 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1605 if ( (instring[ loc:loc+self.matchLen ].upper() == self.caselessmatch) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1606 (loc >= len(instring)-self.matchLen or instring[loc+self.matchLen].upper() not in self.identChars) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1607 return loc+self.matchLen, self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1608 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1609 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1610 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1611 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1612 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1613
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1614 class Word(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1615 """Token for matching words composed of allowed character sets.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1616 Defined with string containing all allowed initial characters,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1617 an optional string containing allowed body characters (if omitted,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1618 defaults to the initial character set), and an optional minimum,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1619 maximum, and/or exact length. The default value for C{min} is 1 (a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1620 minimum value < 1 is not valid); the default values for C{max} and C{exact}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1621 are 0, meaning no maximum or exact length restriction. An optional
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1622 C{exclude} parameter can list characters that might be found in
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1623 the input C{bodyChars} string; useful to define a word of all printables
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1624 except for one or two characters, for instance.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1625 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1626 def __init__( self, initChars, bodyChars=None, min=1, max=0, exact=0, asKeyword=False, excludeChars=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1627 super(Word,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1628 if excludeChars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1629 initChars = ''.join([c for c in initChars if c not in excludeChars])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1630 if bodyChars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1631 bodyChars = ''.join([c for c in bodyChars if c not in excludeChars])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1632 self.initCharsOrig = initChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1633 self.initChars = set(initChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1634 if bodyChars :
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1635 self.bodyCharsOrig = bodyChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1636 self.bodyChars = set(bodyChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1637 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1638 self.bodyCharsOrig = initChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1639 self.bodyChars = set(initChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1640
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1641 self.maxSpecified = max > 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1642
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1643 if min < 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1644 raise ValueError("cannot specify a minimum length < 1; use Optional(Word()) if zero-length word is permitted")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1645
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1646 self.minLen = min
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1647
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1648 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1649 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1650 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1651 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1652
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1653 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1654 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1655 self.minLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1656
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1657 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1658 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1659 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1660 self.asKeyword = asKeyword
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1661
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1662 if ' ' not in self.initCharsOrig+self.bodyCharsOrig and (min==1 and max==0 and exact==0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1663 if self.bodyCharsOrig == self.initCharsOrig:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1664 self.reString = "[%s]+" % _escapeRegexRangeChars(self.initCharsOrig)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1665 elif len(self.bodyCharsOrig) == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1666 self.reString = "%s[%s]*" % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1667 (re.escape(self.initCharsOrig),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1668 _escapeRegexRangeChars(self.bodyCharsOrig),)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1669 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1670 self.reString = "[%s][%s]*" % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1671 (_escapeRegexRangeChars(self.initCharsOrig),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1672 _escapeRegexRangeChars(self.bodyCharsOrig),)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1673 if self.asKeyword:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1674 self.reString = r"\b"+self.reString+r"\b"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1675 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1676 self.re = re.compile( self.reString )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1677 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1678 self.re = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1679
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1680 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1681 if self.re:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1682 result = self.re.match(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1683 if not result:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1684 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1685 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1686 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1687 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1688
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1689 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1690 return loc, result.group()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1691
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1692 if not(instring[ loc ] in self.initChars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1693 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1694 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1695 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1696 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1697 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1698 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1699 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1700 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1701 bodychars = self.bodyChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1702 maxloc = start + self.maxLen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1703 maxloc = min( maxloc, instrlen )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1704 while loc < maxloc and instring[loc] in bodychars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1705 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1706
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1707 throwException = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1708 if loc - start < self.minLen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1709 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1710 if self.maxSpecified and loc < instrlen and instring[loc] in bodychars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1711 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1712 if self.asKeyword:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1713 if (start>0 and instring[start-1] in bodychars) or (loc<instrlen and instring[loc] in bodychars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1714 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1715
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1716 if throwException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1717 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1718 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1719 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1720 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1721 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1722
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1723 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1724
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1725 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1726 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1727 return super(Word,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1728 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1729 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1730
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1731
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1732 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1733
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1734 def charsAsStr(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1735 if len(s)>4:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1736 return s[:4]+"..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1737 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1738 return s
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1739
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1740 if ( self.initCharsOrig != self.bodyCharsOrig ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1741 self.strRepr = "W:(%s,%s)" % ( charsAsStr(self.initCharsOrig), charsAsStr(self.bodyCharsOrig) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1742 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1743 self.strRepr = "W:(%s)" % charsAsStr(self.initCharsOrig)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1744
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1745 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1746
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1747
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1748 class Regex(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1749 """Token for matching strings that match a given regular expression.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1750 Defined with string specifying the regular expression in a form recognized by the inbuilt Python re module.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1751 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1752 compiledREtype = type(re.compile("[A-Z]"))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1753 def __init__( self, pattern, flags=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1754 """The parameters C{pattern} and C{flags} are passed to the C{re.compile()} function as-is. See the Python C{re} module for an explanation of the acceptable patterns and flags."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1755 super(Regex,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1756
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1757 if isinstance(pattern, basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1758 if len(pattern) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1759 warnings.warn("null string passed to Regex; use Empty() instead",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1760 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1761
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1762 self.pattern = pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1763 self.flags = flags
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1764
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1765 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1766 self.re = re.compile(self.pattern, self.flags)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1767 self.reString = self.pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1768 except sre_constants.error:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1769 warnings.warn("invalid pattern (%s) passed to Regex" % pattern,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1770 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1771 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1772
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1773 elif isinstance(pattern, Regex.compiledREtype):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1774 self.re = pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1775 self.pattern = \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1776 self.reString = str(pattern)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1777 self.flags = flags
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1778
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1779 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1780 raise ValueError("Regex may only be constructed with a string or a compiled RE object")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1781
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1782 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1783 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1784 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1785 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1786
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1787 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1788 result = self.re.match(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1789 if not result:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1790 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1791 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1792 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1793 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1794
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1795 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1796 d = result.groupdict()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1797 ret = ParseResults(result.group())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1798 if d:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1799 for k in d:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1800 ret[k] = d[k]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1801 return loc,ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1802
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1803 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1804 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1805 return super(Regex,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1806 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1807 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1808
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1809 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1810 self.strRepr = "Re:(%s)" % repr(self.pattern)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1811
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1812 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1813
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1814
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1815 class QuotedString(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1816 """Token for matching strings that are delimited by quoting characters.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1817 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1818 def __init__( self, quoteChar, escChar=None, escQuote=None, multiline=False, unquoteResults=True, endQuoteChar=None):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1819 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1820 Defined with the following parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1821 - quoteChar - string of one or more characters defining the quote delimiting string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1822 - escChar - character to escape quotes, typically backslash (default=None)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1823 - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=None)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1824 - multiline - boolean indicating whether quotes can span multiple lines (default=False)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1825 - unquoteResults - boolean indicating whether the matched text should be unquoted (default=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1826 - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=None => same as quoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1827 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1828 super(QuotedString,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1829
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1830 # remove white space from quote chars - wont work anyway
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1831 quoteChar = quoteChar.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1832 if len(quoteChar) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1833 warnings.warn("quoteChar cannot be the empty string",SyntaxWarning,stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1834 raise SyntaxError()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1835
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1836 if endQuoteChar is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1837 endQuoteChar = quoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1838 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1839 endQuoteChar = endQuoteChar.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1840 if len(endQuoteChar) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1841 warnings.warn("endQuoteChar cannot be the empty string",SyntaxWarning,stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1842 raise SyntaxError()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1843
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1844 self.quoteChar = quoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1845 self.quoteCharLen = len(quoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1846 self.firstQuoteChar = quoteChar[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1847 self.endQuoteChar = endQuoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1848 self.endQuoteCharLen = len(endQuoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1849 self.escChar = escChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1850 self.escQuote = escQuote
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1851 self.unquoteResults = unquoteResults
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1852
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1853 if multiline:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1854 self.flags = re.MULTILINE | re.DOTALL
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1855 self.pattern = r'%s(?:[^%s%s]' % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1856 ( re.escape(self.quoteChar),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1857 _escapeRegexRangeChars(self.endQuoteChar[0]),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1858 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1859 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1860 self.flags = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1861 self.pattern = r'%s(?:[^%s\n\r%s]' % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1862 ( re.escape(self.quoteChar),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1863 _escapeRegexRangeChars(self.endQuoteChar[0]),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1864 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1865 if len(self.endQuoteChar) > 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1866 self.pattern += (
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1867 '|(?:' + ')|(?:'.join(["%s[^%s]" % (re.escape(self.endQuoteChar[:i]),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1868 _escapeRegexRangeChars(self.endQuoteChar[i]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1869 for i in range(len(self.endQuoteChar)-1,0,-1)]) + ')'
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1870 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1871 if escQuote:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1872 self.pattern += (r'|(?:%s)' % re.escape(escQuote))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1873 if escChar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1874 self.pattern += (r'|(?:%s.)' % re.escape(escChar))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1875 charset = ''.join(set(self.quoteChar[0]+self.endQuoteChar[0])).replace('^',r'\^').replace('-',r'\-')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1876 self.escCharReplacePattern = re.escape(self.escChar)+("([%s])" % charset)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1877 self.pattern += (r')*%s' % re.escape(self.endQuoteChar))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1878
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1879 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1880 self.re = re.compile(self.pattern, self.flags)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1881 self.reString = self.pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1882 except sre_constants.error:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1883 warnings.warn("invalid pattern (%s) passed to Regex" % self.pattern,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1884 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1885 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1886
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1887 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1888 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1889 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1890 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1891
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1892 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1893 result = instring[loc] == self.firstQuoteChar and self.re.match(instring,loc) or None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1894 if not result:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1895 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1896 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1897 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1898 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1899
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1900 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1901 ret = result.group()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1902
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1903 if self.unquoteResults:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1904
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1905 # strip off quotes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1906 ret = ret[self.quoteCharLen:-self.endQuoteCharLen]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1907
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1908 if isinstance(ret,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1909 # replace escaped characters
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1910 if self.escChar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1911 ret = re.sub(self.escCharReplacePattern,"\g<1>",ret)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1912
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1913 # replace escaped quotes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1914 if self.escQuote:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1915 ret = ret.replace(self.escQuote, self.endQuoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1916
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1917 return loc, ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1918
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1919 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1920 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1921 return super(QuotedString,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1922 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1923 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1924
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1925 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1926 self.strRepr = "quoted string, starting with %s ending with %s" % (self.quoteChar, self.endQuoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1927
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1928 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1929
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1930
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1931 class CharsNotIn(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1932 """Token for matching words composed of characters *not* in a given set.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1933 Defined with string containing all disallowed characters, and an optional
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1934 minimum, maximum, and/or exact length. The default value for C{min} is 1 (a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1935 minimum value < 1 is not valid); the default values for C{max} and C{exact}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1936 are 0, meaning no maximum or exact length restriction.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1937 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1938 def __init__( self, notChars, min=1, max=0, exact=0 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1939 super(CharsNotIn,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1940 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1941 self.notChars = notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1942
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1943 if min < 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1944 raise ValueError("cannot specify a minimum length < 1; use Optional(CharsNotIn()) if zero-length char group is permitted")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1945
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1946 self.minLen = min
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1947
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1948 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1949 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1950 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1951 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1952
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1953 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1954 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1955 self.minLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1956
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1957 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1958 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1959 self.mayReturnEmpty = ( self.minLen == 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1960 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1961
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1962 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1963 if instring[loc] in self.notChars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1964 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1965 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1966 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1967 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1968 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1969
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1970 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1971 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1972 notchars = self.notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1973 maxlen = min( start+self.maxLen, len(instring) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1974 while loc < maxlen and \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1975 (instring[loc] not in notchars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1976 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1977
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1978 if loc - start < self.minLen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1979 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1980 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1981 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1982 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1983 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1984
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1985 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1986
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1987 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1988 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1989 return super(CharsNotIn, self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1990 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1991 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1992
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1993 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1994 if len(self.notChars) > 4:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1995 self.strRepr = "!W:(%s...)" % self.notChars[:4]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1996 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1997 self.strRepr = "!W:(%s)" % self.notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1998
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1999 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2000
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2001 class White(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2002 """Special matching class for matching whitespace. Normally, whitespace is ignored
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2003 by pyparsing grammars. This class is included when some whitespace structures
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2004 are significant. Define with a string containing the whitespace characters to be
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2005 matched; default is C{" \\t\\r\\n"}. Also takes optional C{min}, C{max}, and C{exact} arguments,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2006 as defined for the C{Word} class."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2007 whiteStrs = {
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2008 " " : "<SPC>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2009 "\t": "<TAB>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2010 "\n": "<LF>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2011 "\r": "<CR>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2012 "\f": "<FF>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2013 }
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2014 def __init__(self, ws=" \t\r\n", min=1, max=0, exact=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2015 super(White,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2016 self.matchWhite = ws
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2017 self.setWhitespaceChars( "".join([c for c in self.whiteChars if c not in self.matchWhite]) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2018 #~ self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2019 self.name = ("".join([White.whiteStrs[c] for c in self.matchWhite]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2020 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2021 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2022
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2023 self.minLen = min
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2024
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2025 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2026 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2027 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2028 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2029
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2030 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2031 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2032 self.minLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2033
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2034 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2035 if not(instring[ loc ] in self.matchWhite):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2036 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2037 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2038 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2039 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2040 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2041 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2042 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2043 maxloc = start + self.maxLen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2044 maxloc = min( maxloc, len(instring) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2045 while loc < maxloc and instring[loc] in self.matchWhite:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2046 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2047
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2048 if loc - start < self.minLen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2049 #~ raise ParseException( instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2050 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2051 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2052 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2053 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2054
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2055 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2056
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2057
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2058 class _PositionToken(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2059 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2060 super(_PositionToken,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2061 self.name=self.__class__.__name__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2062 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2063 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2064
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2065 class GoToColumn(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2066 """Token to advance to a specific column of input text; useful for tabular report scraping."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2067 def __init__( self, colno ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2068 super(GoToColumn,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2069 self.col = colno
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2070
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2071 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2072 if col(loc,instring) != self.col:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2073 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2074 if self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2075 loc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2076 while loc < instrlen and instring[loc].isspace() and col( loc, instring ) != self.col :
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2077 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2078 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2079
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2080 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2081 thiscol = col( loc, instring )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2082 if thiscol > self.col:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2083 raise ParseException( instring, loc, "Text not in expected column", self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2084 newloc = loc + self.col - thiscol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2085 ret = instring[ loc: newloc ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2086 return newloc, ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2087
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2088 class LineStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2089 """Matches if current position is at the beginning of a line within the parse string"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2090 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2091 super(LineStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2092 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2093 self.errmsg = "Expected start of line"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2094
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2095 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2096 preloc = super(LineStart,self).preParse(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2097 if instring[preloc] == "\n":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2098 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2099 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2100
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2101 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2102 if not( loc==0 or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2103 (loc == self.preParse( instring, 0 )) or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2104 (instring[loc-1] == "\n") ): #col(loc, instring) != 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2105 #~ raise ParseException( instring, loc, "Expected start of line" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2106 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2107 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2108 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2109 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2110 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2111
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2112 class LineEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2113 """Matches if current position is at the end of a line within the parse string"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2114 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2115 super(LineEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2116 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2117 self.errmsg = "Expected end of line"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2118
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2119 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2120 if loc<len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2121 if instring[loc] == "\n":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2122 return loc+1, "\n"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2123 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2124 #~ raise ParseException( instring, loc, "Expected end of line" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2125 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2126 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2127 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2128 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2129 elif loc == len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2130 return loc+1, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2131 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2132 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2133 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2134 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2135 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2136
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2137 class StringStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2138 """Matches if current position is at the beginning of the parse string"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2139 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2140 super(StringStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2141 self.errmsg = "Expected start of text"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2142
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2143 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2144 if loc != 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2145 # see if entire string up to here is just whitespace and ignoreables
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2146 if loc != self.preParse( instring, 0 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2147 #~ raise ParseException( instring, loc, "Expected start of text" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2148 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2149 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2150 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2151 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2152 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2153
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2154 class StringEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2155 """Matches if current position is at the end of the parse string"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2156 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2157 super(StringEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2158 self.errmsg = "Expected end of text"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2159
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2160 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2161 if loc < len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2162 #~ raise ParseException( instring, loc, "Expected end of text" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2163 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2164 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2165 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2166 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2167 elif loc == len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2168 return loc+1, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2169 elif loc > len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2170 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2171 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2172 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2173 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2174 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2175 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2176
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2177 class WordStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2178 """Matches if the current position is at the beginning of a Word, and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2179 is not preceded by any character in a given set of C{wordChars}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2180 (default=C{printables}). To emulate the C{\b} behavior of regular expressions,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2181 use C{WordStart(alphanums)}. C{WordStart} will also match at the beginning of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2182 the string being parsed, or at the beginning of a line.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2183 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2184 def __init__(self, wordChars = printables):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2185 super(WordStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2186 self.wordChars = set(wordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2187 self.errmsg = "Not at the start of a word"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2188
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2189 def parseImpl(self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2190 if loc != 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2191 if (instring[loc-1] in self.wordChars or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2192 instring[loc] not in self.wordChars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2193 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2194 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2195 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2196 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2197 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2198
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2199 class WordEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2200 """Matches if the current position is at the end of a Word, and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2201 is not followed by any character in a given set of C{wordChars}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2202 (default=C{printables}). To emulate the C{\b} behavior of regular expressions,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2203 use C{WordEnd(alphanums)}. C{WordEnd} will also match at the end of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2204 the string being parsed, or at the end of a line.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2205 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2206 def __init__(self, wordChars = printables):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2207 super(WordEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2208 self.wordChars = set(wordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2209 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2210 self.errmsg = "Not at the end of a word"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2211
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2212 def parseImpl(self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2213 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2214 if instrlen>0 and loc<instrlen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2215 if (instring[loc] in self.wordChars or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2216 instring[loc-1] not in self.wordChars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2217 #~ raise ParseException( instring, loc, "Expected end of word" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2218 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2219 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2220 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2221 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2222 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2223
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2224
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2225 class ParseExpression(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2226 """Abstract subclass of ParserElement, for combining and post-processing parsed tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2227 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2228 super(ParseExpression,self).__init__(savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2229 if isinstance( exprs, list ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2230 self.exprs = exprs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2231 elif isinstance( exprs, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2232 self.exprs = [ Literal( exprs ) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2233 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2234 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2235 self.exprs = list( exprs )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2236 except TypeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2237 self.exprs = [ exprs ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2238 self.callPreparse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2239
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2240 def __getitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2241 return self.exprs[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2242
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2243 def append( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2244 self.exprs.append( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2245 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2246 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2247
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2248 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2249 """Extends C{leaveWhitespace} defined in base class, and also invokes C{leaveWhitespace} on
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2250 all contained expressions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2251 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2252 self.exprs = [ e.copy() for e in self.exprs ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2253 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2254 e.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2255 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2256
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2257 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2258 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2259 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2260 super( ParseExpression, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2261 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2262 e.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2263 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2264 super( ParseExpression, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2265 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2266 e.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2267 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2268
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2269 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2270 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2271 return super(ParseExpression,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2272 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2273 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2274
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2275 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2276 self.strRepr = "%s:(%s)" % ( self.__class__.__name__, _ustr(self.exprs) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2277 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2278
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2279 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2280 super(ParseExpression,self).streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2281
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2282 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2283 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2284
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2285 # collapse nested And's of the form And( And( And( a,b), c), d) to And( a,b,c,d )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2286 # but only if there are no parse actions or resultsNames on the nested And's
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2287 # (likewise for Or's and MatchFirst's)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2288 if ( len(self.exprs) == 2 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2289 other = self.exprs[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2290 if ( isinstance( other, self.__class__ ) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2291 not(other.parseAction) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2292 other.resultsName is None and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2293 not other.debug ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2294 self.exprs = other.exprs[:] + [ self.exprs[1] ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2295 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2296 self.mayReturnEmpty |= other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2297 self.mayIndexError |= other.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2298
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2299 other = self.exprs[-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2300 if ( isinstance( other, self.__class__ ) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2301 not(other.parseAction) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2302 other.resultsName is None and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2303 not other.debug ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2304 self.exprs = self.exprs[:-1] + other.exprs[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2305 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2306 self.mayReturnEmpty |= other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2307 self.mayIndexError |= other.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2308
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2309 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2310
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2311 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2312 ret = super(ParseExpression,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2313 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2314
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2315 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2316 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2317 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2318 e.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2319 self.checkRecursion( [] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2320
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2321 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2322 ret = super(ParseExpression,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2323 ret.exprs = [e.copy() for e in self.exprs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2324 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2325
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2326 class And(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2327 """Requires all given C{ParseExpression}s to be found in the given order.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2328 Expressions may be separated by whitespace.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2329 May be constructed using the C{'+'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2330 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2331
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2332 class _ErrorStop(Empty):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2333 def __init__(self, *args, **kwargs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2334 super(Empty,self).__init__(*args, **kwargs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2335 self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2336
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2337 def __init__( self, exprs, savelist = True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2338 super(And,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2339 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2340 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2341 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2342 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2343 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2344 self.setWhitespaceChars( exprs[0].whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2345 self.skipWhitespace = exprs[0].skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2346 self.callPreparse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2347
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2348 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2349 # pass False as last arg to _parse for first element, since we already
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2350 # pre-parsed the string as part of our And pre-parsing
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2351 loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2352 errorStop = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2353 for e in self.exprs[1:]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2354 if isinstance(e, And._ErrorStop):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2355 errorStop = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2356 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2357 if errorStop:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2358 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2359 loc, exprtokens = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2360 except ParseSyntaxException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2361 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2362 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2363 pe = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2364 raise ParseSyntaxException(pe)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2365 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2366 raise ParseSyntaxException( ParseException(instring, len(instring), self.errmsg, self) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2367 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2368 loc, exprtokens = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2369 if exprtokens or exprtokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2370 resultlist += exprtokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2371 return loc, resultlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2372
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2373 def __iadd__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2374 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2375 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2376 return self.append( other ) #And( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2377
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2378 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2379 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2380 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2381 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2382 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2383 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2384
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2385 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2386 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2387 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2388
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2389 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2390 self.strRepr = "{" + " ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2391
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2392 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2393
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2394
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2395 class Or(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2396 """Requires that at least one C{ParseExpression} is found.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2397 If two expressions match, the expression that matches the longest string will be used.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2398 May be constructed using the C{'^'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2399 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2400 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2401 super(Or,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2402 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2403 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2404 if e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2405 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2406 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2407
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2408 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2409 maxExcLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2410 maxMatchLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2411 maxException = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2412 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2413 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2414 loc2 = e.tryParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2415 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2416 err = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2417 if err.loc > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2418 maxException = err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2419 maxExcLoc = err.loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2420 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2421 if len(instring) > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2422 maxException = ParseException(instring,len(instring),e.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2423 maxExcLoc = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2424 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2425 if loc2 > maxMatchLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2426 maxMatchLoc = loc2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2427 maxMatchExp = e
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2428
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2429 if maxMatchLoc < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2430 if maxException is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2431 raise maxException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2432 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2433 raise ParseException(instring, loc, "no defined alternatives to match", self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2434
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2435 return maxMatchExp._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2436
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2437 def __ixor__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2438 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2439 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2440 return self.append( other ) #Or( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2441
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2442 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2443 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2444 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2445
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2446 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2447 self.strRepr = "{" + " ^ ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2448
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2449 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2450
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2451 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2452 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2453 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2454 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2455
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2456
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2457 class MatchFirst(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2458 """Requires that at least one C{ParseExpression} is found.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2459 If two expressions match, the first one listed is the one that will match.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2460 May be constructed using the C{'|'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2461 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2462 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2463 super(MatchFirst,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2464 if exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2465 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2466 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2467 if e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2468 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2469 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2470 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2471 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2472
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2473 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2474 maxExcLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2475 maxException = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2476 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2477 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2478 ret = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2479 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2480 except ParseException, err:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2481 if err.loc > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2482 maxException = err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2483 maxExcLoc = err.loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2484 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2485 if len(instring) > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2486 maxException = ParseException(instring,len(instring),e.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2487 maxExcLoc = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2488
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2489 # only got here if no expression matched, raise exception for match that made it the furthest
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2490 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2491 if maxException is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2492 raise maxException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2493 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2494 raise ParseException(instring, loc, "no defined alternatives to match", self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2495
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2496 def __ior__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2497 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2498 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2499 return self.append( other ) #MatchFirst( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2500
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2501 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2502 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2503 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2504
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2505 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2506 self.strRepr = "{" + " | ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2507
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2508 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2509
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2510 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2511 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2512 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2513 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2514
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2515
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2516 class Each(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2517 """Requires all given C{ParseExpression}s to be found, but in any order.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2518 Expressions may be separated by whitespace.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2519 May be constructed using the C{'&'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2520 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2521 def __init__( self, exprs, savelist = True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2522 super(Each,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2523 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2524 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2525 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2526 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2527 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2528 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2529 self.initExprGroups = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2530
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2531 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2532 if self.initExprGroups:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2533 opt1 = [ e.expr for e in self.exprs if isinstance(e,Optional) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2534 opt2 = [ e for e in self.exprs if e.mayReturnEmpty and e not in opt1 ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2535 self.optionals = opt1 + opt2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2536 self.multioptionals = [ e.expr for e in self.exprs if isinstance(e,ZeroOrMore) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2537 self.multirequired = [ e.expr for e in self.exprs if isinstance(e,OneOrMore) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2538 self.required = [ e for e in self.exprs if not isinstance(e,(Optional,ZeroOrMore,OneOrMore)) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2539 self.required += self.multirequired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2540 self.initExprGroups = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2541 tmpLoc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2542 tmpReqd = self.required[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2543 tmpOpt = self.optionals[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2544 matchOrder = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2545
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2546 keepMatching = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2547 while keepMatching:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2548 tmpExprs = tmpReqd + tmpOpt + self.multioptionals + self.multirequired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2549 failed = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2550 for e in tmpExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2551 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2552 tmpLoc = e.tryParse( instring, tmpLoc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2553 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2554 failed.append(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2555 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2556 matchOrder.append(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2557 if e in tmpReqd:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2558 tmpReqd.remove(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2559 elif e in tmpOpt:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2560 tmpOpt.remove(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2561 if len(failed) == len(tmpExprs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2562 keepMatching = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2563
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2564 if tmpReqd:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2565 missing = ", ".join( [ _ustr(e) for e in tmpReqd ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2566 raise ParseException(instring,loc,"Missing one or more required elements (%s)" % missing )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2567
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2568 # add any unmatched Optionals, in case they have default values defined
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2569 matchOrder += [e for e in self.exprs if isinstance(e,Optional) and e.expr in tmpOpt]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2570
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2571 resultlist = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2572 for e in matchOrder:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2573 loc,results = e._parse(instring,loc,doActions)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2574 resultlist.append(results)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2575
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2576 finalResults = ParseResults([])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2577 for r in resultlist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2578 dups = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2579 for k in r.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2580 if k in finalResults.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2581 tmp = ParseResults(finalResults[k])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2582 tmp += ParseResults(r[k])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2583 dups[k] = tmp
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2584 finalResults += ParseResults(r)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2585 for k,v in dups.items():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2586 finalResults[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2587 return loc, finalResults
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2588
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2589 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2590 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2591 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2592
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2593 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2594 self.strRepr = "{" + " & ".join( [ _ustr(e) for e in self.exprs ] ) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2595
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2596 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2597
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2598 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2599 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2600 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2601 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2602
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2603
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2604 class ParseElementEnhance(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2605 """Abstract subclass of C{ParserElement}, for combining and post-processing parsed tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2606 def __init__( self, expr, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2607 super(ParseElementEnhance,self).__init__(savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2608 if isinstance( expr, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2609 expr = Literal(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2610 self.expr = expr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2611 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2612 if expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2613 self.mayIndexError = expr.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2614 self.mayReturnEmpty = expr.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2615 self.setWhitespaceChars( expr.whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2616 self.skipWhitespace = expr.skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2617 self.saveAsList = expr.saveAsList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2618 self.callPreparse = expr.callPreparse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2619 self.ignoreExprs.extend(expr.ignoreExprs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2620
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2621 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2622 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2623 return self.expr._parse( instring, loc, doActions, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2624 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2625 raise ParseException("",loc,self.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2626
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2627 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2628 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2629 self.expr = self.expr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2630 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2631 self.expr.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2632 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2633
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2634 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2635 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2636 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2637 super( ParseElementEnhance, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2638 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2639 self.expr.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2640 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2641 super( ParseElementEnhance, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2642 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2643 self.expr.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2644 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2645
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2646 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2647 super(ParseElementEnhance,self).streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2648 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2649 self.expr.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2650 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2651
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2652 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2653 if self in parseElementList:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2654 raise RecursiveGrammarException( parseElementList+[self] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2655 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2656 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2657 self.expr.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2658
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2659 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2660 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2661 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2662 self.expr.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2663 self.checkRecursion( [] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2664
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2665 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2666 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2667 return super(ParseElementEnhance,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2668 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2669 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2670
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2671 if self.strRepr is None and self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2672 self.strRepr = "%s:(%s)" % ( self.__class__.__name__, _ustr(self.expr) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2673 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2674
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2675
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2676 class FollowedBy(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2677 """Lookahead matching of the given parse expression. C{FollowedBy}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2678 does *not* advance the parsing position within the input string, it only
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2679 verifies that the specified parse expression matches at the current
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2680 position. C{FollowedBy} always returns a null token list."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2681 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2682 super(FollowedBy,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2683 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2684
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2685 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2686 self.expr.tryParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2687 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2688
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2689
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2690 class NotAny(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2691 """Lookahead to disallow matching with the given parse expression. C{NotAny}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2692 does *not* advance the parsing position within the input string, it only
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2693 verifies that the specified parse expression does *not* match at the current
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2694 position. Also, C{NotAny} does *not* skip over leading whitespace. C{NotAny}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2695 always returns a null token list. May be constructed using the '~' operator."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2696 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2697 super(NotAny,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2698 #~ self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2699 self.skipWhitespace = False # do NOT use self.leaveWhitespace(), don't want to propagate to exprs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2700 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2701 self.errmsg = "Found unwanted token, "+_ustr(self.expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2702
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2703 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2704 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2705 self.expr.tryParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2706 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2707 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2708 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2709 #~ raise ParseException(instring, loc, self.errmsg )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2710 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2711 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2712 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2713 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2714 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2715
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2716 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2717 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2718 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2719
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2720 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2721 self.strRepr = "~{" + _ustr(self.expr) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2722
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2723 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2724
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2725
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2726 class ZeroOrMore(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2727 """Optional repetition of zero or more of the given expression."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2728 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2729 super(ZeroOrMore,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2730 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2731
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2732 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2733 tokens = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2734 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2735 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2736 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2737 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2738 if hasIgnoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2739 preloc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2740 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2741 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2742 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2743 if tmptokens or tmptokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2744 tokens += tmptokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2745 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2746 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2747
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2748 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2749
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2750 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2751 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2752 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2753
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2754 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2755 self.strRepr = "[" + _ustr(self.expr) + "]..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2756
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2757 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2758
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2759 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2760 ret = super(ZeroOrMore,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2761 ret.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2762 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2763
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2764
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2765 class OneOrMore(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2766 """Repetition of one or more of the given expression."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2767 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2768 # must be at least one
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2769 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2770 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2771 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2772 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2773 if hasIgnoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2774 preloc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2775 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2776 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2777 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2778 if tmptokens or tmptokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2779 tokens += tmptokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2780 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2781 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2782
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2783 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2784
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2785 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2786 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2787 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2788
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2789 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2790 self.strRepr = "{" + _ustr(self.expr) + "}..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2791
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2792 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2793
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2794 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2795 ret = super(OneOrMore,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2796 ret.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2797 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2798
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2799 class _NullToken(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2800 def __bool__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2801 return False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2802 __nonzero__ = __bool__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2803 def __str__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2804 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2805
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2806 _optionalNotMatched = _NullToken()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2807 class Optional(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2808 """Optional matching of the given expression.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2809 A default return string can also be specified, if the optional expression
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2810 is not found.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2811 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2812 def __init__( self, exprs, default=_optionalNotMatched ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2813 super(Optional,self).__init__( exprs, savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2814 self.defaultValue = default
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2815 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2816
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2817 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2818 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2819 loc, tokens = self.expr._parse( instring, loc, doActions, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2820 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2821 if self.defaultValue is not _optionalNotMatched:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2822 if self.expr.resultsName:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2823 tokens = ParseResults([ self.defaultValue ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2824 tokens[self.expr.resultsName] = self.defaultValue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2825 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2826 tokens = [ self.defaultValue ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2827 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2828 tokens = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2829 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2830
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2831 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2832 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2833 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2834
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2835 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2836 self.strRepr = "[" + _ustr(self.expr) + "]"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2837
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2838 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2839
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2840
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2841 class SkipTo(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2842 """Token for skipping over all undefined text until the matched expression is found.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2843 If C{include} is set to true, the matched expression is also parsed (the skipped text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2844 and matched expression are returned as a 2-element list). The C{ignore}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2845 argument is used to define grammars (typically quoted strings and comments) that
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2846 might contain false matches.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2847 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2848 def __init__( self, other, include=False, ignore=None, failOn=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2849 super( SkipTo, self ).__init__( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2850 self.ignoreExpr = ignore
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2851 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2852 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2853 self.includeMatch = include
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2854 self.asList = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2855 if failOn is not None and isinstance(failOn, basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2856 self.failOn = Literal(failOn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2857 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2858 self.failOn = failOn
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2859 self.errmsg = "No match found for "+_ustr(self.expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2860
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2861 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2862 startLoc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2863 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2864 expr = self.expr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2865 failParse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2866 while loc <= instrlen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2867 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2868 if self.failOn:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2869 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2870 self.failOn.tryParse(instring, loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2871 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2872 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2873 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2874 failParse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2875 raise ParseException(instring, loc, "Found expression " + str(self.failOn))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2876 failParse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2877 if self.ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2878 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2879 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2880 loc = self.ignoreExpr.tryParse(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2881 # print "found ignoreExpr, advance to", loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2882 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2883 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2884 expr._parse( instring, loc, doActions=False, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2885 skipText = instring[startLoc:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2886 if self.includeMatch:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2887 loc,mat = expr._parse(instring,loc,doActions,callPreParse=False)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2888 if mat:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2889 skipRes = ParseResults( skipText )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2890 skipRes += mat
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2891 return loc, [ skipRes ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2892 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2893 return loc, [ skipText ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2894 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2895 return loc, [ skipText ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2896 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2897 if failParse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2898 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2899 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2900 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2901 exc = self.myException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2902 exc.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2903 exc.pstr = instring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2904 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2905
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2906 class Forward(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2907 """Forward declaration of an expression to be defined later -
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2908 used for recursive grammars, such as algebraic infix notation.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2909 When the expression is known, it is assigned to the C{Forward} variable using the '<<' operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2910
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2911 Note: take care when assigning to C{Forward} not to overlook precedence of operators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2912 Specifically, '|' has a lower precedence than '<<', so that::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2913 fwdExpr << a | b | c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2914 will actually be evaluated as::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2915 (fwdExpr << a) | b | c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2916 thereby leaving b and c out as parseable alternatives. It is recommended that you
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2917 explicitly group the values inserted into the C{Forward}::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2918 fwdExpr << (a | b | c)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2919 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2920 def __init__( self, other=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2921 super(Forward,self).__init__( other, savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2922
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2923 def __lshift__( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2924 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2925 other = Literal(other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2926 self.expr = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2927 self.mayReturnEmpty = other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2928 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2929 self.mayIndexError = self.expr.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2930 self.mayReturnEmpty = self.expr.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2931 self.setWhitespaceChars( self.expr.whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2932 self.skipWhitespace = self.expr.skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2933 self.saveAsList = self.expr.saveAsList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2934 self.ignoreExprs.extend(self.expr.ignoreExprs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2935 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2936
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2937 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2938 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2939 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2940
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2941 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2942 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2943 self.streamlined = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2944 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2945 self.expr.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2946 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2947
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2948 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2949 if self not in validateTrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2950 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2951 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2952 self.expr.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2953 self.checkRecursion([])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2954
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2955 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2956 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2957 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2958
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2959 self._revertClass = self.__class__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2960 self.__class__ = _ForwardNoRecurse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2961 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2962 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2963 retString = _ustr(self.expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2964 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2965 retString = "None"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2966 finally:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2967 self.__class__ = self._revertClass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2968 return self.__class__.__name__ + ": " + retString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2969
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2970 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2971 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2972 return super(Forward,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2973 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2974 ret = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2975 ret << self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2976 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2977
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2978 class _ForwardNoRecurse(Forward):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2979 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2980 return "..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2981
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2982 class TokenConverter(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2983 """Abstract subclass of C{ParseExpression}, for converting parsed results."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2984 def __init__( self, expr, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2985 super(TokenConverter,self).__init__( expr )#, savelist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2986 self.saveAsList = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2987
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2988 class Upcase(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2989 """Converter to upper case all matching tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2990 def __init__(self, *args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2991 super(Upcase,self).__init__(*args)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2992 warnings.warn("Upcase class is deprecated, use upcaseTokens parse action instead",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2993 DeprecationWarning,stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2994
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2995 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2996 return list(map( string.upper, tokenlist ))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2997
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2998
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2999 class Combine(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3000 """Converter to concatenate all matching tokens to a single string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3001 By default, the matching patterns must also be contiguous in the input string;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3002 this can be disabled by specifying C{'adjacent=False'} in the constructor.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3003 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3004 def __init__( self, expr, joinString="", adjacent=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3005 super(Combine,self).__init__( expr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3006 # suppress whitespace-stripping in contained parse expressions, but re-enable it on the Combine itself
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3007 if adjacent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3008 self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3009 self.adjacent = adjacent
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3010 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3011 self.joinString = joinString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3012 self.callPreparse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3013
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3014 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3015 if self.adjacent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3016 ParserElement.ignore(self, other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3017 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3018 super( Combine, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3019 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3020
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3021 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3022 retToks = tokenlist.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3023 del retToks[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3024 retToks += ParseResults([ "".join(tokenlist._asStringList(self.joinString)) ], modal=self.modalResults)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3025
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3026 if self.resultsName and len(retToks.keys())>0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3027 return [ retToks ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3028 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3029 return retToks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3030
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3031 class Group(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3032 """Converter to return the matched tokens as a list - useful for returning tokens of C{ZeroOrMore} and C{OneOrMore} expressions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3033 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3034 super(Group,self).__init__( expr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3035 self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3036
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3037 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3038 return [ tokenlist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3039
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3040 class Dict(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3041 """Converter to return a repetitive expression as a list, but also as a dictionary.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3042 Each element can also be referenced using the first token in the expression as its key.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3043 Useful for tabular report scraping when the first column can be used as a item key.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3044 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3045 def __init__( self, exprs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3046 super(Dict,self).__init__( exprs )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3047 self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3048
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3049 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3050 for i,tok in enumerate(tokenlist):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3051 if len(tok) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3052 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3053 ikey = tok[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3054 if isinstance(ikey,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3055 ikey = _ustr(tok[0]).strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3056 if len(tok)==1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3057 tokenlist[ikey] = _ParseResultsWithOffset("",i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3058 elif len(tok)==2 and not isinstance(tok[1],ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3059 tokenlist[ikey] = _ParseResultsWithOffset(tok[1],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3060 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3061 dictvalue = tok.copy() #ParseResults(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3062 del dictvalue[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3063 if len(dictvalue)!= 1 or (isinstance(dictvalue,ParseResults) and dictvalue.keys()):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3064 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue,i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3065 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3066 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue[0],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3067
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3068 if self.resultsName:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3069 return [ tokenlist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3070 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3071 return tokenlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3072
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3073
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3074 class Suppress(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3075 """Converter for ignoring the results of a parsed expression."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3076 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3077 return []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3078
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3079 def suppress( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3080 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3081
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3082
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3083 class OnlyOnce(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3084 """Wrapper for parse actions, to ensure they are only called once."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3085 def __init__(self, methodCall):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3086 self.callable = _trim_arity(methodCall)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3087 self.called = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3088 def __call__(self,s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3089 if not self.called:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3090 results = self.callable(s,l,t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3091 self.called = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3092 return results
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3093 raise ParseException(s,l,"")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3094 def reset(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3095 self.called = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3096
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3097 def traceParseAction(f):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3098 """Decorator for debugging parse actions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3099 f = _trim_arity(f)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3100 def z(*paArgs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3101 thisFunc = f.func_name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3102 s,l,t = paArgs[-3:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3103 if len(paArgs)>3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3104 thisFunc = paArgs[0].__class__.__name__ + '.' + thisFunc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3105 sys.stderr.write( ">>entering %s(line: '%s', %d, %s)\n" % (thisFunc,line(l,s),l,t) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3106 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3107 ret = f(*paArgs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3108 except Exception:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3109 exc = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3110 sys.stderr.write( "<<leaving %s (exception: %s)\n" % (thisFunc,exc) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3111 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3112 sys.stderr.write( "<<leaving %s (ret: %s)\n" % (thisFunc,ret) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3113 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3114 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3115 z.__name__ = f.__name__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3116 except AttributeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3117 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3118 return z
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3119
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3120 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3121 # global helpers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3122 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3123 def delimitedList( expr, delim=",", combine=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3124 """Helper to define a delimited list of expressions - the delimiter defaults to ','.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3125 By default, the list elements and delimiters can have intervening whitespace, and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3126 comments, but this can be overridden by passing C{combine=True} in the constructor.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3127 If C{combine} is set to True, the matching tokens are returned as a single token
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3128 string, with the delimiters included; otherwise, the matching tokens are returned
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3129 as a list of tokens, with the delimiters suppressed.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3130 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3131 dlName = _ustr(expr)+" ["+_ustr(delim)+" "+_ustr(expr)+"]..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3132 if combine:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3133 return Combine( expr + ZeroOrMore( delim + expr ) ).setName(dlName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3134 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3135 return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3136
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3137 def countedArray( expr, intExpr=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3138 """Helper to define a counted list of expressions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3139 This helper defines a pattern of the form::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3140 integer expr expr expr...
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3141 where the leading integer tells how many expr expressions follow.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3142 The matched tokens returns the array of expr tokens as a list - the leading count token is suppressed.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3143 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3144 arrayExpr = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3145 def countFieldParseAction(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3146 n = t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3147 arrayExpr << (n and Group(And([expr]*n)) or Group(empty))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3148 return []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3149 if intExpr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3150 intExpr = Word(nums).setParseAction(lambda t:int(t[0]))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3151 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3152 intExpr = intExpr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3153 intExpr.setName("arrayLen")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3154 intExpr.addParseAction(countFieldParseAction, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3155 return ( intExpr + arrayExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3156
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3157 def _flatten(L):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3158 ret = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3159 for i in L:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3160 if isinstance(i,list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3161 ret.extend(_flatten(i))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3162 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3163 ret.append(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3164 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3165
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3166 def matchPreviousLiteral(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3167 """Helper to define an expression that is indirectly defined from
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3168 the tokens matched in a previous expression, that is, it looks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3169 for a 'repeat' of a previous expression. For example::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3170 first = Word(nums)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3171 second = matchPreviousLiteral(first)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3172 matchExpr = first + ":" + second
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3173 will match C{"1:1"}, but not C{"1:2"}. Because this matches a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3174 previous literal, will also match the leading C{"1:1"} in C{"1:10"}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3175 If this is not desired, use C{matchPreviousExpr}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3176 Do *not* use with packrat parsing enabled.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3177 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3178 rep = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3179 def copyTokenToRepeater(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3180 if t:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3181 if len(t) == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3182 rep << t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3183 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3184 # flatten t tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3185 tflat = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3186 rep << And( [ Literal(tt) for tt in tflat ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3187 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3188 rep << Empty()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3189 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3190 return rep
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3191
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3192 def matchPreviousExpr(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3193 """Helper to define an expression that is indirectly defined from
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3194 the tokens matched in a previous expression, that is, it looks
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3195 for a 'repeat' of a previous expression. For example::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3196 first = Word(nums)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3197 second = matchPreviousExpr(first)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3198 matchExpr = first + ":" + second
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3199 will match C{"1:1"}, but not C{"1:2"}. Because this matches by
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3200 expressions, will *not* match the leading C{"1:1"} in C{"1:10"};
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3201 the expressions are evaluated first, and then compared, so
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3202 C{"1"} is compared with C{"10"}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3203 Do *not* use with packrat parsing enabled.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3204 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3205 rep = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3206 e2 = expr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3207 rep << e2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3208 def copyTokenToRepeater(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3209 matchTokens = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3210 def mustMatchTheseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3211 theseTokens = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3212 if theseTokens != matchTokens:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3213 raise ParseException("",0,"")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3214 rep.setParseAction( mustMatchTheseTokens, callDuringTry=True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3215 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3216 return rep
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3217
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3218 def _escapeRegexRangeChars(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3219 #~ escape these chars: ^-]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3220 for c in r"\^-]":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3221 s = s.replace(c,_bslash+c)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3222 s = s.replace("\n",r"\n")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3223 s = s.replace("\t",r"\t")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3224 return _ustr(s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3225
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3226 def oneOf( strs, caseless=False, useRegex=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3227 """Helper to quickly define a set of alternative Literals, and makes sure to do
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3228 longest-first testing when there is a conflict, regardless of the input order,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3229 but returns a C{MatchFirst} for best performance.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3230
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3231 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3232 - strs - a string of space-delimited literals, or a list of string literals
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3233 - caseless - (default=False) - treat all literals as caseless
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3234 - useRegex - (default=True) - as an optimization, will generate a Regex
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3235 object; otherwise, will generate a C{MatchFirst} object (if C{caseless=True}, or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3236 if creating a C{Regex} raises an exception)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3237 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3238 if caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3239 isequal = ( lambda a,b: a.upper() == b.upper() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3240 masks = ( lambda a,b: b.upper().startswith(a.upper()) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3241 parseElementClass = CaselessLiteral
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3242 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3243 isequal = ( lambda a,b: a == b )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3244 masks = ( lambda a,b: b.startswith(a) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3245 parseElementClass = Literal
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3246
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3247 if isinstance(strs,(list,tuple)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3248 symbols = list(strs[:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3249 elif isinstance(strs,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3250 symbols = strs.split()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3251 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3252 warnings.warn("Invalid argument to oneOf, expected string or list",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3253 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3254
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3255 i = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3256 while i < len(symbols)-1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3257 cur = symbols[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3258 for j,other in enumerate(symbols[i+1:]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3259 if ( isequal(other, cur) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3260 del symbols[i+j+1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3261 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3262 elif ( masks(cur, other) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3263 del symbols[i+j+1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3264 symbols.insert(i,other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3265 cur = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3266 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3267 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3268 i += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3269
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3270 if not caseless and useRegex:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3271 #~ print (strs,"->", "|".join( [ _escapeRegexChars(sym) for sym in symbols] ))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3272 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3273 if len(symbols)==len("".join(symbols)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3274 return Regex( "[%s]" % "".join( [ _escapeRegexRangeChars(sym) for sym in symbols] ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3275 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3276 return Regex( "|".join( [ re.escape(sym) for sym in symbols] ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3277 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3278 warnings.warn("Exception creating Regex for oneOf, building MatchFirst",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3279 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3280
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3281
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3282 # last resort, just use MatchFirst
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3283 return MatchFirst( [ parseElementClass(sym) for sym in symbols ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3284
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3285 def dictOf( key, value ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3286 """Helper to easily and clearly define a dictionary by specifying the respective patterns
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3287 for the key and value. Takes care of defining the C{Dict}, C{ZeroOrMore}, and C{Group} tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3288 in the proper order. The key pattern can include delimiting markers or punctuation,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3289 as long as they are suppressed, thereby leaving the significant key text. The value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3290 pattern can include named results, so that the C{Dict} results can include named token
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3291 fields.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3292 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3293 return Dict( ZeroOrMore( Group ( key + value ) ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3294
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3295 def originalTextFor(expr, asString=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3296 """Helper to return the original, untokenized text for a given expression. Useful to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3297 restore the parsed fields of an HTML start tag into the raw tag text itself, or to
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3298 revert separate tokens with intervening whitespace back to the original matching
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3299 input text. Simpler to use than the parse action C{L{keepOriginalText}}, and does not
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3300 require the inspect module to chase up the call stack. By default, returns a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3301 string containing the original parsed text.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3302
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3303 If the optional C{asString} argument is passed as C{False}, then the return value is a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3304 C{ParseResults} containing any results names that were originally matched, and a
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3305 single token containing the original matched text from the input string. So if
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3306 the expression passed to C{L{originalTextFor}} contains expressions with defined
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3307 results names, you must set C{asString} to C{False} if you want to preserve those
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3308 results name values."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3309 locMarker = Empty().setParseAction(lambda s,loc,t: loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3310 endlocMarker = locMarker.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3311 endlocMarker.callPreparse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3312 matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3313 if asString:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3314 extractText = lambda s,l,t: s[t._original_start:t._original_end]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3315 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3316 def extractText(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3317 del t[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3318 t.insert(0, s[t._original_start:t._original_end])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3319 del t["_original_start"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3320 del t["_original_end"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3321 matchExpr.setParseAction(extractText)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3322 return matchExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3323
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3324 def ungroup(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3325 """Helper to undo pyparsing's default grouping of And expressions, even
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3326 if all but one are non-empty."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3327 return TokenConverter(expr).setParseAction(lambda t:t[0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3328
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3329 # convenience constants for positional expressions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3330 empty = Empty().setName("empty")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3331 lineStart = LineStart().setName("lineStart")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3332 lineEnd = LineEnd().setName("lineEnd")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3333 stringStart = StringStart().setName("stringStart")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3334 stringEnd = StringEnd().setName("stringEnd")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3335
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3336 _escapedPunc = Word( _bslash, r"\[]-*.$+^?()~ ", exact=2 ).setParseAction(lambda s,l,t:t[0][1])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3337 _printables_less_backslash = "".join([ c for c in printables if c not in r"\]" ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3338 _escapedHexChar = Regex(r"\\0?[xX][0-9a-fA-F]+").setParseAction(lambda s,l,t:unichr(int(t[0][1:],16)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3339 _escapedOctChar = Regex(r"\\0[0-7]+").setParseAction(lambda s,l,t:unichr(int(t[0][1:],8)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3340 _singleChar = _escapedPunc | _escapedHexChar | _escapedOctChar | Word(_printables_less_backslash,exact=1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3341 _charRange = Group(_singleChar + Suppress("-") + _singleChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3342 _reBracketExpr = Literal("[") + Optional("^").setResultsName("negate") + Group( OneOrMore( _charRange | _singleChar ) ).setResultsName("body") + "]"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3343
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3344 _expanded = lambda p: (isinstance(p,ParseResults) and ''.join([ unichr(c) for c in range(ord(p[0]),ord(p[1])+1) ]) or p)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3345
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3346 def srange(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3347 r"""Helper to easily define string ranges for use in Word construction. Borrows
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3348 syntax from regexp '[]' string range definitions::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3349 srange("[0-9]") -> "0123456789"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3350 srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3351 srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3352 The input string must be enclosed in []'s, and the returned string is the expanded
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3353 character set joined into a single string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3354 The values enclosed in the []'s may be::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3355 a single character
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3356 an escaped character with a leading backslash (such as \- or \])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3357 an escaped hex character with a leading '\x' (\x21, which is a '!' character)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3358 (\0x## is also supported for backwards compatibility)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3359 an escaped octal character with a leading '\0' (\041, which is a '!' character)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3360 a range of any of the above, separated by a dash ('a-z', etc.)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3361 any combination of the above ('aeiouy', 'a-zA-Z0-9_$', etc.)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3362 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3363 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3364 return "".join([_expanded(part) for part in _reBracketExpr.parseString(s).body])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3365 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3366 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3367
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3368 def matchOnlyAtCol(n):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3369 """Helper method for defining parse actions that require matching at a specific
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3370 column in the input text.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3371 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3372 def verifyCol(strg,locn,toks):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3373 if col(locn,strg) != n:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3374 raise ParseException(strg,locn,"matched token not at column %d" % n)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3375 return verifyCol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3376
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3377 def replaceWith(replStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3378 """Helper method for common parse actions that simply return a literal value. Especially
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3379 useful when used with C{transformString()}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3380 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3381 def _replFunc(*args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3382 return [replStr]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3383 return _replFunc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3384
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3385 def removeQuotes(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3386 """Helper parse action for removing quotation marks from parsed quoted strings.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3387 To use, add this parse action to quoted string using::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3388 quotedString.setParseAction( removeQuotes )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3389 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3390 return t[0][1:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3391
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3392 def upcaseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3393 """Helper parse action to convert tokens to upper case."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3394 return [ tt.upper() for tt in map(_ustr,t) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3395
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3396 def downcaseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3397 """Helper parse action to convert tokens to lower case."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3398 return [ tt.lower() for tt in map(_ustr,t) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3399
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3400 def keepOriginalText(s,startLoc,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3401 """DEPRECATED - use new helper method C{originalTextFor}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3402 Helper parse action to preserve original parsed text,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3403 overriding any nested parse actions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3404 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3405 endloc = getTokensEndLoc()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3406 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3407 raise ParseFatalException("incorrect usage of keepOriginalText - may only be called as a parse action")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3408 del t[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3409 t += ParseResults(s[startLoc:endloc])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3410 return t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3411
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3412 def getTokensEndLoc():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3413 """Method to be called from within a parse action to determine the end
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3414 location of the parsed tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3415 import inspect
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3416 fstack = inspect.stack()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3417 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3418 # search up the stack (through intervening argument normalizers) for correct calling routine
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3419 for f in fstack[2:]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3420 if f[3] == "_parseNoCache":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3421 endloc = f[0].f_locals["loc"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3422 return endloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3423 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3424 raise ParseFatalException("incorrect usage of getTokensEndLoc - may only be called from within a parse action")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3425 finally:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3426 del fstack
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3427
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3428 def _makeTags(tagStr, xml):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3429 """Internal helper to construct opening and closing tag expressions, given a tag name"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3430 if isinstance(tagStr,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3431 resname = tagStr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3432 tagStr = Keyword(tagStr, caseless=not xml)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3433 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3434 resname = tagStr.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3435
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3436 tagAttrName = Word(alphas,alphanums+"_-:")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3437 if (xml):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3438 tagAttrValue = dblQuotedString.copy().setParseAction( removeQuotes )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3439 openTag = Suppress("<") + tagStr("tag") + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3440 Dict(ZeroOrMore(Group( tagAttrName + Suppress("=") + tagAttrValue ))) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3441 Optional("/",default=[False]).setResultsName("empty").setParseAction(lambda s,l,t:t[0]=='/') + Suppress(">")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3442 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3443 printablesLessRAbrack = "".join( [ c for c in printables if c not in ">" ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3444 tagAttrValue = quotedString.copy().setParseAction( removeQuotes ) | Word(printablesLessRAbrack)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3445 openTag = Suppress("<") + tagStr("tag") + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3446 Dict(ZeroOrMore(Group( tagAttrName.setParseAction(downcaseTokens) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3447 Optional( Suppress("=") + tagAttrValue ) ))) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3448 Optional("/",default=[False]).setResultsName("empty").setParseAction(lambda s,l,t:t[0]=='/') + Suppress(">")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3449 closeTag = Combine(_L("</") + tagStr + ">")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3450
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3451 openTag = openTag.setResultsName("start"+"".join(resname.replace(":"," ").title().split())).setName("<%s>" % tagStr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3452 closeTag = closeTag.setResultsName("end"+"".join(resname.replace(":"," ").title().split())).setName("</%s>" % tagStr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3453 openTag.tag = resname
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3454 closeTag.tag = resname
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3455 return openTag, closeTag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3456
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3457 def makeHTMLTags(tagStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3458 """Helper to construct opening and closing tag expressions for HTML, given a tag name"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3459 return _makeTags( tagStr, False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3460
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3461 def makeXMLTags(tagStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3462 """Helper to construct opening and closing tag expressions for XML, given a tag name"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3463 return _makeTags( tagStr, True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3464
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3465 def withAttribute(*args,**attrDict):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3466 """Helper to create a validating parse action to be used with start tags created
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3467 with C{makeXMLTags} or C{makeHTMLTags}. Use C{withAttribute} to qualify a starting tag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3468 with a required attribute value, to avoid false matches on common tags such as
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3469 C{<TD>} or C{<DIV>}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3470
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3471 Call C{withAttribute} with a series of attribute names and values. Specify the list
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3472 of filter attributes names and values as:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3473 - keyword arguments, as in C{(align="right")}, or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3474 - as an explicit dict with C{**} operator, when an attribute name is also a Python
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3475 reserved word, as in C{**{"class":"Customer", "align":"right"}}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3476 - a list of name-value tuples, as in ( ("ns1:class", "Customer"), ("ns2:align","right") )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3477 For attribute names with a namespace prefix, you must use the second form. Attribute
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3478 names are matched insensitive to upper/lower case.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3479
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3480 To verify that the attribute exists, but without specifying a value, pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3481 C{withAttribute.ANY_VALUE} as the value.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3482 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3483 if args:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3484 attrs = args[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3485 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3486 attrs = attrDict.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3487 attrs = [(k,v) for k,v in attrs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3488 def pa(s,l,tokens):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3489 for attrName,attrValue in attrs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3490 if attrName not in tokens:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3491 raise ParseException(s,l,"no matching attribute " + attrName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3492 if attrValue != withAttribute.ANY_VALUE and tokens[attrName] != attrValue:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3493 raise ParseException(s,l,"attribute '%s' has value '%s', must be '%s'" %
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3494 (attrName, tokens[attrName], attrValue))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3495 return pa
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3496 withAttribute.ANY_VALUE = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3497
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3498 opAssoc = _Constants()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3499 opAssoc.LEFT = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3500 opAssoc.RIGHT = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3501
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3502 def operatorPrecedence( baseExpr, opList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3503 """Helper method for constructing grammars of expressions made up of
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3504 operators working in a precedence hierarchy. Operators may be unary or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3505 binary, left- or right-associative. Parse actions can also be attached
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3506 to operator expressions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3507
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3508 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3509 - baseExpr - expression representing the most basic element for the nested
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3510 - opList - list of tuples, one for each operator precedence level in the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3511 expression grammar; each tuple is of the form
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3512 (opExpr, numTerms, rightLeftAssoc, parseAction), where:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3513 - opExpr is the pyparsing expression for the operator;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3514 may also be a string, which will be converted to a Literal;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3515 if numTerms is 3, opExpr is a tuple of two expressions, for the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3516 two operators separating the 3 terms
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3517 - numTerms is the number of terms for this operator (must
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3518 be 1, 2, or 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3519 - rightLeftAssoc is the indicator whether the operator is
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3520 right or left associative, using the pyparsing-defined
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3521 constants opAssoc.RIGHT and opAssoc.LEFT.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3522 - parseAction is the parse action to be associated with
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3523 expressions matching this operator expression (the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3524 parse action tuple member may be omitted)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3525 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3526 ret = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3527 lastExpr = baseExpr | ( Suppress('(') + ret + Suppress(')') )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3528 for i,operDef in enumerate(opList):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3529 opExpr,arity,rightLeftAssoc,pa = (operDef + (None,))[:4]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3530 if arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3531 if opExpr is None or len(opExpr) != 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3532 raise ValueError("if numterms=3, opExpr must be a tuple or list of two expressions")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3533 opExpr1, opExpr2 = opExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3534 thisExpr = Forward()#.setName("expr%d" % i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3535 if rightLeftAssoc == opAssoc.LEFT:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3536 if arity == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3537 matchExpr = FollowedBy(lastExpr + opExpr) + Group( lastExpr + OneOrMore( opExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3538 elif arity == 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3539 if opExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3540 matchExpr = FollowedBy(lastExpr + opExpr + lastExpr) + Group( lastExpr + OneOrMore( opExpr + lastExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3541 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3542 matchExpr = FollowedBy(lastExpr+lastExpr) + Group( lastExpr + OneOrMore(lastExpr) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3543 elif arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3544 matchExpr = FollowedBy(lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3545 Group( lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3546 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3547 raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3548 elif rightLeftAssoc == opAssoc.RIGHT:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3549 if arity == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3550 # try to avoid LR with this extra test
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3551 if not isinstance(opExpr, Optional):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3552 opExpr = Optional(opExpr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3553 matchExpr = FollowedBy(opExpr.expr + thisExpr) + Group( opExpr + thisExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3554 elif arity == 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3555 if opExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3556 matchExpr = FollowedBy(lastExpr + opExpr + thisExpr) + Group( lastExpr + OneOrMore( opExpr + thisExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3557 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3558 matchExpr = FollowedBy(lastExpr + thisExpr) + Group( lastExpr + OneOrMore( thisExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3559 elif arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3560 matchExpr = FollowedBy(lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3561 Group( lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3562 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3563 raise ValueError("operator must be unary (1), binary (2), or ternary (3)")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3564 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3565 raise ValueError("operator must indicate right or left associativity")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3566 if pa:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3567 matchExpr.setParseAction( pa )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3568 thisExpr << ( matchExpr | lastExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3569 lastExpr = thisExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3570 ret << lastExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3571 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3572
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3573 dblQuotedString = Regex(r'"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*"').setName("string enclosed in double quotes")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3574 sglQuotedString = Regex(r"'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*'").setName("string enclosed in single quotes")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3575 quotedString = Regex(r'''(?:"(?:[^"\n\r\\]|(?:"")|(?:\\x[0-9a-fA-F]+)|(?:\\.))*")|(?:'(?:[^'\n\r\\]|(?:'')|(?:\\x[0-9a-fA-F]+)|(?:\\.))*')''').setName("quotedString using single or double quotes")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3576 unicodeString = Combine(_L('u') + quotedString.copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3577
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3578 def nestedExpr(opener="(", closer=")", content=None, ignoreExpr=quotedString.copy()):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3579 """Helper method for defining nested lists enclosed in opening and closing
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3580 delimiters ("(" and ")" are the default).
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3581
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3582 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3583 - opener - opening character for a nested list (default="("); can also be a pyparsing expression
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3584 - closer - closing character for a nested list (default=")"); can also be a pyparsing expression
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3585 - content - expression for items within the nested lists (default=None)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3586 - ignoreExpr - expression for ignoring opening and closing delimiters (default=quotedString)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3587
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3588 If an expression is not provided for the content argument, the nested
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3589 expression will capture all whitespace-delimited content between delimiters
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3590 as a list of separate values.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3591
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3592 Use the C{ignoreExpr} argument to define expressions that may contain
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3593 opening or closing characters that should not be treated as opening
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3594 or closing characters for nesting, such as quotedString or a comment
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3595 expression. Specify multiple expressions using an C{L{Or}} or C{L{MatchFirst}}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3596 The default is L{quotedString}, but if no expressions are to be ignored,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3597 then pass C{None} for this argument.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3598 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3599 if opener == closer:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3600 raise ValueError("opening and closing strings cannot be the same")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3601 if content is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3602 if isinstance(opener,basestring) and isinstance(closer,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3603 if len(opener) == 1 and len(closer)==1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3604 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3605 content = (Combine(OneOrMore(~ignoreExpr +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3606 CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS,exact=1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3607 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3608 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3609 content = (empty.copy()+CharsNotIn(opener+closer+ParserElement.DEFAULT_WHITE_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3610 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3611 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3612 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3613 content = (Combine(OneOrMore(~ignoreExpr +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3614 ~Literal(opener) + ~Literal(closer) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3615 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3616 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3617 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3618 content = (Combine(OneOrMore(~Literal(opener) + ~Literal(closer) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3619 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3620 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3621 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3622 raise ValueError("opening and closing arguments must be strings if no content expression is given")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3623 ret = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3624 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3625 ret << Group( Suppress(opener) + ZeroOrMore( ignoreExpr | ret | content ) + Suppress(closer) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3626 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3627 ret << Group( Suppress(opener) + ZeroOrMore( ret | content ) + Suppress(closer) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3628 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3629
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3630 def indentedBlock(blockStatementExpr, indentStack, indent=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3631 """Helper method for defining space-delimited indentation blocks, such as
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3632 those used to define block statements in Python source code.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3633
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3634 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3635 - blockStatementExpr - expression defining syntax of statement that
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3636 is repeated within the indented block
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3637 - indentStack - list created by caller to manage indentation stack
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3638 (multiple statementWithIndentedBlock expressions within a single grammar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3639 should share a common indentStack)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3640 - indent - boolean indicating whether block must be indented beyond the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3641 the current level; set to False for block of left-most statements
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3642 (default=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3643
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3644 A valid block must contain at least one C{blockStatement}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3645 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3646 def checkPeerIndent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3647 if l >= len(s): return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3648 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3649 if curCol != indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3650 if curCol > indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3651 raise ParseFatalException(s,l,"illegal nesting")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3652 raise ParseException(s,l,"not a peer entry")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3653
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3654 def checkSubIndent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3655 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3656 if curCol > indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3657 indentStack.append( curCol )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3658 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3659 raise ParseException(s,l,"not a subentry")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3660
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3661 def checkUnindent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3662 if l >= len(s): return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3663 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3664 if not(indentStack and curCol < indentStack[-1] and curCol <= indentStack[-2]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3665 raise ParseException(s,l,"not an unindent")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3666 indentStack.pop()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3667
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3668 NL = OneOrMore(LineEnd().setWhitespaceChars("\t ").suppress())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3669 INDENT = Empty() + Empty().setParseAction(checkSubIndent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3670 PEER = Empty().setParseAction(checkPeerIndent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3671 UNDENT = Empty().setParseAction(checkUnindent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3672 if indent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3673 smExpr = Group( Optional(NL) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3674 #~ FollowedBy(blockStatementExpr) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3675 INDENT + (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) + UNDENT)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3676 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3677 smExpr = Group( Optional(NL) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3678 (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3679 blockStatementExpr.ignore(_bslash + LineEnd())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3680 return smExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3681
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3682 alphas8bit = srange(r"[\0xc0-\0xd6\0xd8-\0xf6\0xf8-\0xff]")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3683 punc8bit = srange(r"[\0xa1-\0xbf\0xd7\0xf7]")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3684
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3685 anyOpenTag,anyCloseTag = makeHTMLTags(Word(alphas,alphanums+"_:"))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3686 commonHTMLEntity = Combine(_L("&") + oneOf("gt lt amp nbsp quot").setResultsName("entity") +";").streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3687 _htmlEntityMap = dict(zip("gt lt amp nbsp quot".split(),'><& "'))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3688 replaceHTMLEntity = lambda t : t.entity in _htmlEntityMap and _htmlEntityMap[t.entity] or None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3689
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3690 # it's easy to get these comment structures wrong - they're very common, so may as well make them available
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3691 cStyleComment = Regex(r"/\*(?:[^*]*\*+)+?/").setName("C style comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3692
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3693 htmlComment = Regex(r"<!--[\s\S]*?-->")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3694 restOfLine = Regex(r".*").leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3695 dblSlashComment = Regex(r"\/\/(\\\n|.)*").setName("// comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3696 cppStyleComment = Regex(r"/(?:\*(?:[^*]*\*+)+?/|/[^\n]*(?:\n[^\n]*)*?(?:(?<!\\)|\Z))").setName("C++ style comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3697
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3698 javaStyleComment = cppStyleComment
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3699 pythonStyleComment = Regex(r"#.*").setName("Python style comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3700 _noncomma = "".join( [ c for c in printables if c != "," ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3701 _commasepitem = Combine(OneOrMore(Word(_noncomma) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3702 Optional( Word(" \t") +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3703 ~Literal(",") + ~LineEnd() ) ) ).streamline().setName("commaItem")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3704 commaSeparatedList = delimitedList( Optional( quotedString.copy() | _commasepitem, default="") ).setName("commaSeparatedList")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3705
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3706
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3707 if __name__ == "__main__":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3708
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3709 def test( teststring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3710 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3711 tokens = simpleSQL.parseString( teststring )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3712 tokenlist = tokens.asList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3713 print (teststring + "->" + str(tokenlist))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3714 print ("tokens = " + str(tokens))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3715 print ("tokens.columns = " + str(tokens.columns))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3716 print ("tokens.tables = " + str(tokens.tables))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3717 print (tokens.asXML("SQL",True))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3718 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3719 err = sys.exc_info()[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3720 print (teststring + "->")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3721 print (err.line)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3722 print (" "*(err.column-1) + "^")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3723 print (err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3724 print()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3725
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3726 selectToken = CaselessLiteral( "select" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3727 fromToken = CaselessLiteral( "from" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3728
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3729 ident = Word( alphas, alphanums + "_$" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3730 columnName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3731 columnNameList = Group( delimitedList( columnName ) )#.setName("columns")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3732 tableName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3733 tableNameList = Group( delimitedList( tableName ) )#.setName("tables")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3734 simpleSQL = ( selectToken + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3735 ( '*' | columnNameList ).setResultsName( "columns" ) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3736 fromToken + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3737 tableNameList.setResultsName( "tables" ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3738
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3739 test( "SELECT * from XYZZY, ABC" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3740 test( "select * from SYS.XYZZY" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3741 test( "Select A from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3742 test( "Select AA,BB,CC from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3743 test( "Select A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3744 test( "Select A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3745 test( "Xelect A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3746 test( "Select A, B, C frox Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3747 test( "Select" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3748 test( "Select ^^^ frox Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3749 test( "Select A, B, C from Sys.dual, Table2 " )