# HG changeset patch # User John W. Eaton # Date 1386994847 18000 # Fri Dec 13 23:20:47 2013 -0500 # Branch stable # Node ID a1d3fc8ea9bb74fb72bbc410b5f32bbde08863e1 # Parent 25e87795f437a14408824bf9745d5ac5bd2d3224 fix block comments for files with CRLF line endings (bug #40894) * lex.ll (looks_like_block_comment_start): New static function. Merge "^{S}*{CCHAR}\{{S}*{NL}" and "{S}*{CCHAR}.*{NL}" patterns. diff --git a/libinterp/parse-tree/lex.ll b/libinterp/parse-tree/lex.ll --- a/libinterp/parse-tree/lex.ll +++ b/libinterp/parse-tree/lex.ll @@ -245,6 +245,8 @@ static std::string strip_trailing_whitespace (char *s); +static bool looks_like_block_comment_start (const std::string& s); + %} D [0-9] @@ -470,28 +472,32 @@ %} %{ -// Start of a block comment. If the comment marker appears immediately +// Start of a comment. If a block comment marker appears immediately // after a block of full-line comments, finish the full line comment // block. %} -^{S}*{CCHAR}\{{S}*{NL} { - curr_lexer->lexer_debug ("^{S}*{CCHAR}\\{{S}*{NL}"); +{S}*{CCHAR}.*{NL} { + curr_lexer->lexer_debug ("{S}*{CCHAR}.*{NL}"); + + if (YY_AT_BOL () && looks_like_block_comment_start (yytext)) + { + if (curr_lexer->start_state () == LINE_COMMENT_START) + { + if (! curr_lexer->comment_text.empty ()) + curr_lexer->finish_comment (octave_comment_elt::full_line); + + curr_lexer->pop_start_state (); + } + + curr_lexer->decrement_promptflag (); + + curr_lexer->push_start_state (BLOCK_COMMENT_START); + } + else + curr_lexer->push_start_state (LINE_COMMENT_START); yyless (0); - - if (curr_lexer->start_state () == LINE_COMMENT_START) - { - if (! curr_lexer->comment_text.empty ()) - curr_lexer->finish_comment (octave_comment_elt::full_line); - - curr_lexer->pop_start_state (); - } - - curr_lexer->decrement_promptflag (); - - curr_lexer->push_start_state (BLOCK_COMMENT_START); - } ^{S}*{CCHAR}\{{S}*{NL} { @@ -549,13 +555,6 @@ // Full-line or end-of-line comment. %} -{S}*{CCHAR}.*{NL} { - curr_lexer->lexer_debug ("{S}*{CCHAR}.*{NL}"); - - curr_lexer->push_start_state (LINE_COMMENT_START); - yyless (0); - } - {S}*{CCHAR}.*{NL} { curr_lexer->lexer_debug ("{S}*{CCHAR}.*{NL}"); @@ -1942,6 +1941,55 @@ } static bool +looks_like_block_comment_start (const std::string& s) +{ + bool retval = false; + + size_t len = s.length (); + + size_t offset = 0; + + // {S}* + while (offset < len && (s[offset] == ' ' || s[offset] == '\t')) + offset++; + + if (offset < len) + { + char c = s[offset++]; + + // {CCHAR} + if (offset < len && (c == '%' || c == '#')) + { + c = s[offset++]; + + // \{ + if (offset < len && c == '{') + { + // {S}* + while (offset < len && (s[offset] == ' ' || s[offset] == '\t')) + offset++; + + // {NL} + if (s[offset] == '\n') + offset++; + else if (s[offset] == '\r') + { + offset++; + + if (offset < len && s[offset] == '\n') + offset++; + } + + if (offset == len) + retval = true; + } + } + } + + return retval; +} + +static bool looks_like_copyright (const std::string& s) { bool retval = false;