Skip to content

Commit

Permalink
fix(Nodlang): suffix/prefix_append
Browse files Browse the repository at this point in the history
  • Loading branch information
berdal84 committed Nov 2, 2024
1 parent 0da4f2f commit e82307a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/ndbl/core/Token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,28 @@ void Token::word_replace(const char* new_word)

void Token::prefix_push_front(const char* str)
{
const size_t str_len = strlen(str);
m_buffer.switch_to_intern_buf_with_data(begin(), length());
if (!m_buffer.intern())
{
size_t size = length();
char* data = begin();
m_buffer.switch_to_intern_buf(size);
m_buffer.intern_buf->append( data, size );
}
m_buffer.intern_buf->insert(0, str);
m_prefix_len += str_len;
m_prefix_len += strlen(str);
}

void Token::suffix_push_back(const char* str)
{
const size_t str_len = strlen(str);
m_buffer.switch_to_intern_buf_with_data(begin(), length());
if (!m_buffer.intern())
{
size_t size = length();
char* data = begin();
m_buffer.switch_to_intern_buf(size);
m_buffer.intern_buf->append( data, size );
}
m_buffer.intern_buf->append(str);
m_suffix_len += str_len;
m_suffix_len += strlen(str);
}

void Token::prefix_reset(size_t size )
Expand Down Expand Up @@ -297,12 +307,6 @@ void Token::BimodalBuffer::switch_to_intern_buf(size_t size)
intern_buf->reserve(size);
}

void Token::BimodalBuffer::switch_to_intern_buf_with_data(char* data, size_t size)
{
switch_to_intern_buf(size);
intern_buf->append( data, size );
}


void Token::BimodalBuffer::delete_intern_buf()
{
Expand Down
2 changes: 1 addition & 1 deletion src/ndbl/core/Token.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace ndbl
~BimodalBuffer();
void delete_intern_buf();
void switch_to_intern_buf(size_t size);
void switch_to_intern_buf_with_data(char *data, size_t len);

char* data() const { return intern() ? intern_buf->data() : extern_buf; }
char* begin() const { return data() + offset; }
bool intern() const { return _flags & BimodalBuffer::Flags_INTERN; }
Expand Down
19 changes: 10 additions & 9 deletions src/ndbl/core/language/Nodlang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,18 +572,19 @@ Optional<Node*> Nodlang::parse_program()
// Create an entry point and push its scope
Node* entry_point = parser_state.graph()->create_entry_point();

// Parse main code block
parser_state.push_scope( entry_point->inner_scope() );
Optional<Node*> block = parse_code_block( entry_point->inner_scope() );
parser_state.pop_scope();


// To preserve any ignored characters stored in the global token
// we put the prefix and suffix in resp. token_begin and end.
Token& tok = parser_state.tokens().global_token();
std::string prefix = tok.prefix_to_string();
std::string suffix = tok.suffix_to_string();
entry_point->inner_scope()->token_begin = {Token_t::ignore, prefix };
entry_point->inner_scope()->token_end = {Token_t::ignore, suffix };

// Parse main code block
parser_state.push_scope( entry_point->inner_scope() );
Optional<Node*> block = parse_code_block( entry_point->inner_scope() );
parser_state.pop_scope();
entry_point->inner_scope()->token_begin.prefix_push_front( prefix.c_str() );
entry_point->inner_scope()->token_end.suffix_push_back( suffix.c_str() );

if ( block )
{
Expand Down Expand Up @@ -1732,8 +1733,8 @@ std::string &Nodlang::serialize_token(std::string& _out, const Token& _token) co

std::string& Nodlang::serialize_graph(std::string &_out, const Graph* graph ) const
{
if ( const Node* root = graph->root().get() )
serialize_node(_out, root, SerializeFlag_RECURSE);
if ( const Scope* scope = graph->root()->inner_scope() )
serialize_scope(_out, scope);
else
LOG_ERROR("Serializer", "a root child_node is expected to serialize the graph\n");
return _out;
Expand Down

0 comments on commit e82307a

Please sign in to comment.