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)
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"])
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)
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)
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)
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")
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)
def _parseGrammar(self, grammar, name="Grammar"): return OMeta(grammar).parseGrammar(name)
def __init__(self, source): self.source = source self.grammar = OMeta(source).parseGrammar('grammar') self.rules = decomposeGrammar(self.grammar)
def makeSAMProtocol(senderFactory, receiverFactory): g = OMeta(grammar.samGrammarSource).parseGrammar('Grammar') return functools.partial(SAMParserProtocol, g, senderFactory, receiverFactory, {})
def getRule(source, name): o = OMeta(source).parseGrammar('grammar') return decomposeGrammar(o)[name]
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):
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)
def __init__(self, sender): self._sender = sender self._messageSetGrammar = OMeta(grammar_source).parseGrammar('messageSetGrammar') self._parsers = {}
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)
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)
def compile(self, grammar, globals=None): g = OMeta(grammar) tree = g.parseGrammar('TestGrammar') return TrampolinedInterpWrapper(tree, globals)