Skip to content

Commit

Permalink
compiler: support lowerCamelCase in UxlParser
Browse files Browse the repository at this point in the history
* Support lowerCamelCase in UXL files
* Robustness in BuildEnvironment.cs
* Update disasm tool
  • Loading branch information
mortend committed Oct 7, 2023
1 parent f0ef44f commit 67a68a4
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 160 deletions.
3 changes: 3 additions & 0 deletions src/common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ public static string ToLowerCamelCase(this string str)
switch (str)
{
case "cMake":
case "cPlusPlus":
case "dotNet":
case "iOS":
case "iPad":
Expand All @@ -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":
Expand Down
12 changes: 7 additions & 5 deletions src/compiler/core/BuildEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class BuildEnvironment : LogObject, IEnvironment
public string BundleDirectory { get; }
public string OutputDirectory { get; }

readonly Dictionary<string, bool> _cachedTests = new Dictionary<string, bool>();
readonly Dictionary<Key, Result> _cachedProperties = new Dictionary<Key, Result>();
readonly Dictionary<string, bool> _cachedTests = new();
readonly Dictionary<Key, Result> _cachedProperties = new();

internal BuildEnvironment(
Backend backend,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -415,9 +415,11 @@ List<string> GetSet(IEnumerable<SourceValue> values)
public HashSet<string> GetWords(string key)
{
var result = new HashSet<string>();
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;
}
Expand Down
32 changes: 16 additions & 16 deletions src/compiler/core/Syntax/UxlProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,20 @@ public void CompileDocuments()

if (def == null)
{
Log.Error(e.Source, ErrorCode.E0000, "Unknown 'Type' attribute on <Declare> element (" + e.Type + ")");
Log.Error(e.Source, ErrorCode.E0000, "Unknown 'type' attribute on <declare> element (" + e.Type + ")");
continue;
}

if (def.Contains(e.Key.String))
Log.Error(e.Key.Source, ErrorCode.E0000, "A <Declare> element for " + e.Key.String.Quote() + " already exists");
Log.Error(e.Key.Source, ErrorCode.E0000, "A <declare> element for " + e.Key.String.Quote() + " already exists");
else
def.Add(e.Key.String);
}

foreach (var e in doc.Deprecations)
{
if (_deprecated.ContainsKey(e.OldName))
Log.Error(e.Source, ErrorCode.E0000, "A <Deprecate> element for " + e.OldName.Quote() + " already exists");
Log.Error(e.Source, ErrorCode.E0000, "A <deprecate> element for " + e.OldName.Quote() + " already exists");
else
_deprecated.Add(e.OldName, e.NewName);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 67a68a4

Please sign in to comment.