Beispiel #1
0
		} // ParseIdentifier
	
	
	// Parse a numeric literal beginning at the current stream location.
	private Token ParseNumber(SrcLoc loc)
		{
		// Get a buffer from which we can parse the number.
		char[] charBuf;
		int startPos;
		int bufLen = buffer.GetBuffer( maxTokenLen, out charBuf,
									   out startPos );
		if (bufLen > maxTokenLen)
			bufLen = maxTokenLen;
		
		// Find the end of the number.  We match for the following
		// components:
		// 
		//    integer part (1 or more digits)
		//    optional fractional part ('.' plus one or more digits)
		//    optional exponent ('E' or 'e', optional '+' or '-', and 1-3 digits)
		// 
		// HACK SN 7/14/01: for now, we aren't enforcing that the fractional
		// part must have at least one digit, or that the exponent must have
		// at least 1 and no more than 3 digits.  Review the ECMAScript spec for
		// the correct rules.  Also, should we allow numbers that begin with a '.'
		// (no integer part)?
		
		int tokenLen = 0;
		while ( tokenLen < bufLen &&
				CharUtils.IsDigitChar(charBuf[startPos+tokenLen]) )
			tokenLen++;
		
		if (tokenLen < bufLen && charBuf[startPos+tokenLen] == '.')
			{
			tokenLen++;
			while ( tokenLen < bufLen &&
					CharUtils.IsDigitChar(charBuf[startPos+tokenLen]) )
				tokenLen++;
			}
		
		if ( tokenLen < bufLen &&
			 ( charBuf[startPos+tokenLen] == 'E' ||
			   charBuf[startPos+tokenLen] == 'e' ) )
			{
			tokenLen++;
			if ( tokenLen < bufLen &&
				 ( charBuf[startPos+tokenLen] == '+' ||
				   charBuf[startPos+tokenLen] == '-' ) )
				tokenLen++;
			
			while ( tokenLen < bufLen &&
					CharUtils.IsDigitChar(charBuf[startPos+tokenLen]) )
				tokenLen++;
			}
		
		NumLit token = new NumLit();
		token.rawText = buffer.ConsumeString(tokenLen);
		loc.len = tokenLen;
		token.loc = loc;
		
		try
			{
			token.value = Double.Parse(token.rawText);
			}
		catch (FormatException e)
			{
			throw new ParseError( "FormatException parsing numeric literal \"" +
								  token.rawText + "\": " + e.ToString(),
								  loc );
			}
		catch (OverflowException e)
			{
			throw new ParseError( "OverflowException parsing numeric literal \"" +
								  token.rawText + "\": " + e.ToString(),
								  loc );
			}
		
		return token;
		} // ParseNumber
Beispiel #2
0
        }         // ParseIdentifier

        // Parse a numeric literal beginning at the current stream location.
        private Token ParseNumber(SrcLoc loc)
        {
            // Get a buffer from which we can parse the number.
            char[] charBuf;
            int    startPos;
            int    bufLen = buffer.GetBuffer(maxTokenLen, out charBuf,
                                             out startPos);

            if (bufLen > maxTokenLen)
            {
                bufLen = maxTokenLen;
            }

            // Find the end of the number.  We match for the following
            // components:
            //
            //    integer part (1 or more digits)
            //    optional fractional part ('.' plus one or more digits)
            //    optional exponent ('E' or 'e', optional '+' or '-', and 1-3 digits)
            //
            // HACK SN 7/14/01: for now, we aren't enforcing that the fractional
            // part must have at least one digit, or that the exponent must have
            // at least 1 and no more than 3 digits.  Review the ECMAScript spec for
            // the correct rules.  Also, should we allow numbers that begin with a '.'
            // (no integer part)?

            int tokenLen = 0;

            while (tokenLen < bufLen &&
                   CharUtils.IsDigitChar(charBuf[startPos + tokenLen]))
            {
                tokenLen++;
            }

            if (tokenLen < bufLen && charBuf[startPos + tokenLen] == '.')
            {
                tokenLen++;
                while (tokenLen < bufLen &&
                       CharUtils.IsDigitChar(charBuf[startPos + tokenLen]))
                {
                    tokenLen++;
                }
            }

            if (tokenLen < bufLen &&
                (charBuf[startPos + tokenLen] == 'E' ||
                 charBuf[startPos + tokenLen] == 'e'))
            {
                tokenLen++;
                if (tokenLen < bufLen &&
                    (charBuf[startPos + tokenLen] == '+' ||
                     charBuf[startPos + tokenLen] == '-'))
                {
                    tokenLen++;
                }

                while (tokenLen < bufLen &&
                       CharUtils.IsDigitChar(charBuf[startPos + tokenLen]))
                {
                    tokenLen++;
                }
            }

            NumLit token = new NumLit();

            token.rawText = buffer.ConsumeString(tokenLen);
            loc.len       = tokenLen;
            token.loc     = loc;

            try
            {
                token.value = Double.Parse(token.rawText);
            }
            catch (FormatException e)
            {
                throw new ParseError("FormatException parsing numeric literal \"" +
                                     token.rawText + "\": " + e.ToString(),
                                     loc);
            }
            catch (OverflowException e)
            {
                throw new ParseError("OverflowException parsing numeric literal \"" +
                                     token.rawText + "\": " + e.ToString(),
                                     loc);
            }

            return(token);
        }         // ParseNumber