From 67a68a4441ef20de905ef9756629cd79a3ad8584 Mon Sep 17 00:00:00 2001 From: Morten Daniel Fornes Date: Thu, 5 Oct 2023 21:44:18 +0700 Subject: [PATCH 1/2] compiler: support lowerCamelCase in UxlParser * Support lowerCamelCase in UXL files * Robustness in BuildEnvironment.cs * Update disasm tool --- src/common/Extensions.cs | 3 + src/compiler/core/BuildEnvironment.cs | 12 +- src/compiler/core/Syntax/UxlProcessor.cs | 32 +-- src/compiler/frontend/UxlParser.cs | 272 +++++++++++------------ src/disasm/core/ILView/ILItemBuilder.cs | 6 +- 5 files changed, 165 insertions(+), 160 deletions(-) diff --git a/src/common/Extensions.cs b/src/common/Extensions.cs index 1ac7dc64e..274788b3a 100644 --- a/src/common/Extensions.cs +++ b/src/common/Extensions.cs @@ -648,6 +648,7 @@ public static string ToLowerCamelCase(this string str) switch (str) { case "cMake": + case "cPlusPlus": case "dotNet": case "iOS": case "iPad": @@ -658,6 +659,8 @@ public static string ToLowerCamelCase(this string str) case "typeOf": case "unoDoc": return str.ToLowerInvariant(); + case "pListDefaults": + return "plistDefaults"; case "typeOfFunction": return "typeofFunction"; case "typeOfType": diff --git a/src/compiler/core/BuildEnvironment.cs b/src/compiler/core/BuildEnvironment.cs index 811a3548c..a034002e8 100644 --- a/src/compiler/core/BuildEnvironment.cs +++ b/src/compiler/core/BuildEnvironment.cs @@ -38,8 +38,8 @@ public class BuildEnvironment : LogObject, IEnvironment public string BundleDirectory { get; } public string OutputDirectory { get; } - readonly Dictionary _cachedTests = new Dictionary(); - readonly Dictionary _cachedProperties = new Dictionary(); + readonly Dictionary _cachedTests = new(); + readonly Dictionary _cachedProperties = new(); internal BuildEnvironment( Backend backend, @@ -295,7 +295,7 @@ public bool GetBool(string propertyName, bool defaultValue = false) public string GetOutputPath(string key) { - return Path.Combine(OutputDirectory, GetString(key).UnixToNative()); + return Path.Combine(OutputDirectory, (GetString(key) ?? "null").UnixToNative()); } public string Combine(string path0, string path1, string path2) @@ -415,9 +415,11 @@ List GetSet(IEnumerable values) public HashSet GetWords(string key) { var result = new HashSet(); + var words = GetString(key); - foreach (var word in GetString(key).Split(' ')) - result.Add(word.Trim()); + if (!string.IsNullOrEmpty(words)) + foreach (var word in words.Split(' ')) + result.Add(word.Trim()); return result; } diff --git a/src/compiler/core/Syntax/UxlProcessor.cs b/src/compiler/core/Syntax/UxlProcessor.cs index 6ddc6ede2..035fed397 100644 --- a/src/compiler/core/Syntax/UxlProcessor.cs +++ b/src/compiler/core/Syntax/UxlProcessor.cs @@ -116,12 +116,12 @@ public void CompileDocuments() if (def == null) { - Log.Error(e.Source, ErrorCode.E0000, "Unknown 'Type' attribute on element (" + e.Type + ")"); + Log.Error(e.Source, ErrorCode.E0000, "Unknown 'type' attribute on element (" + e.Type + ")"); continue; } if (def.Contains(e.Key.String)) - Log.Error(e.Key.Source, ErrorCode.E0000, "A element for " + e.Key.String.Quote() + " already exists"); + Log.Error(e.Key.Source, ErrorCode.E0000, "A element for " + e.Key.String.Quote() + " already exists"); else def.Add(e.Key.String); } @@ -129,7 +129,7 @@ public void CompileDocuments() foreach (var e in doc.Deprecations) { if (_deprecated.ContainsKey(e.OldName)) - Log.Error(e.Source, ErrorCode.E0000, "A element for " + e.OldName.Quote() + " already exists"); + Log.Error(e.Source, ErrorCode.E0000, "A element for " + e.OldName.Quote() + " already exists"); else _deprecated.Add(e.OldName, e.NewName); } @@ -151,9 +151,9 @@ public void CompileDocuments() continue; if (e.Type == UxlElementType.Set) - Apply("Set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, usings), _root.Properties); + Apply("set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, usings), _root.Properties); else - CompileExtensionElement(_root, "Extensions", key, e, usings); + CompileExtensionElement(_root, "extensions", key, e, usings); } CompileFiles(_root, doc); @@ -206,7 +206,7 @@ void CompileRequirements(ExtensionEntity ext) { ExtensionEntity template; if (!_root.Templates.TryGetValue(e.String, out template)) - Log.Error(e.Source, ErrorCode.E0000, "Template " + e.String.Quote() + " is not defined (on this backend)"); + Log.Error(e.Source, ErrorCode.E0000, "template " + e.String.Quote() + " is not defined (on this backend)"); else ext.RequiredTemplates.Add(template); } @@ -219,14 +219,14 @@ void CompileTemplate(UxlTemplate uxl, Namescope[] usings) return; var template = new ExtensionEntity(uxl.Name.Source, uxl.Name.String, uxl.Disambiguation); - Apply("Template", uxl.Name.String, template, _root.Templates); + Apply("template", uxl.Name.String, template, _root.Templates); foreach (var e in uxl.Elements) { if (!TryGetKey(e, out string key) || !Test(e.Condition)) continue; - CompileExtensionElement(template, "Template", key, e, usings); + CompileExtensionElement(template, "template", key, e, usings); } CompileFiles(template, uxl); @@ -262,7 +262,7 @@ void CompileType(UxlType uxl, SourceBundle bundle, Namescope[] usings) var typeScopes = GetTypeScopes(dt, usings); var typeExt = new TypeExtension(uxl.Name.Source, dt, uxl.Disambiguation); - Apply("Type", dt, typeExt, _root.TypeExtensions); + Apply("type", dt, typeExt, _root.TypeExtensions); foreach (var e in uxl.Methods) CompileMethod(e, dt, typeExt, typeScopes); @@ -273,9 +273,9 @@ void CompileType(UxlType uxl, SourceBundle bundle, Namescope[] usings) continue; if (e.Type == UxlElementType.Set && _root.TypePropertyDefinitions.Contains(key)) - Apply("Set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, typeScopes), typeExt.Properties); + Apply("set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, typeScopes), typeExt.Properties); else - CompileTypeElement(typeExt, "Type", key, e, typeScopes); + CompileTypeElement(typeExt, "type", key, e, typeScopes); } CompileFiles(typeExt, uxl); @@ -332,7 +332,7 @@ void CompileMethod(UxlMethod uxl, DataType dt, TypeExtension typeExt, Namescope[ return; var methodExt = new FunctionExtension(uxl.Signature.Source, method, uxl.Disambiguation, methodScopes); - Apply("Method", method, methodExt, typeExt.MethodExtensions); + Apply("method", method, methodExt, typeExt.MethodExtensions); foreach (var impl in uxl.Implementations) { @@ -352,9 +352,9 @@ void CompileMethod(UxlMethod uxl, DataType dt, TypeExtension typeExt, Namescope[ continue; if (e.Type == UxlElementType.Set && _root.MethodPropertyDefinitions.Contains(key)) - Apply("Set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, methodScopes), methodExt.Properties); + Apply("set", key, new Element(e.Value.Source, e.Value.String, e.Disambiguation, methodScopes), methodExt.Properties); else - CompileTypeElement(methodExt, "Method", key, e, methodScopes); + CompileTypeElement(methodExt, "method", key, e, methodScopes); } CompileFiles(typeExt, uxl); @@ -396,14 +396,14 @@ void CompileExtensionElement(ExtensionEntity ext, string type, string key, UxlEl if (elm.Type == UxlElementType.Require && _root.ElementDefinitions.Contains(key)) ext.Requirements.Add(key, new Element(elm.Value.Source, elm.Value.String, elm.Disambiguation, usings)); else - Log.Error(elm.Source, ErrorCode.E0000, "<" + elm.Type + " Key=\"" + key + "\"> is not valid in <" + type + "> (on this backend)"); + Log.Error(elm.Source, ErrorCode.E0000, "<" + elm.Type + " key=\"" + key + "\"> is not valid in <" + type + "> (on this backend)"); } bool TryGetKey(UxlElement e, out string key) { if (string.IsNullOrEmpty(e.Key.String)) { - Log.Error(e.Key.Source, ErrorCode.E0000, "<" + e.Type + "> must provide a non-empty 'Key' attribute"); + Log.Error(e.Key.Source, ErrorCode.E0000, "<" + e.Type + "> must provide a non-empty 'key' attribute"); key = null; return false; } diff --git a/src/compiler/frontend/UxlParser.cs b/src/compiler/frontend/UxlParser.cs index 0774b4db1..21828d119 100644 --- a/src/compiler/frontend/UxlParser.cs +++ b/src/compiler/frontend/UxlParser.cs @@ -35,19 +35,19 @@ public static bool Parse(Log log, SourceBundle bundle, string filename, List result) { - switch (FindRootElement()) + switch (FindRootElement().ToLowerCamelCase()) { - case "Extensions": + case "extensions": result.Add(ParseDocument()); return Log.ErrorCount - StartErrorCount == 0; - case "Package": + case "package": ParseAttributes(x => false); ParseElements(name => { - switch (name) + switch (name.ToLowerCamelCase()) { - case "Extensions": + case "extensions": result.Add(ParseDocument()); return true; default: @@ -58,7 +58,7 @@ bool Parse(List result) return Log.ErrorCount - StartErrorCount == 0; default: - Log.Error(GetSource(), ErrorCode.E0000, "Expected element"); + Log.Error(GetSource(), ErrorCode.E0000, "Expected element"); return false; } } @@ -71,12 +71,12 @@ UxlDocument ParseDocument() ParseAttributes( name => { - switch (name) + switch (name.ToLowerCamelCase()) { - case "Backend": + case "backend": backend = GetValue(); return true; - case "Condition": + case "condition": cond = GetValue(); return true; default: @@ -87,8 +87,8 @@ UxlDocument ParseDocument() UxlBackendType backendType = 0; if (backend == null) - Log.Error(GetSource(), ErrorCode.E0000, "Expected 'Backend' attribute on "); - else if (!Enum.TryParse(backend.Value.String, out backendType) || backendType == UxlBackendType.Unknown) + Log.Error(GetSource(), ErrorCode.E0000, "Expected 'backend' attribute on "); + else if (!Enum.TryParse(backend.Value.String, true, out backendType) || backendType == UxlBackendType.Unknown) Log.Error(backend.Value.Source, ErrorCode.E0000, "Unknown backend " + backend.Value.String.Quote()); var result = new UxlDocument(File.Bundle, backendType, cond); @@ -96,36 +96,36 @@ UxlDocument ParseDocument() ParseElements( name => { - switch (name) + switch (name.ToLowerCamelCase()) { - case "Using": + case "using": ParseUsing(result); return true; - case "Define": + case "define": ParseDefine(result); return true; - case "Declare": + case "declare": ParseDeclare(result); return true; - case "Deprecate": + case "deprecate": ParseDeprecate(result); return true; - case "Definition": - Log.Warning(GetSource(), ErrorCode.W0000, " is deprecated, replace with "); + case "definition": + Log.Warning(GetSource(), ErrorCode.W0000, " is deprecated, replace with "); ParseDeclare(result); return true; - case "Type": + case "type": ParseType(name, result); return true; - case "Template": + case "template": ParseTemplate(name, result.Templates); return true; - case "Library": - Log.Warning(GetSource(), ErrorCode.W0000, " is deprecated, replace with