Skip to content

Commit

Permalink
Add support for hexadecimal colour codes (#15).
Browse files Browse the repository at this point in the history
  • Loading branch information
c272 committed May 18, 2023
1 parent e777cb3 commit d496e71
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
28 changes: 23 additions & 5 deletions Compiler/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,31 @@ public static List<CompileResult> Compile(Dictionary<string, IroVariable> vars,
//Switch on all the values in that set and define style based on the keys.
foreach (var styleProperty in styleDefinition)
{
//Is the value a string?
if (styleProperty.Value.Type != VariableType.Value)
//Is the value for a colour, or another random property?
string value;
if (styleProperty.Key == "color" || styleProperty.Key == "colour"
|| styleProperty.Key == "background_color" || styleProperty.Key == "background_colour")
{
Error.CompileWarning("Failed to create style with name '" + style.Key + ", non-string value defined in the style object.");
continue;
//For a colour, so it must be hex or string type.
if (styleProperty.Value.Type != VariableType.Value && styleProperty.Value.Type != VariableType.Hex)
{
Error.CompileWarning("Failed to create style with name '" + style.Key + ", colour values must be of type hex or string.");
continue;
}

//It is, grab value out.
value = (styleProperty.Value is IroValue) ? ((IroValue)styleProperty.Value).Value : ((IroHex)styleProperty.Value).Value;
}
else
{
//Ensure property is a value type, then grab value out.
if (styleProperty.Value.Type != VariableType.Value)
{
Error.CompileWarning("Failed to create style with name '" + style.Key + "', non-string value defined in the style object.");
continue;
}
value = ((IroValue)styleProperty.Value).Value;
}
string value = ((IroValue)styleProperty.Value).Value;

//Switch on the property.
switch (styleProperty.Key)
Expand Down
1 change: 1 addition & 0 deletions Data Structures/Parsing/IroScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum VariableType
Array,
Regex,
Value,
Hex,
Set,
Include,
Reference
Expand Down
22 changes: 22 additions & 0 deletions Data Structures/Parsing/IroValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ public IroValue(string val)
Value = val;
}
}

//Represents a single hexadecimal value in Iro.
public class IroHex : IroVariable
{
public string Value;

private IroHex(string hexVal)
{
base.Type = VariableType.Hex;
Value = hexVal;
}

//Attempts to parse the given string into an Iro hex value.
//On failure, returns null.
public static IroHex Parse(string hexVal)
{
//Ensure the value conforms to the standard.
if (!hexVal.StartsWith('#') || (hexVal.Length != 4 && hexVal.Length != 7))
return null;
return new IroHex(hexVal);
}
}

public class IroRegex : IroVariable
{
Expand Down
16 changes: 13 additions & 3 deletions Grammar/iro.g4
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ grammar iro;
@parser::header {#pragma warning disable 3021}
@lexer::header {#pragma warning disable 3021}

@lexer::members {
private bool rhsmode = false;
}

/*
* Parser Rules
*/
Expand Down Expand Up @@ -31,6 +35,7 @@ include: COLON_SYM IDENTIFIER QUOTE_SYM IDENTIFIER QUOTE_SYM SEMICOLON_SYM;

//Definition, possible right hand sides of attribute.
definition: ARRAY_SYM? (EQUALS_SYM | REG_EQUALS_SYM) (definition_ident //Literal (eg. myname)
| HEX_VALUE
| regex //Regular expression.
| constant_ref //Reference to a defined const (eg. $${__MYCONST}).
| array
Expand Down Expand Up @@ -58,7 +63,7 @@ REGEX: L_BRACKET (~[()\n\r] | '\\(' | '\\)' | REGEX)* R_BRACKET ('|' | '?' | '*'
//Operators & symbols.
ESCAPED_BRACKET: '\\(' | '\\)';
REG_EQUALS_SYM: '\\=';
EQUALS_SYM: '=';
EQUALS_SYM: '=' {rhsmode=true;};
ARRAY_SYM: '[]';
COMMA_SYM: ',';
L_SQUARE_BRACKET: '[';
Expand All @@ -75,11 +80,16 @@ R_BRACKET: ')';
//An identifier.
IDENTIFIER: [A-Za-z0-9_.-]+;

//A hexadecimal colour value.
HEX_VALUE: {rhsmode}? '#' [0-9A-Za-z]+;

//Comments start with "#" in Iro.
COMMENT: '#' (.)*? '\n' -> channel(HIDDEN);
//Only valid when not in RHS mode.
COMMENT: {!rhsmode}? '#' (.)*? '\n' -> channel(HIDDEN);

//Ignore all newlines and whitespace.
WS: [ \n\r\t]+ -> channel(HIDDEN);
WS: [ \r\t]+ -> channel(HIDDEN);
ENDL: '\n' {rhsmode=false;} -> channel(HIDDEN);

//Capture token for unknowns.
UNKNOWN_SYMBOL: .;
25 changes: 19 additions & 6 deletions Visitors/AttributeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,28 @@ public override IroVariable VisitDefinition([NotNull] iroParser.DefinitionContex
//Normal equality type?
else if (context.EQUALS_SYM() != null)
{
//Must be a normal identifier.
if (context.definition_ident() == null)
//Yes, if it's a normal definition identifier return it.
if (context.definition_ident() != null)
{
return new IroValue(context.definition_ident().IDENTIFIER().GetText());

}
else if (context.HEX_VALUE() != null)
{
Error.Fatal(context, "Value provided for standard non-regex variable must be a string.");
//Not a normal definition identifier. Hex value!
var hex = IroHex.Parse(context.HEX_VALUE().GetText());
if (hex == null)
{
Error.Fatal(context, "Invalid hexadecimal value provided, must be of the format '#NNN or #NNNNNN'.");
return null;
}
return hex;
}
else
{
Error.Fatal(context, "Value provided for standard non-regex variable must be a string or hexadecimal value.");
return null;
}

//Return the value.
return new IroValue(context.definition_ident().IDENTIFIER().GetText());
}

//Unknown.
Expand Down

0 comments on commit d496e71

Please sign in to comment.