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']
def test_constant(self): g = Grammar("""\ "abc"\ """) result,error = g.apply("string") assert result == '"abc"' g = Grammar("""\ 'abc'\ """) result,error = g.apply("string") assert result == "'abc'"
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')
def test_nested_productions(self): Grammar = OMeta.makeGrammar(r""" attributes = spaces (attribute:a spaces -> a)+:as -> as attribute = bool | valued bool = (letter+):name ~('=') -> (''.join(name),True) valued = (letter+):name '=' (letter+):value -> (''.join(name),''.join(value)) """, {}) g = Grammar("a=b") result,error = g.apply("attributes") assert result == [('a','b')] g = Grammar("a=b c d=e") result,error = g.apply("attributes") assert result == [('a','b'),('c',True),('d','e')]
def test_import_macros(self): self.assertStatements('''@import a.b.c as c;''',[ ["import","a.b.c","c"] ]) self.assertStatements('''@import a.b.* as *;''',[ ["starimport","a.b"] ]) g = Grammar("""\ @import a.b.c as c;""") result,error = g.apply("statements") resolved = expand_macros(result) assert resolved == [["statement","var", " ","c","=","resolver",["parenthesis",[]],["parenthesis",['"',"a.b.c",'"']],";"]] self.assertStatementsOut(resolved,'var c=resolver()("a.b.c");')
def test_comment(self): g = Grammar("""\ // hello""") result,error = g.apply("comment") assert result == ["comment", " hello"] g = Grammar("""\ /* hello */""") result,error = g.apply("slcomment") assert result == ["slcomment", " hello "] g = Grammar("""\ /* hello */""") result,error = g.apply("mlcomment") assert result == ["mlcomment", " hello "] g = Grammar("""\ /* hello "a/b/c"*/""") result,error = g.apply("mlcomment") assert result == ["mlcomment", ' hello "a/b/c"']