コード例 #1
0
ファイル: JTokenizer.cs プロジェクト: emtees/old-code
		} // ParseNumber
	
	
	// Parse a string literal or regexp expression beginning at the current
	// stream location.
	private Token ParseStringLiteral(SrcLoc loc)
		{
		// HACK SN 7/14/01: rewrite this to support literals longer
		// than maxTokenLen.
		
		// HACK SN 7/14/01: parse regexps as well as single- or double-quoted strings
		
		// Get a buffer from which we can parse the string.
		char[] charBuf;
		int startPos;
		int bufLen = buffer.GetBuffer(maxTokenLen, out charBuf, out startPos);
		Trace.Assert(bufLen > 1);
		
		// Scan to find the end of the string.
		char quoteChar = charBuf[startPos];
		int tokenLen = 1;
		StringBuilder parsedValueBuilder = new StringBuilder();
		while (tokenLen < bufLen)
			{
			char curChar = charBuf[startPos+tokenLen];
			if (curChar == quoteChar)
				break;
			else if (curChar == '\\')
				{
				// HACK SN 7/14/01: extend this to parse all ECMAScript backslash
				// sequences.
				
				if (tokenLen+1 >= bufLen)
					throw new ParseError("unterminated string literal or regexp", loc);
				
				curChar = charBuf[startPos+tokenLen+1];
				switch (curChar)
					{
					case 'b': curChar = '\b'; break;
					case 'f': curChar = '\f'; break;
					case 'n': curChar = '\n'; break;
					case 'r': curChar = '\r'; break;
					case 't': curChar = '\t'; break;
					case 'v': curChar = '\v'; break;
					}
				
				parsedValueBuilder.Append(curChar);
				tokenLen += 2;
				}
			else
				{
				parsedValueBuilder.Append(curChar);
				tokenLen++;
				}
			
			} // while (tokenLen < bufLen)
		
		if (tokenLen >= bufLen)
			throw new ParseError("unterminated string literal or regexp", loc);
		
		loc.len = tokenLen + 1;
		StringLit litToken = new StringLit();
		litToken.rawText    = buffer.ConsumeString(tokenLen+1);
		litToken.loc        = loc;
		litToken.parsedText = parsedValueBuilder.ToString();
		
		switch (quoteChar)
			{
			case '\'': litToken.quoteType = StringLit.QuoteType.singleQ; break;
			case '"':  litToken.quoteType = StringLit.QuoteType.doubleQ; break;
			case '/':  litToken.quoteType = StringLit.QuoteType.regexp;  break;
			
			default:
				Trace.Assert(false);
				break;
			}
		
		return litToken;
		} // ParseStringLiteral
コード例 #2
0
        }         // ParseNumber

        // Parse a string literal or regexp expression beginning at the current
        // stream location.
        private Token ParseStringLiteral(SrcLoc loc)
        {
            // HACK SN 7/14/01: rewrite this to support literals longer
            // than maxTokenLen.

            // HACK SN 7/14/01: parse regexps as well as single- or double-quoted strings

            // Get a buffer from which we can parse the string.
            char[] charBuf;
            int    startPos;
            int    bufLen = buffer.GetBuffer(maxTokenLen, out charBuf, out startPos);

            Trace.Assert(bufLen > 1);

            // Scan to find the end of the string.
            char          quoteChar          = charBuf[startPos];
            int           tokenLen           = 1;
            StringBuilder parsedValueBuilder = new StringBuilder();

            while (tokenLen < bufLen)
            {
                char curChar = charBuf[startPos + tokenLen];
                if (curChar == quoteChar)
                {
                    break;
                }
                else if (curChar == '\\')
                {
                    // HACK SN 7/14/01: extend this to parse all ECMAScript backslash
                    // sequences.

                    if (tokenLen + 1 >= bufLen)
                    {
                        throw new ParseError("unterminated string literal or regexp", loc);
                    }

                    curChar = charBuf[startPos + tokenLen + 1];
                    switch (curChar)
                    {
                    case 'b': curChar = '\b'; break;

                    case 'f': curChar = '\f'; break;

                    case 'n': curChar = '\n'; break;

                    case 'r': curChar = '\r'; break;

                    case 't': curChar = '\t'; break;

                    case 'v': curChar = '\v'; break;
                    }

                    parsedValueBuilder.Append(curChar);
                    tokenLen += 2;
                }
                else
                {
                    parsedValueBuilder.Append(curChar);
                    tokenLen++;
                }
            }             // while (tokenLen < bufLen)

            if (tokenLen >= bufLen)
            {
                throw new ParseError("unterminated string literal or regexp", loc);
            }

            loc.len = tokenLen + 1;
            StringLit litToken = new StringLit();

            litToken.rawText    = buffer.ConsumeString(tokenLen + 1);
            litToken.loc        = loc;
            litToken.parsedText = parsedValueBuilder.ToString();

            switch (quoteChar)
            {
            case '\'': litToken.quoteType = StringLit.QuoteType.singleQ; break;

            case '"':  litToken.quoteType = StringLit.QuoteType.doubleQ; break;

            case '/':  litToken.quoteType = StringLit.QuoteType.regexp;  break;

            default:
                Trace.Assert(false);
                break;
            }

            return(litToken);
        }         // ParseStringLiteral