Debian Codegraph @

ly/lex/mup.py

raw
1 # This file is part of python-ly, https://pypi.python.org/pypi/python-ly
2 #
3 # Copyright (c) 2008 - 2015 by Wilbert Berendsen
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 # See http://www.gnu.org/licenses/ for more information.
19
20 """
21 Parses and tokenizes MUP input.
22
23 MUP (www.arkkra.com) is an open source music typesetter (formerly shareware).
24
25 We add a tokenizer here, to enable a decent mup2ly conversion.
26
27 """
28
29 from __future__ import unicode_literals
30
31
32 from . import _token
33 from . import Parser, FallthroughParser
34
35
36 class Comment(_token.Comment):
37 pass
38
39
40 class LineComment(Comment):
41 rx = r"//.*$"
42
43
44 class String(_token.String):
45 pass
46
47
48 class StringQuotedStart(String, _token.StringStart):
49 rx = r'"'
50 def update_state(self, state):
51 state.enter(ParseString())
52
53
54 class StringQuotedEnd(String, _token.StringEnd):
55 rx = r'"'
56 def update_state(self, state):
57 state.leave()
58 state.endArgument()
59
60
61 class StringQuoteEscape(_token.Character):
62 rx = r'\\[\\"]'
63
64
65 class Macro(_token.Token):
66 rx = r'\b[A-Z][A-Z0-9_]*'
67
68
69 class Preprocessor(_token.Token):
70 rx = (r'\b('
71 'if|then|else|endif|define|undef|ifdef|ifndef'
72 r')\b|@')
73
74
75 class ParseMup(Parser):
76 mode = "mup"
77 items = (
78 LineComment,
79 StringQuotedStart,
80 Macro,
81 Preprocessor,
82 )
83
84
85 class ParseString(Parser):
86 default = String
87 items = (
88 StringQuotedEnd,
89 StringQuoteEscape,
90 )
91