/// <summary>On the fly additions like operation1|operation2</summary> internal void AddItem(String pdiname, Int64 pdivalue) { PythonDataItem[] newitems = new PythonDataItem[this._pythonitems.Length + 1]; Array.Copy(this._pythonitems, newitems, this._pythonitems.Length); newitems[this._pythonitems.Length] = new PythonDataItem(this._pythonitems.Length); newitems[this._pythonitems.Length].Name = pdiname; newitems[this._pythonitems.Length].Value = pdivalue; this._pythonitems = newitems; }
/// <summary>Reads lines, for Python it's just looking for a = b, recording the source but not /// attempting to resolve the values at this stage if they're not literals.</summary> internal override void Read() { if (!this.SourceFile.Exists) { throw new FileNotFoundException("File not found: " + this.SourceFile.FullName); } List <PythonDataItem> tempitems = new List <PythonDataItem>(); using (TextReader tr = new StreamReader(this.SourceFile.FullName)) { PythonDataItem newitem = ReadPythonItem(tr, tempitems.Count); if (newitem != null) { do { tempitems.Add(newitem); newitem = ReadPythonItem(tr, tempitems.Count - 1); }while (newitem != null); } tr.Close(); } this._pythonitems = tempitems.ToArray(); PostResolve(); }
/// <summary> Specific PythonDataItem return method to avoid boxing.</summary> private PythonDataItem ReadPythonItem(TextReader reader, int CurrentIndex) { PythonDataItem pdi = new PythonDataItem(CurrentIndex); String linestr = reader.ReadLine(); Int32 indexequals, indexhash; while (linestr != null) { //Source in PythonDataItem isn't going to be the whole thing just after the = and before any # linestr = linestr.Trim().Replace(",", String.Empty);//remove commas indexequals = linestr.IndexOf('='); indexhash = linestr.IndexOf('#'); //if there is a hash and it's before any equals sigh, if there's no equals sign or if there's a deprecated if (linestr.IndexOf('[') > -1 && (indexhash == -1 || indexhash > linestr.IndexOf('['))) { //start of an array like lhs_operations etc //keep going until we hit a ] - if it's a one liner then it will just go to the next readline while (linestr != null && linestr.IndexOf(']') == -1) { linestr = reader.ReadLine(); } if (linestr == null) { break; } //EOF else { linestr = reader.ReadLine(); } //have to do it this way in case we're EOF if (linestr == null) { break; } linestr = linestr.Trim(); //prep for next line } else if (linestr.StartsWith("def")) { //start of a function - they don't all have to return but in the 2 we look at they do at this time //keep looping till we find a return (or hit EOF) while (linestr != null && linestr.IndexOf("return") == -1) { linestr = reader.ReadLine().TrimStart();//has to be trimmed so we can use StartsWith } if (linestr == null) { break; } //EOF else { linestr = reader.ReadLine(); } //have to do it this way in case we're EOF if (linestr == null) { break; } linestr = linestr.Trim(); //prep for next line } else if ((indexhash > -1 && indexhash <= indexequals) || indexequals == -1 || linestr.IndexOf("deprecated") > -1) {//skip line linestr = reader.ReadLine(); } else { if (indexhash > -1) {//chop off end #comments for now but store them in InlineComment for possible future use pdi.InlineComment = linestr.Substring(indexhash + 1).Trim(); linestr = linestr.Remove(indexhash); } pdi.Name = linestr.Remove(indexequals).Trim(); pdi.Source = (indexequals + 1 < linestr.Length ? linestr.Substring(indexequals + 1).Trim() : String.Empty); //short attempt to resolve, if it's not a literal or hex literal then it will not happen here Int64 parsedval = 0; //because we can't pass pdi.Value to Int64.TryParse if (Int64TryParse(pdi.Source, out parsedval)) { pdi.Value = parsedval; } break; } } if (linestr == null) { return(null); } //means we hit EOF return(pdi); }