public static ISExpression[] Parse(string input) { AntlrInputStream inputStream = new AntlrInputStream(input); SchemeLexer lexer = new SchemeLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(lexer); SchemeParser parser = new SchemeParser(commonTokenStream); var context = parser.body(); var visitor = new SchemeVisitor(); var expr = visitor.Visit(context); ISExpression[] arr; if (expr is ISExpression) { arr = new ISExpression[] { (ISExpression)expr } } ; else if (expr is ISExpression[]) { arr = (ISExpression[])expr; } else { throw new InvalidOperationException("parser error, unknown type: " + expr.GetType().Name); } return(arr); }
/// <summary> /// Loads a scheme template if possible. /// </summary> private void LoadSchemeTemplate(bool showMsg) { // display empty data changing = true; cbTitleComponent.DataSource = null; bsBindings.DataSource = null; if (string.IsNullOrEmpty(interfaceDir)) { if (showMsg) { ScadaUiUtils.ShowWarning(AppPhrases.UnableLoadTemplate); } } else { string templateFileName = Path.Combine(interfaceDir, txtTemplateFileName.Text); if (File.Exists(templateFileName)) { if (SchemeParser.Parse(templateFileName, out List <ComponentItem> components, out List <ComponentBindingItem> newComponentBindings, out string errMsg)) { // merge bindings foreach (ComponentBindingItem bindingItem in newComponentBindings) { if (templateBindings.ComponentBindings.TryGetValue(bindingItem.CompID, out ComponentBinding binding)) { bindingItem.InCnlNum = binding.InCnlNum; bindingItem.CtrlCnlNum = binding.CtrlCnlNum; } } templateBindings.ComponentBindings.Clear(); newComponentBindings.ForEach(x => { templateBindings.ComponentBindings[x.CompID] = x; }); // fill the component combo box components.Sort(); cbTitleComponent.ValueMember = "ID"; cbTitleComponent.DisplayMember = "DisplayName"; cbTitleComponent.DataSource = components; cbTitleComponent.SelectedValue = templateBindings.TitleCompID; // display bindings bsBindings.DataSource = templateBindings.ComponentBindings.Values; if (showMsg) { ScadaUiUtils.ShowInfo(AppPhrases.TemplateLoaded); } } else { bsBindings.DataSource = null; ScadaUiUtils.ShowError(errMsg); } } else if (showMsg) { ScadaUiUtils.ShowWarning(string.Format(AppPhrases.TemplateNotFound, templateFileName)); } } changing = false; }