예제 #1
0
 private void AssertTokenType(StyleTokenType styleTokenType)
 {
     if (tokenStream.Current.styleTokenType != styleTokenType)
     {
         throw new ParseException(tokenStream.Current, $"Parsing stylesheet failed. Expected token '{styleTokenType}' but got '{tokenStream.Current.styleTokenType}'");
     }
 }
예제 #2
0
        public int FindNextIndex(StyleTokenType targetStyleTokenType)
        {
            int i       = 0;
            int counter = 0;

            while (HasTokenAt(i))
            {
                StyleTokenType styleToken = Peek(i);
                if (styleToken == StyleTokenType.ParenOpen)
                {
                    counter++;
                }
                else if (styleToken == StyleTokenType.ParenClose)
                {
                    counter--;
                }
                else if (styleToken == targetStyleTokenType && counter == 0)
                {
                    return(i);
                }

                i++;
            }

            return(-1);
        }
예제 #3
0
        private StyleASTNode ParseLiteralOrReference(StyleTokenType literalType)
        {
            StyleToken currentToken = tokenStream.Current;

            if (AdvanceIfTokenType(StyleTokenType.At))
            {
                return(ParseConstReference().WithLocation(currentToken));
            }

            string value = AssertTokenTypeAndAdvance(literalType);

            switch (literalType)
            {
            case StyleTokenType.String:
                return(StyleASTNodeFactory.StringLiteralNode(value).WithLocation(currentToken));

            case StyleTokenType.Number:
                return(StyleASTNodeFactory.NumericLiteralNode(value).WithLocation(currentToken));

            case StyleTokenType.Boolean:
                return(StyleASTNodeFactory.BooleanLiteralNode(value).WithLocation(currentToken));

            case StyleTokenType.Identifier:
                return(StyleASTNodeFactory.IdentifierNode(value).WithLocation(currentToken));
            }

            throw new ParseException(currentToken, $"Please add support for this type: {literalType}!");
        }
예제 #4
0
        public int FindNextIndexAtSameLevel(StyleTokenType targetStyleTokenType)
        {
            int i     = 0;
            int level = 0;

            while (HasTokenAt(i))
            {
                StyleTokenType styleToken = Peek(i);
                if (styleToken == StyleTokenType.ParenOpen || styleToken == StyleTokenType.BracketOpen || styleToken == StyleTokenType.LessThan)
                {
                    level++;
                }
                else if (styleToken == StyleTokenType.ParenClose || styleToken == StyleTokenType.BracketClose || styleToken == StyleTokenType.GreaterThan)
                {
                    level--;
                }
                else if (styleToken == targetStyleTokenType && level == 0)
                {
                    return(i);
                }

                i++;
            }

            return(-1);
        }
예제 #5
0
 public StyleToken(StyleTokenType styleTokenType, string value, int line, int column)
 {
     this.styleTokenType = styleTokenType;
     this.value          = value;
     this.line           = line;
     this.column         = column;
 }
예제 #6
0
        private bool AdvanceIfTokenType(StyleTokenType styleTokenType)
        {
            if (tokenStream.Current.styleTokenType == styleTokenType)
            {
                tokenStream.Advance();
                return(true);
            }

            return(false);
        }
예제 #7
0
        private string AssertTokenTypeAndAdvance(StyleTokenType styleTokenType)
        {
            if (tokenStream.Current.styleTokenType != styleTokenType)
            {
                throw new ParseException(tokenStream.Current, $"Parsing stylesheet failed. Expected token '{styleTokenType}' but got '{tokenStream.Current.styleTokenType}'");
            }

            tokenStream.Advance();
            return(tokenStream.Previous.value);
        }
예제 #8
0
        private RunAction ParseCommandAttribute()
        {
            StyleTokenType styleTokenType = tokenStream.Current.styleTokenType;

            tokenStream.Advance();

            switch (styleTokenType)
            {
            case StyleTokenType.Run: return(RunAction.Run);

            case StyleTokenType.Pause: return(RunAction.Pause);

            case StyleTokenType.Stop: return(RunAction.Stop);

            default:
                throw new ParseException(tokenStream.Previous,
                                         $"The following command actions are supported: run, pause and stop. Found '{tokenStream.Previous.value}' instead.");
            }
        }
예제 #9
0
        public int FindMatchingIndex(StyleTokenType braceOpen, StyleTokenType braceClose)
        {
            if (Current != braceOpen)
            {
                return(-1);
            }

            Save();

            int i       = -1;
            int counter = 0;

            while (ptr != tokens.Count)
            {
                i++;

                if (Current == braceOpen)
                {
                    counter++;
                }

                if (Current == braceClose)
                {
                    counter--;
                    if (counter == 0)
                    {
                        Restore();
                        return(i);
                    }
                }

                Advance();
            }

            Restore();
            return(-1);
        }
예제 #10
0
        private bool IsAnyTokenType(StyleTokenType type0, StyleTokenType type1, StyleTokenType type2 = 0, StyleTokenType type3 = 0, StyleTokenType type4 = 0, StyleTokenType type5 = 0)
        {
            StyleTokenType tokenType = tokenStream.Current.styleTokenType;

            return(tokenType == type0 || tokenType == type1 || tokenType == type2 || tokenType == type3 || tokenType == type4 || tokenType == type5);
        }
예제 #11
0
        private static void TryReadCharacters(TokenizerContext context, string match, StyleTokenType styleTokenType, List <StyleToken> output)
        {
            if (context.ptr + match.Length > context.input.Length)
            {
                return;
            }
            for (int i = 0; i < match.Length; i++)
            {
                if (context.input[context.ptr + i] != match[i])
                {
                    return;
                }
            }

            output.Add(new StyleToken(styleTokenType, match, context.line, context.column));
            TryConsumeWhiteSpace(context.Advance(match.Length));
        }
예제 #12
0
 public StyleToken(StyleTokenType styleTokenType, int line, int column) :
     this(styleTokenType, string.Empty, line, column)
 {
 }