public string Read(JObject j) // empty string if ok { string errlist = ""; string progname = (string)j["Name"]; JArray steps = (JArray)j["Steps"]; if (steps != null) { Name = progname; programsteps = new List <ActionBase>(); int lineno = 1; foreach (JObject js in steps) { string stepname = (string)js["StepName"]; string stepUC = (string)js["StepUC"]; int stepLU = js["StepLevelUp"].Int(0); // optional int whitespace = js["StepWhitespace"].Int(0); // was not in earlier version, optional string comment = js["StepComment"].Str(""); // was not in earlier version, optional ActionBase cmd = ActionBase.CreateAction(stepname, stepUC, comment, stepLU, whitespace); if (cmd != null && cmd.VerifyActionCorrect() == null) // throw away ones with bad names { cmd.LineNumber = lineno++; lineno += cmd.Whitespace; programsteps.Add(cmd); } else { errlist += "Failed to create " + progname + " step: " + stepname + ":" + stepUC + Environment.NewLine; } } } return(errlist); }
public string Read(System.IO.TextReader sr, ref int lineno) // read from stream { string err = ""; programsteps = new List <ActionBase>(); Name = ""; List <int> indents = new List <int>(); List <int> level = new List <int>(); int indentpos = -1; int structlevel = 0; string completeline; int initiallineno = lineno; while ((completeline = sr.ReadLine()) != null) { lineno++; completeline = completeline.Replace("\t", " "); // detab, to spaces, tabs are worth 4. BaseUtils.StringParser p = new BaseUtils.StringParser(completeline); if (!p.IsEOL) { int curindent = p.Position; string cmd = ""; if (p.IsStringMoveOn("//")) // special, this is allowed to butt against text and still work { cmd = "//"; } else { cmd = p.NextWord(); } if (cmd.Equals("Else", StringComparison.InvariantCultureIgnoreCase)) { //System.Diagnostics.Debug.WriteLine("Else " + cmd + " " + p.LineLeft); if (p.IsStringMoveOn("If", StringComparison.InvariantCultureIgnoreCase)) // if Else followed by IF { cmd = "Else If"; } } string line = p.LineLeft; // and the rest of the line.. int commentpos = line.LastIndexOf("//"); string comment = ""; if (cmd != "//" && commentpos >= 0 && !line.InQuotes(commentpos)) // if not // command, and we have one.. { comment = line.Substring(commentpos + 2).Trim(); line = line.Substring(0, commentpos).TrimEnd(); //System.Diagnostics.Debug.WriteLine("Line <" + line + "> <" + comment + ">"); } if (cmd.Equals("PROGRAM", StringComparison.InvariantCultureIgnoreCase)) { Name = line; } else if (cmd.Equals("END", StringComparison.InvariantCultureIgnoreCase) && line.Equals("PROGRAM", StringComparison.InvariantCultureIgnoreCase)) { break; } else { ActionBase a = ActionBase.CreateAction(cmd, line, comment); string vmsg; if (a == null) { err += lineno + " " + Name + " Unrecognised command " + cmd + Environment.NewLine; } else if ((vmsg = a.VerifyActionCorrect()) != null) { err += lineno + " " + Name + ":" + vmsg + Environment.NewLine + " " + completeline.Trim() + Environment.NewLine; } else { //System.Diagnostics.Debug.WriteLine(indentpos + ":" + structlevel + ": Cmd " + cmd + " : " + line); if (indentpos == -1) { indentpos = curindent; } else if (curindent > indentpos) // more indented, up one structure { structlevel++; indentpos = curindent; } else if (curindent < indentpos) // deindented { int tolevel = -1; for (int i = indents.Count - 1; i >= 0; i--) // search up and find the entry with the indent.. { if (indents[i] <= curindent) { tolevel = i; break; } } if (tolevel >= 0) // if found, we have a statement to hook to.. { // while in DO loop, or Else/Elseif in IF if ((a.Type == ActionBase.ActionType.While && programsteps[tolevel].Type == ActionBase.ActionType.Do) || ((a.Type == ActionBase.ActionType.Else || a.Type == ActionBase.ActionType.ElseIf) && programsteps[tolevel].Type == ActionBase.ActionType.If)) { int reallevel = level[tolevel] + 1; // else, while are dedented, but they really are on level+1 a.LevelUp = structlevel - reallevel; structlevel = reallevel; indentpos = indents[tolevel] + 4; // and our indent should continue 4 in, so we don't match against this when we do indent } else { a.LevelUp = structlevel - level[tolevel]; structlevel = level[tolevel]; // if found, we are at that.. except.. indentpos = indents[tolevel]; // and back to this level } } } a.LineNumber = lineno; //System.Diagnostics.Debug.WriteLine(" >>>> " + indentpos + ":" + structlevel); indents.Add(indentpos); level.Add(structlevel); programsteps.Add(a); } } } else { if (programsteps.Count > 0) { programsteps[programsteps.Count - 1].Whitespace = 1; } } } if (programsteps.Count > 0) { programsteps[programsteps.Count - 1].Whitespace = 0; // last cannot have whitespace.. } progclass = Classify(); return(err); }