Skip to content

Commit

Permalink
Only valid groupings are highlighted
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDTill committed Aug 5, 2023
1 parent b6c77a1 commit 10d1224
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
42 changes: 31 additions & 11 deletions src/forscape_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <code_parsenode_ops.h>
#include <forscape_common.h>
#include <forscape_program.h>
#include <forscape_unicode.h>
#include "typeset_construct.h"
#include "typeset_model.h"

Expand Down Expand Up @@ -59,21 +60,43 @@ void Parser::reset() noexcept {
loops = 0;
}

static constexpr uint64_t groupingPairingCode(uint32_t left, uint32_t right) noexcept {
return static_cast<uint64_t>(left) | (static_cast<uint64_t>(right) << 32uLL);
}

static constexpr std::array<uint64_t, 9> RECOGNISED_GROUPINGS = {
groupingPairingCode(codepointInt(std::string_view("(")), codepointInt(std::string_view(")"))),
groupingPairingCode(codepointInt(std::string_view("[")), codepointInt(std::string_view("]"))),
groupingPairingCode(codepointInt(std::string_view("{")), codepointInt(std::string_view("}"))),
groupingPairingCode(codepointInt(std::string_view("|")), codepointInt(std::string_view("|"))),
groupingPairingCode(codepointInt(std::string_view("")), codepointInt(std::string_view(""))),
groupingPairingCode(codepointInt(std::string_view("")), codepointInt(std::string_view(""))),
groupingPairingCode(codepointInt(std::string_view("")), codepointInt(std::string_view(""))),
groupingPairingCode(codepointInt(std::string_view("")), codepointInt(std::string_view(""))),
groupingPairingCode(codepointInt(std::string_view("")), codepointInt(std::string_view(""))),
};

void Parser::registerGrouping(const Typeset::Selection& sel) alloc_except {
registerGrouping(sel.left, sel.right);
}

void Parser::registerGrouping(const Typeset::Marker& l, const Typeset::Marker& r) alloc_except {
#ifndef FORSCAPE_TYPESET_HEADLESS
//DO THIS: should have a list of allowed pairings, and assert one is used here
if(l.atTextEnd() || r.atTextStart()) return;

const uint64_t grouping_code = groupingPairingCode(l.codepointRight(), r.codepointLeft());
const bool validGrouping = RECOGNISED_GROUPINGS.cend() != std::find(
RECOGNISED_GROUPINGS.cbegin(), RECOGNISED_GROUPINGS.cend(), grouping_code);

if(!validGrouping) return;
open_symbols[l] = r;
close_symbols[r] = l;
#endif
}

ParseNode Parser::checkedStatement() alloc_except {
ParseNode n = statement();
if(!noErrors()) recover(); //DO THIS: what is error recovery?
if(!noErrors()) recover();
return n;
}

Expand Down Expand Up @@ -364,7 +387,7 @@ ParseNode Parser::blockStatement() alloc_except {
}

Typeset::Selection sel(left, rMarkPrev());
if(noErrors()) registerGrouping(sel);
registerGrouping(sel);

return parse_tree.finishNary(OP_BLOCK, sel);
}
Expand All @@ -381,7 +404,7 @@ ParseNode Parser::lexicalScopeStatement() alloc_except {
}

Typeset::Selection sel(left, rMarkPrev());
if(noErrors()) registerGrouping(sel);
registerGrouping(sel);

return parse_tree.finishNary(OP_LEXICAL_SCOPE, sel);
}
Expand Down Expand Up @@ -1112,7 +1135,7 @@ ParseNode Parser::braceGrouping() alloc_except {
Typeset::Marker right = rMark();
Typeset::Selection sel(left, right);
consume(RIGHTBRACE);
if(noErrors()) registerGrouping(sel);
registerGrouping(sel);
return parse_tree.finishNary(OP_LIST, sel);
}else{
//It's an interval
Expand Down Expand Up @@ -1160,10 +1183,8 @@ ParseNode Parser::parenGrouping() alloc_except {
}
} while(!peek(RIGHTPAREN) && noErrors());
Typeset::Selection sel(left, rMark());
if(noErrors()){
registerGrouping(sel);
advance();
}
registerGrouping(sel);
if(noErrors()) advance();

ParseNode list = parse_tree.finishNary(OP_LIST, sel);

Expand Down Expand Up @@ -1256,7 +1277,7 @@ ParseNode Parser::set() alloc_except {
consume(RIGHTBRACKET);

Typeset::Selection sel(left, rMarkPrev());
if(noErrors()) registerGrouping(sel);
registerGrouping(sel);
return parse_tree.addNode<2>(OP_SET_BUILDER, sel, {first_element, predicate});
}else{
//Enumerated set
Expand Down Expand Up @@ -1674,7 +1695,6 @@ ParseNode Parser::superscript(ParseNode lhs, Typeset::Marker left) alloc_except
parse_tree.setFlag(n, TYPESET_RATHER_THAN_CARET);
}

//DO THIS: this will result in invalid selections (read: crashes)
consume(ARGCLOSE);

return n;
Expand Down
22 changes: 21 additions & 1 deletion src/forscape_unicode.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ inline constexpr bool isPathChar(char ch) noexcept {
return ch != ' ';
}

static uint32_t expand(char ch) noexcept {
static constexpr uint32_t expand(char ch) noexcept {
return static_cast<uint32_t>(static_cast<uint8_t>(ch));
}

Expand All @@ -96,6 +96,26 @@ inline uint32_t codepointInt(StringType str, size_t index = 0) noexcept {
}
}

inline constexpr uint32_t codepointInt(std::string_view str) noexcept {
uint8_t ch = str[0];

if(isAscii(ch)){
return ch;
}else if(sixthBitUnset(ch)){
uint32_t bit2 = expand(str[1]) << 8;
return ch | bit2;
}else if(fifthBitUnset(ch)){
uint32_t bit2 = expand(str[1]) << 8;
uint32_t bit3 = expand(str[2]) << 16;
return ch | bit2 | bit3;
}else{
uint32_t bit2 = expand(str[1]) << 8;
uint32_t bit3 = expand(str[2]) << 16;
uint32_t bit4 = expand(str[3]) << 24;
return ch | bit2 | bit3 | bit4;
}
}

inline bool isSingleCodepoint(const std::string& str) noexcept {
if(str.empty()) return false;
return codepointSize(str[0]) == (str.size() - (str.back() == '\0'));
Expand Down
5 changes: 5 additions & 0 deletions src/typeset_marker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ uint32_t Marker::codepointLeft() const noexcept {
return m.scanGlyph();
}

uint32_t Marker::codepointRight() const noexcept {
Marker m = *this;
return m.scanGlyph();
}

bool Marker::onlySpacesLeft() const noexcept {
if(!atFirstTextInPhrase()) return false;
size_t i = index;
Expand Down
1 change: 1 addition & 0 deletions src/typeset_marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct Marker{
size_t countSpacesLeft() const noexcept;
std::string_view checkKeyword() const noexcept;
uint32_t codepointLeft() const noexcept;
uint32_t codepointRight() const noexcept;
bool onlySpacesLeft() const noexcept;
std::string_view strRight() const noexcept;
std::string_view strLeft() const noexcept;
Expand Down

0 comments on commit 10d1224

Please sign in to comment.