Example #1
0
        public Token(string str, string fileName, int lineIndex, int lineOffset, NmProgram program,
                     bool isString = false)
        {
            FileName    = fileName;
            LineIndex   = lineIndex;
            LineOffset  = lineOffset;
            StringValue = str;

            if (!isString)
            {
                if (!TokenDict.TryGetValue(str, out Type))
                {
                    if (LineIndex == -1)
                    {
                        Type = TokenType.Identifier;
                        return;
                    }

                    bool found = false;
                    foreach (var constantFormat in ConstantFormats)
                    {
                        if (constantFormat.Verify(StringValue))
                        {
                            Constant = constantFormat.Parse(this, program);

                            if (!constantFormat.VerifyBounds(Constant))
                            {
                                throw new CompileException(CompileErrorType.OutOfBoundsConstant, this);
                            }

                            Type  = Constant.ToTokenType();
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        if (IdentifierFormat.Match(StringValue))
                        {
                            Type = TokenType.Identifier;
                        }
                        else
                        {
                            throw new CompileException(CompileErrorType.WrongIdentifierFormat, this);
                        }
                    }
                }
            }
            else
            {
                var chars = new List <int>();
                CompileErrorType error;
                if ((error = StringFormat.CheckEscapeSymbols(StringValue, out chars)) != 0)
                {
                    throw new CompileException(error, this);
                }

                Constant = new Constant(this, program, chars);

                Type = TokenType.StringToken;
            }
        }