Teach the tokenizer to report escaped newlines

Add fields and flags so that escaped newlines can be reported, for the
benefit of fish_indent.
This commit is contained in:
ridiculousfish
2018-05-07 15:22:09 -07:00
parent 678fd86107
commit 6f57fef8f8
4 changed files with 44 additions and 24 deletions

View File

@@ -28,15 +28,18 @@ constexpr source_offset_t SOURCE_OFFSET_INVALID = static_cast<source_offset_t>(-
/// A struct representing the token type that we use internally.
struct parse_token_t {
enum parse_token_type_t type; // The type of the token as represented by the parser
enum parse_keyword_t keyword; // Any keyword represented by this token
bool has_dash_prefix; // Hackish: whether the source contains a dash prefix
bool is_help_argument; // Hackish: whether the source looks like '-h' or '--help'
bool is_newline; // Hackish: if TOK_END, whether the source is a newline.
source_offset_t source_start;
source_offset_t source_length;
enum parse_keyword_t keyword{parse_keyword_none}; // Any keyword represented by this token
bool has_dash_prefix{false}; // Hackish: whether the source contains a dash prefix
bool is_help_argument{false}; // Hackish: whether the source looks like '-h' or '--help'
bool is_newline{false}; // Hackish: if TOK_END, whether the source is a newline.
bool preceding_escaped_nl{false}; // Whether there was an escaped newline preceding this token.
source_offset_t source_start{SOURCE_OFFSET_INVALID};
source_offset_t source_length{0};
wcstring describe() const;
wcstring user_presentable_description() const;
constexpr parse_token_t(parse_token_type_t type) : type(type) {}
};
enum {
@@ -66,6 +69,11 @@ const wchar_t *keyword_description(parse_keyword_t type);
enum {
/// Flag indicating that the node has associated comment nodes.
parse_node_flag_has_comments = 1 << 0,
/// Flag indicating that the token was preceded by an escaped newline, e.g.
/// echo abc | \
/// cat
parse_node_flag_preceding_escaped_nl = 1 << 1,
};
typedef uint8_t parse_node_flags_t;
@@ -123,7 +131,12 @@ class parse_node_t {
/// Indicate if the node has comment nodes.
bool has_comments() const {
return static_cast<bool>(this->flags & parse_node_flag_has_comments);
return this->flags & parse_node_flag_has_comments;
}
/// Indicates if we have a preceding escaped newline.
bool has_preceding_escaped_newline() const {
return this->flags & parse_node_flag_preceding_escaped_nl;
}
/// Gets source for the node, or the empty string if it has no source.