TVariablePtr ExpressionParser::Parse(StringPtrLen str) { CheckBracketBalance(str); TVariablePtr variable; auto assignment = FindWithZeroBalance(str, g_token_assignment); if (assignment != nullptr) { variable = ParseVariableDeclaration(str.Left(assignment)); str = str.Right(assignment + std::strlen(g_token_assignment)); } else { // Unnamed variable variable = std::make_unique<Variable>(); } m_curr_variable = variable.get(); auto expression = ParseExpression(str); m_curr_variable = nullptr; NormalizeExpression(expression); SimplifyExpression(expression); variable->SetExpression(std::move(expression)); return variable; }
// ParseUnnamedVariableConcatenation //------------------------------------------------------------------------------ bool BFFParser::ParseUnnamedVariableConcatenation( BFFIterator & iter ) { ASSERT( *iter == BFF_VARIABLE_CONCATENATION ); // have we assigned a variable before? if ( m_SeenAVariable == false ) { Error::Error_1011_UnnamedConcatMustFollowAssignment( iter ); return false; } return ParseVariableDeclaration( iter, m_LastVarNameStart, m_LastVarNameEnd ); }
// ParseUnnamedVariableModification //------------------------------------------------------------------------------ bool BFFParser::ParseUnnamedVariableModification( BFFIterator & iter ) { ASSERT( ( *iter == BFF_VARIABLE_CONCATENATION ) || ( *iter == BFF_VARIABLE_SUBTRACTION ) ); // have we assigned a variable before? if ( m_SeenAVariable == false ) { Error::Error_1011_UnnamedModifcationMustFollowAssignment( iter ); return false; } return ParseVariableDeclaration( iter, m_LastVarName, m_LastVarFrame ); }
ListaVariables* Sintactico::ParseVariableDeclarationList() { DeclaracionVariable* decl_var = ParseVariableDeclaration(); ListaVariables* lista_var = _ParseVariableDeclarationList(); list<string> ids = decl_var->GetIds(); list<string>::iterator it; for ( it = ids.begin() ; it != ids.end() ; it++) { lista_var->insert((*it),decl_var->GetTipo() ); } return lista_var; }
// ParseNamedVariableDeclaration //------------------------------------------------------------------------------ bool BFFParser::ParseNamedVariableDeclaration( BFFIterator & iter ) { // skip over the declaration symbol ASSERT( *iter == BFF_DECLARE_VAR_INTERNAL ); m_LastVarNameStart = iter; // include type token in var name iter++; // make sure we haven't hit the end of the file if ( iter.IsAtEnd() ) { Error::Error_1012_UnexpectedEndOfFile( iter ); return false; } // make sure immediately after the symbol starts a variable name if ( iter.IsAtValidVariableNameCharacter() == false ) { Error::Error_1013_UnexpectedCharInVariableName( iter, nullptr ); return false; } // find the end of the variable name iter.SkipVariableName(); if ( iter.IsAtEnd() ) { Error::Error_1012_UnexpectedEndOfFile( iter ); return false; } m_LastVarNameEnd = iter; // sanity check it is a sensible length size_t varNameLen = m_LastVarNameStart.GetDistTo( m_LastVarNameEnd ); if ( varNameLen > MAX_VARIABLE_NAME_LENGTH ) { Error::Error_1014_VariableNameIsTooLong( iter, (uint32_t)varNameLen, (uint32_t)MAX_VARIABLE_NAME_LENGTH ); return false; } // find the start of the assignment iter.SkipWhiteSpaceAndComments(); if ( iter.IsAtEnd() ) { Error::Error_1012_UnexpectedEndOfFile( iter ); return false; } return ParseVariableDeclaration( iter, m_LastVarNameStart, m_LastVarNameEnd ); }
// ParseNamedVariableDeclaration //------------------------------------------------------------------------------ bool BFFParser::ParseNamedVariableDeclaration( BFFIterator & iter ) { const BFFIterator varNameStart( iter ); bool parentScope = false; if ( ParseVariableName( iter, m_LastVarName, parentScope ) == false ) { return false; // ParseVariableName() would have display an error } // find the start of the assignment iter.SkipWhiteSpaceAndComments(); if ( iter.IsAtEnd() ) { Error::Error_1012_UnexpectedEndOfFile( iter ); return false; } // check if points to a previous declaration in a parent scope const BFFVariable * parentVar = nullptr; m_LastVarFrame = ( parentScope ) ? BFFStackFrame::GetParentDeclaration( m_LastVarName, nullptr, parentVar ) : nullptr; if ( parentScope ) { // check if a parent definition exists if ( nullptr == m_LastVarFrame ) { Error::Error_1009_UnknownVariable( varNameStart, nullptr ); return false; } ASSERT( nullptr != parentVar ); // check if the parent definition is frozen if ( parentVar->Frozen() ) { Error::Error_1060_CantModifyFrozenVar( varNameStart, nullptr, parentVar ); return false; } } return ParseVariableDeclaration( iter, m_LastVarName, m_LastVarFrame ); }
ListaVariables* Sintactico::_ParseVariableDeclarationList() { ListaVariables* lista_var = new ListaVariables(); //Utilizamos Primero( VariableDeclarationList ) if ( proximo_token.GetTipo() == id ) { DeclaracionVariable* decl_var = ParseVariableDeclaration(); lista_var = _ParseVariableDeclarationList(); list<string> ids = decl_var->GetIds(); list<string>::iterator it; for ( it = ids.begin() ; it != ids.end() ; it++) { lista_var->insert((*it),decl_var->GetTipo() ); } } return lista_var; }