本文整理汇总了Python中pymeta.grammar.OMeta类的典型用法代码示例。如果您正苦于以下问题:Python OMeta类的具体用法?Python OMeta怎么用?Python OMeta使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了OMeta类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, theme):
self._handlebars = OMeta.makeGrammar(handlebars_grammar, {}, 'handlebars')
self._builder = CodeBuilder()
self._compiler = OMeta.makeGrammar(compile_grammar, {'builder': self._builder})
self._helpers = {}
self.template_counter = 1
_ghostpy_['theme'] = theme
示例2: __init__
def __init__(self, theme, domain='', lookup_dir='', lookup_url=''):
self._handlebars = OMeta.makeGrammar(handlebars_grammar, {}, 'handlebars')
self._builder = CodeBuilder()
self._compiler = OMeta.makeGrammar(compile_grammar, {'builder': self._builder})
self._helpers = {}
self.template_counter = 1
_ghostpy_['base'] = domain
_ghostpy_['lookup_dir'] = lookup_dir
_ghostpy_['lookup_url'] = lookup_url
_ghostpy_['theme'] = theme
示例3: run_tests
def run_tests(grammar_iter, tests_iter, expecting=False):
""" Creates an OMeta grammar from the given grammar iterable, and
tries to parse each test in the given test iterable
"""
grammar_string = "".join(grammar_iter)
parser = OMeta.makeGrammar(grammar_string, {})
results = []
failures = []
for test in tests_iter:
if expecting:
to_parse, production, expected = [x.strip() for x in test.split("|||")]
expected_value = eval(expected)
else:
to_parse, production = [x.strip() for x in test.split("|||")]
application = parser(to_parse)
try:
result_value = application.apply(production)
if not expecting or result_value == expected_value:
results.append(".")
else:
results.append("F")
fail = "Test %s, %s failed" % (to_parse, production)
ex = "Expected '%s', got '%s'" % (expected, result_value)
failures.append((fail, ex))
except Exception as e:
results.append("F")
fail = "Test %s, %s failed" % (to_parse, production)
ex = "%s: %s" % (e.__class__.__name__, e)
failures.append((fail, ex))
return results, failures
示例4: test_spaces
def test_spaces(self):
Grammar = OMeta.makeGrammar(r"""
spaces_and_letters = spaces letter+:letters -> letters
""", {})
result, error = Grammar(" a").apply("spaces_and_letters")
assert result == ['a']
示例5: test_constant_bits
def test_constant_bits(self):
Grammar = OMeta.makeGrammar(r"""
var = "var" spaces letter+:name ';' -> ['var', ''.join(name)]
""", {})
g = Grammar("var myvar;")
result,error = g.apply("var")
assert result == ['var', 'myvar']
示例6: test_subclassing
def test_subclassing(self):
"""
A subclass of an OMeta subclass should be able to call rules on its
parent, and access variables in its scope.
"""
from pymeta.grammar import OMeta
grammar1 = """
dig ::= :x ?(a <= x <= b) => int(x)
"""
TestGrammar1 = OMeta.makeGrammar(grammar1, {'a':'0', 'b':'9'})
grammar2 = """
num ::= (<num>:n <dig>:d => n * base + d
| <dig>)
"""
TestGrammar2 = TestGrammar1.makeGrammar(grammar2, {'base':10})
g = TestGrammar2("314159")
self.assertEqual(g.apply("num")[0], 314159)
grammar3 = """
dig ::= :x ?(a <= x <= b or c <= x <= d) => int(x, base)
"""
TestGrammar3 = TestGrammar2.makeGrammar(grammar3, {'c':'a', 'd':'f', 'base':16})
g = TestGrammar3("abc123")
self.assertEqual(g.apply("num")[0], 11256099)
示例7: rdfaProcessSLAXML
def rdfaProcessSLAXML(node):
"""
Given an SLA node that has been preprocessed, remove sep tags and freqStart
tags, put in frequencies and spell wrappers.
"""
globs = globals().copy()
tree = NodeTree()
tree.useNode(node)
globs.update({'t':tree,
'ww': lambda *x:None,
# 'ww': debugWrite,
})
RDFaParser = OMeta.makeGrammar(rdfaGrammar, globs, 'RDFaParser')
seq = flattenSLATree(tree.node)[1:]
try:
parser = RDFaParser(seq)
parser.apply('sla')
except ParseError:
def propify(x):
if hasattr(x, 'nodeName') and x.nodeName == 'span':
if x.hasAttribute('p:property'):
return '<PROP {0}>'.format(x.getAttribute('p:property'))
return x
print map(propify, parser.input.data[:parser.input.position])
print map(propify, parser.input.data[parser.input.position:])
raise
return tree.node
示例8: preprocessSLAXML
def preprocessSLAXML(node):
"""
Given that node is a DOM element containing spell-like ability descriptions, insert
additional DOM nodes into the text where interesting properties are found.
"""
node.ownerDocument.documentElement.setAttribute(u'xmlns:p', PROP)
title = node.getElementsByTagName(u'b')[0]
title.setAttribute(u'p:property', u'powerName')
def addSpellNameRDFa(node):
node.setAttribute(u'p:property', u'spellName')
#
spellNames = list( util.doNodes(node, lambda n: n.nodeName == 'i', addSpellNameRDFa) )
assert len(spellNames) > 0
globs = globals().copy()
Preprocessor = OMeta.makeGrammar(preprocGrammar, globs, "Preprocessor")
todo = node.childNodes[:]
for n, cn in enumerate(todo):
if cn.nodeName == 'p':
todo[n+1:n+1] = cn.childNodes[:]
continue
if cn.nodeName == '#text':
parsed = []
globs['A'] = lambda *x: parsed.extend(x)
Preprocessor(cn.data).apply('slaText')
nodes = joinRaw(parsed)
substituteSLAText(cn, nodes)
return node
示例9: no_test_begin
def no_test_begin(self):
Grammar = OMeta.makeGrammar(r"""
from_start = begin 'a' -> 'a'
at_end = 'b' end -> 'b'
any = (from_start | at_end)*
""", {})
result, error = Grammar(" a").apply("any")
assert result == []
示例10: test_allow_comments
def test_allow_comments(self):
"""
Full line comments before a rule is allowed
Make sure that the boot.py version supports comments
"""
from pymeta.grammar import OMeta
g = OMeta.makeGrammar("""
# comment for interp
interp = "Foo" 1 2 -> 3
""",{})
self.assertEqual(g(['F', 'o', 'o', 1, 2]).apply("interp")[0], 3)
g = OMeta.makeGrammar("""
// comment for interp
interp = "Foo" 1 2 -> 3
""",{})
self.assertEqual(g(['F', 'o', 'o', 1, 2]).apply("interp")[0], 3)
示例11: parse
def parse(s):
"""
Produce a sequence of UIState objects from a text representation of a
seedo diagram
"""
globs = globals().copy()
global Parser
if Parser is None:
Parser = OMeta.makeGrammar(seedoGrammar, globs, "Parser")
return Parser(s).apply('uiTests')
示例12: test_end
def test_end(self):
Grammar = OMeta.makeGrammar(r"""
at_end = 'b' end -> 'b|'
any = (at_end | anything)*
""", {})
result, error = Grammar(" a").apply("any")
assert result == [' ','a']
result, error = Grammar("b a").apply("any")
assert result == ['b',' ','a']
result, error = Grammar(" ab").apply("any")
assert result == [' ','a','b|']
示例13: test_super
def test_super(self):
"""
Rules can call the implementation in a superclass.
"""
from pymeta.grammar import OMeta
grammar1 = "expr ::= <letter>"
TestGrammar1 = OMeta.makeGrammar(grammar1, {})
grammar2 = "expr ::= <super> | <digit>"
TestGrammar2 = TestGrammar1.makeGrammar(grammar2, {})
self.assertEqual(TestGrammar2("x").apply("expr")[0], "x")
self.assertEqual(TestGrammar2("3").apply("expr")[0], "3")
示例14: test_read_ahead
def test_read_ahead(self):
Grammar = OMeta.makeGrammar(r"""
attribute = bool | valued
bool = (letter+):name ~('=') -> (''.join(name),True)
valued = (letter+):name '=' (letter+):value -> (''.join(name),''.join(value))
""", {})
g = Grammar("a")
result,error = g.apply("attribute")
assert result == ('a',True)
g = Grammar("a=b")
result,error = g.apply("attribute")
assert result == ('a','b')
示例15: test_makeGrammar
def test_makeGrammar(self):
#imported here to prevent OMetaGrammar from being constructed before
#tests are run
from pymeta.grammar import OMeta
results = []
grammar = """
digit ::= :x ?('0' <= x <= '9') => int(x)
num ::= (<num>:n <digit>:d !(results.append(True)) => n * 10 + d
| <digit>)
"""
TestGrammar = OMeta.makeGrammar(grammar, {'results':results})
g = TestGrammar("314159")
self.assertEqual(g.apply("num")[0], 314159)
self.assertNotEqual(len(results), 0)