Exemplo n.º 1
0
EXPORT_C void CTestConfig::ReadScriptL(const TDesC& aScript)
	{
	iSections.ResetAndDestroy();

	CTestConfigSection* section = NULL;
	CTestConfigItem* currentItem = NULL;
	TInt currentItemStart = 0;
	CTestConfigSection* sectionDefaults = NULL;

	HBufC8* scriptContents = ReadFileL(aScript);
	CleanupStack::PushL(scriptContents);

	TLex8 input(*scriptContents);

	while (!input.Eos())
		{
		input.SkipSpaceAndMark();
		input.SkipCharacters();

		if ( input.TokenLength() == 0)    // if valid potential token
			{
			//end of the script file found
			break;
			}

		const TPtrC8 token(input.MarkedToken());

		if (token.CompareF(_L8("endscript")) == 0)
			{
			//end of the script file found
			break;
			}
		else if (IsNewSection(*scriptContents, input))
			{
			ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem);

			TInt mid = 1;
			TInt len = token.Length() - 2;

			const TPtrC8 sectionName(token.Mid(mid, len));

			if (sectionDefaults != NULL)
				section = CTestConfigSection::NewLC(sectionName, *sectionDefaults);
			else
				section = CTestConfigSection::NewLC(sectionName);

			if (sectionDefaults == NULL && IsDefaultSection(section->SectionName()))
				sectionDefaults = section;

			User::LeaveIfError(iSections.Append(section));
			CleanupStack::Pop(section);
			}
		else if (section != NULL)
			{
			TInt valueOffset;
			TPtrC8 newItem;

			if (IsNewComment(*scriptContents, input))
				{
				ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem);
				__ASSERT_DEBUG(currentItem == NULL, User::Invariant());
				SkipToNextLine(input);
				}
			else if (IsNewItem(*scriptContents, input, newItem, valueOffset))
				{
				ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem);
				currentItemStart = input.MarkedOffset() + valueOffset;
				currentItem = &section->AddItemL(newItem, KNullDesC8);
				}
			}
		}

	ParseAndSetItemValueL(*scriptContents, input, currentItemStart, currentItem);
	CleanupStack::PopAndDestroy(scriptContents);
	}
Exemplo n.º 2
0
    Token GetNextToken()
    {
        //When we read the source code line by line, we use two index to indicate the both ends of a source line.
        //At first, the right end index equals to the left end index: they both are 0.
        sasm.lexer.iIndex0 = sasm.lexer.iIndex1;
        if (sasm.lexer.iIndex0 >= strlen(sasm.sourceCode[sasm.lexer.iCurrentSourceLine]))
        {
            if (!SkipToNextLine())
                return ASM_END_OF_TOKEN_STREAM;
        }
    
        if (sasm.lexer.iState == ASM_LEX_STATE_END_STRING)
            sasm.lexer.iState = ASM_LEX_STATE_NO_STRING;
        //locate index to the first non-whitespace character of a source code line
        if (sasm.lexer.iState != ASM_LEX_STATE_IN_STRING)
        {
            while (TRUE)
            {
                if (!IsCharWhiteSpace((sasm.sourceCode[sasm.lexer.iCurrentSourceLine][sasm.lexer.iIndex0])))
                    break;
                sasm.lexer.iIndex0++;
            }
        }
        sasm.lexer.iIndex1 = sasm.lexer.iIndex0;

		int isGetEscapeChar = FALSE;
    
        //move iIndex1 to the location of next token end
        while (TRUE)
        {
            if (sasm.lexer.iState == ASM_LEX_STATE_IN_STRING)
            {
                if (sasm.lexer.iIndex1 >= strlen(sasm.sourceCode[sasm.lexer.iCurrentSourceLine]))
                {
                    sasm.lexer.currentToken = ASM_TOKEN_TYPE_INVALID;
                    return sasm.lexer.currentToken;
                }
                if (sasm.sourceCode[sasm.lexer.iCurrentSourceLine][sasm.lexer.iIndex1] == '\\')
                {
					//isGetEscapeChar = TRUE;
                    sasm.lexer.iIndex1 += 2;
                    continue;
                }
                if (sasm.sourceCode[sasm.lexer.iCurrentSourceLine][sasm.lexer.iIndex1] == '"')
                    break;
                sasm.lexer.iIndex1++;
            }
            else
            {
                if (sasm.lexer.iIndex1 >= strlen(sasm.sourceCode[sasm.lexer.iCurrentSourceLine]))
                    break;
                if (IsCharDelimiter(sasm.sourceCode[sasm.lexer.iCurrentSourceLine][sasm.lexer.iIndex1]))
                    break;
                sasm.lexer.iIndex1++;
            }
        }
    
        if (sasm.lexer.iIndex1 - sasm.lexer.iIndex0 == 0)
            sasm.lexer.iIndex1++;
    
        //Get the next lexeme
        unsigned int currentTargetIndex = 0;
        for (int i = sasm.lexer.iIndex0; i < sasm.lexer.iIndex1; i++)
        {
			if (sasm.lexer.iState == ASM_LEX_STATE_IN_STRING)
				if (sasm.sourceCode[sasm.lexer.iCurrentSourceLine][i] == '\\')
				{
					switch (sasm.sourceCode[sasm.lexer.iCurrentSourceLine][i + 1])
					{
					case 'a':
						 sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\n';
						 break;
					case 'b':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\b';
						break;
					case 'f':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\f';
						break;
					case 'n':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\n';
						break;
					case 'r':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\r';
						break;
					case 't':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\t';
						break;
					case 'v':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\n';
						break;
					case '\\':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\\';
						break;
					case '\'':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\'';
						break;
					case '"':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\"';
						break;
					case '0':
						sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\0';
						break;

					default:
						break;
					}
					currentTargetIndex += 1;
					i++;
					continue;
				}
            sasm.lexer.pCurrentLexeme[currentTargetIndex] = sasm.sourceCode[sasm.lexer.iCurrentSourceLine][i];
            currentTargetIndex++;
        }
        sasm.lexer.pCurrentLexeme[currentTargetIndex] = '\0';
    	/*if (sasm.lexer.iState != ASM_LEX_STATE_IN_STRING)
    	strtoupper(sasm.lexer.pCurrentLexeme);*/
    
        //Decide which token the lexeme is
        sasm.lexer.currentToken = ASM_TOKEN_TYPE_INVALID;
        if (strlen(sasm.lexer.pCurrentLexeme) > 1 || sasm.lexer.pCurrentLexeme[0] != '"')
        {
            if (sasm.lexer.iState == ASM_LEX_STATE_IN_STRING)
            {
                sasm.lexer.currentToken = ASM_TOKEN_TYPE_STRING;
                return ASM_TOKEN_TYPE_STRING;
            }
        }
    
        if (IsStringInt(sasm.lexer.pCurrentLexeme))
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_INT;
    
        if (IsStringFloat(sasm.lexer.pCurrentLexeme))
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_FLOAT;
    
        if (IsStringIdent(sasm.lexer.pCurrentLexeme))
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_IDENT;
    
        if (strcmp(sasm.lexer.pCurrentLexeme, ASM_KW_SET_STACK_SIZE) == 0)
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_SETSTACKSIZE;
    
        if (strcmp(sasm.lexer.pCurrentLexeme, ASM_KW_VAR) == 0)
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_VAR;
    
        if (strcmp(sasm.lexer.pCurrentLexeme, ASM_KW_FUNCTION) == 0)
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_FUNC;
    
        if (strcmp(sasm.lexer.pCurrentLexeme, ASM_KW_PARAM) == 0)
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_PARAM;
    
        if (strcmp(sasm.lexer.pCurrentLexeme, ASM_KW_RETVAL) == 0)
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_REG_RETVAL;
    
    	if (strlen(sasm.lexer.pCurrentLexeme) == 1)
    	{
    		switch (sasm.lexer.pCurrentLexeme[0])
    		{
    		case '"':
    			switch (sasm.lexer.iState)
    			{
    			case ASM_LEX_STATE_NO_STRING:
    				sasm.lexer.iState = ASM_LEX_STATE_IN_STRING;
    				break;
    			case ASM_LEX_STATE_IN_STRING:
    				sasm.lexer.iState = ASM_LEX_STATE_END_STRING;
    			}
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_QUATE;
    			break;
    		case ',':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_COMMA;
    			break;
    		case ':':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_COLON;
    			break;
    		case '[':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_OPEN_BRACKET;
    			break;
    		case ']':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_CLOSE_BRACKET;
    			break;
    		case '{':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_OPEN_BRACE;
    			break;
    		case '}':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_CLOSE_BRACE;
    			break;
    		case '\n':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_NEWLINE;
    			break;
    		case '\r':
    			sasm.lexer.currentToken = ASM_TOKEN_TYPE_NEWLINE;
    			break;
    		}
    	}
    
        InstrLookup instrLookup;
        if (GetInstruction(sasm.instrLookup, sasm.lexer.pCurrentLexeme, &instrLookup))
            sasm.lexer.currentToken = ASM_TOKEN_TYPE_INSTR;
    
        return sasm.lexer.currentToken;
    }
Exemplo n.º 3
0
bool mcMapParser::ReadNextToken (csString& str)
{
  char c, next_c;

  str.Clear();
  m_empty_token = false;

  /* Skip commented and empty lines */
  for(;;)
  {
    if( !SkipWhitespace() ) 
    {
      return false;
    }

    if( !GetChar(c) )
    {
      return false;
    }

    if(c == '\n')
    {
      /* An empty line */
      ++m_current_line;
      continue;
    }

    if( PeekChar(next_c) )
    {
      if( c == '/' && next_c == '/')
      {
        if( !SkipToNextLine() )
        {
          return false;
        }
      }
      else
      {
        break;
      }
    }
    else
    {
      break;
    }
  }

  /* Quoted strings are returned directly */
  if (c == '\"')
  {
    for(;;)
    {
      if ( !GetChar(c) )
      {
        /* If we fail now, the token is not complete! */
        return false; 
      }

      if (c == '\"')
      {
        /* The end of the string has been reached */
	m_empty_token = str.IsEmpty();
        return true;
      }

      str << c;
    }
  }

  /* Check for special single character tokens */
  if ( c == '{'  || c == '}' ||
       c == '('  || c == ')' ||
       c == '\'' || c ==':'  )
  {
    str << c;
    return true;
  }

  /* Parse a regular word */
  for(;;)
  {
    str << c;

    if (!PeekChar(next_c))
    {
      break;
    }

    if ( next_c == '{'  || next_c == '}'  ||
         next_c == '('  || next_c == ')'  ||
         next_c == '\'' || next_c ==':'   ||
         next_c == ' '  || next_c == '\t' || 
         next_c == '\r' || next_c == '\n' )
    {
      break;
    }

    if(!GetChar(c))
    {
      break;
    }
  }

  return true;
}