Beispiel #1
0
def makeProtocol(source,
                 senderFactory,
                 receiverFactory,
                 bindings=None,
                 name='Grammar'):
    """
    Create a Twisted ``Protocol`` factory from a Parsley grammar.

    :param source: A grammar, as a string.
    :param senderFactory: A one-argument callable that takes a twisted
        ``Transport`` and returns a :ref:`sender <senders>`.
    :param receiverFactory: A one-argument callable that takes the sender
        returned by the ``senderFactory`` and returns a :ref:`receiver
        <receivers>`.
    :param bindings: A mapping of variable names to objects which will be
        accessible from python code in the grammar.
    :param name: The name used for the generated grammar class.
    :returns: A nullary callable which will return an instance of
        :class:`~.ParserProtocol`.
    """

    from ometa.protocol import ParserProtocol
    if bindings is None:
        bindings = {}
    grammar = OMeta(source).parseGrammar(name)
    return functools.partial(ParserProtocol, grammar, senderFactory,
                             receiverFactory, bindings)
Beispiel #2
0
 def test_stringConsumedBy(self):
     called = []
     grammarSource = "rule = <'x'+>:y -> y"
     grammar = OMeta(grammarSource).parseGrammar("Parser")
     def interp(result, error):
         called.append(result)
     trampoline = TrampolinedGrammarInterpreter(grammar, "rule", interp)
     trampoline.receive("xxxxx")
     trampoline.end()
     self.assertEqual(called, ["xxxxx"])
Beispiel #3
0
 def __init__(self):
     source = OMeta(grammar.grammarSource).parseGrammar('Grammar')
     bindings = grammar.bindings
     if bindings is None:
         bindings = {}
     ParserProtocol.__init__(self,
                             grammar=source,
                             senderFactory=SOCKS4Sender,
                             receiverFactory=SOCKS4Receiver,
                             bindings=bindings)
Beispiel #4
0
    def compile(self, grammar, globals=None):
        """
        Produce an object capable of parsing via this grammar.

        @param grammar: A string containing an OMeta grammar.
        """
        g = OMeta(grammar)
        tree = g.parseGrammar('TestGrammar')
        g = GrammarInterpreter(tree, OMetaBase, globals)
        return HandyInterpWrapper(g)
Beispiel #5
0
 def __init__(self):
     source = OMeta(grammar.grammarSource).parseGrammar('Grammar')
     bindings = grammar.bindings
     if bindings is None:
         bindings = {}
     ParserProtocol.__init__(self,
                             grammar=source,
                             senderFactory=SOCKS5Sender,
                             receiverFactory=stack(SOCKS5AuthDispatcher,
                                                   SOCKS5Receiver),
                             bindings=bindings)
Beispiel #6
0
 def test_failure(self):
     g = OMeta("""
        foo = 'a':one baz:two 'd'+ 'e' -> (one, two)
        baz = 'b' | 'c'
        """, {})
     tree = g.parseGrammar('TestGrammar')
     i = TrampolinedGrammarInterpreter(
         tree, 'foo', callback=lambda x: setattr(self, 'result', x))
     e = self.assertRaises(ParseError, i.receive, 'foobar')
     self.assertEqual(str(e),
     "\nfoobar\n^\nParse error at line 2, column 0:"
     " expected the character 'a'. trail: []\n")
Beispiel #7
0
class SOCKS4ClientProtocol(ParserProtocol):
    source = OMeta(grammar.grammarSource).parseGrammar('Grammar')

    def __init__(self):
        bindings = grammar.bindings
        if bindings is None:
            bindings = {}
        ParserProtocol.__init__(self,
                                grammar=self.source,
                                senderFactory=SOCKS4Sender,
                                receiverFactory=SOCKS4Receiver,
                                bindings=bindings)

    def dataReceived(self, data):
        data = to_string(data)
        return ParserProtocol.dataReceived(self, data)
Beispiel #8
0
 def _parseGrammar(self, grammar, name="Grammar"):
     return OMeta(grammar).parseGrammar(name)
Beispiel #9
0
 def __init__(self, source):
     self.source = source
     self.grammar = OMeta(source).parseGrammar('grammar')
     self.rules = decomposeGrammar(self.grammar)
Beispiel #10
0
def makeSAMProtocol(senderFactory, receiverFactory):
    g = OMeta(grammar.samGrammarSource).parseGrammar('Grammar')
    return functools.partial(SAMParserProtocol, g, senderFactory,
                             receiverFactory, {})
Beispiel #11
0
def getRule(source, name):
    o = OMeta(source).parseGrammar('grammar')
    return decomposeGrammar(o)[name]
Beispiel #12
0
from ometa.runtime import ParseError

protocol = pytest.importorskip('ometa.protocol')
ParserProtocol = protocol.ParserProtocol

testingGrammarSource = """

someA = ('a' 'a') -> receiver('a')
someB = ('b' 'b') -> receiver('b')
someC = ('c' 'c') -> receiver('c')
someExc = 'e' -> receiver.raiseSomething()

initial = someA | someExc

"""
testGrammar = OMeta(testingGrammarSource).parseGrammar('testGrammar')


class SenderFactory(object):
    def __init__(self, transport):
        self.transport = transport


class SomeException(Exception):
    pass


class ReceiverFactory(object):
    currentRule = 'initial'

    def __init__(self, sender):
Beispiel #13
0
import sys
from ometa.grammar import OMeta
from ometa.builder import writePython

if len(sys.argv) != 3:
    print "Usage: %s grammar-filename python-filename" % (sys.argv[0], )
    sys.exit(1)

with open(sys.argv[1]) as infile:
    grammar = infile.read()
g = OMeta(grammar)
tree = g.parseGrammar("Parser")
source = writePython(tree, grammar) + '\n'
with open(sys.argv[2], 'w') as outfile:
    outfile.write(source)
Beispiel #14
0
 def __init__(self, sender):
     self._sender = sender
     self._messageSetGrammar = OMeta(grammar_source).parseGrammar('messageSetGrammar')
     self._parsers = {}
Beispiel #15
0
def makeProtocol(source, sender, receiver, bindings=None, name='Grammar'):
    if bindings is None:
        bindings = {}
    grammar = OMeta(source).parseGrammar(name)
    return functools.partial(ParserProtocol, grammar, sender, receiver,
                             bindings)
Beispiel #16
0
def getGrammar(pkg, name):
    base = os.path.dirname(os.path.abspath(pkg.__file__))
    src = open(os.path.join(base, name + ".parsley")).read()
    return OMeta(src).parseGrammar(name)
Beispiel #17
0
 def compile(self, grammar, globals=None):
     g = OMeta(grammar)
     tree = g.parseGrammar('TestGrammar')
     return TrampolinedInterpWrapper(tree, globals)