Function: parse-partial-sexp

parse-partial-sexp is a function defined in syntax.c.

Signature

(parse-partial-sexp FROM TO &optional TARGETDEPTH STOPBEFORE OLDSTATE COMMENTSTOP)

Documentation

Parse Lisp syntax starting at FROM until TO; return status of parse at TO.

Parsing stops at TO or when certain criteria are met;
 point is set to where parsing stops.

If OLDSTATE is omitted or nil, parsing assumes that FROM is the
 beginning of a function. If not, OLDSTATE should be the state at
 FROM.

Value is a list of elements describing final state of parsing:
 0. depth in parens.
 1. character address of start of innermost containing list; nil if none.
 2. character address of start of last complete sexp terminated.
 3. non-nil if inside a string.
    (it is the character that will terminate the string,
     or t if the string should be terminated by a generic string delimiter.)
 4. nil if outside a comment, t if inside a non-nestable comment,
    else an integer (the current comment nesting).
 5. t if following a quote character.
 6. the minimum paren-depth encountered during this scan.
 7. style of comment, if any.
 8. character address of start of comment or string; nil if not in one.
 9. List of positions of currently open parens, outermost first.
10. When the last position scanned holds the first character of a
    (potential) two character construct, the syntax of that position,
    otherwise nil. That construct can be a two character comment
    delimiter or an Escaped or Char-quoted character.
11..... Possible further internal information used by parse-partial-sexp.

If third arg TARGETDEPTH is non-nil, parsing stops if the depth in parentheses becomes equal to TARGETDEPTH. Fourth arg STOPBEFORE non-nil means stop when we come to
 any character that starts a sexp.
Fifth arg OLDSTATE is a list like what this function returns.
 It is used to initialize the state of the parse. Elements number 1, 2, 6
 are ignored.
Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
 If it is the symbol syntax-table, stop after the start of a comment or a
 string, or after end of a comment or a string.

View in manual

Probably introduced at or before Emacs version 20.1.

Source Code

// Defined in /usr/src/emacs/src/syntax.c
{
  struct lisp_parse_state state;
  EMACS_INT target;

  if (!NILP (targetdepth))
    {
      CHECK_FIXNUM (targetdepth);
      target = XFIXNUM (targetdepth);
    }
  else
    target = TYPE_MINIMUM (EMACS_INT);	/* We won't reach this depth.  */

  if (fix_position (to) < fix_position (from))
    error ("End position is smaller than start position");

  validate_region (&from, &to);
  internalize_parse_state (oldstate, &state);
  scan_sexps_forward (&state, XFIXNUM (from), CHAR_TO_BYTE (XFIXNUM (from)),
		      XFIXNUM (to),
		      target, !NILP (stopbefore),
		      (NILP (commentstop)
		       ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));

  SET_PT_BOTH (state.location, state.location_byte);

  return
    Fcons (make_fixnum (state.depth),
	   Fcons (state.prevlevelstart < 0
		  ? Qnil : make_fixnum (state.prevlevelstart),
	     Fcons (state.thislevelstart < 0
		    ? Qnil : make_fixnum (state.thislevelstart),
	       Fcons (state.instring >= 0
		      ? (state.instring == ST_STRING_STYLE
			 ? Qt : make_fixnum (state.instring)) : Qnil,
		 Fcons (state.incomment < 0 ? Qt :
			(state.incomment == 0 ? Qnil :
			 make_fixnum (state.incomment)),
		   Fcons (state.quoted ? Qt : Qnil,
		     Fcons (make_fixnum (state.mindepth),
		       Fcons ((state.comstyle
			       ? (state.comstyle == ST_COMMENT_STYLE
				  ? Qsyntax_table
				  : make_fixnum (state.comstyle))
			       : Qnil),
                         Fcons (((state.incomment
                                  || (state.instring >= 0))
                                 ? make_fixnum (state.comstr_start)
                                 : Qnil),
			   Fcons (state.levelstarts,
                             Fcons (state.prev_syntax == Smax
                                    ? Qnil
                                    : make_fixnum (state.prev_syntax),
                                Qnil)))))))))));
}