#!/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()

goal = bnf.Nonterminal("goal")
pkg_decl = bnf.Nonterminal("package_declaration")
import_decl = bnf.Nonterminal("import_declaration")
class_decl = bnf.Nonterminal("class_declaration")
class_modifier = bnf.Nonterminal("class_modifier")
class_body = bnf.Nonterminal("class_body")
super_decl = bnf.Nonterminal("super")
method_declaration = bnf.Nonterminal("method_declaration")
method_header = bnf.Nonterminal("method_header")
method_mod = bnf.Nonterminal("method_modifier")
method_body = bnf.Nonterminal("method_body")
method_declarator = bnf.Nonterminal("method_declarator")
result_type = bnf.Nonterminal("result_type")

t_void = bnf.Terminal("void")
t_package = bnf.Terminal("package")
t_import = bnf.Terminal("import")
t_semicolon = bnf.Terminal(";")
t_class = bnf.Terminal("class")
t_public = bnf.Terminal("public")
t_abstract = bnf.Terminal("abstract")
t_final = bnf.Terminal("final")
t_extends = bnf.Terminal("extends")
t_open_brace = bnf.Terminal("{")
t_close_brace = bnf.Terminal("}")
t_open_paren = bnf.Terminal("(")
t_close_paren = bnf.Terminal(")")
t_comma = bnf.Terminal(",")
t_protected = bnf.Terminal("protected")
t_private = bnf.Terminal("private")
t_static = bnf.Terminal("static")
t_abstract = bnf.Terminal("abstract")
t_final = bnf.Terminal("final")
t_synchronized = bnf.Terminal("synchronized")
t_native = bnf.Terminal("native")
t_dot_star = bnf.Terminal(".*")

s_goal = goal.seq
s_goal.targets.append(bnf.Target(pkg_decl, 0, 1))
s_goal.targets.append(bnf.Target(import_decl, 0, -1))
s_goal.targets.append(bnf.Target(class_decl, 0, -1))
s_pkg_decl = pkg_decl.seq
s_pkg_decl.targets.append(bnf.Target(t_package))
s_pkg_decl.targets.append(bnf.Target(bnf.String))
s_pkg_decl.targets.append(bnf.Target(t_semicolon))
s_import_decl = import_decl.seq
s_import_decl.targets.append(bnf.Target(t_import))
s_import_decl.targets.append(bnf.Target(bnf.String))
s_import_decl.targets.append(bnf.Target(t_dot_star, 0, 1))
s_import_decl.targets.append(bnf.Target(t_semicolon))
s_class_decl = class_decl.seq
s_class_decl.targets.append(bnf.Target(class_modifier, 0, -1))
s_class_decl.targets.append(bnf.Target(t_class))
s_class_decl.targets.append(bnf.Target(bnf.String))
s_class_decl.targets.append(bnf.Target(super_decl, 0, 1))
s_class_decl.targets.append(bnf.Target(class_body))
s_class_modifier = class_modifier.seq
g = bnf.Group()
s_class_modifier.targets.append(bnf.Target(g))
s1 = bnf.Sequence()
s2 = bnf.Sequence()
s3 = bnf.Sequence()
g.choices.append(s1)
g.choices.append(s2)
g.choices.append(s3)
s1.targets.append(bnf.Target(t_public))
s2.targets.append(bnf.Target(t_abstract))
s3.targets.append(bnf.Target(t_final))
s_super_decl = super_decl.seq
s_super_decl.targets.append(bnf.Target(t_extends))
s_super_decl.targets.append(bnf.Target(bnf.String))
s_class_body = class_body.seq
s_class_body.targets.append(bnf.Target(t_open_brace))
s_class_body.targets.append(bnf.Target(method_declaration, 1, -1))
s_class_body.targets.append(bnf.Target(t_close_brace))
s_method_declaration = method_declaration.seq
s_method_declaration.targets.append(bnf.Target(method_header))
s_method_declaration.targets.append(bnf.Target(method_body))
s_method_header = method_header.seq
s_method_header.targets.append(bnf.Target(method_mod, 1, -1))
s_method_header.targets.append(bnf.Target(result_type))
s_method_header.targets.append(bnf.Target(method_declarator))
s_method_mod = method_mod.seq
g = bnf.Group()
s_method_mod.targets.append(bnf.Target(g))
s1 = bnf.Sequence()
s2 = bnf.Sequence()
s3 = bnf.Sequence()
s4 = bnf.Sequence()
s5 = bnf.Sequence()
s6 = bnf.Sequence()
s7 = bnf.Sequence()
s8 = bnf.Sequence()
s1.targets.append(bnf.Target(t_public))
s2.targets.append(bnf.Target(t_protected))
s3.targets.append(bnf.Target(t_private))
s4.targets.append(bnf.Target(t_static))
s5.targets.append(bnf.Target(t_abstract))
s6.targets.append(bnf.Target(t_final))
s7.targets.append(bnf.Target(t_synchronized))
s8.targets.append(bnf.Target(t_native))
g.choices.append(s1)
g.choices.append(s2)
g.choices.append(s3)
g.choices.append(s4)
g.choices.append(s5)
g.choices.append(s6)
g.choices.append(s7)

s_result_type = result_type.seq
g = bnf.Group()
s_result_type.targets.append(bnf.Target(g))
s1 = bnf.Sequence()
s2 = bnf.Sequence()
s1.targets.append(bnf.Target(bnf.String))
s2.targets.append(bnf.Target(t_void))
g.choices.append(s1)
g.choices.append(s2)
s_method_declarator = method_declarator.seq
g1 = bnf.Group()
s1 = bnf.Sequence()
g1.choices.append(s1)
g2 = bnf.Group()
s2 = bnf.Sequence()
g2.choices.append(s2)
s_method_declarator.targets.append(bnf.Target(bnf.String))
s_method_declarator.targets.append(bnf.Target(t_open_paren))
s_method_declarator.targets.append(bnf.Target(g1, 0, 1))
s1.targets.append(bnf.Target(bnf.String))
s1.targets.append(bnf.Target(g2, 0, -1))
s2.targets.append(bnf.Target(t_comma))
s2.targets.append(bnf.Target(bnf.String))
s_method_declarator.targets.append(bnf.Target(t_close_paren))
s_method_declarator.targets.append(bnf.Target(t_semicolon))
s_method_body = method_body.seq
s_method_body.targets.append(bnf.Target(t_open_brace))
s_method_body.targets.append(bnf.Target(bnf.String))
s_method_body.targets.append(bnf.Target(t_close_brace))

B.start_token = goal
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)

print "--------------------"
print "Original BNF:"
print B
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)
