| 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 Scheme input. |
| 22 |
""" |
| 23 |
|
| 24 |
from __future__ import unicode_literals |
| 25 |
|
| 26 |
from . import _token |
| 27 |
from . import Parser, FallthroughParser |
| 28 |
|
| 29 |
|
| 30 |
class Scheme(_token.Token): |
| 31 |
"""Baseclass for Scheme tokens.""" |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
class String(_token.String): |
| 36 |
pass |
| 37 |
|
| 38 |
|
| 39 |
class StringQuotedStart(String, _token.StringStart): |
| 40 |
rx = r'"' |
| 41 |
def update_state(self, state): |
| 42 |
state.enter(ParseString()) |
| 43 |
|
| 44 |
|
| 45 |
class StringQuotedEnd(String, _token.StringEnd): |
| 46 |
rx = r'"' |
| 47 |
def update_state(self, state): |
| 48 |
state.leave() |
| 49 |
state.endArgument() |
| 50 |
|
| 51 |
|
| 52 |
class StringQuoteEscape(_token.Character): |
| 53 |
rx = r'\\[\\"]' |
| 54 |
|
| 55 |
|
| 56 |
class Comment(_token.Comment): |
| 57 |
pass |
| 58 |
|
| 59 |
|
| 60 |
class LineComment(Comment, _token.LineComment): |
| 61 |
rx = r";.*$" |
| 62 |
|
| 63 |
|
| 64 |
class BlockCommentStart(Comment, _token.BlockCommentStart): |
| 65 |
rx = r"#!" |
| 66 |
def update_state(self, state): |
| 67 |
state.enter(ParseBlockComment()) |
| 68 |
|
| 69 |
|
| 70 |
class BlockCommentEnd(Comment, _token.BlockCommentEnd, _token.Leaver): |
| 71 |
rx = "!#" |
| 72 |
|
| 73 |
|
| 74 |
class BlockComment(Comment, _token.BlockComment): |
| 75 |
pass |
| 76 |
|
| 77 |
|
| 78 |
class OpenParen(Scheme, _token.MatchStart, _token.Indent): |
| 79 |
rx = r"\(" |
| 80 |
matchname = "schemeparen" |
| 81 |
def update_state(self, state): |
| 82 |
state.enter(ParseScheme()) |
| 83 |
|
| 84 |
|
| 85 |
class CloseParen(Scheme, _token.MatchEnd, _token.Dedent): |
| 86 |
rx = r"\)" |
| 87 |
matchname = "schemeparen" |
| 88 |
def update_state(self, state): |
| 89 |
state.leave() |
| 90 |
state.endArgument() |
| 91 |
|
| 92 |
|
| 93 |
class Quote(Scheme): |
| 94 |
rx = r"['`,]" |
| 95 |
|
| 96 |
|
| 97 |
class Dot(Scheme): |
| 98 |
rx = r"\.(?!\S)" |
| 99 |
|
| 100 |
|
| 101 |
class Bool(Scheme, _token.Item): |
| 102 |
rx = r"#[tf]\b" |
| 103 |
|
| 104 |
|
| 105 |
class Char(Scheme, _token.Item): |
| 106 |
rx = r"#\\([a-z]+|.)" |
| 107 |
|
| 108 |
|
| 109 |
class Word(Scheme, _token.Item): |
| 110 |
rx = r'[^()"{}\s]+' |
| 111 |
|
| 112 |
|
| 113 |
class Keyword(Word): |
| 114 |
@classmethod |
| 115 |
def test_match(cls, match): |
| 116 |
from .. import data |
| 117 |
return match.group() in data.scheme_keywords() |
| 118 |
|
| 119 |
|
| 120 |
class Function(Word): |
| 121 |
@classmethod |
| 122 |
def test_match(cls, match): |
| 123 |
from .. import data |
| 124 |
return match.group() in data.scheme_functions() |
| 125 |
|
| 126 |
|
| 127 |
class Variable(Word): |
| 128 |
@classmethod |
| 129 |
def test_match(cls, match): |
| 130 |
from .. import data |
| 131 |
return match.group() in data.scheme_variables() |
| 132 |
|
| 133 |
|
| 134 |
class Constant(Word): |
| 135 |
@classmethod |
| 136 |
def test_match(cls, match): |
| 137 |
from .. import data |
| 138 |
return match.group() in data.scheme_constants() |
| 139 |
|
| 140 |
|
| 141 |
class Number(_token.Item, _token.Numeric): |
| 142 |
rx = (r"(" |
| 143 |
r"-?\d+|" |
| 144 |
r"#(b[0-1]+|o[0-7]+|x[0-9a-fA-F]+)|" |
| 145 |
r"[-+]inf.0|[-+]?nan.0" |
| 146 |
r")(?=$|[)\s])") |
| 147 |
|
| 148 |
|
| 149 |
class Fraction(Number): |
| 150 |
rx = r"-?\d+/\d+(?=$|[)\s])" |
| 151 |
|
| 152 |
|
| 153 |
class Float(Number): |
| 154 |
rx = r"-?((\d+(\.\d*)|\.\d+)(E\d+)?)(?=$|[)\s])" |
| 155 |
|
| 156 |
|
| 157 |
class VectorStart(OpenParen): |
| 158 |
rx = r"#\(" |
| 159 |
|
| 160 |
|
| 161 |
class LilyPond(_token.Token): |
| 162 |
pass |
| 163 |
|
| 164 |
|
| 165 |
class LilyPondStart(LilyPond, _token.MatchStart, _token.Indent): |
| 166 |
rx = r"#{" |
| 167 |
matchname = "schemelily" |
| 168 |
def update_state(self, state): |
| 169 |
state.enter(ParseLilyPond()) |
| 170 |
|
| 171 |
|
| 172 |
class LilyPondEnd(LilyPond, _token.Leaver, _token.MatchEnd, _token.Dedent): |
| 173 |
rx = r"#}" |
| 174 |
matchname = "schemelily" |
| 175 |
|
| 176 |
|
| 177 |
# Parsers |
| 178 |
|
| 179 |
class ParseScheme(Parser): |
| 180 |
mode = 'scheme' |
| 181 |
items = ( |
| 182 |
_token.Space, |
| 183 |
OpenParen, |
| 184 |
CloseParen, |
| 185 |
LineComment, |
| 186 |
BlockCommentStart, |
| 187 |
LilyPondStart, |
| 188 |
VectorStart, |
| 189 |
Dot, |
| 190 |
Bool, |
| 191 |
Char, |
| 192 |
Quote, |
| 193 |
Fraction, |
| 194 |
Float, |
| 195 |
Number, |
| 196 |
Constant, |
| 197 |
Keyword, |
| 198 |
Function, |
| 199 |
Variable, |
| 200 |
Word, |
| 201 |
StringQuotedStart, |
| 202 |
) |
| 203 |
|
| 204 |
|
| 205 |
class ParseString(Parser): |
| 206 |
default = String |
| 207 |
items = ( |
| 208 |
StringQuotedEnd, |
| 209 |
StringQuoteEscape, |
| 210 |
) |
| 211 |
|
| 212 |
|
| 213 |
class ParseBlockComment(Parser): |
| 214 |
default = BlockComment |
| 215 |
items = ( |
| 216 |
BlockCommentEnd, |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
from . import lilypond |
| 221 |
|
| 222 |
class ParseLilyPond(lilypond.ParseMusic): |
| 223 |
items = (LilyPondEnd,) + lilypond.ParseMusic.items |
| 224 |
|