public static void CodeParser_GetNextArgument_Test(string code, List <Tuple <string, string> > testcases) { Tuple <string, string> tuple; string next, remainder = code; foreach (Tuple <string, string> testcase in testcases) { tuple = CodeParser.GetNextArgument(remainder); next = tuple.Item1; remainder = tuple.Item2; Console.WriteLine(next); if (remainder != null) { Console.WriteLine(remainder); } else { Console.WriteLine("null"); } Assert.IsTrue(next.Equals(testcase.Item1, StringComparison.Ordinal)); if (remainder != null) { Assert.IsTrue(remainder.Equals(testcase.Item2, StringComparison.Ordinal)); } else { Assert.IsTrue(remainder == null); } } }
public static UICommand ParseUICommand(List <string> rawLines, SectionAddress addr, ref int idx) { UIType type = UIType.None; string rawLine = rawLines[idx].Trim(); // Check if rawCode is Empty if (rawLine.Equals(string.Empty)) { return(new UICommand(rawLine, addr, string.Empty)); } // Comment Format : starts with '//' or '#', ';' if (rawLine.StartsWith("//") || rawLine.StartsWith("#") || rawLine.StartsWith(";")) { return(new UICommand(rawLine, addr, string.Empty)); } // Find key of interface control string key = string.Empty; string rawValue = string.Empty; int equalIdx = rawLine.IndexOf('='); if (equalIdx != -1) { key = rawLine.Substring(0, equalIdx); rawValue = rawLine.Substring(equalIdx + 1); } else { throw new InvalidCommandException($"Interface control [{rawValue}] does not have name", rawLine); } // Parse Arguments List <string> args = new List <string>(); try { string remainder = rawValue; while (remainder != null) { Tuple <string, string> tuple = CodeParser.GetNextArgument(remainder); args.Add(tuple.Item1); remainder = tuple.Item2; } } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } // Check doublequote's occurence - must be 2n if (StringHelper.CountOccurrences(rawValue, "\"") % 2 == 1) { throw new InvalidCommandException($"Interface control [{rawValue}]'s doublequotes mismatch", rawLine); } // Check if last operand is \ - MultiLine check - only if one or more operands exists if (0 < args.Count) { while (args.Last().Equals(@"\", StringComparison.OrdinalIgnoreCase)) { // Split next line and append to List<string> operands if (rawLines.Count <= idx) // Section ended with \, invalid grammar! { throw new InvalidCommandException($@"Last interface control [{rawValue}] cannot end with '\' ", rawLine); } idx++; args.AddRange(rawLines[idx].Trim().Split(',')); } } // UICommand should have at least 7 operands // Text, Visibility, Type, X, Y, width, height, [Optional] if (args.Count < 7) { throw new InvalidCommandException($"Interface control [{rawValue}] must have at least 7 arguments", rawLine); } // Parse opcode try { type = UIParser.ParseControlType(args[2]); } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } // Remove UIControlType from operands // Leftover : Text, Visibility, X, Y, width, height, [Optional] args.RemoveAt(2); // Forge UICommand string text = StringEscaper.Unescape(args[0]); bool visibility = string.Equals(args[1], "1", StringComparison.Ordinal); NumberHelper.ParseInt32(args[2], out int x); NumberHelper.ParseInt32(args[3], out int y); NumberHelper.ParseInt32(args[4], out int width); NumberHelper.ParseInt32(args[5], out int height); Rect rect = new Rect(x, y, width, height); UIInfo info; try { info = ParseUICommandInfo(type, args); } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } return(new UICommand(rawLine, addr, key, text, visibility, type, rect, info)); }
public static UIControl ParseUIControl(IList <string> rawLines, ScriptSection section, ref int idx) { // UICommand's line number in physical file int lineIdx = section.LineIdx + 1 + idx; // Get rawLine string rawLine = rawLines[idx].Trim(); // Check if rawLine is empty if (rawLine.Length == 0) { return(null); } // Line Comment Identifier : '//', '#', ';' if (rawLine.StartsWith("//", StringComparison.Ordinal) || rawLine[0] == '#' || rawLine[0] == ';') { return(null); } // Find key of interface control string key; string rawValue = string.Empty; int equalIdx = rawLine.IndexOf('='); if (equalIdx != -1 && equalIdx != 0) { key = rawLine.Substring(0, equalIdx); rawValue = rawLine.Substring(equalIdx + 1); } else { throw new InvalidCommandException($"Interface control [{rawValue}] must have a key", rawLine); } // Parse Arguments List <string> args = new List <string>(); try { string remainder = rawValue; while (remainder != null) { string next; (next, remainder) = CodeParser.GetNextArgument(remainder); args.Add(next); } } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } // Check double-quote's occurence - must be 2n if (StringHelper.CountSubStr(rawValue, "\"") % 2 == 1) { throw new InvalidCommandException("Double-quote's number should be even", rawLine); } // Check if last operand is \ - MultiLine check - only if one or more operands exists if (0 < args.Count) { while (args.Last().Equals(@"\", StringComparison.OrdinalIgnoreCase)) { // Split next line and append to List<string> operands if (rawLines.Count <= idx) // Section ended with \, invalid grammar! { throw new InvalidCommandException(@"Last interface control cannot end with '\'", rawLine); } idx++; args.AddRange(rawLines[idx].Trim().Split(',')); } } // UIControl should have at least 7 operands // Text, Visibility, Type, X, Y, width, height, [Optional] if (args.Count < 7) { throw new InvalidCommandException($"Interface control [{rawValue}] must have at least 7 arguments", rawLine); } // Parse UIControlType UIControlType type; try { type = UIParser.ParseControlTypeVal(args[2]); } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } // Remove UIControlType from operands // Leftover : Text, Visibility, X, Y, width, height, [Optional] args.RemoveAt(2); // Forge UIControl string text = args[0]; string visibilityStr = args[1]; bool visibility; if (visibilityStr.Equals("1", StringComparison.Ordinal) || visibilityStr.Equals("True", StringComparison.OrdinalIgnoreCase)) { visibility = true; } else if (visibilityStr.Equals("0", StringComparison.Ordinal) || visibilityStr.Equals("False", StringComparison.OrdinalIgnoreCase)) { visibility = false; } else { throw new InvalidCommandException($"Invalid value in [{visibilityStr}]", rawLine); } bool intParse = true; intParse &= NumberHelper.ParseInt32(args[2], out int x); intParse &= NumberHelper.ParseInt32(args[3], out int y); intParse &= NumberHelper.ParseInt32(args[4], out int width); intParse &= NumberHelper.ParseInt32(args[5], out int height); if (!intParse) { throw new InvalidCommandException($"Invalid integers in [{rawValue}]", rawLine); } UIInfo info; try { info = ParseUIControlInfo(type, args); } catch (InvalidCommandException e) { throw new InvalidCommandException(e.Message, rawLine); } return(new UIControl(rawLine, section, key, text, visibility, type, x, y, width, height, info, lineIdx)); }
public (List <LogInfo>, Result) CheckScript() { List <LogInfo> logs = new List <LogInfo>(); // Codes if (_sc.Sections.ContainsKey(ScriptSection.Names.Process)) { logs.AddRange(CheckCodeSection(_sc.Sections[ScriptSection.Names.Process])); } // UICtrls - [Interface] List <string> processedInterfaces = new List <string>(); if (_sc.Sections.ContainsKey(ScriptSection.Names.Interface)) { processedInterfaces.Add(ScriptSection.Names.Interface); logs.AddRange(CheckInterfaceSection(_sc.Sections[ScriptSection.Names.Interface])); } // UICtrls - Interface= if (_sc.MainInfo.ContainsKey(ScriptSection.Names.Interface)) { string ifaceSection = _sc.MainInfo[ScriptSection.Names.Interface]; processedInterfaces.Add(ifaceSection); if (_sc.Sections.ContainsKey(ifaceSection)) { logs.AddRange(CheckInterfaceSection(_sc.Sections[ifaceSection])); } else { logs.Add(new LogInfo(LogState.Error, $"Section [{ifaceSection}] does not exist (Interface={ifaceSection})")); } } // UICtrls - InterfaceList= (Stage 1) if (_sc.MainInfo.ContainsKey(Script.Const.InterfaceList)) { // Check if InterfaceList contains proper sections string interfaceList = _sc.MainInfo[Script.Const.InterfaceList]; try { string remainder = interfaceList; while (remainder != null) { string next; (next, remainder) = CodeParser.GetNextArgument(remainder); // Does section exist? if (!_sc.Sections.ContainsKey(next)) { logs.Add(new LogInfo(LogState.Error, $"Section [{next}] does not exist (InterfaceList={interfaceList})")); } } } catch (InvalidCommandException e) { logs.Add(new LogInfo(LogState.Error, e)); } } // UICtrls - InterfaceList= (Stage 2) // Do not enable deepScan, SyntaxChecker have its own implementation of `IniWrite` pattern scanning foreach (string ifaceSection in _sc.GetInterfaceSectionNames(false) .Where(x => !processedInterfaces.Contains(x, StringComparer.OrdinalIgnoreCase))) { if (_sc.Sections.ContainsKey(ifaceSection)) { logs.AddRange(CheckInterfaceSection(_sc.Sections[ifaceSection])); } } // Result Result result = Result.Clean; if (logs.Any(x => x.State == LogState.Error || x.State == LogState.CriticalError)) { result = Result.Error; } else if (logs.Any(x => x.State == LogState.Warning)) { result = Result.Warning; } return(logs, result); }