diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-10-30 02:24:12 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-10-30 02:24:12 +0800 |
| commit | 464313d02b76385d3a2e8809a4595afabca88dae (patch) | |
| tree | 6cbf81416b856ff15fdc4239de5e3624ef10b2cc /ricochet/src/parser | |
| parent | cf58b3b4dfa0dbbd0ca492484ca241b320c4ba9e (diff) | |
| parent | bdefdb301db843e87aee8c6c35808d295bb66ee5 (diff) | |
| download | rcc-project-464313d02b76385d3a2e8809a4595afabca88dae.tar.gz rcc-project-464313d02b76385d3a2e8809a4595afabca88dae.tar.xz | |
Diffstat (limited to 'ricochet/src/parser')
| -rw-r--r-- | ricochet/src/parser/html_lexer.cc | 119 | ||||
| -rw-r--r-- | ricochet/src/parser/tree_builder.cc | 85 |
2 files changed, 204 insertions, 0 deletions
diff --git a/ricochet/src/parser/html_lexer.cc b/ricochet/src/parser/html_lexer.cc new file mode 100644 index 0000000..4d70288 --- /dev/null +++ b/ricochet/src/parser/html_lexer.cc @@ -0,0 +1,119 @@ +/* + * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation. + * Copyright (C) 2026 Richard Qin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ricochet/parser/html_lexer.hh" + +#include <cstdint> +#include <string_view> + +namespace ricochet::parser { + +namespace { +enum class LexerState : std::uint8_t +{ + Data, // Reading plain text + TagOpen, // Just encountered '<' + TagName, // Reading the name of an opening tag (e.g., "div") + ClosingTagName, // Reading the name of a closing tag (e.g., "/div") + SwallowAttributes // Ignore attributes mode (ignore until encountering '>') +}; + +void emit_text( std::string& buffer, std::vector<HtmlToken>& tokens ) +{ + if ( buffer.empty() ) { + return; + } + + bool all_whitespace = true; + for ( const char c : buffer ) { + if ( !std::isspace( static_cast<unsigned char>( c ) ) ) { + all_whitespace = false; + break; + } + } + + if ( !all_whitespace ) { + tokens.emplace_back( TextToken { buffer } ); + } + buffer.clear(); +} + +} // namespace + +std::vector<HtmlToken> HtmlLexer::tokenize( // NOLINT(readability-convert-member-functions-to-static) + std::string_view html ) const +{ + std::vector<HtmlToken> tokens; + + LexerState state = LexerState::Data; + std::string buffer {}; + + for ( const char c : html ) { + switch ( state ) { + case LexerState::Data: + if ( c == '<' ) { + emit_text( buffer, tokens ); + state = LexerState::TagOpen; + } else { + buffer += c; + } + break; + case LexerState::TagOpen: + if ( c == '/' ) { + state = LexerState::ClosingTagName; + } else if ( std::isalpha( static_cast<unsigned char>( c ) ) ) { + buffer.push_back( c ); + state = LexerState::TagName; + } else { + buffer.push_back( '<' ); + buffer.push_back( c ); + state = LexerState::Data; + } + break; + case LexerState::TagName: + if ( c == '>' ) { + tokens.emplace_back( TagOpenToken { buffer } ); + buffer.clear(); + state = LexerState::Data; + } else { + buffer += c; + } + break; + case LexerState::ClosingTagName: + if ( c == '>' ) { + tokens.emplace_back( TagCloseToken { buffer } ); + buffer.clear(); + state = LexerState::Data; + } else { + buffer += static_cast<char>( std::tolower( static_cast<unsigned char>( c ) ) ); + } + break; + case LexerState::SwallowAttributes: + if ( c == '>' ) { + state = LexerState::Data; + } + break; + } + } + + emit_text( buffer, tokens ); + + return tokens; +} + +} // namespace ricochet::parser
\ No newline at end of file diff --git a/ricochet/src/parser/tree_builder.cc b/ricochet/src/parser/tree_builder.cc new file mode 100644 index 0000000..64f841b --- /dev/null +++ b/ricochet/src/parser/tree_builder.cc @@ -0,0 +1,85 @@ +/* + * Ricochet - A minimalist CLI web browser for fast, distraction-free navigation. + * Copyright (C) 2026 Richard Qin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#include "ricochet/parser/tree_builder.hh" +#include "ricochet/parser/dom_tree.hh" +#include <utility> +#include <vector> + +namespace ricochet::parser { + +namespace { +struct TokenVisitor +{ + std::vector<DomNode>* stack; + + void operator()( const TextToken& token ) const + { + stack->back().children.emplace_back( + DomNode { .tag_name = "", .text_content = token.content, .children = {} } ); + } + + void operator()( const TagOpenToken& token ) const + { + const std::size_t space_pos = token.name.find( ' ' ); + const std::string base_name + = ( space_pos == std::string::npos ) ? token.name : token.name.substr( 0, space_pos ); + + DomNode new_node { .tag_name = token.name, .text_content = "", .children = {} }; + + if ( base_name == "meta" || base_name == "link" || base_name == "img" || base_name == "br" || base_name == "hr" + || base_name == "input" || base_name == "base" || base_name == "col" ) { + + stack->back().children.push_back( std::move( new_node ) ); + } else { + stack->push_back( std::move( new_node ) ); + } + } + + void operator()( const TagCloseToken& /*token*/ ) const + { + if ( stack->size() > 1 ) { + DomNode completed_node = std::move( stack->back() ); + stack->pop_back(); + stack->back().children.push_back( std::move( completed_node ) ); + } + } +}; +} // namespace + +DomNode TreeBuilder::build( // NOLINT(readability-convert-member-functions-to-static) + const std::vector<HtmlToken>& tokens ) const +{ + std::vector<DomNode> stack; + stack.emplace_back( DomNode { .tag_name = "Document", .text_content = "", .children = {} } ); + + TokenVisitor visitor { &stack }; + for ( const auto& token : tokens ) { + std::visit( visitor, token ); + } + + while ( stack.size() > 1 ) { + DomNode completed_node = std::move( stack.back() ); + stack.pop_back(); + stack.back().children.push_back( std::move( completed_node ) ); + } + + return std::move( stack.front() ); +} + +} // namespace ricochet::parser
\ No newline at end of file |
