struct hmap *dummies,
struct string *outputs, size_t n_outputs)
{
- struct segmenter segmenter;
-
- segmenter_init (&segmenter, mode);
-
+ struct segmenter segmenter = segmenter_init (mode, false);
while (!ss_is_empty (s))
{
enum segment_type type;
int i;
i = 0;
- string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE);
+ string_lexer_init (&slex, s, strlen (s), SEG_MODE_INTERACTIVE, true);
while (string_lexer_next (&slex, &token))
if (token.type != SCAN_SKIP)
{
src->journal_pos = src->seg_pos = src->line_pos = 0;
src->n_newlines = 0;
src->suppress_next_newline = false;
- segmenter_init (&src->segmenter, segmenter_get_mode (&src->segmenter));
+ src->segmenter = segmenter_init (segmenter_get_mode (&src->segmenter),
+ false);
while (!deque_is_empty (&src->deque))
lex_source_pop__ (src);
lex_source_push_endcmd__ (src);
static struct lex_source *
lex_source_create (struct lex_reader *reader)
{
- struct lex_source *src;
-
- src = xzalloc (sizeof *src);
- src->reader = reader;
- segmenter_init (&src->segmenter, reader->syntax);
- src->tokens = deque_init (&src->deque, 4, sizeof *src->tokens);
+ struct lex_source *src = xmalloc (sizeof *src);
+ *src = (struct lex_source) {
+ .reader = reader,
+ .segmenter = segmenter_init (reader->syntax, false),
+ .tokens = deque_init (&src->deque, 4, sizeof *src->tokens),
+ };
lex_source_push_endcmd__ (src);
INPUT must not be modified or freed while SLEX is still in use. */
void
string_lexer_init (struct string_lexer *slex, const char *input, size_t length,
- enum segmenter_mode mode)
+ enum segmenter_mode mode, bool is_snippet)
{
- slex->input = input;
- slex->length = length;
- slex->offset = 0;
- segmenter_init (&slex->segmenter, mode);
+ *slex = (struct string_lexer) {
+ .input = input,
+ .length = length,
+ .offset = 0,
+ .segmenter = segmenter_init (mode, is_snippet),
+ };
}
/* */
};
void string_lexer_init (struct string_lexer *, const char *input,
- size_t length, enum segmenter_mode);
+ size_t length, enum segmenter_mode, bool is_snippet);
bool string_lexer_next (struct string_lexer *, struct token *);
#endif /* scan.h */
}
}
-/* Initializes S as a segmenter with the given syntax MODE.
+/* Returns a segmenter with the given syntax MODE.
+
+ If IS_SNIPPET is false, then the segmenter will parse as if it's being given
+ a whole file. This means, for example, that it will interpret - or + at the
+ beginning of the syntax as a separator between commands (since - or + at the
+ beginning of a line has this meaning).
+
+ If IS_SNIPPET is true, then the segmenter will parse as if it's being given
+ an isolated piece of syntax. This means that, for example, that it will
+ interpret - or + at the beginning of the syntax as an operator token or (if
+ followed by a digit) as part of a number.
A segmenter does not contain any external references, so nothing needs to be
done to destroy one. For the same reason, segmenters may be copied with
plain struct assignment (or memcpy). */
-void
-segmenter_init (struct segmenter *s, enum segmenter_mode mode)
+struct segmenter
+segmenter_init (enum segmenter_mode mode, bool is_snippet)
{
- s->state = S_SHBANG;
- s->substate = 0;
- s->mode = mode;
+ return (struct segmenter) {
+ .state = is_snippet ? S_GENERAL : S_SHBANG,
+ .mode = mode,
+ };
}
/* Returns the mode passed to segmenter_init() for S. */
unsigned char mode;
};
-void segmenter_init (struct segmenter *, enum segmenter_mode);
+struct segmenter segmenter_init (enum segmenter_mode, bool is_snippet);
enum segmenter_mode segmenter_get_mode (const struct segmenter *);
length--;
}
- string_lexer_init (&slex, input, length, mode);
+ string_lexer_init (&slex, input, length, mode, false);
do
{
struct token token;
static void
check_segmentation (const char *input, size_t length, bool print_segments)
{
- struct segmenter s;
- segmenter_init (&s, mode);
+ struct segmenter s = segmenter_init (mode, false);
size_t line_number = 1;
size_t line_offset = 0;