Example #1
0
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from parsegen.output import register_formatter
from parsegen.output.callback import CallbackOutputFormatter

class PrettyPrintFormatter(CallbackOutputFormatter):
	"""Pretty Print Formatter
	
	Prints out a human readable representation of the computed grammar.
	"""
	
	def __init__(self, *args):
		CallbackOutputFormatter.__init__(self, *args)
		self.register_callback(self._output_symbol, CallbackOutputFormatter.MAIN)
	
	def _output_symbol(self, symbol, file):
		kind = "NULLABLE" if symbol.is_nullable() else "COMPULSORY"
		file.write("%s SYMBOL %s {\n" % (kind, symbol.name))
		for exp in symbol.expansions:
			file.write("  {%s}\n" % ", ".join(exp.predictions))
			file.write("  ~> %s\n" % ", ".join(exp.tokens))
		file.write("}\n\n")

register_formatter("pretty_print", PrettyPrintFormatter)
Example #2
0
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from parsegen.output import register_formatter
from parsegen.output.mustache import MustacheFormatter
	
class RubyFormatter(MustacheFormatter):
	"""Ruby Formatter
	
	Outputs a grammar as an executable ruby program.
	"""

	def __init__(self, *args):
		MustacheFormatter.__init__(self, "ruby.mustache",  *args)
		self.register_option("ruby_module", default="", prefix=False)

register_formatter("ruby", RubyFormatter)
Example #3
0
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from parsegen.output import register_formatter
from parsegen.output.mustache import MustacheFormatter
	
class COutputFormatter(MustacheFormatter):
	"""C Output Formatter
	
	Represents the formatter required to write out to a C file.
	"""
	
	def __init__(self, *args):
		MustacheFormatter.__init__(self, "c.mustache", *args)

register_formatter("c", COutputFormatter)
Example #4
0
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from parsegen.output import register_formatter
from parsegen.output.callback import CallbackOutputFormatter


class PrettyPrintFormatter(CallbackOutputFormatter):
    """Pretty Print Formatter
	
	Prints out a human readable representation of the computed grammar.
	"""
    def __init__(self, *args):
        CallbackOutputFormatter.__init__(self, *args)
        self.register_callback(self._output_symbol,
                               CallbackOutputFormatter.MAIN)

    def _output_symbol(self, symbol, file):
        kind = "NULLABLE" if symbol.is_nullable() else "COMPULSORY"
        file.write("%s SYMBOL %s {\n" % (kind, symbol.name))
        for exp in symbol.expansions:
            file.write("  {%s}\n" % ", ".join(exp.predictions))
            file.write("  ~> %s\n" % ", ".join(exp.tokens))
        file.write("}\n\n")


register_formatter("pretty_print", PrettyPrintFormatter)
Example #5
0
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from parsegen.output import register_formatter
from parsegen.output.mustache import MustacheFormatter


class COutputFormatter(MustacheFormatter):
    """C Output Formatter
	
	Represents the formatter required to write out to a C file.
	"""
    def __init__(self, *args):
        MustacheFormatter.__init__(self, "c.mustache", *args)


register_formatter("c", COutputFormatter)