Source code for ly.lex.texinfo

# This file is part of python-ly, https://pypi.python.org/pypi/python-ly
#
# Copyright (c) 2008 - 2015 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Parses and tokenizes Texinfo input, recognizing LilyPond in Texinfo.
"""

from __future__ import unicode_literals

from . import _token
from . import Parser, FallthroughParser


[docs]class Comment(_token.Comment): pass
[docs]class LineComment(Comment, _token.LineComment): rx = r"@c\b.*$"
[docs]class BlockCommentStart(Comment, _token.BlockCommentStart): rx = r"@ignore\b"
[docs] def update_state(self, state): state.enter(ParseComment())
[docs]class BlockCommentEnd(Comment, _token.Leaver, _token.BlockCommentEnd): rx = r"@end\s+ignore\b"
[docs]class Attribute(_token.Token): pass
[docs]class Keyword(_token.Token): rx = r"@[a-zA-Z]+"
[docs]class Block(_token.Token): pass
[docs]class BlockStart(Block): rx = r"@[a-zA-Z]+\{"
[docs] def update_state(self, state): state.enter(ParseBlock())
[docs]class BlockEnd(Block, _token.Leaver): rx = r"\}"
[docs]class EscapeChar(_token.Character): rx = r"@[@{}]"
[docs]class Accent(EscapeChar): rx = "@['\"',=^`~](\\{[a-zA-Z]\\}|[a-zA-Z]\\b)"
[docs]class Verbatim(_token.Token): pass
[docs]class VerbatimStart(Keyword): rx = r"@verbatim\b"
[docs] def update_state(self, state): state.enter(ParseVerbatim())
[docs]class VerbatimEnd(Keyword, _token.Leaver): rx = r"@end\s+verbatim\b"
[docs]class LilyPondBlockStart(Block): rx = r"@lilypond(?=(\[[a-zA-Z,=0-9\\\s]+\])?\{)"
[docs] def update_state(self, state): state.enter(ParseLilyPondBlockAttr())
[docs]class LilyPondBlockStartBrace(Block): rx = r"\{"
[docs] def update_state(self, state): state.replace(ParseLilyPondBlock())
[docs]class LilyPondBlockEnd(Block, _token.Leaver): rx = r"\}"
[docs]class LilyPondEnvStart(Keyword): rx = r"@lilypond\b"
[docs] def update_state(self, state): state.enter(ParseLilyPondEnvAttr())
[docs]class LilyPondEnvEnd(Keyword, _token.Leaver): rx = r"@end\s+lilypond\b"
[docs]class LilyPondFileStart(Block): rx = r"@lilypondfile\b"
[docs] def update_state(self, state): state.enter(ParseLilyPondFile())
[docs]class LilyPondFileStartBrace(Block): rx = r"\{"
[docs] def update_state(self, state): state.replace(ParseBlock())
[docs]class LilyPondAttrStart(Attribute): rx = r"\["
[docs] def update_state(self, state): state.enter(ParseLilyPondAttr())
[docs]class LilyPondAttrEnd(Attribute, _token.Leaver): rx = r"\]"
# Parsers:
[docs]class ParseTexinfo(Parser): mode = "texinfo" items = ( LineComment, BlockCommentStart, Accent, EscapeChar, LilyPondBlockStart, LilyPondEnvStart, LilyPondFileStart, BlockStart, VerbatimStart, Keyword, )
[docs]class ParseComment(Parser): default = Comment items = ( BlockCommentEnd, )
[docs]class ParseBlock(Parser): items = ( BlockEnd, Accent, EscapeChar, BlockStart, Keyword, )
[docs]class ParseVerbatim(Parser): default = Verbatim items = ( VerbatimEnd, )
[docs]class ParseLilyPondBlockAttr(Parser): items = ( LilyPondAttrStart, LilyPondBlockStartBrace, )
[docs]class ParseLilyPondEnvAttr(FallthroughParser): items = ( LilyPondAttrStart, )
[docs] def fallthrough(self, state): state.replace(ParseLilyPondEnv())
[docs]class ParseLilyPondAttr(Parser): default = Attribute items = ( LilyPondAttrEnd, )
[docs]class ParseLilyPondFile(Parser): items = ( LilyPondAttrStart, LilyPondFileStartBrace, )
from . import lilypond
[docs]class ParseLilyPondBlock(lilypond.ParseGlobal): items = ( LilyPondBlockEnd, ) + lilypond.ParseGlobal.items
[docs]class ParseLilyPondEnv(lilypond.ParseGlobal): items = ( LilyPondEnvEnd, ) + lilypond.ParseGlobal.items