annotate abc2xml/pyparsing.py @ 1084:b1dbb76f4eb9 build-default-404

Update abc2xml to latest - Python3 friendly.
author Jim Hague <jim.hague@acm.org>
date Fri, 18 Nov 2022 21:42:55 +0000
parents 4fab69a1027d
children
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 #
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3 # Copyright (c) 2003-2013 Paul T. McGuire
484
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
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
25 __doc__ = \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
26 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
27 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
28
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
29 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
30 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
31 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
32 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
33
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
34 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
35
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
36 from pyparsing import Word, alphas
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
37
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
38 # define grammar of a greeting
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
39 greet = Word( alphas ) + "," + Word( alphas ) + "!"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
40
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
41 hello = "Hello, World!"
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
42 print (hello, "->", greet.parseString( hello ))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
43
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
44 The program outputs the following::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
45
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
46 Hello, World! -> ['Hello', ',', 'World', '!']
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
47
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
48 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
49 class names, and the use of '+', '|' and '^' operators.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
50
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
51 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
52 object with named attributes.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
53
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
54 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
55 - 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
56 - quoted strings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
57 - embedded comments
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
58 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
59
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
60 __version__ = "2.0.1"
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
61 __versionTime__ = "16 July 2013 22:22"
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
62 __author__ = "Paul McGuire <ptmcg@users.sourceforge.net>"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
63
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
64 import string
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
65 from weakref import ref as wkref
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
66 import copy
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
67 import sys
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
68 import warnings
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
69 import re
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
70 import sre_constants
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
71 import collections
484
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',
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
84 'dblSlashComment', 'delimitedList', 'dictOf', 'downcaseTokens', 'empty', 'hexnums',
484
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',
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
91 'indentedBlock', 'originalTextFor', 'ungroup', 'infixNotation',
484
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
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
94 PY_3 = sys.version.startswith('3')
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
95 if PY_3:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
96 _MAX_INT = sys.maxsize
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
97 basestring = str
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
98 unichr = chr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
99 _ustr = str
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
100
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
101 # build list of single arg builtins, that can be used as parse actions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
102 singleArgBuiltins = [sum, len, sorted, reversed, list, tuple, set, any, all, min, max]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
103
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
104 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
105 _MAX_INT = sys.maxint
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
106 range = xrange
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
107
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
108 def _ustr(obj):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
109 """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
110 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
111 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
112 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
113 if isinstance(obj,unicode):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
114 return obj
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 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
117 # 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
118 # it won't break any existing code.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
119 return str(obj)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
120
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
121 except UnicodeEncodeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
122 # 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
123 # 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
124 # 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
125 # object"?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
126 # If so, then return a unicode object:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
127 return unicode(obj)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
128 # 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
129 # Replace unprintables with escape codes?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
130 #return unicode(obj).encode(sys.getdefaultencoding(), 'backslashreplace_errors')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
131 # Replace unprintables with question marks?
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
132 #return unicode(obj).encode(sys.getdefaultencoding(), 'replace')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
133 # ...
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
134
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
135 # build list of single arg builtins, tolerant of Python version, that can be used as parse actions
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
136 singleArgBuiltins = []
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
137 import __builtin__
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
138 for fname in "sum len sorted reversed list tuple set any all min max".split():
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
139 try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
140 singleArgBuiltins.append(getattr(__builtin__,fname))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
141 except AttributeError:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
142 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
143
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
144
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
145 def _xml_escape(data):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
146 """Escape &, <, >, ", ', etc. in a string of data."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
147
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
148 # ampersand must be replaced first
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
149 from_symbols = '&><"\''
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
150 to_symbols = ('&'+s+';' for s in "amp gt lt quot apos".split())
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
151 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
152 data = data.replace(from_, to_)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
153 return data
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
154
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
155 class _Constants(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
156 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
157
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
158 alphas = string.ascii_lowercase + string.ascii_uppercase
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
159 nums = "0123456789"
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
160 hexnums = nums + "ABCDEFabcdef"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
161 alphanums = alphas + nums
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
162 _bslash = chr(92)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
163 printables = "".join(c for c in string.printable if c not in string.whitespace)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
164
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
165 class ParseBaseException(Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
166 """base exception class for all parsing runtime exceptions"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
167 # 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
168 # constructor as small and fast as possible
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
169 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
170 self.loc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
171 if msg is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
172 self.msg = pstr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
173 self.pstr = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
174 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
175 self.msg = msg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
176 self.pstr = pstr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
177 self.parserElement = elem
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
178
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
179 def __getattr__( self, aname ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
180 """supported attributes by name are:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
181 - 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
182 - 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
183 - line - returns the line containing the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
184 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
185 if( aname == "lineno" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
186 return lineno( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
187 elif( aname in ("col", "column") ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
188 return col( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
189 elif( aname == "line" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
190 return line( self.loc, self.pstr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
191 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
192 raise AttributeError(aname)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
193
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
194 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
195 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
196 ( self.msg, self.loc, self.lineno, self.column )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
197 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
198 return _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
199 def markInputline( self, markerString = ">!<" ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
200 """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
201 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
202 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
203 line_str = self.line
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
204 line_column = self.column - 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
205 if markerString:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
206 line_str = "".join(line_str[:line_column],
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
207 markerString, line_str[line_column:])
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
208 return line_str.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
209 def __dir__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
210 return "loc msg pstr parserElement lineno col line " \
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
211 "markInputline __str__ __repr__".split()
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
212
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
213 class ParseException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
214 """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
215 supported attributes by name are:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
216 - 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
217 - 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
218 - line - returns the line containing the exception text
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
219 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
220 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
221
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
222 class ParseFatalException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
223 """user-throwable exception thrown when inconsistent parse content
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
224 is found; stops all parsing immediately"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
225 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
226
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
227 class ParseSyntaxException(ParseFatalException):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
228 """just like C{L{ParseFatalException}}, but thrown internally when an
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
229 C{L{ErrorStop<And._ErrorStop>}} ('-' operator) indicates that parsing is to stop immediately because
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
230 an unbacktrackable syntax error has been found"""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
231 def __init__(self, pe):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
232 super(ParseSyntaxException, self).__init__(
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
233 pe.pstr, pe.loc, pe.msg, pe.parserElement)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
234
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
235 #~ class ReparseException(ParseBaseException):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
236 #~ """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
237 #~ pyparsing to reparse the input string:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
238 #~ - with a modified input string, and/or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
239 #~ - with a modified start location
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
240 #~ 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
241 #~ 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
242 #~ 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
243 #~ """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
244 #~ def __init_( self, newstring, restartLoc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
245 #~ self.newParseText = newstring
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
246 #~ self.reparseLoc = restartLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
247
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
248 class RecursiveGrammarException(Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
249 """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
250 def __init__( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
251 self.parseElementTrace = parseElementList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
252
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
253 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
254 return "RecursiveGrammarException: %s" % self.parseElementTrace
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 class _ParseResultsWithOffset(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
257 def __init__(self,p1,p2):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
258 self.tup = (p1,p2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
259 def __getitem__(self,i):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
260 return self.tup[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
261 def __repr__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
262 return repr(self.tup)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
263 def setOffset(self,i):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
264 self.tup = (self.tup[0],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
265
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
266 class ParseResults(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
267 """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
268 - as a list (C{len(results)})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
269 - 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
270 - by attribute (C{results.<resultsName>})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
271 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
272 #~ __slots__ = ( "__toklist", "__tokdict", "__doinit", "__name", "__parent", "__accumNames", "__weakref__" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
273 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
274 if isinstance(toklist, cls):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
275 return toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
276 retobj = object.__new__(cls)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
277 retobj.__doinit = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
278 return retobj
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
279
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
280 # 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
281 # constructor as small and fast as possible
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
282 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
283 if self.__doinit:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
284 self.__doinit = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
285 self.__name = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
286 self.__parent = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
287 self.__accumNames = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
288 if isinstance(toklist, list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
289 self.__toklist = toklist[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
290 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
291 self.__toklist = [toklist]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
292 self.__tokdict = dict()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
293
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
294 if name is not None and name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
295 if not modal:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
296 self.__accumNames[name] = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
297 if isinstance(name,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
298 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
299 self.__name = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
300 if not toklist in (None,'',[]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
301 if isinstance(toklist,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
302 toklist = [ toklist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
303 if asList:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
304 if isinstance(toklist,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
305 self[name] = _ParseResultsWithOffset(toklist.copy(),0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
306 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
307 self[name] = _ParseResultsWithOffset(ParseResults(toklist[0]),0)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
308 self[name].__name = name
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 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
311 self[name] = toklist[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
312 except (KeyError,TypeError,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
313 self[name] = toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
314
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
315 def __getitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
316 if isinstance( i, (int,slice) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
317 return self.__toklist[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
318 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
319 if i not in self.__accumNames:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
320 return self.__tokdict[i][-1][0]
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 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
323
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
324 def __setitem__( self, k, v, isinstance=isinstance ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
325 if isinstance(v,_ParseResultsWithOffset):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
326 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
327 sub = v[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
328 elif isinstance(k,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
329 self.__toklist[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
330 sub = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
331 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
332 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
333 sub = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
334 if isinstance(sub,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
335 sub.__parent = wkref(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
336
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
337 def __delitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
338 if isinstance(i,(int,slice)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
339 mylen = len( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
340 del self.__toklist[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
341
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
342 # convert int to slice
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
343 if isinstance(i, int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
344 if i < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
345 i += mylen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
346 i = slice(i, i+1)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
347 # get removed indices
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
348 removed = list(range(*i.indices(mylen)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
349 removed.reverse()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
350 # fixup indices in token dictionary
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
351 for name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
352 occurrences = self.__tokdict[name]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
353 for j in removed:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
354 for k, (value, position) in enumerate(occurrences):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
355 occurrences[k] = _ParseResultsWithOffset(value, position - (position > j))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
356 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
357 del self.__tokdict[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
358
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
359 def __contains__( self, k ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
360 return k in self.__tokdict
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 __len__( self ): return len( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
363 def __bool__(self): return len( self.__toklist ) > 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
364 __nonzero__ = __bool__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
365 def __iter__( self ): return iter( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
366 def __reversed__( self ): return iter( self.__toklist[::-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
367 def keys( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
368 """Returns all named result keys."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
369 return self.__tokdict.keys()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
370
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
371 def pop( self, index=-1 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
372 """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
373 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
374 ret = self[index]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
375 del self[index]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
376 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
377
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
378 def get(self, key, defaultValue=None):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
379 """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
380 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
381 C{defaultValue} is specified."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
382 if key in self:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
383 return self[key]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
384 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
385 return defaultValue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
386
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
387 def insert( self, index, insStr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
388 """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
389 self.__toklist.insert(index, insStr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
390 # fixup indices in token dictionary
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
391 for name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
392 occurrences = self.__tokdict[name]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
393 for k, (value, position) in enumerate(occurrences):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
394 occurrences[k] = _ParseResultsWithOffset(value, position + (position > index))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
395
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
396 def items( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
397 """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
398 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
399
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
400 def values( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
401 """Returns all named result values."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
402 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
403
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
404 def __getattr__( self, name ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
405 if True: #name not in self.__slots__:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
406 if name in self.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
407 if name not in self.__accumNames:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
408 return self.__tokdict[name][-1][0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
409 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
410 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
411 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
412 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
413 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
414
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
415 def __add__( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
416 ret = self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
417 ret += other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
418 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
419
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
420 def __iadd__( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
421 if other.__tokdict:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
422 offset = len(self.__toklist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
423 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
424 otheritems = other.__tokdict.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
425 otherdictitems = [(k, _ParseResultsWithOffset(v[0],addoffset(v[1])) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
426 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
427 for k,v in otherdictitems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
428 self[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
429 if isinstance(v[0],ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
430 v[0].__parent = wkref(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
431
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
432 self.__toklist += other.__toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
433 self.__accumNames.update( other.__accumNames )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
434 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
435
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
436 def __radd__(self, other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
437 if isinstance(other,int) and other == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
438 return self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
439
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
440 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
441 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
442
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
443 def __str__( self ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
444 out = []
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
445 for i in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
446 if isinstance(i, ParseResults):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
447 out.append(_ustr(i))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
448 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
449 out.append(repr(i))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
450 return '[' + ', '.join(out) + ']'
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
451
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
452 def _asStringList( self, sep='' ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
453 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
454 for item in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
455 if out and sep:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
456 out.append(sep)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
457 if isinstance( item, ParseResults ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
458 out += item._asStringList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
459 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
460 out.append( _ustr(item) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
461 return out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
462
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
463 def asList( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
464 """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
465 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
466 for res in self.__toklist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
467 if isinstance(res,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
468 out.append( res.asList() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
469 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
470 out.append( res )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
471 return out
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
472
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
473 def asDict( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
474 """Returns the named parse results as dictionary."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
475 return dict( self.items() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
476
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
477 def copy( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
478 """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
479 ret = ParseResults( self.__toklist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
480 ret.__tokdict = self.__tokdict.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
481 ret.__parent = self.__parent
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
482 ret.__accumNames.update( self.__accumNames )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
483 ret.__name = self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
484 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
485
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
486 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
487 """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
488 nl = "\n"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
489 out = []
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
490 namedItems = dict((v[1],k) for (k,vlist) in self.__tokdict.items()
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
491 for v in vlist)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
492 nextLevelIndent = indent + " "
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
493
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
494 # collapse out indents if formatting is not desired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
495 if not formatted:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
496 indent = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
497 nextLevelIndent = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
498 nl = ""
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 selfTag = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
501 if doctag is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
502 selfTag = doctag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
503 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
504 if self.__name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
505 selfTag = self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
506
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
507 if not selfTag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
508 if namedItemsOnly:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
509 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
510 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
511 selfTag = "ITEM"
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 out += [ nl, indent, "<", selfTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
514
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
515 worklist = self.__toklist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
516 for i,res in enumerate(worklist):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
517 if isinstance(res,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
518 if i in namedItems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
519 out += [ res.asXML(namedItems[i],
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
520 namedItemsOnly and doctag is None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
521 nextLevelIndent,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
522 formatted)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
523 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
524 out += [ res.asXML(None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
525 namedItemsOnly and doctag is None,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
526 nextLevelIndent,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
527 formatted)]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
528 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
529 # 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
530 resTag = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
531 if i in namedItems:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
532 resTag = namedItems[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
533 if not resTag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
534 if namedItemsOnly:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
535 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
536 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
537 resTag = "ITEM"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
538 xmlBodyText = _xml_escape(_ustr(res))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
539 out += [ nl, nextLevelIndent, "<", resTag, ">",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
540 xmlBodyText,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
541 "</", resTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
542
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
543 out += [ nl, indent, "</", selfTag, ">" ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
544 return "".join(out)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
545
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
546 def __lookup(self,sub):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
547 for k,vlist in self.__tokdict.items():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
548 for v,loc in vlist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
549 if sub is v:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
550 return k
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
551 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
552
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
553 def getName(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
554 """Returns the results name for this token expression."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
555 if self.__name:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
556 return self.__name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
557 elif self.__parent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
558 par = self.__parent()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
559 if par:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
560 return par.__lookup(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
561 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
562 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
563 elif (len(self) == 1 and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
564 len(self.__tokdict) == 1 and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
565 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
566 return self.__tokdict.keys()[0]
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
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
570 def dump(self,indent='',depth=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
571 """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
572 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
573 in a nested display of other data."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
574 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
575 out.append( indent+_ustr(self.asList()) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
576 keys = self.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
577 keys.sort()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
578 for k,v in keys:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
579 if out:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
580 out.append('\n')
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
581 out.append( "%s%s- %s: " % (indent,(' '*depth), k) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
582 if isinstance(v,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
583 if v.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
584 out.append( v.dump(indent,depth+1) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
585 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
586 out.append(_ustr(v))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
587 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
588 out.append(_ustr(v))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
589 return "".join(out)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
590
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
591 # add support for pickle protocol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
592 def __getstate__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
593 return ( self.__toklist,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
594 ( self.__tokdict.copy(),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
595 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
596 self.__accumNames,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
597 self.__name ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
598
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
599 def __setstate__(self,state):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
600 self.__toklist = state[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
601 (self.__tokdict,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
602 par,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
603 inAccumNames,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
604 self.__name) = state[1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
605 self.__accumNames = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
606 self.__accumNames.update(inAccumNames)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
607 if par is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
608 self.__parent = wkref(par)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
609 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
610 self.__parent = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
611
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
612 def __dir__(self):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
613 return dir(super(ParseResults,self)) + list(self.keys())
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
614
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
615 if hasattr (collections, 'MutableMapping'):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
616 collections.MutableMapping.register(ParseResults)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
617 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
618 from collections.abc import MutableMapping
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
619 MutableMapping.register (ParseResults)
484
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
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
627 on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
484
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
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
639 on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
484
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 ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
656 print (("Match " + _ustr(expr) + " at loc " + _ustr(loc) + "(%d,%d)" % ( lineno(loc,instring), col(loc,instring) )))
484
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
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
668 # Only works on Python 3.x - nonlocal is toxic to Python 2 installs
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
669 #~ 'decorator to trim function calls to match the arity of the target'
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
670 #~ def _trim_arity(func, maxargs=3):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
671 #~ if func in singleArgBuiltins:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
672 #~ return lambda s,l,t: func(t)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
673 #~ limit = 0
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
674 #~ foundArity = False
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
675 #~ def wrapper(*args):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
676 #~ nonlocal limit,foundArity
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
677 #~ while 1:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
678 #~ try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
679 #~ ret = func(*args[limit:])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
680 #~ foundArity = True
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
681 #~ return ret
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
682 #~ except TypeError:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
683 #~ if limit == maxargs or foundArity:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
684 #~ raise
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
685 #~ limit += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
686 #~ continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
687 #~ return wrapper
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
688
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
689 # this version is Python 2.x-3.x cross-compatible
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
690 'decorator to trim function calls to match the arity of the target'
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
691 def _trim_arity(func, maxargs=2):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
692 if func in singleArgBuiltins:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
693 return lambda s,l,t: func(t)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
694 limit = [0]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
695 foundArity = [False]
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
696 def wrapper(*args):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
697 while 1:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
698 try:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
699 ret = func(*args[limit[0]:])
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
700 foundArity[0] = True
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
701 return ret
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
702 except TypeError:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
703 if limit[0] <= maxargs and not foundArity[0]:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
704 limit[0] += 1
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
705 continue
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
706 raise
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
707 return wrapper
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
708
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
709 class ParserElement(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
710 """Abstract base level parser element class."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
711 DEFAULT_WHITE_CHARS = " \n\t\r"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
712 verbose_stacktrace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
713
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
714 def setDefaultWhitespaceChars( chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
715 """Overrides the default whitespace chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
716 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
717 ParserElement.DEFAULT_WHITE_CHARS = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
718 setDefaultWhitespaceChars = staticmethod(setDefaultWhitespaceChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
719
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
720 def inlineLiteralsUsing(cls):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
721 """
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
722 Set class to be used for inclusion of string literals into a parser.
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
723 """
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
724 ParserElement.literalStringClass = cls
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
725 inlineLiteralsUsing = staticmethod(inlineLiteralsUsing)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
726
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
727 def __init__( self, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
728 self.parseAction = list()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
729 self.failAction = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
730 #~ 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
731 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
732 self.resultsName = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
733 self.saveAsList = savelist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
734 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
735 self.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
736 self.copyDefaultWhiteChars = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
737 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
738 self.keepTabs = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
739 self.ignoreExprs = list()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
740 self.debug = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
741 self.streamlined = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
742 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
743 self.errmsg = ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
744 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
745 self.debugActions = ( None, None, None ) #custom debug actions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
746 self.re = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
747 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
748 self.callDuringTry = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
749
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
750 def copy( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
751 """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
752 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
753 cpy = copy.copy( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
754 cpy.parseAction = self.parseAction[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
755 cpy.ignoreExprs = self.ignoreExprs[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
756 if self.copyDefaultWhiteChars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
757 cpy.whiteChars = ParserElement.DEFAULT_WHITE_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
758 return cpy
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 def setName( self, name ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
761 """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
762 self.name = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
763 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
764 if hasattr(self,"exception"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
765 self.exception.msg = self.errmsg
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
766 return self
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 setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
769 """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
770 of the returned parse results.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
771 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
772 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
773 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
774
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
775 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
776 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
777 see L{I{__call__}<__call__>}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
778 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
779 newself = self.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
780 if name.endswith("*"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
781 name = name[:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
782 listAllMatches=True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
783 newself.resultsName = name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
784 newself.modalResults = not listAllMatches
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
785 return newself
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
786
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
787 def setBreak(self,breakFlag = True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
788 """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
789 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
790 disable.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
791 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
792 if breakFlag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
793 _parseMethod = self._parse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
794 def breaker(instring, loc, doActions=True, callPreParse=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
795 import pdb
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
796 pdb.set_trace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
797 return _parseMethod( instring, loc, doActions, callPreParse )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
798 breaker._originalParseMethod = _parseMethod
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
799 self._parse = breaker
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
800 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
801 if hasattr(self._parse,"_originalParseMethod"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
802 self._parse = self._parse._originalParseMethod
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
803 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
804
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
805 def setParseAction( self, *fns, **kwargs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
806 """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
807 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
808 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
809 - 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
810 - loc = the location of the matching substring
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
811 - toks = a list of the matched tokens, packaged as a C{L{ParseResults}} object
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
812 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
813 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
814 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
815
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
816 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
817 before starting the parsing process. See L{I{parseString}<parseString>} for more information
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
818 on parsing strings containing C{<TAB>}s, and suggested methods to maintain a
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
819 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
820 positions within the parsed string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
821 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
822 self.parseAction = list(map(_trim_arity, list(fns)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
823 self.callDuringTry = ("callDuringTry" in kwargs and kwargs["callDuringTry"])
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 addParseAction( self, *fns, **kwargs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
827 """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
828 self.parseAction += list(map(_trim_arity, list(fns)))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
829 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
830 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
831
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
832 def setFailAction( self, fn ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
833 """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
834 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
835 C{fn(s,loc,expr,err)} where:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
836 - s = string being parsed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
837 - 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
838 - expr = the parse expression that failed
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
839 - err = the exception thrown
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
840 The function returns no value. It may throw C{L{ParseFatalException}}
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
841 if it is desired to stop parsing immediately."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
842 self.failAction = fn
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
843 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
844
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
845 def _skipIgnorables( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
846 exprsFound = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
847 while exprsFound:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
848 exprsFound = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
849 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
850 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
851 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
852 loc,dummy = e._parse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
853 exprsFound = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
854 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
855 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
856 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
857
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
858 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
859 if self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
860 loc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
861
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
862 if self.skipWhitespace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
863 wt = self.whiteChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
864 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
865 while loc < instrlen and instring[loc] in wt:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
866 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
867
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
868 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
869
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
870 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
871 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
872
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
873 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
874 return tokenlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
875
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
876 #~ @profile
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
877 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
878 debugging = ( self.debug ) #and doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
879
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
880 if debugging or self.failAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
881 #~ 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
882 if (self.debugActions[0] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
883 self.debugActions[0]( instring, loc, self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
884 if callPreParse and self.callPreparse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
885 preloc = self.preParse( instring, loc )
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 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
888 tokensStart = preloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
889 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
890 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
891 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
892 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
893 raise ParseException( instring, len(instring), self.errmsg, self )
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
894 except ParseBaseException as err:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
895 #~ print ("Exception raised:", err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
896 if self.debugActions[2]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
897 self.debugActions[2]( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
898 if self.failAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
899 self.failAction( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
900 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
901 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
902 if callPreParse and self.callPreparse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
903 preloc = self.preParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
904 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
905 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
906 tokensStart = preloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
907 if self.mayIndexError or loc >= len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
908 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
909 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
910 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
911 raise ParseException( instring, len(instring), self.errmsg, self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
912 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
913 loc,tokens = self.parseImpl( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
914
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
915 tokens = self.postParse( instring, loc, tokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
916
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
917 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
918 if self.parseAction and (doActions or self.callDuringTry):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
919 if debugging:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
920 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
921 for fn in self.parseAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
922 tokens = fn( instring, tokensStart, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
923 if tokens is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
924 retTokens = ParseResults( tokens,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
925 self.resultsName,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
926 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
927 modal=self.modalResults )
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
928 except ParseBaseException as err:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
929 #~ print "Exception raised in user parse action:", err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
930 if (self.debugActions[2] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
931 self.debugActions[2]( instring, tokensStart, self, err )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
932 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
933 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
934 for fn in self.parseAction:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
935 tokens = fn( instring, tokensStart, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
936 if tokens is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
937 retTokens = ParseResults( tokens,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
938 self.resultsName,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
939 asList=self.saveAsList and isinstance(tokens,(ParseResults,list)),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
940 modal=self.modalResults )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
941
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
942 if debugging:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
943 #~ print ("Matched",self,"->",retTokens.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
944 if (self.debugActions[1] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
945 self.debugActions[1]( instring, tokensStart, loc, self, retTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
946
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
947 return loc, retTokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
948
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
949 def tryParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
950 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
951 return self._parse( instring, loc, doActions=False )[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
952 except ParseFatalException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
953 raise ParseException( instring, loc, self.errmsg, self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
954
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
955 # 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
956 # 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
957 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
958 lookup = (self,instring,loc,callPreParse,doActions)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
959 if lookup in ParserElement._exprArgCache:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
960 value = ParserElement._exprArgCache[ lookup ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
961 if isinstance(value, Exception):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
962 raise value
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
963 return (value[0],value[1].copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
964 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
965 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
966 value = self._parseNoCache( instring, loc, doActions, callPreParse )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
967 ParserElement._exprArgCache[ lookup ] = (value[0],value[1].copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
968 return value
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
969 except ParseBaseException as pe:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
970 pe.__traceback__ = None
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
971 ParserElement._exprArgCache[ lookup ] = pe
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
972 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
973
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
974 _parse = _parseNoCache
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 # 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
977 _exprArgCache = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
978 def resetCache():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
979 ParserElement._exprArgCache.clear()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
980 resetCache = staticmethod(resetCache)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
981
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
982 _packratEnabled = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
983 def enablePackrat():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
984 """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
985 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
986 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
987 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
988 both valid results and parsing exceptions.
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 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
991 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
992 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
993 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
994 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
995 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
996 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
997 after importing pyparsing.
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 if not ParserElement._packratEnabled:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1000 ParserElement._packratEnabled = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1001 ParserElement._parse = ParserElement._parseCache
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1002 enablePackrat = staticmethod(enablePackrat)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1003
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1004 def parseString( self, instring, parseAll=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1005 """Execute the parse expression with the given string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1006 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
1007 expression has been built.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1008
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1009 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
1010 successfully parsed, then set C{parseAll} to True (equivalent to ending
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1011 the grammar with C{L{StringEnd()}}).
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1012
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1013 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
1014 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
1015 If the input string contains tabs and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1016 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
1017 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
1018 string by:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1019 - 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
1020 (see L{I{parseWithTabs}<parseWithTabs>})
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1021 - 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
1022 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
1023 - 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
1024 C{parseString}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1025 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1026 ParserElement.resetCache()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1027 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1028 self.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1029 #~ self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1030 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1031 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1032 if not self.keepTabs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1033 instring = instring.expandtabs()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1034 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1035 loc, tokens = self._parse( instring, 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1036 if parseAll:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1037 loc = self.preParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1038 se = Empty() + StringEnd()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1039 se._parse( instring, loc )
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1040 except ParseBaseException as exc:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1041 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1042 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1043 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1044 # 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
1045 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1046 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1047 return tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1048
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1049 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
1050 """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
1051 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
1052 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
1053 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
1054
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1055 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
1056 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
1057 strings with embedded tabs."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1058 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1059 self.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1060 for e in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1061 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1062
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1063 if not self.keepTabs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1064 instring = _ustr(instring).expandtabs()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1065 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1066 loc = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1067 preparseFn = self.preParse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1068 parseFn = self._parse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1069 ParserElement.resetCache()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1070 matches = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1071 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1072 while loc <= instrlen and matches < maxMatches:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1073 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1074 preloc = preparseFn( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1075 nextLoc,tokens = parseFn( instring, preloc, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1076 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1077 loc = preloc+1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1078 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1079 if nextLoc > loc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1080 matches += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1081 yield tokens, preloc, nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1082 if overlap:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1083 nextloc = preparseFn( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1084 if nextloc > loc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1085 loc = nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1086 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1087 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1088 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1089 loc = nextLoc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1090 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1091 loc = preloc+1
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1092 except ParseBaseException as exc:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1093 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1094 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1095 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1096 # 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
1097 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1098
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1099 def transformString( self, instring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1100 """Extension to C{L{scanString}}, to modify matching text with modified tokens that may
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1101 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
1102 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
1103 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
1104 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
1105 action. C{transformString()} returns the resulting transformed string."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1106 out = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1107 lastE = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1108 # 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
1109 # keep string locs straight between transformString and scanString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1110 self.keepTabs = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1111 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1112 for t,s,e in self.scanString( instring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1113 out.append( instring[lastE:s] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1114 if t:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1115 if isinstance(t,ParseResults):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1116 out += t.asList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1117 elif isinstance(t,list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1118 out += t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1119 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1120 out.append(t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1121 lastE = e
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1122 out.append(instring[lastE:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1123 out = [o for o in out if o]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1124 return "".join(map(_ustr,_flatten(out)))
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1125 except ParseBaseException as exc:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1126 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1127 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1128 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1129 # 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
1130 raise exc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1131
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1132 def searchString( self, instring, maxMatches=_MAX_INT ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1133 """Another extension to C{L{scanString}}, simplifying the access to the tokens found
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1134 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
1135 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
1136 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1137 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1138 return ParseResults([ t for t,s,e in self.scanString( instring, maxMatches ) ])
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1139 except ParseBaseException as exc:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1140 if ParserElement.verbose_stacktrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1141 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1142 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1143 # 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
1144 raise exc
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 __add__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1147 """Implementation of + operator - returns C{L{And}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1148 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1149 other = ParserElement.literalStringClass( other )
484
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 And( [ self, other ] )
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 __radd__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1157 """Implementation of + operator when left operand is not a C{L{ParserElement}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1158 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1159 other = ParserElement.literalStringClass( other )
484
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 other + self
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 __sub__(self, other):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1167 """Implementation of - operator, returns C{L{And}} with error stop"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1168 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1169 other = ParserElement.literalStringClass( other )
484
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 And( [ self, And._ErrorStop(), other ] )
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 __rsub__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1177 """Implementation of - operator when left operand is not a C{L{ParserElement}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1178 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1179 other = ParserElement.literalStringClass( other )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1180 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1181 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
1182 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1183 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1184 return other - self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1185
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1186 def __mul__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1187 """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
1188 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
1189 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
1190 may also include C{None} as in:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1191 - C{expr*(n,None)} or C{expr*(n,)} is equivalent
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1192 to C{expr*n + L{ZeroOrMore}(expr)}
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1193 (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
1194 - 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
1195 (read as "0 to n instances of C{expr}")
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1196 - C{expr*(None,None)} is equivalent to C{L{ZeroOrMore}(expr)}
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1197 - C{expr*(1,None)} is equivalent to C{L{OneOrMore}(expr)}
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1198
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1199 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
1200 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
1201 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
1202 occurrences. If this behavior is desired, then write
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1203 C{expr*(None,n) + ~expr}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1204
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1205 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1206 if isinstance(other,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1207 minElements, optElements = other,0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1208 elif isinstance(other,tuple):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1209 other = (other + (None, None))[:2]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1210 if other[0] is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1211 other = (0, other[1])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1212 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
1213 if other[0] == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1214 return ZeroOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1215 if other[0] == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1216 return OneOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1217 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1218 return self*other[0] + ZeroOrMore(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1219 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
1220 minElements, optElements = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1221 optElements -= minElements
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1222 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1223 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
1224 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1225 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
1226
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1227 if minElements < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1228 raise ValueError("cannot multiply ParserElement by negative value")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1229 if optElements < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1230 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
1231 if minElements == optElements == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1232 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
1233
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1234 if (optElements):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1235 def makeOptionalList(n):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1236 if n>1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1237 return Optional(self + makeOptionalList(n-1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1238 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1239 return Optional(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1240 if minElements:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1241 if minElements == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1242 ret = self + makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1243 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1244 ret = And([self]*minElements) + makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1245 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1246 ret = makeOptionalList(optElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1247 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1248 if minElements == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1249 ret = self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1250 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1251 ret = And([self]*minElements)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1252 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1253
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1254 def __rmul__(self, other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1255 return self.__mul__(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 __or__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1258 """Implementation of | operator - returns C{L{MatchFirst}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1259 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1260 other = ParserElement.literalStringClass( other )
484
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 MatchFirst( [ self, other ] )
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 __ror__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1268 """Implementation of | operator when left operand is not a C{L{ParserElement}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1269 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1270 other = ParserElement.literalStringClass( other )
484
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 other | self
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 __xor__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1278 """Implementation of ^ operator - returns C{L{Or}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1279 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1280 other = ParserElement.literalStringClass( other )
484
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 Or( [ self, other ] )
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 __rxor__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1288 """Implementation of ^ operator when left operand is not a C{L{ParserElement}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1289 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1290 other = ParserElement.literalStringClass( other )
484
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 other ^ self
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 __and__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1298 """Implementation of & operator - returns C{L{Each}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1299 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1300 other = ParserElement.literalStringClass( other )
484
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 Each( [ self, other ] )
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 __rand__(self, other ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1308 """Implementation of & operator when left operand is not a C{L{ParserElement}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1309 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1310 other = ParserElement.literalStringClass( other )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1311 if not isinstance( other, ParserElement ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1312 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
1313 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1314 return None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1315 return other & self
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 def __invert__( self ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1318 """Implementation of ~ operator - returns C{L{NotAny}}"""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1319 return NotAny( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1320
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1321 def __call__(self, name):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1322 """Shortcut for C{L{setResultsName}}, with C{listAllMatches=default}::
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1323 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
1324 could be written as::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1325 userdata = Word(alphas)("name") + Word(nums+"-")("socsecno")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1326
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1327 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
1328 passed as C{True}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1329 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1330 return self.setResultsName(name)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1331
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1332 def suppress( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1333 """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
1334 cluttering up returned output.
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 return Suppress( self )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1337
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1338 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1339 """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
1340 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
1341 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
1342 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1343 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1344 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1345
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1346 def setWhitespaceChars( self, chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1347 """Overrides the default whitespace chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1348 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1349 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1350 self.whiteChars = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1351 self.copyDefaultWhiteChars = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1352 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1353
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1354 def parseWithTabs( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1355 """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
1356 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
1357 match C{<TAB>} characters."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1358 self.keepTabs = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1359 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1360
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1361 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1362 """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
1363 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
1364 ignorable patterns.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1365 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1366 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1367 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1368 self.ignoreExprs.append( other.copy() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1369 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1370 self.ignoreExprs.append( Suppress( other.copy() ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1371 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1372
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1373 def setDebugActions( self, startAction, successAction, exceptionAction ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1374 """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
1375 self.debugActions = (startAction or _defaultStartDebugAction,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1376 successAction or _defaultSuccessDebugAction,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1377 exceptionAction or _defaultExceptionDebugAction)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1378 self.debug = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1379 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1380
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1381 def setDebug( self, flag=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1382 """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
1383 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
1384 if flag:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1385 self.setDebugActions( _defaultStartDebugAction, _defaultSuccessDebugAction, _defaultExceptionDebugAction )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1386 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1387 self.debug = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1388 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1389
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1390 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1391 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1392
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1393 def __repr__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1394 return _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1395
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1396 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1397 self.streamlined = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1398 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1399 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1400
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1401 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1402 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1403
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1404 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1405 """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
1406 self.checkRecursion( [] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1407
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1408 def parseFile( self, file_or_filename, parseAll=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1409 """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
1410 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
1411 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
1412 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1413 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1414 file_contents = file_or_filename.read()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1415 except AttributeError:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1416 f = open(file_or_filename, "r")
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1417 file_contents = f.read()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1418 f.close()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1419 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1420 return self.parseString(file_contents, parseAll)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1421 except ParseBaseException as exc:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1422 if ParserElement.verbose_stacktrace:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1423 raise
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1424 else:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1425 # catch and re-raise exception from here, clears out pyparsing internal stack trace
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1426 raise exc
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1427
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1428 def __eq__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1429 if isinstance(other, ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1430 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
1431 elif isinstance(other, basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1432 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1433 self.parseString(_ustr(other), parseAll=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1434 return True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1435 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1436 return False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1437 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1438 return super(ParserElement,self)==other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1439
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1440 def __ne__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1441 return not (self == other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1442
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1443 def __hash__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1444 return hash(id(self))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1445
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1446 def __req__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1447 return self == other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1448
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1449 def __rne__(self,other):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1450 return not (self == other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1451
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1452
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1453 class Token(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1454 """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
1455 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1456 super(Token,self).__init__( savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1457
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1458 def setName(self, name):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1459 s = super(Token,self).setName(name)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1460 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1461 return s
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1462
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1463
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1464 class Empty(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1465 """An empty token, will always match."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1466 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1467 super(Empty,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1468 self.name = "Empty"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1469 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1470 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1471
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1472
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1473 class NoMatch(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1474 """A token that will never match."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1475 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1476 super(NoMatch,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1477 self.name = "NoMatch"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1478 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1479 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1480 self.errmsg = "Unmatchable token"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1481
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1482 def parseImpl( self, instring, loc, doActions=True ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1483 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1484
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 class Literal(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1487 """Token to exactly match a specified string."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1488 def __init__( self, matchString ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1489 super(Literal,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1490 self.match = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1491 self.matchLen = len(matchString)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1492 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1493 self.firstMatchChar = matchString[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1494 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1495 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
1496 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1497 self.__class__ = Empty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1498 self.name = '"%s"' % _ustr(self.match)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1499 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1500 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1501 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1502
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1503 # Performance tuning: this routine gets called a *lot*
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1504 # 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
1505 # 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
1506 #~ @profile
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1507 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1508 if (instring[loc] == self.firstMatchChar and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1509 (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
1510 return loc+self.matchLen, self.match
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1511 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1512 _L = Literal
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1513 ParserElement.literalStringClass = Literal
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1514
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1515 class Keyword(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1516 """Token to exactly match a specified string as a keyword, that is, it must be
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1517 immediately followed by a non-keyword character. Compare with C{L{Literal}}::
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1518 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
1519 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
1520 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
1521 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
1522 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
1523 matching, default is C{False}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1524 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1525 DEFAULT_KEYWORD_CHARS = alphanums+"_$"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1526
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1527 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
1528 super(Keyword,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1529 self.match = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1530 self.matchLen = len(matchString)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1531 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1532 self.firstMatchChar = matchString[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1533 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1534 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
1535 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1536 self.name = '"%s"' % self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1537 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1538 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1539 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1540 self.caseless = caseless
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1541 if caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1542 self.caselessmatch = matchString.upper()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1543 identChars = identChars.upper()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1544 self.identChars = set(identChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1545
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1546 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1547 if self.caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1548 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
1549 (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
1550 (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
1551 return loc+self.matchLen, self.match
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1552 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1553 if (instring[loc] == self.firstMatchChar and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1554 (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
1555 (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
1556 (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
1557 return loc+self.matchLen, self.match
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1558 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1559
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1560 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1561 c = super(Keyword,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1562 c.identChars = Keyword.DEFAULT_KEYWORD_CHARS
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1563 return c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1564
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1565 def setDefaultKeywordChars( chars ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1566 """Overrides the default Keyword chars
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 Keyword.DEFAULT_KEYWORD_CHARS = chars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1569 setDefaultKeywordChars = staticmethod(setDefaultKeywordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1570
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1571 class CaselessLiteral(Literal):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1572 """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
1573 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
1574 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
1575 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1576 def __init__( self, matchString ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1577 super(CaselessLiteral,self).__init__( matchString.upper() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1578 # Preserve the defining literal.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1579 self.returnString = matchString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1580 self.name = "'%s'" % self.returnString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1581 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1582
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1583 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1584 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
1585 return loc+self.matchLen, self.returnString
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1586 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1587
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1588 class CaselessKeyword(Keyword):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1589 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
1590 super(CaselessKeyword,self).__init__( matchString, identChars, caseless=True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1591
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1592 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1593 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
1594 (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
1595 return loc+self.matchLen, self.match
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1596 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1597
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1598 class Word(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1599 """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
1600 Defined with string containing all allowed initial characters,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1601 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
1602 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
1603 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
1604 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
1605 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
1606 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
1607 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
1608 except for one or two characters, for instance.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1609 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1610 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
1611 super(Word,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1612 if excludeChars:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1613 initChars = ''.join(c for c in initChars if c not in excludeChars)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1614 if bodyChars:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1615 bodyChars = ''.join(c for c in bodyChars if c not in excludeChars)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1616 self.initCharsOrig = initChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1617 self.initChars = set(initChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1618 if bodyChars :
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1619 self.bodyCharsOrig = bodyChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1620 self.bodyChars = set(bodyChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1621 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1622 self.bodyCharsOrig = initChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1623 self.bodyChars = set(initChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1624
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1625 self.maxSpecified = max > 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1626
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1627 if min < 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1628 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
1629
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1630 self.minLen = min
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1631
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1632 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1633 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1634 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1635 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1636
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1637 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1638 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1639 self.minLen = exact
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.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1642 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1643 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1644 self.asKeyword = asKeyword
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 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
1647 if self.bodyCharsOrig == self.initCharsOrig:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1648 self.reString = "[%s]+" % _escapeRegexRangeChars(self.initCharsOrig)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1649 elif len(self.bodyCharsOrig) == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1650 self.reString = "%s[%s]*" % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1651 (re.escape(self.initCharsOrig),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1652 _escapeRegexRangeChars(self.bodyCharsOrig),)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1653 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1654 self.reString = "[%s][%s]*" % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1655 (_escapeRegexRangeChars(self.initCharsOrig),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1656 _escapeRegexRangeChars(self.bodyCharsOrig),)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1657 if self.asKeyword:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1658 self.reString = r"\b"+self.reString+r"\b"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1659 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1660 self.re = re.compile( self.reString )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1661 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1662 self.re = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1663
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1664 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1665 if self.re:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1666 result = self.re.match(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1667 if not result:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1668 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1669
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1670 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1671 return loc, result.group()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1672
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1673 if not(instring[ loc ] in self.initChars):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1674 raise ParseException(instring, loc, self.errmsg, self)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1675
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1676 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1677 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1678 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1679 bodychars = self.bodyChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1680 maxloc = start + self.maxLen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1681 maxloc = min( maxloc, instrlen )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1682 while loc < maxloc and instring[loc] in bodychars:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1683 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1684
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1685 throwException = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1686 if loc - start < self.minLen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1687 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1688 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
1689 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1690 if self.asKeyword:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1691 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
1692 throwException = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1693
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1694 if throwException:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1695 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1696
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1697 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1698
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1699 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1700 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1701 return super(Word,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1702 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1703 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1704
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1705
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1706 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1707
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1708 def charsAsStr(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1709 if len(s)>4:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1710 return s[:4]+"..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1711 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1712 return s
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1713
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1714 if ( self.initCharsOrig != self.bodyCharsOrig ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1715 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
1716 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1717 self.strRepr = "W:(%s)" % charsAsStr(self.initCharsOrig)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1718
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1719 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1720
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1721
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1722 class Regex(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1723 """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
1724 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
1725 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1726 compiledREtype = type(re.compile("[A-Z]"))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1727 def __init__( self, pattern, flags=0):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1728 """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
1729 super(Regex,self).__init__()
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 if isinstance(pattern, basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1732 if len(pattern) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1733 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
1734 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1735
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1736 self.pattern = pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1737 self.flags = flags
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1738
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1739 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1740 self.re = re.compile(self.pattern, self.flags)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1741 self.reString = self.pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1742 except sre_constants.error:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1743 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
1744 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1745 raise
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 elif isinstance(pattern, Regex.compiledREtype):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1748 self.re = pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1749 self.pattern = \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1750 self.reString = str(pattern)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1751 self.flags = flags
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1752
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1753 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1754 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
1755
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1756 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1757 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1758 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1759 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1760
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1761 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1762 result = self.re.match(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1763 if not result:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1764 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1765
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1766 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1767 d = result.groupdict()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1768 ret = ParseResults(result.group())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1769 if d:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1770 for k in d:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1771 ret[k] = d[k]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1772 return loc,ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1773
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1774 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1775 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1776 return super(Regex,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1777 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1778 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1779
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1780 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1781 self.strRepr = "Re:(%s)" % repr(self.pattern)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1782
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1783 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1784
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1785
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1786 class QuotedString(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1787 """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
1788 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1789 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
1790 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1791 Defined with the following parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1792 - 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
1793 - 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
1794 - escQuote - special quote sequence to escape an embedded quote string (such as SQL's "" to escape an embedded ") (default=None)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1795 - multiline - boolean indicating whether quotes can span multiple lines (default=C{False})
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1796 - unquoteResults - boolean indicating whether the matched text should be unquoted (default=C{True})
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1797 - endQuoteChar - string of one or more characters defining the end of the quote delimited string (default=C{None} => same as quoteChar)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1798 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1799 super(QuotedString,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1800
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1801 # 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
1802 quoteChar = quoteChar.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1803 if len(quoteChar) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1804 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
1805 raise SyntaxError()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1806
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1807 if endQuoteChar is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1808 endQuoteChar = quoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1809 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1810 endQuoteChar = endQuoteChar.strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1811 if len(endQuoteChar) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1812 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
1813 raise SyntaxError()
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 self.quoteChar = quoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1816 self.quoteCharLen = len(quoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1817 self.firstQuoteChar = quoteChar[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1818 self.endQuoteChar = endQuoteChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1819 self.endQuoteCharLen = len(endQuoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1820 self.escChar = escChar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1821 self.escQuote = escQuote
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1822 self.unquoteResults = unquoteResults
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1823
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1824 if multiline:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1825 self.flags = re.MULTILINE | re.DOTALL
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1826 self.pattern = r'%s(?:[^%s%s]' % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1827 ( re.escape(self.quoteChar),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1828 _escapeRegexRangeChars(self.endQuoteChar[0]),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1829 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1830 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1831 self.flags = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1832 self.pattern = r'%s(?:[^%s\n\r%s]' % \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1833 ( re.escape(self.quoteChar),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1834 _escapeRegexRangeChars(self.endQuoteChar[0]),
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1835 (escChar is not None and _escapeRegexRangeChars(escChar) or '') )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1836 if len(self.endQuoteChar) > 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1837 self.pattern += (
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1838 '|(?:' + ')|(?:'.join("%s[^%s]" % (re.escape(self.endQuoteChar[:i]),
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1839 _escapeRegexRangeChars(self.endQuoteChar[i]))
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1840 for i in range(len(self.endQuoteChar)-1,0,-1)) + ')'
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1841 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1842 if escQuote:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1843 self.pattern += (r'|(?:%s)' % re.escape(escQuote))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1844 if escChar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1845 self.pattern += (r'|(?:%s.)' % re.escape(escChar))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1846 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
1847 self.escCharReplacePattern = re.escape(self.escChar)+("([%s])" % charset)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1848 self.pattern += (r')*%s' % re.escape(self.endQuoteChar))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1849
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1850 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1851 self.re = re.compile(self.pattern, self.flags)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1852 self.reString = self.pattern
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1853 except sre_constants.error:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1854 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
1855 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1856 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1857
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1858 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1859 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1860 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1861 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1862
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1863 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1864 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
1865 if not result:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1866 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1867
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1868 loc = result.end()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1869 ret = result.group()
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 self.unquoteResults:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1872
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1873 # strip off quotes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1874 ret = ret[self.quoteCharLen:-self.endQuoteCharLen]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1875
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1876 if isinstance(ret,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1877 # replace escaped characters
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1878 if self.escChar:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1879 ret = re.sub(self.escCharReplacePattern,"\g<1>",ret)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1880
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1881 # replace escaped quotes
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1882 if self.escQuote:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1883 ret = ret.replace(self.escQuote, self.endQuoteChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1884
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1885 return loc, ret
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 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1888 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1889 return super(QuotedString,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1890 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1891 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1892
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1893 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1894 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
1895
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1896 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1897
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1898
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1899 class CharsNotIn(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1900 """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
1901 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
1902 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
1903 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
1904 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
1905 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1906 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
1907 super(CharsNotIn,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1908 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1909 self.notChars = notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1910
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1911 if min < 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1912 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
1913
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1914 self.minLen = min
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1915
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1916 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1917 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1918 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1919 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1920
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1921 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1922 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1923 self.minLen = exact
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 self.name = _ustr(self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1926 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1927 self.mayReturnEmpty = ( self.minLen == 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1928 self.mayIndexError = False
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 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1931 if instring[loc] in self.notChars:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1932 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1933
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1934 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1935 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1936 notchars = self.notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1937 maxlen = min( start+self.maxLen, len(instring) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1938 while loc < maxlen and \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1939 (instring[loc] not in notchars):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1940 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1941
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1942 if loc - start < self.minLen:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1943 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1944
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1945 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1946
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1947 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1948 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1949 return super(CharsNotIn, self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1950 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1951 pass
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 self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1954 if len(self.notChars) > 4:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1955 self.strRepr = "!W:(%s...)" % self.notChars[:4]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1956 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1957 self.strRepr = "!W:(%s)" % self.notChars
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1958
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1959 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1960
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1961 class White(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1962 """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
1963 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
1964 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
1965 matched; default is C{" \\t\\r\\n"}. Also takes optional C{min}, C{max}, and C{exact} arguments,
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1966 as defined for the C{L{Word}} class."""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1967 whiteStrs = {
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1968 " " : "<SPC>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1969 "\t": "<TAB>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1970 "\n": "<LF>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1971 "\r": "<CR>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1972 "\f": "<FF>",
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1973 }
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1974 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
1975 super(White,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1976 self.matchWhite = ws
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1977 self.setWhitespaceChars( "".join(c for c in self.whiteChars if c not in self.matchWhite) )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1978 #~ self.leaveWhitespace()
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1979 self.name = ("".join(White.whiteStrs[c] for c in self.matchWhite))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1980 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1981 self.errmsg = "Expected " + self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1982
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1983 self.minLen = min
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 if max > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1986 self.maxLen = max
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1987 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1988 self.maxLen = _MAX_INT
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1989
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1990 if exact > 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1991 self.maxLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1992 self.minLen = exact
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1993
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1994 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1995 if not(instring[ loc ] in self.matchWhite):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
1996 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1997 start = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1998 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
1999 maxloc = start + self.maxLen
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2000 maxloc = min( maxloc, len(instring) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2001 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
2002 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2003
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2004 if loc - start < self.minLen:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2005 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2006
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2007 return loc, instring[start:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2008
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2009
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2010 class _PositionToken(Token):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2011 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2012 super(_PositionToken,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2013 self.name=self.__class__.__name__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2014 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2015 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2016
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2017 class GoToColumn(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2018 """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
2019 def __init__( self, colno ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2020 super(GoToColumn,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2021 self.col = colno
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 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2024 if col(loc,instring) != self.col:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2025 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2026 if self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2027 loc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2028 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
2029 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2030 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2031
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2032 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2033 thiscol = col( loc, instring )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2034 if thiscol > self.col:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2035 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
2036 newloc = loc + self.col - thiscol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2037 ret = instring[ loc: newloc ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2038 return newloc, ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2039
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2040 class LineStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2041 """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
2042 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2043 super(LineStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2044 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2045 self.errmsg = "Expected start of line"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2046
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2047 def preParse( self, instring, loc ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2048 preloc = super(LineStart,self).preParse(instring,loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2049 if instring[preloc] == "\n":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2050 loc += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2051 return loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2052
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2053 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2054 if not( loc==0 or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2055 (loc == self.preParse( instring, 0 )) or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2056 (instring[loc-1] == "\n") ): #col(loc, instring) != 1:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2057 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2058 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2059
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2060 class LineEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2061 """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
2062 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2063 super(LineEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2064 self.setWhitespaceChars( ParserElement.DEFAULT_WHITE_CHARS.replace("\n","") )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2065 self.errmsg = "Expected end of line"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2066
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2067 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2068 if loc<len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2069 if instring[loc] == "\n":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2070 return loc+1, "\n"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2071 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2072 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2073 elif loc == len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2074 return loc+1, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2075 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2076 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2077
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2078 class StringStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2079 """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
2080 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2081 super(StringStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2082 self.errmsg = "Expected start of text"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2083
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2084 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2085 if loc != 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2086 # 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
2087 if loc != self.preParse( instring, 0 ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2088 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2089 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2090
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2091 class StringEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2092 """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
2093 def __init__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2094 super(StringEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2095 self.errmsg = "Expected end of text"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2096
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2097 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2098 if loc < len(instring):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2099 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2100 elif loc == len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2101 return loc+1, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2102 elif loc > len(instring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2103 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2104 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2105 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2106
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2107 class WordStart(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2108 """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
2109 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
2110 (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
2111 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
2112 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
2113 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2114 def __init__(self, wordChars = printables):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2115 super(WordStart,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2116 self.wordChars = set(wordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2117 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
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 != 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2121 if (instring[loc-1] in self.wordChars or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2122 instring[loc] not in self.wordChars):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2123 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2124 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2125
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2126 class WordEnd(_PositionToken):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2127 """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
2128 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
2129 (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
2130 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
2131 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
2132 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2133 def __init__(self, wordChars = printables):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2134 super(WordEnd,self).__init__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2135 self.wordChars = set(wordChars)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2136 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2137 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
2138
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2139 def parseImpl(self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2140 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2141 if instrlen>0 and loc<instrlen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2142 if (instring[loc] in self.wordChars or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2143 instring[loc-1] not in self.wordChars):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2144 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2145 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2146
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2147
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2148 class ParseExpression(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2149 """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
2150 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2151 super(ParseExpression,self).__init__(savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2152 if isinstance( exprs, list ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2153 self.exprs = exprs
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2154 elif isinstance( exprs, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2155 self.exprs = [ Literal( exprs ) ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2156 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2157 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2158 self.exprs = list( exprs )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2159 except TypeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2160 self.exprs = [ exprs ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2161 self.callPreparse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2162
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2163 def __getitem__( self, i ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2164 return self.exprs[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2165
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2166 def append( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2167 self.exprs.append( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2168 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2169 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2170
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2171 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2172 """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
2173 all contained expressions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2174 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2175 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
2176 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2177 e.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2178 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2179
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2180 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2181 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2182 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2183 super( ParseExpression, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2184 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2185 e.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2186 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2187 super( ParseExpression, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2188 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2189 e.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2190 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2191
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2192 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2193 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2194 return super(ParseExpression,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2195 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2196 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2197
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2198 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2199 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
2200 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2201
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2202 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2203 super(ParseExpression,self).streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2204
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2205 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2206 e.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2207
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2208 # 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
2209 # 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
2210 # (likewise for Or's and MatchFirst's)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2211 if ( len(self.exprs) == 2 ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2212 other = self.exprs[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2213 if ( isinstance( other, self.__class__ ) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2214 not(other.parseAction) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2215 other.resultsName is None and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2216 not other.debug ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2217 self.exprs = other.exprs[:] + [ self.exprs[1] ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2218 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2219 self.mayReturnEmpty |= other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2220 self.mayIndexError |= other.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2221
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2222 other = self.exprs[-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2223 if ( isinstance( other, self.__class__ ) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2224 not(other.parseAction) and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2225 other.resultsName is None and
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2226 not other.debug ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2227 self.exprs = self.exprs[:-1] + other.exprs[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2228 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2229 self.mayReturnEmpty |= other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2230 self.mayIndexError |= other.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2231
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2232 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2233
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2234 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2235 ret = super(ParseExpression,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2236 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2237
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2238 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2239 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2240 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2241 e.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2242 self.checkRecursion( [] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2243
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2244 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2245 ret = super(ParseExpression,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2246 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
2247 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2248
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2249 class And(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2250 """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
2251 Expressions may be separated by whitespace.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2252 May be constructed using the C{'+'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2253 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2254
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2255 class _ErrorStop(Empty):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2256 def __init__(self, *args, **kwargs):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2257 super(And._ErrorStop,self).__init__(*args, **kwargs)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2258 self.name = '-'
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2259 self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2260
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2261 def __init__( self, exprs, savelist = True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2262 super(And,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2263 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2264 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2265 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2266 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2267 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2268 self.setWhitespaceChars( exprs[0].whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2269 self.skipWhitespace = exprs[0].skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2270 self.callPreparse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2271
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2272 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2273 # 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
2274 # 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
2275 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
2276 errorStop = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2277 for e in self.exprs[1:]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2278 if isinstance(e, And._ErrorStop):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2279 errorStop = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2280 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2281 if errorStop:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2282 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2283 loc, exprtokens = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2284 except ParseSyntaxException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2285 raise
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2286 except ParseBaseException as pe:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2287 pe.__traceback__ = None
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2288 raise ParseSyntaxException(pe)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2289 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2290 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
2291 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2292 loc, exprtokens = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2293 if exprtokens or exprtokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2294 resultlist += exprtokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2295 return loc, resultlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2296
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2297 def __iadd__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2298 if isinstance( other, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2299 other = Literal( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2300 return self.append( other ) #And( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2301
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2302 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2303 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2304 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2305 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2306 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2307 break
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 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2310 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2311 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2312
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2313 if self.strRepr is None:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2314 self.strRepr = "{" + " ".join(_ustr(e) for e in self.exprs) + "}"
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2315
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2316 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2317
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2318
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2319 class Or(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2320 """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
2321 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
2322 May be constructed using the C{'^'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2323 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2324 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2325 super(Or,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2326 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2327 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2328 if e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2329 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2330 break
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 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2333 maxExcLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2334 maxMatchLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2335 maxException = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2336 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2337 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2338 loc2 = e.tryParse( instring, loc )
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2339 except ParseException as err:
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2340 err.__traceback__ = None
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2341 if err.loc > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2342 maxException = err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2343 maxExcLoc = err.loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2344 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2345 if len(instring) > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2346 maxException = ParseException(instring,len(instring),e.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2347 maxExcLoc = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2348 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2349 if loc2 > maxMatchLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2350 maxMatchLoc = loc2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2351 maxMatchExp = e
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2352
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2353 if maxMatchLoc < 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2354 if maxException is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2355 raise maxException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2356 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2357 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
2358
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2359 return maxMatchExp._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2360
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2361 def __ixor__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2362 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2363 other = ParserElement.literalStringClass( other )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2364 return self.append( other ) #Or( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2365
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2366 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2367 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2368 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2369
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2370 if self.strRepr is None:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2371 self.strRepr = "{" + " ^ ".join(_ustr(e) for e in self.exprs) + "}"
484
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 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2374
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2375 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2376 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2377 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2378 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2379
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2380
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2381 class MatchFirst(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2382 """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
2383 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
2384 May be constructed using the C{'|'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2385 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2386 def __init__( self, exprs, savelist = False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2387 super(MatchFirst,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2388 if exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2389 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2390 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2391 if e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2392 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2393 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2394 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2395 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2396
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2397 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2398 maxExcLoc = -1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2399 maxException = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2400 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2401 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2402 ret = e._parse( instring, loc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2403 return ret
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2404 except ParseException as err:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2405 if err.loc > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2406 maxException = err
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2407 maxExcLoc = err.loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2408 except IndexError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2409 if len(instring) > maxExcLoc:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2410 maxException = ParseException(instring,len(instring),e.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2411 maxExcLoc = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2412
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2413 # 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
2414 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2415 if maxException is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2416 raise maxException
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2417 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2418 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
2419
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2420 def __ior__(self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2421 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2422 other = ParserElement.literalStringClass( other )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2423 return self.append( other ) #MatchFirst( [ self, other ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2424
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2425 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2426 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2427 return self.name
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 self.strRepr is None:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2430 self.strRepr = "{" + " | ".join(_ustr(e) for e in self.exprs) + "}"
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2431
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2432 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2433
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2434 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2435 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2436 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2437 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2438
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2439
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2440 class Each(ParseExpression):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2441 """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
2442 Expressions may be separated by whitespace.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2443 May be constructed using the C{'&'} operator.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2444 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2445 def __init__( self, exprs, savelist = True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2446 super(Each,self).__init__(exprs, savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2447 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2448 for e in self.exprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2449 if not e.mayReturnEmpty:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2450 self.mayReturnEmpty = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2451 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2452 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2453 self.initExprGroups = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2454
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2455 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2456 if self.initExprGroups:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2457 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
2458 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
2459 self.optionals = opt1 + opt2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2460 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
2461 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
2462 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
2463 self.required += self.multirequired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2464 self.initExprGroups = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2465 tmpLoc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2466 tmpReqd = self.required[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2467 tmpOpt = self.optionals[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2468 matchOrder = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2469
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2470 keepMatching = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2471 while keepMatching:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2472 tmpExprs = tmpReqd + tmpOpt + self.multioptionals + self.multirequired
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2473 failed = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2474 for e in tmpExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2475 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2476 tmpLoc = e.tryParse( instring, tmpLoc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2477 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2478 failed.append(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2479 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2480 matchOrder.append(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2481 if e in tmpReqd:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2482 tmpReqd.remove(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2483 elif e in tmpOpt:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2484 tmpOpt.remove(e)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2485 if len(failed) == len(tmpExprs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2486 keepMatching = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2487
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2488 if tmpReqd:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2489 missing = ", ".join(_ustr(e) for e in tmpReqd)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2490 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
2491
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2492 # 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
2493 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
2494
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2495 resultlist = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2496 for e in matchOrder:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2497 loc,results = e._parse(instring,loc,doActions)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2498 resultlist.append(results)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2499
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2500 finalResults = ParseResults([])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2501 for r in resultlist:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2502 dups = {}
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2503 for k in r.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2504 if k in finalResults.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2505 tmp = ParseResults(finalResults[k])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2506 tmp += ParseResults(r[k])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2507 dups[k] = tmp
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2508 finalResults += ParseResults(r)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2509 for k,v in dups.items():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2510 finalResults[k] = v
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2511 return loc, finalResults
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2512
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2513 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2514 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2515 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2516
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2517 if self.strRepr is None:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2518 self.strRepr = "{" + " & ".join(_ustr(e) for e in self.exprs) + "}"
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2519
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2520 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2521
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2522 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2523 subRecCheckList = parseElementList[:] + [ self ]
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 e.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2526
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2527
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2528 class ParseElementEnhance(ParserElement):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2529 """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
2530 def __init__( self, expr, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2531 super(ParseElementEnhance,self).__init__(savelist)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2532 if isinstance( expr, basestring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2533 expr = Literal(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2534 self.expr = expr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2535 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2536 if expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2537 self.mayIndexError = expr.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2538 self.mayReturnEmpty = expr.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2539 self.setWhitespaceChars( expr.whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2540 self.skipWhitespace = expr.skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2541 self.saveAsList = expr.saveAsList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2542 self.callPreparse = expr.callPreparse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2543 self.ignoreExprs.extend(expr.ignoreExprs)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2544
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2545 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2546 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2547 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
2548 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2549 raise ParseException("",loc,self.errmsg,self)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2550
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2551 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2552 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2553 self.expr = self.expr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2554 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2555 self.expr.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2556 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2557
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2558 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2559 if isinstance( other, Suppress ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2560 if other not in self.ignoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2561 super( ParseElementEnhance, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2562 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2563 self.expr.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2564 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2565 super( ParseElementEnhance, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2566 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2567 self.expr.ignore( self.ignoreExprs[-1] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2568 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2569
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2570 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2571 super(ParseElementEnhance,self).streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2572 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2573 self.expr.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2574 return self
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 def checkRecursion( self, parseElementList ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2577 if self in parseElementList:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2578 raise RecursiveGrammarException( parseElementList+[self] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2579 subRecCheckList = parseElementList[:] + [ self ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2580 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2581 self.expr.checkRecursion( subRecCheckList )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2582
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2583 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2584 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2585 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2586 self.expr.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2587 self.checkRecursion( [] )
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 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2591 return super(ParseElementEnhance,self).__str__()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2592 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2593 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2594
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2595 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
2596 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
2597 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2598
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2599
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2600 class FollowedBy(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2601 """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
2602 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
2603 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
2604 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
2605 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2606 super(FollowedBy,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2607 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2608
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2609 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2610 self.expr.tryParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2611 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2612
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2613
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2614 class NotAny(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2615 """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
2616 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
2617 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
2618 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
2619 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
2620 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2621 super(NotAny,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2622 #~ self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2623 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
2624 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2625 self.errmsg = "Found unwanted token, "+_ustr(self.expr)
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 parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2628 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2629 self.expr.tryParse( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2630 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2631 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2632 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2633 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2634 return loc, []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2635
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2636 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2637 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2638 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2639
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2640 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2641 self.strRepr = "~{" + _ustr(self.expr) + "}"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2642
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2643 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2644
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 class ZeroOrMore(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2647 """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
2648 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2649 super(ZeroOrMore,self).__init__(expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2650 self.mayReturnEmpty = True
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 parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2653 tokens = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2654 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2655 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
2656 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2657 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2658 if hasIgnoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2659 preloc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2660 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2661 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2662 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2663 if tmptokens or tmptokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2664 tokens += tmptokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2665 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2666 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2667
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2668 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2669
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2670 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2671 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2672 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2673
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2674 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2675 self.strRepr = "[" + _ustr(self.expr) + "]..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2676
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2677 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2678
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2679 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2680 ret = super(ZeroOrMore,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2681 ret.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2682 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2683
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 class OneOrMore(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2686 """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
2687 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2688 # must be at least one
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2689 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
2690 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2691 hasIgnoreExprs = ( len(self.ignoreExprs) > 0 )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2692 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2693 if hasIgnoreExprs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2694 preloc = self._skipIgnorables( instring, loc )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2695 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2696 preloc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2697 loc, tmptokens = self.expr._parse( instring, preloc, doActions )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2698 if tmptokens or tmptokens.keys():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2699 tokens += tmptokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2700 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2701 pass
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 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2704
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2705 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2706 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2707 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2708
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2709 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2710 self.strRepr = "{" + _ustr(self.expr) + "}..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2711
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2712 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2713
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2714 def setResultsName( self, name, listAllMatches=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2715 ret = super(OneOrMore,self).setResultsName(name,listAllMatches)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2716 ret.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2717 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2718
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2719 class _NullToken(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2720 def __bool__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2721 return False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2722 __nonzero__ = __bool__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2723 def __str__(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2724 return ""
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 _optionalNotMatched = _NullToken()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2727 class Optional(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2728 """Optional matching of the given expression.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2729 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
2730 is not found.
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 __init__( self, exprs, default=_optionalNotMatched ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2733 super(Optional,self).__init__( exprs, savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2734 self.defaultValue = default
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2735 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2736
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2737 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2738 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2739 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
2740 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2741 if self.defaultValue is not _optionalNotMatched:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2742 if self.expr.resultsName:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2743 tokens = ParseResults([ self.defaultValue ])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2744 tokens[self.expr.resultsName] = self.defaultValue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2745 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2746 tokens = [ self.defaultValue ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2747 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2748 tokens = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2749 return loc, tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2750
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2751 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2752 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2753 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2754
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2755 if self.strRepr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2756 self.strRepr = "[" + _ustr(self.expr) + "]"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2757
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2758 return self.strRepr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2759
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2760
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2761 class SkipTo(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2762 """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
2763 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
2764 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
2765 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
2766 might contain false matches.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2767 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2768 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
2769 super( SkipTo, self ).__init__( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2770 self.ignoreExpr = ignore
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2771 self.mayReturnEmpty = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2772 self.mayIndexError = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2773 self.includeMatch = include
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2774 self.asList = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2775 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
2776 self.failOn = Literal(failOn)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2777 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2778 self.failOn = failOn
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2779 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
2780
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2781 def parseImpl( self, instring, loc, doActions=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2782 startLoc = loc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2783 instrlen = len(instring)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2784 expr = self.expr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2785 failParse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2786 while loc <= instrlen:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2787 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2788 if self.failOn:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2789 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2790 self.failOn.tryParse(instring, loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2791 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2792 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2793 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2794 failParse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2795 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
2796 failParse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2797 if self.ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2798 while 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2799 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2800 loc = self.ignoreExpr.tryParse(instring,loc)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2801 # print("found ignoreExpr, advance to", loc)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2802 except ParseBaseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2803 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2804 expr._parse( instring, loc, doActions=False, callPreParse=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2805 skipText = instring[startLoc:loc]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2806 if self.includeMatch:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2807 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
2808 if mat:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2809 skipRes = ParseResults( skipText )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2810 skipRes += mat
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2811 return loc, [ skipRes ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2812 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2813 return loc, [ skipText ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2814 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2815 return loc, [ skipText ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2816 except (ParseException,IndexError):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2817 if failParse:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2818 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2819 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2820 loc += 1
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2821 raise ParseException(instring, loc, self.errmsg, self)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2822
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2823 class Forward(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2824 """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
2825 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
2826 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
2827
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2828 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
2829 Specifically, '|' has a lower precedence than '<<', so that::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2830 fwdExpr << a | b | c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2831 will actually be evaluated as::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2832 (fwdExpr << a) | b | c
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2833 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
2834 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
2835 fwdExpr << (a | b | c)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2836 Converting to use the '<<=' operator instead will avoid this problem.
484
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 def __init__( self, other=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2839 super(Forward,self).__init__( other, savelist=False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2840
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2841 def __ilshift__( self, other ):
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2842 if isinstance( other, basestring ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2843 other = ParserElement.literalStringClass(other)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2844 self.expr = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2845 self.mayReturnEmpty = other.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2846 self.strRepr = None
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2847 self.mayIndexError = self.expr.mayIndexError
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2848 self.mayReturnEmpty = self.expr.mayReturnEmpty
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2849 self.setWhitespaceChars( self.expr.whiteChars )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2850 self.skipWhitespace = self.expr.skipWhitespace
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2851 self.saveAsList = self.expr.saveAsList
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2852 self.ignoreExprs.extend(self.expr.ignoreExprs)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2853 return self
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2854
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2855 def __lshift__(self, other):
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2856 warnings.warn("Operator '<<' is deprecated, use '<<=' instead",
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2857 DeprecationWarning,stacklevel=2)
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2858 self <<= other
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2859 return None
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2860
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2861 def leaveWhitespace( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2862 self.skipWhitespace = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2863 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2864
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2865 def streamline( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2866 if not self.streamlined:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2867 self.streamlined = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2868 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2869 self.expr.streamline()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2870 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2871
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2872 def validate( self, validateTrace=[] ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2873 if self not in validateTrace:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2874 tmp = validateTrace[:]+[self]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2875 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2876 self.expr.validate(tmp)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2877 self.checkRecursion([])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2878
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2879 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2880 if hasattr(self,"name"):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2881 return self.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2882
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2883 self._revertClass = self.__class__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2884 self.__class__ = _ForwardNoRecurse
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2885 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2886 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2887 retString = _ustr(self.expr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2888 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2889 retString = "None"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2890 finally:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2891 self.__class__ = self._revertClass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2892 return self.__class__.__name__ + ": " + retString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2893
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2894 def copy(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2895 if self.expr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2896 return super(Forward,self).copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2897 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2898 ret = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2899 ret << self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2900 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2901
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2902 class _ForwardNoRecurse(Forward):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2903 def __str__( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2904 return "..."
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 TokenConverter(ParseElementEnhance):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2907 """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
2908 def __init__( self, expr, savelist=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2909 super(TokenConverter,self).__init__( expr )#, savelist )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2910 self.saveAsList = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2911
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2912 class Upcase(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2913 """Converter to upper case all matching tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2914 def __init__(self, *args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2915 super(Upcase,self).__init__(*args)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2916 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
2917 DeprecationWarning,stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2918
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2919 def postParse( self, instring, loc, tokenlist ):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2920 return list(map( str.upper, tokenlist ))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2921
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 class Combine(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2924 """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
2925 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
2926 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
2927 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2928 def __init__( self, expr, joinString="", adjacent=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2929 super(Combine,self).__init__( expr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2930 # 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
2931 if adjacent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2932 self.leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2933 self.adjacent = adjacent
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2934 self.skipWhitespace = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2935 self.joinString = joinString
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2936 self.callPreparse = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2937
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2938 def ignore( self, other ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2939 if self.adjacent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2940 ParserElement.ignore(self, other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2941 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2942 super( Combine, self).ignore( other )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2943 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2944
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2945 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2946 retToks = tokenlist.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2947 del retToks[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2948 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
2949
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2950 if self.resultsName and len(retToks.keys())>0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2951 return [ retToks ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2952 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2953 return retToks
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 class Group(TokenConverter):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
2956 """Converter to return the matched tokens as a list - useful for returning tokens of C{L{ZeroOrMore}} and C{L{OneOrMore}} expressions."""
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2957 def __init__( self, expr ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2958 super(Group,self).__init__( expr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2959 self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2960
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2961 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2962 return [ tokenlist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2963
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2964 class Dict(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2965 """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
2966 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
2967 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
2968 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2969 def __init__( self, exprs ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2970 super(Dict,self).__init__( exprs )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2971 self.saveAsList = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2972
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2973 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2974 for i,tok in enumerate(tokenlist):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2975 if len(tok) == 0:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2976 continue
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2977 ikey = tok[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2978 if isinstance(ikey,int):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2979 ikey = _ustr(tok[0]).strip()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2980 if len(tok)==1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2981 tokenlist[ikey] = _ParseResultsWithOffset("",i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2982 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
2983 tokenlist[ikey] = _ParseResultsWithOffset(tok[1],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2984 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2985 dictvalue = tok.copy() #ParseResults(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2986 del dictvalue[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2987 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
2988 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue,i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2989 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2990 tokenlist[ikey] = _ParseResultsWithOffset(dictvalue[0],i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2991
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2992 if self.resultsName:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2993 return [ tokenlist ]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2994 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2995 return tokenlist
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2996
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 class Suppress(TokenConverter):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
2999 """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
3000 def postParse( self, instring, loc, tokenlist ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3001 return []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3002
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3003 def suppress( self ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3004 return self
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3005
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3006
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3007 class OnlyOnce(object):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3008 """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
3009 def __init__(self, methodCall):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3010 self.callable = _trim_arity(methodCall)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3011 self.called = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3012 def __call__(self,s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3013 if not self.called:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3014 results = self.callable(s,l,t)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3015 self.called = True
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3016 return results
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3017 raise ParseException(s,l,"")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3018 def reset(self):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3019 self.called = False
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 traceParseAction(f):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3022 """Decorator for debugging parse actions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3023 f = _trim_arity(f)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3024 def z(*paArgs):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3025 thisFunc = f.func_name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3026 s,l,t = paArgs[-3:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3027 if len(paArgs)>3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3028 thisFunc = paArgs[0].__class__.__name__ + '.' + thisFunc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3029 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
3030 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3031 ret = f(*paArgs)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3032 except Exception as exc:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3033 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
3034 raise
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3035 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
3036 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3037 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3038 z.__name__ = f.__name__
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3039 except AttributeError:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3040 pass
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3041 return z
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3042
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3043 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3044 # global helpers
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3045 #
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3046 def delimitedList( expr, delim=",", combine=False ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3047 """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
3048 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
3049 comments, but this can be overridden by passing C{combine=True} in the constructor.
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3050 If C{combine} is set to C{True}, the matching tokens are returned as a single token
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3051 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
3052 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
3053 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3054 dlName = _ustr(expr)+" ["+_ustr(delim)+" "+_ustr(expr)+"]..."
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3055 if combine:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3056 return Combine( expr + ZeroOrMore( delim + expr ) ).setName(dlName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3057 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3058 return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3059
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3060 def countedArray( expr, intExpr=None ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3061 """Helper to define a counted list of expressions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3062 This helper defines a pattern of the form::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3063 integer expr expr expr...
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3064 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
3065 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
3066 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3067 arrayExpr = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3068 def countFieldParseAction(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3069 n = t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3070 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
3071 return []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3072 if intExpr is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3073 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
3074 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3075 intExpr = intExpr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3076 intExpr.setName("arrayLen")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3077 intExpr.addParseAction(countFieldParseAction, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3078 return ( intExpr + arrayExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3079
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3080 def _flatten(L):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3081 ret = []
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3082 for i in L:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3083 if isinstance(i,list):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3084 ret.extend(_flatten(i))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3085 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3086 ret.append(i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3087 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3088
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3089 def matchPreviousLiteral(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3090 """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
3091 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
3092 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
3093 first = Word(nums)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3094 second = matchPreviousLiteral(first)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3095 matchExpr = first + ":" + second
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3096 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
3097 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
3098 If this is not desired, use C{matchPreviousExpr}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3099 Do *not* use with packrat parsing enabled.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3100 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3101 rep = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3102 def copyTokenToRepeater(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3103 if t:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3104 if len(t) == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3105 rep << t[0]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3106 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3107 # flatten t tokens
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3108 tflat = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3109 rep << And( [ Literal(tt) for tt in tflat ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3110 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3111 rep << Empty()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3112 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3113 return rep
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3114
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3115 def matchPreviousExpr(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3116 """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
3117 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
3118 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
3119 first = Word(nums)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3120 second = matchPreviousExpr(first)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3121 matchExpr = first + ":" + second
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3122 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
3123 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
3124 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
3125 C{"1"} is compared with C{"10"}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3126 Do *not* use with packrat parsing enabled.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3127 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3128 rep = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3129 e2 = expr.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3130 rep << e2
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3131 def copyTokenToRepeater(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3132 matchTokens = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3133 def mustMatchTheseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3134 theseTokens = _flatten(t.asList())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3135 if theseTokens != matchTokens:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3136 raise ParseException("",0,"")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3137 rep.setParseAction( mustMatchTheseTokens, callDuringTry=True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3138 expr.addParseAction(copyTokenToRepeater, callDuringTry=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3139 return rep
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3140
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3141 def _escapeRegexRangeChars(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3142 #~ escape these chars: ^-]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3143 for c in r"\^-]":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3144 s = s.replace(c,_bslash+c)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3145 s = s.replace("\n",r"\n")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3146 s = s.replace("\t",r"\t")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3147 return _ustr(s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3148
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3149 def oneOf( strs, caseless=False, useRegex=True ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3150 """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
3151 longest-first testing when there is a conflict, regardless of the input order,
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3152 but returns a C{L{MatchFirst}} for best performance.
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3153
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3154 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3155 - 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
3156 - caseless - (default=False) - treat all literals as caseless
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3157 - 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
3158 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
3159 if creating a C{Regex} raises an exception)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3160 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3161 if caseless:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3162 isequal = ( lambda a,b: a.upper() == b.upper() )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3163 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
3164 parseElementClass = CaselessLiteral
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3165 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3166 isequal = ( lambda a,b: a == b )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3167 masks = ( lambda a,b: b.startswith(a) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3168 parseElementClass = Literal
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3169
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3170 if isinstance(strs,(list,tuple)):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3171 symbols = list(strs[:])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3172 elif isinstance(strs,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3173 symbols = strs.split()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3174 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3175 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
3176 SyntaxWarning, stacklevel=2)
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 i = 0
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3179 while i < len(symbols)-1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3180 cur = symbols[i]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3181 for j,other in enumerate(symbols[i+1:]):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3182 if ( isequal(other, cur) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3183 del symbols[i+j+1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3184 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3185 elif ( masks(cur, other) ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3186 del symbols[i+j+1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3187 symbols.insert(i,other)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3188 cur = other
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3189 break
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3190 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3191 i += 1
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3192
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3193 if not caseless and useRegex:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3194 #~ 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
3195 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3196 if len(symbols)==len("".join(symbols)):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3197 return Regex( "[%s]" % "".join(_escapeRegexRangeChars(sym) for sym in symbols) )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3198 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3199 return Regex( "|".join(re.escape(sym) for sym in symbols) )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3200 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3201 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
3202 SyntaxWarning, stacklevel=2)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3203
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 # last resort, just use MatchFirst
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3206 return MatchFirst( [ parseElementClass(sym) for sym in symbols ] )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3207
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3208 def dictOf( key, value ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3209 """Helper to easily and clearly define a dictionary by specifying the respective patterns
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3210 for the key and value. Takes care of defining the C{L{Dict}}, C{L{ZeroOrMore}}, and C{L{Group}} tokens
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3211 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
3212 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
3213 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
3214 fields.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3215 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3216 return Dict( ZeroOrMore( Group ( key + value ) ) )
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 originalTextFor(expr, asString=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3219 """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
3220 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
3221 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
3222 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
3223 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
3224 string containing the original parsed text.
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 If the optional C{asString} argument is passed as C{False}, then the return value is a
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3227 C{L{ParseResults}} containing any results names that were originally matched, and a
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3228 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
3229 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
3230 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
3231 results name values."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3232 locMarker = Empty().setParseAction(lambda s,loc,t: loc)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3233 endlocMarker = locMarker.copy()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3234 endlocMarker.callPreparse = False
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3235 matchExpr = locMarker("_original_start") + expr + endlocMarker("_original_end")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3236 if asString:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3237 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
3238 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3239 def extractText(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3240 del t[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3241 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
3242 del t["_original_start"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3243 del t["_original_end"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3244 matchExpr.setParseAction(extractText)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3245 return matchExpr
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 def ungroup(expr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3248 """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
3249 if all but one are non-empty."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3250 return TokenConverter(expr).setParseAction(lambda t:t[0])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3251
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3252 # convenience constants for positional expressions
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3253 empty = Empty().setName("empty")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3254 lineStart = LineStart().setName("lineStart")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3255 lineEnd = LineEnd().setName("lineEnd")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3256 stringStart = StringStart().setName("stringStart")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3257 stringEnd = StringEnd().setName("stringEnd")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3258
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3259 _escapedPunc = Word( _bslash, r"\[]-*.$+^?()~ ", exact=2 ).setParseAction(lambda s,l,t:t[0][1])
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3260 _escapedHexChar = Regex(r"\\0?[xX][0-9a-fA-F]+").setParseAction(lambda s,l,t:unichr(int(t[0].lstrip(r'\0x'),16)))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3261 _escapedOctChar = Regex(r"\\0[0-7]+").setParseAction(lambda s,l,t:unichr(int(t[0][1:],8)))
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3262 _singleChar = _escapedPunc | _escapedHexChar | _escapedOctChar | Word(printables, excludeChars=r'\]', exact=1)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3263 _charRange = Group(_singleChar + Suppress("-") + _singleChar)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3264 _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
3265
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3266 _expanded = lambda p: (isinstance(p,ParseResults) and ''.join(unichr(c) for c in range(ord(p[0]),ord(p[1])+1)) or p)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3267
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3268 def srange(s):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3269 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
3270 syntax from regexp '[]' string range definitions::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3271 srange("[0-9]") -> "0123456789"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3272 srange("[a-z]") -> "abcdefghijklmnopqrstuvwxyz"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3273 srange("[a-z$_]") -> "abcdefghijklmnopqrstuvwxyz$_"
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3274 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
3275 character set joined into a single string.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3276 The values enclosed in the []'s may be::
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3277 a single character
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3278 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
3279 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
3280 (\0x## is also supported for backwards compatibility)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3281 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
3282 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
3283 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
3284 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3285 try:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3286 return "".join(_expanded(part) for part in _reBracketExpr.parseString(s).body)
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3287 except:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3288 return ""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3289
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3290 def matchOnlyAtCol(n):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3291 """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
3292 column in the input text.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3293 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3294 def verifyCol(strg,locn,toks):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3295 if col(locn,strg) != n:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3296 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
3297 return verifyCol
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3298
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3299 def replaceWith(replStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3300 """Helper method for common parse actions that simply return a literal value. Especially
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3301 useful when used with C{L{transformString<ParserElement.transformString>}()}.
484
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 def _replFunc(*args):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3304 return [replStr]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3305 return _replFunc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3306
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3307 def removeQuotes(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3308 """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
3309 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
3310 quotedString.setParseAction( removeQuotes )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3311 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3312 return t[0][1:-1]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3313
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3314 def upcaseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3315 """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
3316 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
3317
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3318 def downcaseTokens(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3319 """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
3320 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
3321
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3322 def keepOriginalText(s,startLoc,t):
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3323 """DEPRECATED - use new helper method C{L{originalTextFor}}.
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3324 Helper parse action to preserve original parsed text,
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3325 overriding any nested parse actions."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3326 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3327 endloc = getTokensEndLoc()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3328 except ParseException:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3329 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
3330 del t[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3331 t += ParseResults(s[startLoc:endloc])
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3332 return t
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3333
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3334 def getTokensEndLoc():
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3335 """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
3336 location of the parsed tokens."""
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3337 import inspect
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3338 fstack = inspect.stack()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3339 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3340 # 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
3341 for f in fstack[2:]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3342 if f[3] == "_parseNoCache":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3343 endloc = f[0].f_locals["loc"]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3344 return endloc
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3345 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3346 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
3347 finally:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3348 del fstack
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3349
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3350 def _makeTags(tagStr, xml):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3351 """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
3352 if isinstance(tagStr,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3353 resname = tagStr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3354 tagStr = Keyword(tagStr, caseless=not xml)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3355 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3356 resname = tagStr.name
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3357
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3358 tagAttrName = Word(alphas,alphanums+"_-:")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3359 if (xml):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3360 tagAttrValue = dblQuotedString.copy().setParseAction( removeQuotes )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3361 openTag = Suppress("<") + tagStr("tag") + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3362 Dict(ZeroOrMore(Group( tagAttrName + Suppress("=") + tagAttrValue ))) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3363 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
3364 else:
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3365 printablesLessRAbrack = "".join(c for c in printables if c not in ">")
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3366 tagAttrValue = quotedString.copy().setParseAction( removeQuotes ) | Word(printablesLessRAbrack)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3367 openTag = Suppress("<") + tagStr("tag") + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3368 Dict(ZeroOrMore(Group( tagAttrName.setParseAction(downcaseTokens) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3369 Optional( Suppress("=") + tagAttrValue ) ))) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3370 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
3371 closeTag = Combine(_L("</") + tagStr + ">")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3372
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3373 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
3374 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
3375 openTag.tag = resname
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3376 closeTag.tag = resname
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3377 return openTag, closeTag
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3378
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3379 def makeHTMLTags(tagStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3380 """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
3381 return _makeTags( tagStr, False )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3382
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3383 def makeXMLTags(tagStr):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3384 """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
3385 return _makeTags( tagStr, True )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3386
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3387 def withAttribute(*args,**attrDict):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3388 """Helper to create a validating parse action to be used with start tags created
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3389 with C{L{makeXMLTags}} or C{L{makeHTMLTags}}. Use C{withAttribute} to qualify a starting tag
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3390 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
3391 C{<TD>} or C{<DIV>}.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3392
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3393 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
3394 of filter attributes names and values as:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3395 - keyword arguments, as in C{(align="right")}, or
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3396 - 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
3397 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
3398 - 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
3399 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
3400 names are matched insensitive to upper/lower case.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3401
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3402 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
3403 C{withAttribute.ANY_VALUE} as the value.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3404 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3405 if args:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3406 attrs = args[:]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3407 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3408 attrs = attrDict.items()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3409 attrs = [(k,v) for k,v in attrs]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3410 def pa(s,l,tokens):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3411 for attrName,attrValue in attrs:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3412 if attrName not in tokens:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3413 raise ParseException(s,l,"no matching attribute " + attrName)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3414 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
3415 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
3416 (attrName, tokens[attrName], attrValue))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3417 return pa
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3418 withAttribute.ANY_VALUE = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3419
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3420 opAssoc = _Constants()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3421 opAssoc.LEFT = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3422 opAssoc.RIGHT = object()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3423
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3424 def infixNotation( baseExpr, opList, lpar=Suppress('('), rpar=Suppress(')') ):
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3425 """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
3426 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
3427 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
3428 to operator expressions.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3429
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3430 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3431 - 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
3432 - 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
3433 expression grammar; each tuple is of the form
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3434 (opExpr, numTerms, rightLeftAssoc, parseAction), where:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3435 - opExpr is the pyparsing expression for the operator;
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3436 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
3437 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
3438 two operators separating the 3 terms
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3439 - 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
3440 be 1, 2, or 3)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3441 - rightLeftAssoc is the indicator whether the operator is
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3442 right or left associative, using the pyparsing-defined
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3443 constants C{opAssoc.RIGHT} and C{opAssoc.LEFT}.
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3444 - 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
3445 expressions matching this operator expression (the
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3446 parse action tuple member may be omitted)
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3447 - lpar - expression for matching left-parentheses (default=Suppress('('))
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3448 - rpar - expression for matching right-parentheses (default=Suppress(')'))
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3449 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3450 ret = Forward()
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3451 lastExpr = baseExpr | ( lpar + ret + rpar )
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3452 for i,operDef in enumerate(opList):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3453 opExpr,arity,rightLeftAssoc,pa = (operDef + (None,))[:4]
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3454 if arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3455 if opExpr is None or len(opExpr) != 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3456 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
3457 opExpr1, opExpr2 = opExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3458 thisExpr = Forward()#.setName("expr%d" % i)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3459 if rightLeftAssoc == opAssoc.LEFT:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3460 if arity == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3461 matchExpr = FollowedBy(lastExpr + opExpr) + Group( lastExpr + OneOrMore( opExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3462 elif arity == 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3463 if opExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3464 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
3465 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3466 matchExpr = FollowedBy(lastExpr+lastExpr) + Group( lastExpr + OneOrMore(lastExpr) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3467 elif arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3468 matchExpr = FollowedBy(lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3469 Group( lastExpr + opExpr1 + lastExpr + opExpr2 + lastExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3470 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3471 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
3472 elif rightLeftAssoc == opAssoc.RIGHT:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3473 if arity == 1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3474 # try to avoid LR with this extra test
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3475 if not isinstance(opExpr, Optional):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3476 opExpr = Optional(opExpr)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3477 matchExpr = FollowedBy(opExpr.expr + thisExpr) + Group( opExpr + thisExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3478 elif arity == 2:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3479 if opExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3480 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
3481 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3482 matchExpr = FollowedBy(lastExpr + thisExpr) + Group( lastExpr + OneOrMore( thisExpr ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3483 elif arity == 3:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3484 matchExpr = FollowedBy(lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3485 Group( lastExpr + opExpr1 + thisExpr + opExpr2 + thisExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3486 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3487 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
3488 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3489 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
3490 if pa:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3491 matchExpr.setParseAction( pa )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3492 thisExpr << ( matchExpr | lastExpr )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3493 lastExpr = thisExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3494 ret << lastExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3495 return ret
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3496 operatorPrecedence = infixNotation
484
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 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
3499 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
3500 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
3501 unicodeString = Combine(_L('u') + quotedString.copy())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3502
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3503 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
3504 """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
3505 delimiters ("(" and ")" are the default).
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3506
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3507 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3508 - 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
3509 - 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
3510 - 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
3511 - 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
3512
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3513 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
3514 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
3515 as a list of separate values.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3516
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3517 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
3518 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
3519 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
3520 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
3521 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
3522 then pass C{None} for this argument.
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3523 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3524 if opener == closer:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3525 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
3526 if content is None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3527 if isinstance(opener,basestring) and isinstance(closer,basestring):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3528 if len(opener) == 1 and len(closer)==1:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3529 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3530 content = (Combine(OneOrMore(~ignoreExpr +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3531 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
3532 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3533 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3534 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
3535 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3536 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3537 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3538 content = (Combine(OneOrMore(~ignoreExpr +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3539 ~Literal(opener) + ~Literal(closer) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3540 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3541 ).setParseAction(lambda t:t[0].strip()))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3542 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3543 content = (Combine(OneOrMore(~Literal(opener) + ~Literal(closer) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3544 CharsNotIn(ParserElement.DEFAULT_WHITE_CHARS,exact=1))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3545 ).setParseAction(lambda t:t[0].strip()))
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("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
3548 ret = Forward()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3549 if ignoreExpr is not None:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3550 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
3551 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3552 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
3553 return ret
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3554
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3555 def indentedBlock(blockStatementExpr, indentStack, indent=True):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3556 """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
3557 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
3558
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3559 Parameters:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3560 - blockStatementExpr - expression defining syntax of statement that
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3561 is repeated within the indented block
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3562 - 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
3563 (multiple statementWithIndentedBlock expressions within a single grammar
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3564 should share a common indentStack)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3565 - 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
3566 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
3567 (default=True)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3568
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3569 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
3570 """
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3571 def checkPeerIndent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3572 if l >= len(s): return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3573 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3574 if curCol != indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3575 if curCol > indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3576 raise ParseFatalException(s,l,"illegal nesting")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3577 raise ParseException(s,l,"not a peer entry")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3578
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3579 def checkSubIndent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3580 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3581 if curCol > indentStack[-1]:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3582 indentStack.append( curCol )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3583 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3584 raise ParseException(s,l,"not a subentry")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3585
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3586 def checkUnindent(s,l,t):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3587 if l >= len(s): return
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3588 curCol = col(l,s)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3589 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
3590 raise ParseException(s,l,"not an unindent")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3591 indentStack.pop()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3592
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3593 NL = OneOrMore(LineEnd().setWhitespaceChars("\t ").suppress())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3594 INDENT = Empty() + Empty().setParseAction(checkSubIndent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3595 PEER = Empty().setParseAction(checkPeerIndent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3596 UNDENT = Empty().setParseAction(checkUnindent)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3597 if indent:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3598 smExpr = Group( Optional(NL) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3599 #~ FollowedBy(blockStatementExpr) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3600 INDENT + (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) + UNDENT)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3601 else:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3602 smExpr = Group( Optional(NL) +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3603 (OneOrMore( PEER + Group(blockStatementExpr) + Optional(NL) )) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3604 blockStatementExpr.ignore(_bslash + LineEnd())
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3605 return smExpr
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3606
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3607 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
3608 punc8bit = srange(r"[\0xa1-\0xbf\0xd7\0xf7]")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3609
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3610 anyOpenTag,anyCloseTag = makeHTMLTags(Word(alphas,alphanums+"_:"))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3611 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
3612 _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
3613 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
3614
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3615 # 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
3616 cStyleComment = Regex(r"/\*(?:[^*]*\*+)+?/").setName("C style comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3617
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3618 htmlComment = Regex(r"<!--[\s\S]*?-->")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3619 restOfLine = Regex(r".*").leaveWhitespace()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3620 dblSlashComment = Regex(r"\/\/(\\\n|.)*").setName("// comment")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3621 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
3622
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3623 javaStyleComment = cppStyleComment
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3624 pythonStyleComment = Regex(r"#.*").setName("Python style comment")
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3625 _commasepitem = Combine(OneOrMore(Word(printables, excludeChars=',') +
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3626 Optional( Word(" \t") +
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3627 ~Literal(",") + ~LineEnd() ) ) ).streamline().setName("commaItem")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3628 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
3629
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3630
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3631 if __name__ == "__main__":
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3632
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3633 def test( teststring ):
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3634 try:
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3635 tokens = simpleSQL.parseString( teststring )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3636 tokenlist = tokens.asList()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3637 print (teststring + "->" + str(tokenlist))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3638 print ("tokens = " + str(tokens))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3639 print ("tokens.columns = " + str(tokens.columns))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3640 print ("tokens.tables = " + str(tokens.tables))
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3641 print (tokens.asXML("SQL",True))
1084
b1dbb76f4eb9 Update abc2xml to latest - Python3 friendly.
Jim Hague <jim.hague@acm.org>
parents: 484
diff changeset
3642 except ParseBaseException as err:
484
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3643 print (teststring + "->")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3644 print (err.line)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3645 print (" "*(err.column-1) + "^")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3646 print (err)
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3647 print()
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3648
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3649 selectToken = CaselessLiteral( "select" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3650 fromToken = CaselessLiteral( "from" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3651
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3652 ident = Word( alphas, alphanums + "_$" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3653 columnName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3654 columnNameList = Group( delimitedList( columnName ) )#.setName("columns")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3655 tableName = delimitedList( ident, ".", combine=True ).setParseAction( upcaseTokens )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3656 tableNameList = Group( delimitedList( tableName ) )#.setName("tables")
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3657 simpleSQL = ( selectToken + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3658 ( '*' | columnNameList ).setResultsName( "columns" ) + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3659 fromToken + \
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3660 tableNameList.setResultsName( "tables" ) )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3661
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3662 test( "SELECT * from XYZZY, ABC" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3663 test( "select * from SYS.XYZZY" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3664 test( "Select A from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3665 test( "Select AA,BB,CC from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3666 test( "Select A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3667 test( "Select A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3668 test( "Xelect A, B, C from Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3669 test( "Select A, B, C frox Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3670 test( "Select" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3671 test( "Select ^^^ frox Sys.dual" )
4fab69a1027d Add MusicXML conversion to tune pages.
Jim Hague <jim.hague@acm.org>
parents:
diff changeset
3672 test( "Select A, B, C from Sys.dual, Table2 " )