-static enum scan_result
-scan_dash__ (enum segment_type type, struct substring s, struct token *token)
-{
- switch (type)
- {
- case SEG_SPACES:
- case SEG_COMMENT:
- return SCAN_MORE;
-
- case SEG_NUMBER:
- token->type = T_NEG_NUM;
- token->number = -scan_number__ (s);
- return SCAN_DONE;
-
- default:
- token->type = T_DASH;
- return SCAN_BACK;
- }
-}
-
-/* Initializes SCANNER for scanning a token from a sequence of segments.
- Initializes TOKEN as the output token. (The client retains ownership of
- TOKEN, but it must be preserved across subsequent calls to scanner_push()
- for SCANNER.)
-
- A scanner only produces a single token. To obtain the next token,
- re-initialize it by calling this function again.
-
- A scanner does not contain any external references, so nothing needs to be
- done to destroy one. For the same reason, scanners may be copied with plain
- struct assignment (or memcpy). */
-void
-scanner_init (struct scanner *scanner, struct token *token)
-{
- scanner->state = S_START;
- *token = (struct token) { .type = T_STOP };
-}
-
-/* Adds the segment with type TYPE and UTF-8 text S to SCANNER. TOKEN must be
- the same token passed to scanner_init() for SCANNER, or a copy of it.
- scanner_push() may modify TOKEN. The client retains ownership of TOKEN,
-
- The possible return values are:
-
- - SCAN_DONE: All of the segments that have been passed to scanner_push()
- form the token now stored in TOKEN. SCANNER is now "used up" and must
- be reinitialized with scanner_init() if it is to be used again.
-
- Most tokens only consist of a single segment, so this is the most common
- return value.
-
- - SCAN_MORE: The segments passed to scanner_push() don't yet determine a
- token. The caller should call scanner_push() again with the next token.
- (This won't happen if TYPE is SEG_END indicating the end of input.)
-
- - SCAN_SAVE: This is similar to SCAN_MORE, with one difference: the caller
- needs to "save its place" in the stream of segments for a possible
- future SCAN_BACK return. This value can be returned more than once in a
- sequence of scanner_push() calls for SCANNER, but the caller only needs
- to keep track of the most recent position.
-
- - SCAN_BACK: This is similar to SCAN_DONE, but the token consists of only
- the segments up to and including the segment for which SCAN_SAVE was
- most recently returned. Segments following that one should be passed to
- the next scanner to be initialized.
-*/
-enum scan_result
-scanner_push (struct scanner *scanner, enum segment_type type,
- struct substring s, struct token *token)
-{
- switch (scanner->state)
- {
- case S_START:
- return scan_start__ (scanner, type, s, token);
-
- case S_DASH:
- return scan_dash__ (type, s, token);
-
- case S_STRING:
- return scan_string__ (scanner, type, s, token);
- }
-
- NOT_REACHED ();
-}