Esempio n. 1
0
    public UssUnexpectedTokenException(UssToken _token) :
        base("Unexpected token: " + _token.body + ".")
    {
        source = ExceptionSource.Parser;

        token = _token;
    }
Esempio n. 2
0
    public UssUnexpectedTokenException(UssToken _token, UssTokenType _expected) :
        base("Unexpected token: " + _token.body + ". (expected " + _expected + ")")
    {
        source = ExceptionSource.Parser;

        token    = _token;
        expected = _expected;
    }
Esempio n. 3
0
    private void ParseValues(UssToken token)
    {
        if (token.IsIgnoreable)
        {
            return;
        }

        if (token.type == UssTokenType.SemiColon)
        {
            if (valueState != ValueParsingState.End)
            {
                throw new UssUnexpectedTokenException(token);
            }

            state = ParsingState.Root;
            return;
        }

        if (valueState == ValueParsingState.Key)
        {
            if (token.type != UssTokenType.ValueRef)
            {
                throw new UssUnexpectedTokenException(token, UssTokenType.ValueRef);
            }

            valueKey   = token.body.Substring(1);
            valueState = ValueParsingState.Colon;
        }
        else if (valueState == ValueParsingState.Colon)
        {
            if (token.type == UssTokenType.LeftBracket)
            {
                state    = ParsingState.Properties;
                nodeType = CurrentNodeType.Bundle;
            }
            else if (token.type == UssTokenType.Colon)
            {
                valueState = ValueParsingState.Value;
            }
            else
            {
                throw new UssUnexpectedTokenException(token);
            }
        }
        else if (valueState == ValueParsingState.Value)
        {
            if (token.IsValue == false)
            {
                throw new UssUnexpectedTokenException(token);
            }

            values.Add(valueKey, UssValue.Create(token));
            valueState = ValueParsingState.End;
        }
    }
Esempio n. 4
0
    private UssToken[] ParseAll(string src)
    {
        var current = new UssToken();

        src      += " "; // padding
        state.src = src;

        while (state.cur != src.Length)
        {
            var found = false;

            foreach (var pair in seperators)
            {
                if (state.cur + pair.Key.Length >= src.Length)
                {
                    continue;
                }
                var candidate = src.Substring(state.cur, pair.Key.Length);
                if (candidate == pair.Key)
                {
                    Flush();
                    current.body = candidate;
                    current.type = pair.Value;
                    AppendToken(current);
                    current = new UssToken();

                    state.cur   += pair.Key.Length;
                    state.offset = state.cur;

                    found = true;
                    break;
                }
            }

            if (found == false)
            {
                state.cur++;
            }
        }

        return(tokens.ToArray());
    }
Esempio n. 5
0
    private void ParseConditions(UssToken token)
    {
        if (token.IsIgnoreable)
        {
            return;
        }

        if (token.type == UssTokenType.LeftBracket)
        {
            state    = ParsingState.Properties;
            nodeType = CurrentNodeType.Style;
            return;
        }

        // Every types of token can be accepted here
        // since name of the Unity's gameobject can contain almost characters.
        // (ex: Zu!ZU##!)
        var rawCondition   = token.body;
        var styleCondition = new UssStyleCondition();

        // CLASS
        if (rawCondition[0] == '.')
        {
            styleCondition.target = UssStyleTarget.Class;
            styleCondition.name   = rawCondition.Substring(1);
        }
        // NAME
        else if (rawCondition[0] == '#')
        {
            styleCondition.target = UssStyleTarget.Name;
            styleCondition.name   = rawCondition.Substring(1);
        }
        else
        {
            styleCondition.target = UssStyleTarget.Component;
            styleCondition.name   = rawCondition;
        }

        conditions.Add(styleCondition);
    }
Esempio n. 6
0
    private bool ParseProperties(UssToken token)
    {
        if (token.IsIgnoreable)
        {
            return(false);
        }

        if (propertyState == PropertyParsingState.Key &&
            token.type == UssTokenType.RightBracket)
        {
            state = ParsingState.Root;
            return(true);
        }

        if (propertyState == PropertyParsingState.Key)
        {
            if (token.type == UssTokenType.ValueRef)
            {
                bundles.Add(token.body.Substring(1));

                if (GetNextToken().type == UssTokenType.SemiColon)
                {
                    WasteNextToken();
                }
                else
                {
                    throw new UssUnexpectedTokenException(GetNextToken(), UssTokenType.SemiColon);
                }
            }
            else if (token.type == UssTokenType.Id)
            {
                propertyValues = new List <UssValue>();

                propertyKey   = token.body;
                propertyState = PropertyParsingState.Colon;
            }
            else
            {
                throw new InvalidOperationException("Invalid token `" + token.body + "`. (expected Id)");
            }
        }
        else if (propertyState == PropertyParsingState.Colon)
        {
            if (token.type != UssTokenType.Colon)
            {
                throw new InvalidOperationException("Invalid token `" + token.body + "`. (expected Colon)");
            }

            propertyState = PropertyParsingState.Value;
        }
        else
        {
            if (token.IsValue)
            {
                propertyValues.Add(UssValue.Create(token));
            }
            else if (token.type == UssTokenType.SemiColon)
            {
                properties.Add(new UssStyleProperty()
                {
                    key    = propertyKey,
                    values = propertyValues.ToArray()
                });

                propertyState = PropertyParsingState.Key;
            }
            else
            {
                throw new InvalidOperationException("Invalid token `" + token.body + "`. (expected Value)");
            }
        }

        return(false);
    }
Esempio n. 7
0
    private void ParseConditions(UssToken token)
    {
        if (token.IsIgnoreable)
        {
            return;
        }

        if (token.type == UssTokenType.LeftBracket)
        {
            state    = ParsingState.Properties;
            nodeType = CurrentNodeType.Style;
            return;
        }

        if (token.type == UssTokenType.RightArrow)
        {
            if (conditions.Count == 0)
            {
                throw new UssUnexpectedTokenException(token);
            }

            nextConditionType = UssStyleConditionType.DirectDescendant;
            return;
        }
        if (token.type == UssTokenType.Colon)
        {
            if (conditions.Count == 0)
            {
                throw new UssUnexpectedTokenException(token);
            }
            if (GetNextToken(true).type != UssTokenType.Id)
            {
                throw new UssUnexpectedTokenException(GetNextToken(), UssTokenType.Id);
            }

            conditions.Last().targetState = GetNextToken().body;
            WasteNextToken();
            return;
        }

        // Every types of token can be accepted here
        // since name of the Unity's gameobject can contain almost characters.
        // (ex: Zu!ZU##!)
        var rawCondition   = token.body;
        var styleCondition = new UssStyleCondition();

        if (rawCondition[0] == '*')
        {
            styleCondition.target = UssSelectorType.All;
            styleCondition.name   = "*";
        }
        // CLASS
        else if (rawCondition[0] == '.')
        {
            styleCondition.target = UssSelectorType.Class;
            styleCondition.name   = rawCondition.Substring(1);
        }
        // NAME
        else if (rawCondition[0] == '#')
        {
            styleCondition.target = UssSelectorType.Name;
            styleCondition.name   = rawCondition.Substring(1);
        }
        else
        {
            styleCondition.target = UssSelectorType.Component;
            styleCondition.name   = rawCondition;
        }

        styleCondition.type = nextConditionType;
        conditions.Add(styleCondition);
        nextConditionType = UssStyleConditionType.None;
    }
Esempio n. 8
0
    public static UssValue Create(UssToken token)
    {
        if (token.IsValue == false)
        {
            throw new ArgumentException("token is not a value");
        }

        if (token.type == UssTokenType.Null)
        {
            return new UssNullValue()
                   {
                       body = token.body
                   }
        }
        ;
        if (token.type == UssTokenType.Int)
        {
            return new UssIntValue()
                   {
                       body = token.body, value = int.Parse(token.body)
                   }
        }
        ;
        if (token.type == UssTokenType.Float)
        {
            return new UssFloatValue()
                   {
                       body = token.body, value = float.Parse(token.body)
                   }
        }
        ;
        if (token.type == UssTokenType.Id)
        {
            return new UssStringValue()
                   {
                       body = token.body, value = token.body
                   }
        }
        ;
        if (token.type == UssTokenType.HexColor)
        {
            Color color;

            if (ColorUtility.TryParseHtmlString(token.body, out color))
            {
                return new UssColorValue()
                       {
                           body = token.body, value = color
                       }
            }
            ;
            else
            {
                throw new UssParsingException("Invalid color format: " + token.body);
            }
        }
        if (token.type == UssTokenType.ValueRef)
        {
            return new UssRefValue()
                   {
                       body = token.body, key = token.body.Substring(1)
                   }
        }
        ;

        throw new InvalidOperationException("Unknown type: " + token.type);
    }
Esempio n. 9
0
 private UssToken InjectState(UssToken token)
 {
     token.line = state.line;
     return(token);
 }
Esempio n. 10
0
 private void AppendToken(UssToken token)
 {
     tokens.Add(InjectState(token));
 }