#!/usr/bin/env python
from coral import kernel as coral
from coral.lgen.bnf import bnf
from coral.lgen.bnf import grammar2lang
from coral.lgen.bnf import lang2grammar
from coral.lgen import lang2smd

B = bnf.BNF()
n0 = bnf.Nonterminal("init")
n1 = bnf.Nonterminal("an_enum")
t1 = bnf.Terminal("public")
t2 = bnf.Terminal("private")
t3 = bnf.Terminal("protected")
t4 = bnf.Terminal("package")

s0_0 = bnf.Sequence()
s0_1 = bnf.Sequence()
s0_0.targets.append(bnf.Target(n0))
s0_0.targets.append(bnf.Target(n1))
s0_1.targets.append(bnf.Target(n1))
g = bnf.Group()
g.choices.append(s0_0)
g.choices.append(s0_1)
n0.seq.targets.append(bnf.Target(g))

s1 = bnf.Sequence()
s2 = bnf.Sequence()
s3 = bnf.Sequence()
s4 = bnf.Sequence()
r1 = bnf.Target(t1)
r2 = bnf.Target(t2)
r3 = bnf.Target(t3)
r4 = bnf.Target(t4)
s1.targets.append(r1)
s2.targets.append(r2)
s3.targets.append(r3)
s4.targets.append(r4)

g1 = bnf.Group()
g1.choices.append(s1)
g1.choices.append(s2)
g1.choices.append(s3)
g1.choices.append(s4)
n1.seq.targets.append(bnf.Target(g1))

B.start_token = n0

print B

info = grammar2lang.information()
info.name = "mylang"
info.version = 1
info.revision = 1
info.URInamespace = "<< invalid >>"

L = grammar2lang.grammar2lang(B, info)
Lmodel = lang2smd.lang2smd(L)
coral.saveModel("tmp/out.xmi", Lmodel)


B = lang2grammar.lang2grammar(L)
print "Reverse creation:"
print B

L = grammar2lang.grammar2lang(B, info)
B = lang2grammar.lang2grammar(L)
print "Reverse creation:"
print B
coral.saveModel("tmp/out2.xmi", Lmodel)
