Mercurial > hg > octave-nkf
diff libinterp/parse-tree/lex.ll @ 16098:24b3800d30e7
move octave_read to lex.ll
* lex.ll (octave_read): Move here from input.cc and make static.
Return YY_NULL when no more characters are available. Error here if
eof is not set when no more characters are available instead of in
YY_INPUT macro.
* input.h, input.cc (get_user_input): Now extern.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 25 Feb 2013 01:04:15 -0500 |
parents | 6437fa7263dd |
children | 6b26e18d1dcb |
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.ll +++ b/libinterp/parse-tree/lex.ll @@ -103,8 +103,7 @@ #undef YY_INPUT #endif #define YY_INPUT(buf, result, max_size) \ - if ((result = octave_read (buf, max_size)) < 0) \ - YY_FATAL_ERROR ("octave_read () in flex scanner failed"); + result = octave_read (buf, max_size) // Try to avoid crashing out completely on fatal scanner errors. // The call to yy_fatal_error should never happen, but it avoids a @@ -322,6 +321,7 @@ static void scan_for_comments (const char *); static yum_yum eat_whitespace (void); static yum_yum eat_continuation (void); +static int octave_read (char *buf, unsigned int max_size); static void maybe_warn_separator_insert (char sep); static void gripe_single_quote_string (void); static void gripe_matlab_incompatible (const std::string& msg); @@ -3550,6 +3550,70 @@ BEGIN (FUNCTION_FILE_BEGIN); } +static int +octave_read (char *buf, unsigned max_size) +{ + static const char * const eol = "\n"; + static std::string input_buf; + static const char *pos = 0; + static size_t chars_left = 0; + static bool eof = false; + + int status = 0; + + if (chars_left == 0) + { + pos = 0; + + input_buf = get_user_input (eof); + + chars_left = input_buf.length (); + + pos = input_buf.c_str (); + } + + if (chars_left > 0) + { + size_t len = max_size > chars_left ? chars_left : max_size; + assert (len > 0); + + memcpy (buf, pos, len); + + chars_left -= len; + pos += len; + + // Make sure input ends with a new line character. + if (chars_left == 0 && buf[len-1] != '\n') + { + if (len < max_size) + { + // There is enough room to plug the newline character in + // the buffer. + buf[len++] = '\n'; + } + else + { + // There isn't enough room to plug the newline character + // in the buffer so make sure it is returned on the next + // octave_read call. + pos = eol; + chars_left = 1; + } + } + + status = len; + } + else + { + status = YY_NULL; + + if (! eof) + YY_FATAL_ERROR ("octave_read () in flex scanner failed"); + } + + return status; +} + static void maybe_warn_separator_insert (char sep) {