public idToken ExpectTokenType(TokenType type, TokenSubType subType)
        {
            idToken token;

            if ((token = ReadToken()) == null)
            {
                Error("couldn't read expected token");
                return(null);
            }
            else if (token.Type != type)
            {
                Error("expected a '{0}' but found '{1}'", type.ToString().ToLower(), token.ToString());
                return(null);
            }
            else if (token.Type == TokenType.Number)
            {
                if ((token.SubType & subType) != subType)
                {
                    Error("expected '{0}' but found '{1}'", subType.ToString().ToLower(), token.ToString());
                    return(null);
                }
            }
            else if (token.Type == TokenType.Punctuation)
            {
                if (token.SubType != subType)
                {
                    Error("expected '{0}' but found '{1}'", _scriptStack.Peek().GetPunctuationFromID(subType), token.ToString());
                    return(null);
                }
            }

            return(token);
        }
Exemple #2
0
 public Token(TokenType type, TokenSubType subType, string sourceString, uint line, uint position)
 {
     Type         = type;
     SubType      = subType;
     SourceString = sourceString;
     Line         = line;
     Position     = position;
 }
Exemple #3
0
 public Token(string fileName, int line, int column, TokenType type, string contents, TokenSubType subType = TokenSubType.None, object tag = null)
 {
     FileName = fileName;
     Line     = line;
     Column   = column;
     Type     = type;
     SubType  = subType;
     Contents = contents;
     Tag      = tag;
 }
Exemple #4
0
        /// <summary>
        /// Check if the next token matches the given subtype. If they match, take the token.
        /// </summary>
        public bool MatchAndTake(TokenSubType subType)
        {
            var isMatch = Match(subType);

            if (isMatch)
            {
                Take();
            }

            return(isMatch);
        }
Exemple #5
0
        /// <summary>
        /// Take a token from the stream. Throws an exception if the given subtype does not match the token subtype.
        /// </summary>
        public Token Take(TokenSubType subType)
        {
            var token = Take();

            if (token.SubType != subType)
            {
                throw new MondCompilerException(token, CompilerError.ExpectedButFound, subType, token);
            }

            return(token);
        }
Exemple #6
0
 public Token
 (
     string value,
     TokenType type, TokenSubType subType
 )
     : this(
         0, value, 0, 0,
         Tokenizer.State.Context.Global,
         type, subType
         )
 {
 }
Exemple #7
0
        private static void TestNumber(string code, TokenSubType targetType)
        {
            var tokenizer = new Tokenizer(code, new TokenizerOptions());

            Assert.IsTrue(
                TokenizationTestUtils.Match(
                    tokenizer.GetNextToken(),
                    new Token(
                        0, code, 0, 0,
                        Tokenizer.State.Context.Global,
                        TokenType.Number, targetType
                        )
                    ),
                $"Should tokenize number '{code}' and classify it as '{targetType.ToString()}'"
                );
            TokenizationTestUtils.TestEOF(tokenizer);
        }
        private void TestOperator(string op, TokenSubType st, TokenType t)
        {
            var tokenizer = new Tokenizer(op, new TokenizerOptions());

            Assert.IsTrue(
                TokenizationTestUtils.Match(
                    tokenizer.GetNextToken(),
                    new Token(
                        0, op, 0, 0,
                        Tokenizer.State.Context.Global,
                        t, st
                        )
                    ),
                $"Should classify '{op}' as '{st}'"
                );
            TokenizationTestUtils.TestEOF(tokenizer);
        }
        public static Token Parse(Tokenizer.State state)
        {
            TokenSubType st = GetTypeFor(state.CurrentCharacter);

            if (st == TokenSubType.Unknown)
            {
                return(null);
            }

            Token t = new Token(
                value: state.CurrentCharacter.ToString(),

                type: IsMathOperator(state.CurrentCharacter)
                        ? TokenType.MathOperator
                        : TokenType.SpecialOperator,
                subType: st
                );

            state.Index += 1;
            return(t);
        }
Exemple #10
0
        public Token
        (
            int index,
            string value,

            int line, int position,

            Tokenizer.State.Context context,
            TokenType type, TokenSubType subType
        )
        {
            this.Context = context;

            this.Index    = index;
            this.Line     = line;
            this.Position = position;

            this.Value  = value;
            this.Length = value.Length;

            this.Type    = type;
            this.SubType = subType;
        }
Exemple #11
0
        public Token
        (
            int index,
            string value,

            int line, int position,

            Tokenizer.State.Context context,
            TokenType type, TokenSubType subType
        )
        {
            Context = context;

            Index    = index;
            Line     = line;
            Position = position;

            Value  = value;
            Length = value.Length;

            Type    = type;
            SubType = subType;
        }
Exemple #12
0
 /// <summary>
 /// Check if the next token matches the given subtype.
 /// </summary>
 public bool Match(TokenSubType subType, int distance = 0)
 {
     return(Peek(distance).SubType == subType);
 }
Exemple #13
0
 public PdfToken(TokenType tokenType, TokenSubType tokenSubType, byte[] rawData)
 {
     this.Type    = tokenType;
     this.SubType = tokenSubType;
     this.RawData = rawData;
 }
Exemple #14
0
 public PdfToken(TokenType tokenType, byte[] rawData, TokenSubType tokenSubType, double value)
     : this(tokenType, tokenSubType, rawData)
 {
     this.DoubleValue = value;
 }
 private void TestSpecOp(char op, TokenSubType st)
 => TestOperator(op.ToString(), st, TokenType.SpecialOperator);
 private void TestMathOp(char op, TokenSubType st)
 => TestOperator(op.ToString(), st, TokenType.MathOperator);
 private static void AddToken(string value, TokenType type, TokenSubType subType=TokenSubType.None, int pos=0, int line=0)
 {
     if (type == TokenType.Operation)
         Lexems.Add(new Token(value, type, Helper.operationType(value[0]),pos,line,1));
     else
         if (type==TokenType.Identifier)
             Lexems.Add(Helper.parseIdentifier(value,pos,line));
         else
             Lexems.Add(new Token(value, type, subType,pos,line,1));
     if (type == TokenType.Identifier)
         buffer = "";
 }
Exemple #18
0
 /// <summary>
 /// Checks if token corresponds to given subtype
 /// </summary>
 /// <param name="st">Target extended type</param>
 /// <returns>True if token matches needed type</returns>
 public bool Is(TokenSubType st) => SubType == st;
Exemple #19
0
 /// <summary>
 /// Checks if token corresponds to given subtype and matches given value(string representation)
 /// </summary>
 /// <param name="st">Target extended type</param>
 /// <param name="v">Target value(string representation)</param>
 /// <returns>True if token matches needed value</returns>
 public bool Is(TokenSubType st, string v) => SubType == st && Value == v;
Exemple #20
0
		public string GetPunctuationFromID(TokenSubType id)
		{
			foreach(LexerPunctuation p in _punctuation)
			{
				if((int) p.N == (int) id)
				{
					return p.P;
				}
			}

			return "unknown punctuation";
		}
Exemple #21
0
		public idToken ExpectTokenType(TokenType type, TokenSubType subType)
		{
			idToken token;

			if((token = ReadToken()) == null)
			{
				Error("couldn't read expected token");
				return null;
			}

			string tokenValue = token.ToString();

			if(token.Type != type)
			{
				Error("expected a '{0}' but found '{1}'", type.ToString().ToLower(), tokenValue);
				return null;
			}
			else if(token.Type == TokenType.Number)
			{
				if((token.SubType & subType) != subType)
				{
					Error("expected '{0}' but found '{1}'", subType.ToString().ToLower(), tokenValue);
					return null;
				}
			}
			else if(token.Type == TokenType.Punctuation)
			{
				if(token.SubType != subType)
				{
					Error("expected '{0}' but found '{1}'", GetPunctuationFromID(subType), tokenValue);
					return null;
				}
			}

			return token;
		}
Exemple #22
0
 public Token(
     TokenType type, TokenSubType subType, string sourceString,
     uint line, uint position, object value) :
     this(type, subType, sourceString, line, position) => Value = value;
 public Token(string value, TokenType type, TokenSubType subtype, int pos = 0, int line=0, int length = 1)
 {
     Value = value;
     Type = type;
     SubType = subtype;
     Position = pos;
     Line = line;
     Length = length;
 }
 private static void AddToken(char c, TokenType type, TokenSubType subType, int pos = 0, int line = 0)
 {
     AddToken(c.ToString(), type, subType, pos, line);
 }
Exemple #25
0
 public PdfToken(TokenType tokenType, byte[] rawData, TokenSubType tokenSubType, int value)
     : this(tokenType, tokenSubType, rawData)
 {
     this.IntValue = value;
 }
Exemple #26
0
		/// <summary>
		/// Reads the token when a token with the given type is available.
		/// </summary>
		/// <param name="type"></param>
		/// <param name="subType"></param>
		/// <returns></returns>
		public idToken CheckTokenType(TokenType type, TokenSubType subType)
		{
			idToken token;

			if((token = ReadToken()) == null)
			{
				return null;
			}

			// if the type matches
			if((token.Type == type) && ((token.SubType & subType) == subType))
			{
				return token;
			}

			// unread token
			_scriptPosition = _lastScriptPosition;
			_line = _lastLine;

			return null;
		}
Exemple #27
0
 public Token(Token token, TokenType type, string contents, TokenSubType subType = TokenSubType.None, object tag = null)
     : this(token.FileName, token.Line, token.Column, type, contents, subType, tag)
 {
 }