changeset 29835:5dec8ee96c0c

(back_comment): Simplify string-parity counting (with the added benefit of handling multiple string-styles as long as they are not nested). Jump to the slow code as soon as a comment starter is found in a "string_lossage" position. Fixes the case: " /* " /* " */.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Wed, 21 Jun 2000 14:56:24 +0000
parents fb20d35ffa9a
children f3aaf610f8a9
files src/syntax.c
diffstat 1 files changed, 32 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/src/syntax.c	Wed Jun 21 14:26:56 2000 +0000
+++ b/src/syntax.c	Wed Jun 21 14:56:24 2000 +0000
@@ -467,8 +467,7 @@
      OFROM[I] is position of the earliest comment-starter seen
      which is I+2X quotes from the comment-end.
      PARITY is current parity of quotes from the comment end.  */
-  int parity = 0;
-  int my_stringend = 0;
+  int string_style = -1;	/* Presumed outside of any string. */
   int string_lossage = 0;
   int comment_end = from;
   int comment_end_byte = from_byte;
@@ -476,7 +475,6 @@
   int comstart_byte;
   /* Value that PARITY had, when we reached the position
      in COMSTART_POS.  */
-  int comstart_parity = 0;
   int scanstart = from - 1;
   /* Place where the containing defun starts,
      or 0 if we didn't come across it yet.  */
@@ -530,34 +528,33 @@
 	continue;
 
       /* Track parity of quotes.  */
-      if (code == Sstring)
-	{
-	  parity ^= 1;
-	  if (my_stringend == 0)
-	    my_stringend = c;
-	  /* If we have two kinds of string delimiters.
-	     There's no way to grok this scanning backwards.  */
-	  else if (my_stringend != c)
-	    string_lossage = 1;
-	}
-
-      if (code == Sstring_fence || code == Scomment_fence)
+      switch (code)
 	{
-	  parity ^= 1;
-	  if (my_stringend == 0)
-	    my_stringend
-	      = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
-	  /* If we have two kinds of string delimiters.
-	     There's no way to grok this scanning backwards.  */
-	  else if (my_stringend != (code == Sstring_fence 
-				    ? ST_STRING_STYLE : ST_COMMENT_STYLE))
+	case Sstring_fence:
+	case Scomment_fence:
+	  c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
+	case Sstring:
+	  /* Track parity of quotes.  */
+	  if (string_style == -1)
+	    /* Entering a string.  */
+	    string_style = c;
+	  else if (string_style == c)
+	    /* Leaving the string.  */
+	    string_style = -1;
+	  else
+	    /* If we have two kinds of string delimiters.
+	       There's no way to grok this scanning backwards.  */
 	    string_lossage = 1;
-	}
+	  break;
+	  
+	case Scomment:
+	  /* We've already checked that it is the relevant comstyle.  */
+	  if (string_style != -1 || string_lossage)
+	    /* There are odd string quotes involved, so let's be careful.
+	       Test case in Pascal: " { " a { " } */
+	    goto lossage;
 
-      if (code == Scomment)
-	/* We've already checked that it is the relevant comstyle.  */
-	{
-	  if (comnested && --nesting <= 0 && parity == 0 && !string_lossage)
+	  if (comnested && --nesting <= 0)
 	    /* nested comments have to be balanced, so we don't need to
 	       keep looking for earlier ones.  We use here the same (slightly
 	       incorrect) reasoning as below:  since it is followed by uniform
@@ -567,9 +564,12 @@
 
 	  /* Record comment-starters according to that
 	     quote-parity to the comment-end.  */
-	  comstart_parity = parity;
 	  comstart_pos = from;
 	  comstart_byte = from_byte;
+	  break;
+
+	default:
+	  ;
 	}
 
       /* If we find another earlier comment-ender,
@@ -607,7 +607,7 @@
      we know it can't be inside a string
      since if it were then the comment ender would be inside one.
      So it does start a comment.  Skip back to it.  */
-  else if (!comnested && comstart_parity == 0 && !string_lossage) 
+  else if (!comnested)
     {
       from = comstart_pos;
       from_byte = comstart_byte;
@@ -615,12 +615,13 @@
     }
   else
     {
+      struct lisp_parse_state state;
+    lossage:
       /* We had two kinds of string delimiters mixed up
 	 together.  Decode this going forwards.
 	 Scan fwd from the previous comment ender
 	 to the one in question; this records where we
 	 last passed a comment starter.  */
-      struct lisp_parse_state state;
       /* If we did not already find the defun start, find it now.  */
       if (defun_start == 0)
 	{