From e210aadfd758b1b5a46477c03cafa9ebf325e6b3 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Thu, 17 Oct 2013 22:09:29 -0400 Subject: [PATCH] nitcc: better name of generated classes to avoid name conflicts Signed-off-by: Jean Privat --- contrib/nitcc/examples/calc.nit | 2 +- contrib/nitcc/examples/minilang.nit | 2 +- contrib/nitcc/src/autom.nit | 12 +++++++----- contrib/nitcc/src/grammar.nit | 10 +++++----- contrib/nitcc/src/nitcc.nit | 16 ++++++++-------- contrib/nitcc/src/nitcc_lexer0.nit | 2 +- contrib/nitcc/src/nitcc_parser_gen.nit | 2 +- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/contrib/nitcc/examples/calc.nit b/contrib/nitcc/examples/calc.nit index 1ee7bc3..d444f2c 100644 --- a/contrib/nitcc/examples/calc.nit +++ b/contrib/nitcc/examples/calc.nit @@ -75,7 +75,7 @@ redef class Ne_div end end -var t = new MyTest +var t = new TestParser_calc var n = t.main var v = new Calulator v.enter_visit(n) diff --git a/contrib/nitcc/examples/minilang.nit b/contrib/nitcc/examples/minilang.nit index c639f14..4160f01 100644 --- a/contrib/nitcc/examples/minilang.nit +++ b/contrib/nitcc/examples/minilang.nit @@ -175,7 +175,7 @@ redef class Ne_read end end -var t = new MyTest +var t = new TestParser_minilang var n = t.main var v = new Interpretor diff --git a/contrib/nitcc/src/autom.nit b/contrib/nitcc/src/autom.nit index 2d1ca78..86befbb 100644 --- a/contrib/nitcc/src/autom.nit +++ b/contrib/nitcc/src/autom.nit @@ -393,9 +393,9 @@ class Automaton # Generate the Nit source code of the lexer # `filepath` is the name of the ouptit file # `parser` is the name of the parser module (used to import the token classes) - fun gen_to_nit(filepath: String, parser: nullable String) + fun gen_to_nit(filepath: String, name: String, parser: nullable String) do - var gen = new DFAGenerator(filepath, self, parser) + var gen = new DFAGenerator(filepath, name, self, parser) gen.gen_to_nit end end @@ -403,12 +403,14 @@ end # Generate the Nit source code of the lexer private class DFAGenerator var filepath: String + var name: String var automaton: Automaton var parser: nullable String var out: OStream - init(filepath: String, automaton: Automaton, parser: nullable String) do + init(filepath: String, name: String, automaton: Automaton, parser: nullable String) do self.filepath = filepath + self.name = name self.automaton = automaton self.parser = parser self.out = new OFStream.open(filepath) @@ -425,13 +427,13 @@ private class DFAGenerator i += 1 end - add "# Lexer generated by nitcc" + add "# Lexer generated by nitcc for the grammar {name}" add("import nitcc_runtime\n") var p = parser if p != null then add("import {p}\n") - add("class MyLexer\n") + add("class Lexer_{name}\n") add("\tsuper Lexer\n") add("\tredef fun start_state do return dfastate_{names[automaton.start]}\n") add("end\n") diff --git a/contrib/nitcc/src/grammar.nit b/contrib/nitcc/src/grammar.nit index 6cb7956..72a917c 100644 --- a/contrib/nitcc/src/grammar.nit +++ b/contrib/nitcc/src/grammar.nit @@ -568,10 +568,10 @@ class LRAutomaton end # Generate the parser of the automaton - fun gen_to_nit(filepath: String) + fun gen_to_nit(filepath: String, name: String) do var gen = new Generator - gen.gen_to_nit(self) + gen.gen_to_nit(self, name) var f = new OFStream.open(filepath) for s in gen.out do f.write(s) @@ -592,15 +592,15 @@ end private class Generator var out = new Array[String] fun add(s: String) do out.add(s) - fun gen_to_nit(autom: LRAutomaton) + fun gen_to_nit(autom: LRAutomaton, name: String) do var states = autom.states var gram = autom.grammar - add "# Parser generated by nitcc" + add "# Parser generated by nitcc for the grammar {name}" add "import nitcc_runtime" - add "class MyParser" + add "class Parser_{name}" add "\tsuper Parser" add "\tredef fun start_state do return state_{states.first.cname}" add "end" diff --git a/contrib/nitcc/src/nitcc.nit b/contrib/nitcc/src/nitcc.nit index bc2e8b3..8063b43 100644 --- a/contrib/nitcc/src/nitcc.nit +++ b/contrib/nitcc/src/nitcc.nit @@ -36,10 +36,10 @@ end # Parse the grammar file -var l = new MyLexer(text) +var l = new Lexer_nitcc(text) var ts = l.lex -var p = new MyParser +var p = new Parser_nitcc p.tokens.add_all ts var node = p.parse @@ -129,21 +129,21 @@ dfa.to_dot("{name}.dfa.dot") # Generate Nit code print "Generate {name}_lexer.nit {name}_parser.nit {name}_test_parser.nit" -dfa.gen_to_nit("{name}_lexer.nit", "{name}_parser") -lr.gen_to_nit("{name}_parser.nit") +dfa.gen_to_nit("{name}_lexer.nit", name, "{name}_parser") +lr.gen_to_nit("{name}_parser.nit", name) f = new OFStream.open("{name}_test_parser.nit") f.write """# Generated by nitcc for the language {{{name}}} import nitcc_runtime import {{{name}}}_lexer import {{{name}}}_parser -class MyTest +class TestParser_{{{name}}} super TestParser redef fun name do return \"{{{name}}}\" - redef fun new_lexer(text) do return new MyLexer(text) - redef fun new_parser do return new MyParser + redef fun new_lexer(text) do return new Lexer_{{{name}}}(text) + redef fun new_parser do return new Parser_{{{name}}} end -var t = new MyTest +var t = new TestParser_{{{name}}} t.main """ f.close diff --git a/contrib/nitcc/src/nitcc_lexer0.nit b/contrib/nitcc/src/nitcc_lexer0.nit index 50d590e..91767bf 100644 --- a/contrib/nitcc/src/nitcc_lexer0.nit +++ b/contrib/nitcc/src/nitcc_lexer0.nit @@ -22,7 +22,7 @@ import nitcc_parser # Hand-writen lexer of nitcc # Used only for the boostrap of the tool. -class MyLexer +class Lexer_nitcc var text: String var iter: Iterator[Char] = "".iterator diff --git a/contrib/nitcc/src/nitcc_parser_gen.nit b/contrib/nitcc/src/nitcc_parser_gen.nit index 0118a4c..adde5b5 100644 --- a/contrib/nitcc/src/nitcc_parser_gen.nit +++ b/contrib/nitcc/src/nitcc_parser_gen.nit @@ -143,7 +143,7 @@ var a = g.lr0 print "LR automaton: {a.states.length} states (see nitcc0.lr.dot)" a.to_dot("nitcc0.lr.dot") -a.gen_to_nit("nitcc_parser.nit") +a.gen_to_nit("nitcc_parser.nit", "nitcc") var f = new OFStream.open("nitcc_lexer.nit") f.write("import nitcc_lexer0\n") -- 1.7.9.5