//инициализация критических секций с созданием класса private static void InitCriticals(syntax_tree_visitor visitor) { //генерируем класс SyntaxTree.type_declarations TypeDecls = new PascalABCCompiler.SyntaxTree.type_declarations(); SyntaxTree.type_declaration TypeDecl = new PascalABCCompiler.SyntaxTree.type_declaration(); TypeDecls.types_decl.Add(TypeDecl); LocksName = visitor.context.get_free_name("$locks_container{0}"); TypeDecl.type_name = new PascalABCCompiler.SyntaxTree.ident(LocksName); SyntaxTree.class_definition ClassDef = new PascalABCCompiler.SyntaxTree.class_definition(); TypeDecl.type_def = ClassDef; SyntaxTree.class_body ClassBody = new PascalABCCompiler.SyntaxTree.class_body(); ClassDef.body = ClassBody; SyntaxTree.class_members ClassMember = new PascalABCCompiler.SyntaxTree.class_members(); ClassBody.class_def_blocks.Add(ClassMember); ClassMember.access_mod = new PascalABCCompiler.SyntaxTree.access_modifer_node(PascalABCCompiler.SyntaxTree.access_modifer.public_modifer); List<string> ProcessedNames = new List<string>(); foreach (KeyValuePair<SyntaxTree.compiler_directive, DirectiveInfo> pair in DirInfosTable) { if (pair.Value.Kind == DirectiveKind.Critical) { string LockName = "$default"; if (pair.Value.Name.Length != 0) LockName = pair.Value.Name; if (ProcessedNames.Contains(LockName)) continue; ProcessedNames.Add(LockName); SyntaxTree.var_def_statement vds = new PascalABCCompiler.SyntaxTree.var_def_statement(); SyntaxTree.ident_list idl = new PascalABCCompiler.SyntaxTree.ident_list(); vds.vars = idl; idl.Add(new SyntaxTree.ident(LockName)); SyntaxTree.named_type_reference ntr = new PascalABCCompiler.SyntaxTree.named_type_reference(); vds.vars_type = ntr; ntr.Add(new SyntaxTree.ident("object")); SyntaxTree.new_expr ne = new PascalABCCompiler.SyntaxTree.new_expr(); vds.inital_value = ne; ne.type = ntr; vds.var_attr = PascalABCCompiler.SyntaxTree.definition_attribute.Static; ClassMember.members.Add(vds); } } //сохраняем контекст ContextInfo contextInfo = new ContextInfo(visitor); try { visitor.visit(TypeDecls); LocksInitialized = true; } finally { //восстанавливаем контекст contextInfo.RestoreContext(visitor); } }
public string BuildCode(string existing_text, string generated_text, EventDescription event_description) { if (FirstCodeGeneration) { FirstCodeGeneration = false; string form_name = (Designer.Host.RootComponent as Control).Name; if (form_name == "Form1") { } string beg = string.Format(string_consts.begin_unit, Path.GetFileNameWithoutExtension(_file_name), (Designer.Host.RootComponent as Control).Name); existing_text = beg + generated_text + string_consts.end_unit; TextEditor.Text = existing_text; if (event_description == null) { return(existing_text); //beg + generated_text + SampleDesignerHost.string_consts.end_unit; } } //StringBuilder sb = new StringBuilder(existing_text); //TextEditor.Text); string[] sep = new string[1] { string_consts.nr }; string[] lines = existing_text.Split(sep, StringSplitOptions.None); int count = lines.Length; int s_num = 0; string trimed; //int end_region_num; while (s_num < count) { trimed = lines[s_num].TrimStart(' ', '\t'); if (trimed.StartsWith(string_consts.begin_designer_region, StringComparison.InvariantCultureIgnoreCase)) { break; } s_num++; } if (s_num == count) { MessageDesignerCodeGenerationFailed(); return(null); } int e_num = s_num + 1; while (e_num < count) { trimed = lines[e_num].TrimStart(' ', '\t'); if (trimed.StartsWith(string_consts.end_designer_region, StringComparison.InvariantCultureIgnoreCase)) { break; } e_num++; } if (e_num == count) { MessageDesignerCodeGenerationFailed(); return(null); } List <PascalABCCompiler.Errors.Error> Errors = new List <PascalABCCompiler.Errors.Error>(); List <PascalABCCompiler.Errors.CompilerWarning> Warnings = new List <PascalABCCompiler.Errors.CompilerWarning>(); //PascalABCCompiler.SyntaxTree.syntax_tree_node sn = // MainForm.VisualEnvironmentCompiler.Compiler.ParsersController.Compile( // FileName, TextEditor.Text, null, Errors, PascalABCCompiler.Parsers.ParseMode.Normal); PascalABCCompiler.SyntaxTree.compilation_unit sn = CodeCompletion.CodeCompletionController.ParsersController.GetCompilationUnit( VisualPABCSingleton.MainForm._currentCodeFileDocument.FileName, existing_text, //VisualPascalABC.Form1.Form1_object._currentCodeFileDocument.TextEditor.Text, Errors, Warnings); PascalABCCompiler.SyntaxTree.unit_module um = sn as PascalABCCompiler.SyntaxTree.unit_module; bool good_syntax = um != null; PascalABCCompiler.SyntaxTree.type_declaration form_decl = null; if (good_syntax) { good_syntax = um.implementation_part != null && um.interface_part != null && um.interface_part.interface_definitions != null && um.interface_part.interface_definitions.defs != null && um.interface_part.interface_definitions.defs.Count > 0; } if (good_syntax) { foreach (PascalABCCompiler.SyntaxTree.declaration decl in um.interface_part.interface_definitions.defs) { PascalABCCompiler.SyntaxTree.type_declarations tdecls = decl as PascalABCCompiler.SyntaxTree.type_declarations; if (tdecls != null) { foreach (PascalABCCompiler.SyntaxTree.type_declaration tdecl in tdecls.types_decl) { if (tdecl.source_context.begin_position.line_num - 1 < s_num && tdecl.source_context.end_position.line_num - 1 > e_num) { form_decl = tdecl; } } } } } PascalABCCompiler.SyntaxTree.class_definition form_def = null; if (form_decl != null) { form_def = form_decl.type_def as PascalABCCompiler.SyntaxTree.class_definition; } if (form_decl == null || form_def == null || form_def.body == null) { MessageBox.Show(PascalABCCompiler.StringResources.Get("VP_MF_CODE_GENERATION_UNSUCCEFULL"), PascalABCCompiler.StringResources.Get("VP_MF_FORM_DESIGNER"), MessageBoxButtons.OK, MessageBoxIcon.Warning); return(null); } else { string old_form_name = form_decl.type_name.name; string new_form_name = (Designer.Host.RootComponent as Control).Name; bool implementation_not_null = um.implementation_part.implementation_definitions != null && um.implementation_part.implementation_definitions.defs != null && um.implementation_part.implementation_definitions.defs.Count > 0; if (new_form_name != old_form_name) { ReplaceName(form_decl.type_name, new_form_name, lines); if (implementation_not_null) { foreach (PascalABCCompiler.SyntaxTree.declaration decl in um.implementation_part.implementation_definitions.defs) { PascalABCCompiler.SyntaxTree.procedure_definition pd = decl as PascalABCCompiler.SyntaxTree.procedure_definition; if (pd != null) { if (pd.proc_header.name.class_name != null && string.Compare(pd.proc_header.name.class_name.name, old_form_name, true) == 0) { ReplaceName(pd.proc_header.name.class_name, new_form_name, lines); } } } } } if (event_description != null) { MethodInfo mi = event_description.e.EventType.GetMethod( PascalABCCompiler.TreeConverter.compiler_string_consts.invoke_method_name); ParameterInfo[] pinfos = mi.GetParameters(); bool handler_found = false; event_description.editor = TextEditor; System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(generated_text, string_consts.nr); //строка, на которой последнее описание из секции реализаций PascalABCCompiler.SyntaxTree.file_position last_defs_pos = null; if (implementation_not_null) { last_defs_pos = um.implementation_part.implementation_definitions.defs[ um.implementation_part.implementation_definitions.defs.Count - 1 ].source_context.end_position; //Ищем описание обработчика foreach (PascalABCCompiler.SyntaxTree.declaration decl in um.implementation_part.implementation_definitions.defs) { PascalABCCompiler.SyntaxTree.procedure_definition pd = decl as PascalABCCompiler.SyntaxTree.procedure_definition; if (pd == null) { continue; } if (pd.proc_header.name == null || pd.proc_header.name.class_name == null || //roman// String.Compare(pd.proc_header.name.class_name.name, new_form_name, true) != 0 || String.Compare(pd.proc_header.name.meth_name.name, event_description.EventName, true) != 0) { continue; } List <PascalABCCompiler.SyntaxTree.typed_parameters> syn_pars = pd.proc_header.parameters.params_list; bool should_continue = false; int par_count = syn_pars.Count; if (par_count != pinfos.Length) { continue; } for (int i = 0; i < par_count; ++i) { if (syn_pars[i].idents.idents.Count != 1) { should_continue = true; break; } PascalABCCompiler.SyntaxTree.named_type_reference ntr = syn_pars[i].vars_type as PascalABCCompiler.SyntaxTree.named_type_reference; if (ntr == null || syn_pars[i].param_kind != PascalABCCompiler.SyntaxTree.parametr_kind.none) { should_continue = true; break; } string syn_name = BuildName(ntr.names); if (String.Compare(syn_name, pinfos[i].ParameterType.Name) != 0 && String.Compare(syn_name, pinfos[i].ParameterType.FullName) != 0) { should_continue = true; break; } } if (should_continue) { continue; } handler_found = true; event_description.line_num = pd.proc_body.source_context.begin_position.line_num + matches.Count + s_num - e_num + 2; //last_defs_pos.line_num + s_num - e_num + matches.Count + 7; event_description.column_num = pd.proc_body.source_context.begin_position.column_num; } } else { last_defs_pos = um.implementation_part.source_context.end_position; } if (!handler_found) { string new_event = event_description.EventName; if (pinfos.Length != 0) { new_event += "("; new_event += pinfos[0].Name + ": " + pinfos[0].ParameterType.FullName.Replace("System.Windows.Forms.", "").Replace("System.", ""); for (int i = 1; i < pinfos.Length; ++i) { new_event += "; "; new_event += pinfos[i].Name + ": " + pinfos[i].ParameterType.FullName.Replace("System.Windows.Forms.", "").Replace("System.", ""); } new_event += ")"; } new_event += ";"; string new_event_header = new_event; new_event = string_consts.nr + string_consts.nr + "procedure " + new_form_name + "." + new_event; new_event += string_consts.nr + "begin" + string_consts.nr + " " + string_consts.nr + "end;"; event_description.column_num = 3; event_description.line_num = last_defs_pos.line_num + s_num - e_num + matches.Count + 7; lines[last_defs_pos.line_num - 1] = lines[last_defs_pos.line_num - 1].Insert(last_defs_pos.column_num, new_event); //Добавляем заголовок события //int last_form_member_line = form_def.body.class_def_blocks[form_def.body.class_def_blocks.Count - 1].source_context.end_position.line_num - 1; lines[s_num] = string_consts.event_handler_header_trim + "procedure " + new_event_header + string_consts.nr + lines[s_num]; } } } //generated_text = SampleDesignerHost.string_consts.tab + "private" + // SampleDesignerHost.string_consts.nr + generated_text; string s1 = string.Join(string_consts.nr, lines, 0, s_num + 1); string s2 = string.Join(string_consts.nr, lines, e_num, lines.Length - e_num); return(s1 + string_consts.nr + string_consts.tab + "internal" + string_consts.nr + string_consts.tab2 + generated_text + s2); }
private static SyntaxTree.type_declarations CreateClass(string ClassName,out SyntaxTree.class_members ClassMember, VarInfoContainer Vars) { //генерация класса SyntaxTree.type_declarations TypeDecls = new PascalABCCompiler.SyntaxTree.type_declarations(); SyntaxTree.type_declaration TypeDecl = new PascalABCCompiler.SyntaxTree.type_declaration(); TypeDecls.types_decl.Add(TypeDecl); TypeDecl.type_name = new PascalABCCompiler.SyntaxTree.ident(ClassName); SyntaxTree.class_definition ClassDef = new PascalABCCompiler.SyntaxTree.class_definition(); TypeDecl.type_def = ClassDef; SyntaxTree.class_body ClassBody = new PascalABCCompiler.SyntaxTree.class_body(); ClassDef.body = ClassBody; ClassMember = new PascalABCCompiler.SyntaxTree.class_members(); ClassBody.class_def_blocks.Add(ClassMember); ClassMember.access_mod = new PascalABCCompiler.SyntaxTree.access_modifer_node(PascalABCCompiler.SyntaxTree.access_modifer.public_modifer); // генерация полей класса // shared переменные for (int i = 0; i < Vars.SharedVariables.Count; ++i) ClassMember.members.Add(CreateClassMember(Vars.SharedVariables[i], "")); // переменные редукции - с долларовым префиксом for (int i = 0; i < Vars.ReductionVariables.Count; ++i) ClassMember.members.Add(CreateClassMember(Vars.ReductionVariables[i], "$")); return TypeDecls; }