Пример #1
0
 public ScriptToken(ScriptTokenType type, string value, int index, int length)
 {
     _type   = type;
     _value  = value;
     _index  = index;
     _length = length;
 }
Пример #2
0
        private void SkipBlock(ScriptTokenType start, ScriptTokenType stop)
        {
            if (_current.Type != start)
            {
                throw new InvalidOperationException("Expected block.");
            }

            var nesting = 0;

            while (true)
            {
                if (_current.Type == start)
                {
                    nesting++;
                }
                if (_current.Type == stop)
                {
                    nesting--;
                }
                if (nesting == 0)
                {
                    break;
                }
                if (!Read())
                {
                    break;
                }
            }
        }
Пример #3
0
 public BinaryOperatorExpression(ScriptTokenType op, Expression left, Expression right)
     : base(left.Start, right.End)
 {
     Operation = op;
     Left      = left;
     Right     = right;
 }
Пример #4
0
 private static bool IsCastable(ScriptTokenType token)
 {
     return(token == ScriptTokenType.Identifier ||
            token == ScriptTokenType.Number ||
            token == ScriptTokenType.Null ||
            token == ScriptTokenType.LeftParen ||
            token == ScriptTokenType.SingleString ||
            token == ScriptTokenType.String);
 }
Пример #5
0
        /// <summary>
        /// Take a token from the stream. Throws an exception if the given type does not match the token type.
        /// </summary>
        public ScriptToken Take(ScriptTokenType type)
        {
            var token = Take();

            if (token.Type != type)
            {
                throw new CompilerException(token, CompilerError.ExpectedButFound, type, token);
            }

            return(token);
        }
Пример #6
0
        /// <summary>
        /// Check if the next token matches the given type. If they match, take the token.
        /// </summary>
        public bool MatchAndTake(ScriptTokenType type)
        {
            var isMatch = Match(type);

            if (isMatch)
            {
                Take();
            }

            return(isMatch);
        }
Пример #7
0
        private ScriptBlock ParseStatement(ScriptTokenType type)
        {
            switch (type)
            {
            case ScriptTokenType.If:
            case ScriptTokenType.While:
            {
                return(ParseConditionalScope());
            }

            case ScriptTokenType.Switch:
            {
                return(ParseConditionalScope(requireBraces: true));
            }

            case ScriptTokenType.Else:
            {
                return(ParseElseStatement());
            }
            }
            throw new InvalidOperationException("Unknown statement type.");
        }
Пример #8
0
        public IEnumerable <T> ParseSeparatedBy <T>(ScriptTokenType separator, Func <Parser, bool, T> parseFunc) where T : class
        {
            var first = parseFunc(this, true);

            if (first == null)
            {
                yield break;
            }

            yield return(first);

            while (MatchAndTake(separator))
            {
                var next = parseFunc(this, false);

                if (next == null)
                {
                    yield break;
                }

                yield return(next);
            }
        }
Пример #9
0
 /// <summary>
 /// Check if the next token matches the given type.
 /// </summary>
 public bool Match(ScriptTokenType type, int distance = 0)
 {
     return(Peek(distance).Type == type);
 }
Пример #10
0
 static void RegisterInfix(ScriptTokenType type, IInfixParselet parselet)
 {
     _infixParselets.Add(type, parselet);
 }
Пример #11
0
 static void RegisterPrefix(ScriptTokenType type, IPrefixParselet parselet)
 {
     _prefixParselets.Add(type, parselet);
 }
Пример #12
0
 static void RegisterStatement(ScriptTokenType type, IStatementParselet parselet)
 {
     _statementParselets.Add(type, parselet);
 }
Пример #13
0
 static void RegisterDeclaration(ScriptTokenType type, IDeclarationParselet parselet)
 {
     _declarationParselets.Add(type, parselet);
 }