Пример #1
0
        /// <summary>
        /// Reads the next token from the stream. If it's not a literal,
        /// throws an exception.
        /// </summary>
        private LiteralToken ReadNextAsLiteral()
        {
            Token nextToken  = ReadNextToken();
            bool  isNegative = false;

            if ((nextToken is OperatorToken) &&
                (nextToken.Name == "-"))
            {
                isNegative = true;
                nextToken  = ReadNextToken();
            }

            if (!(nextToken is LiteralToken))
            {
                throw new CompilerMessage(ErrorCode.UnexpectedToken, "Literal expected at '" + nextToken.Name + "'");
            }

            Token tokenToReturn = nextToken;

            if (isNegative)
            {
                if (_source == null)
                {
                    throw new CompilerMessage(ErrorCode.InternalError, "Internal error: attempted to use tokenized script with no source");
                }
                tokenToReturn = _source.FindTokenWithName("-" + nextToken.Name);
                if (tokenToReturn == null)
                {
                    tokenToReturn = new LiteralToken("-" + nextToken.Name);
                    _source.AddToken(tokenToReturn);
                }
            }

            return((LiteralToken)tokenToReturn);
        }
Пример #2
0
        private void ParseAndAddMemberToStruct(Token variableType, Token structName, ScriptStruct structDefinition, Token parentStruct)
        {
            _source.IgnoreAsteriskIfPresent();
            Token memberName = _source.ReadNextToken();

            if ((memberName is KeywordToken) || (memberName is OperatorToken) ||
                (memberName is ModifierToken))
            {
                throw new CompilerMessage(ErrorCode.TokenAlreadyDefined, "Cannot use '" + memberName.Name + "' as variable name since it has another meaning");
            }

            string mangledName;

            memberName = GetTokenForStructMember(structName, memberName, out mangledName);
            if (memberName != null)
            {
                throw new CompilerMessage(ErrorCode.TokenAlreadyDefined, "Member '" + mangledName + "' already exists");
            }
            memberName = new Token(mangledName, true);
            // TODO: Set necessary fields on new token for this struct member
            _tokenizedScript.AddToken(memberName);

            CompilerUtils.SetArrayPropertiesOnTokenFromStream(_source, memberName);

            if (_source.NextIsKeyword(PredefinedSymbol.OpenParenthesis))
            {
                VerifyModifiersAgainstType(ModifierTargets.MemberFunction);
                VerifyReturnTypeValidForFunction(variableType);

                ScriptFunction func = new ScriptFunction(variableType, memberName);
                ParseFunctionParameterList(func);

                func.IsPrototypeOnly = true;
            }
            else
            {
                VerifyModifiersAgainstType(ModifierTargets.MemberVariable);
                ProcessVariableDeclaration(variableType, structDefinition, parentStruct, _state.NextTokenModifiers);
            }
        }