Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ namespace broma {
std::string inner; ///< The inline body of the function as a raw string.
};

/// @brief Comment field
struct CommentField {
std::string inner; ///< The comment content.
bool trailing = false; ///< Whether the comment is trailing (i.e., appears after code on the same line).
};

/// @brief A class field.
struct Field {
size_t field_id; ///< The index of the field. This starts from 0 and counts up across all classes.
std::string parent; ///< The name of the parent class.
std::variant<InlineField, FunctionBindField, PadField, MemberField> inner;
std::variant<InlineField, FunctionBindField, PadField, MemberField, CommentField> inner;

/// @brief Cast the field into a variant type. This is useful to extract data from the field.
template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion src/basic_components.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace broma {
>> {};

/// @brief Noisy filler grammar elements we want to ignore when parsing.
struct ignore : sor<comment, one<'\n', '\t', '\r', ' '>> {};
struct ignore : sor<one<'\n', '\t', '\r', ' '>> {};

/// @brief Separator (zero or more ignoreable elements).
struct sep : star<ignore> {};
Expand Down
34 changes: 33 additions & 1 deletion src/class.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,39 @@ namespace broma {
}
};

struct field : sor<inline_expr, pad_expr, member_expr, bind_expr> {};
struct comment_expr : sor<
seq<at<string<'/', '/'>, not_at<one<'/'>>>, until<eolf>>,
seq<string<'/', '*'>, until<seq<string<'*', '/'>>>>
> {};

template <>
struct run_action<comment_expr> {
template <typename T>
static void apply(T& input, Root* root, ScratchData* scratch) {
CommentField cf;
cf.inner = input.string();

auto start = input.begin();
auto end = input.input().begin();
while (start != end && *start != '\n') {
--start;
if (*start == '\n') {
break;
}
if (*start != ' ' && *start != '\t') {
cf.trailing = true;
break;
}
}

cf.inner.erase(0, cf.inner.find_first_not_of(" \t\n\r"));
cf.inner.erase(cf.inner.find_last_not_of(" \t\n\r") + 1);

scratch->wip_field.inner = std::move(cf);
}
};

struct field : sor<inline_expr, pad_expr, member_expr, bind_expr, comment_expr> {};

template <>
struct run_action<field> {
Expand Down