aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2006-12-17 22:02:52 +0000
committerRichard M. Stallman2006-12-17 22:02:52 +0000
commitfe963f844bbb6feaf626e1aa096c12e7681f603d (patch)
treed5e342a284e5439518512814dda664fa3d616996
parent07af30248aef20ee769333daf00f37fbeb1bbfce (diff)
downloademacs-fe963f844bbb6feaf626e1aa096c12e7681f603d.tar.gz
emacs-fe963f844bbb6feaf626e1aa096c12e7681f603d.zip
(Parsing Expressions): Split up node.
(Motion via Parsing, Position Parse, Parser State) (Low-Level Parsing, Control Parsing): New subnodes. (Parser State): Document syntax-ppss-toplevel-pos.
-rw-r--r--lispref/ChangeLog7
-rw-r--r--lispref/syntax.texi307
2 files changed, 182 insertions, 132 deletions
diff --git a/lispref/ChangeLog b/lispref/ChangeLog
index e2bfbf2680e..0e0079c73f3 100644
--- a/lispref/ChangeLog
+++ b/lispref/ChangeLog
@@ -1,5 +1,12 @@
12006-12-17 Richard Stallman <rms@gnu.org> 12006-12-17 Richard Stallman <rms@gnu.org>
2 2
3 * syntax.texi (Parsing Expressions): Split up node.
4 (Motion via Parsing, Position Parse, Parser State)
5 (Low-Level Parsing, Control Parsing): New subnodes.
6 (Parser State): Document syntax-ppss-toplevel-pos.
7
8 * positions.texi (List Motion): Punctuation fix.
9
3 * files.texi (File Name Completion): Document PREDICATE arg 10 * files.texi (File Name Completion): Document PREDICATE arg
4 to file-name-completion. 11 to file-name-completion.
5 12
diff --git a/lispref/syntax.texi b/lispref/syntax.texi
index 54b0d4a0bc0..4458547f7d2 100644
--- a/lispref/syntax.texi
+++ b/lispref/syntax.texi
@@ -597,26 +597,26 @@ expression prefix syntax class, and characters with the @samp{p} flag.
597@end defun 597@end defun
598 598
599@node Parsing Expressions 599@node Parsing Expressions
600@section Parsing Balanced Expressions 600@section Parsing Expressions
601 601
602 Here are several functions for parsing and scanning balanced 602 This section describes functions for parsing and scanning balanced
603expressions, also known as @dfn{sexps}. Basically, a sexp is either a 603expressions, also known as @dfn{sexps}. Basically, a sexp is either a
604balanced parenthetical grouping, or a symbol name (a sequence of 604balanced parenthetical grouping, a string, or a symbol name (a
605characters whose syntax is either word constituent or symbol 605sequence of characters whose syntax is either word constituent or
606constituent). However, characters whose syntax is expression prefix 606symbol constituent). However, characters whose syntax is expression
607are treated as part of the sexp if they appear next to it. 607prefix are treated as part of the sexp if they appear next to it.
608 608
609 The syntax table controls the interpretation of characters, so these 609 The syntax table controls the interpretation of characters, so these
610functions can be used for Lisp expressions when in Lisp mode and for C 610functions can be used for Lisp expressions when in Lisp mode and for C
611expressions when in C mode. @xref{List Motion}, for convenient 611expressions when in C mode. @xref{List Motion}, for convenient
612higher-level functions for moving over balanced expressions. 612higher-level functions for moving over balanced expressions.
613 613
614 A syntax table only describes how each character changes the state 614 A character's syntax controls how it changes the state of the
615of the parser, rather than describing the state itself. For example, 615parser, rather than describing the state itself. For example, a
616a string delimiter character toggles the parser state between 616string delimiter character toggles the parser state between
617``in-string'' and ``in-code'' but the characters inside the string do 617``in-string'' and ``in-code,'' but the syntax of characters does not
618not have any particular syntax to identify them as such. For example 618directly say whether they are inside a string. For example (note that
619(note that 15 is the syntax code for generic string delimiters), 61915 is the syntax code for generic string delimiters),
620 620
621@example 621@example
622(put-text-property 1 9 'syntax-table '(15 . nil)) 622(put-text-property 1 9 'syntax-table '(15 . nil))
@@ -627,46 +627,128 @@ does not tell Emacs that the first eight chars of the current buffer
627are a string, but rather that they are all string delimiters. As a 627are a string, but rather that they are all string delimiters. As a
628result, Emacs treats them as four consecutive empty string constants. 628result, Emacs treats them as four consecutive empty string constants.
629 629
630 Every time you use the parser, you specify it a starting state as 630@menu
631well as a starting position. If you omit the starting state, the 631* Motion via Parsing:: Motion functions that work by parsing.
632default is ``top level in parenthesis structure,'' as it would be at 632* Position Parse:: Determining the syntactic state of a position.
633the beginning of a function definition. (This is the case for 633* Parser State:: How Emacs represents a syntactic state.
634@code{forward-sexp}, which blindly assumes that the starting point is 634* Low-Level Parsing:: Parsing across a specified region.
635in such a state.) 635* Control Parsing:: Parameters that affect parsing.
636@end menu
636 637
637@defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment 638@node Motion via Parsing
638This function parses a sexp in the current buffer starting at 639@subsection Motion Commands Based on Parsing
639@var{start}, not scanning past @var{limit}. It stops at position
640@var{limit} or when certain criteria described below are met, and sets
641point to the location where parsing stops. It returns a value
642describing the status of the parse at the point where it stops.
643 640
644If @var{state} is @code{nil}, @var{start} is assumed to be at the top 641 This section describes simple point-motion functions that operate
645level of parenthesis structure, such as the beginning of a function 642based on parsing expressions.
646definition. Alternatively, you might wish to resume parsing in the
647middle of the structure. To do this, you must provide a @var{state}
648argument that describes the initial status of parsing.
649 643
650@cindex parenthesis depth 644@defun scan-lists from count depth
651If the third argument @var{target-depth} is non-@code{nil}, parsing 645This function scans forward @var{count} balanced parenthetical groupings
652stops if the depth in parentheses becomes equal to @var{target-depth}. 646from position @var{from}. It returns the position where the scan stops.
653The depth starts at 0, or at whatever is given in @var{state}. 647If @var{count} is negative, the scan moves backwards.
654 648
655If the fourth argument @var{stop-before} is non-@code{nil}, parsing 649If @var{depth} is nonzero, parenthesis depth counting begins from that
656stops when it comes to any character that starts a sexp. If 650value. The only candidates for stopping are places where the depth in
657@var{stop-comment} is non-@code{nil}, parsing stops when it comes to the 651parentheses becomes zero; @code{scan-lists} counts @var{count} such
658start of a comment. If @var{stop-comment} is the symbol 652places and then stops. Thus, a positive value for @var{depth} means go
659@code{syntax-table}, parsing stops after the start of a comment or a 653out @var{depth} levels of parenthesis.
660string, or the end of a comment or a string, whichever comes first. 654
655Scanning ignores comments if @code{parse-sexp-ignore-comments} is
656non-@code{nil}.
657
658If the scan reaches the beginning or end of the buffer (or its
659accessible portion), and the depth is not zero, an error is signaled.
660If the depth is zero but the count is not used up, @code{nil} is
661returned.
662@end defun
663
664@defun scan-sexps from count
665This function scans forward @var{count} sexps from position @var{from}.
666It returns the position where the scan stops. If @var{count} is
667negative, the scan moves backwards.
668
669Scanning ignores comments if @code{parse-sexp-ignore-comments} is
670non-@code{nil}.
671
672If the scan reaches the beginning or end of (the accessible part of) the
673buffer while in the middle of a parenthetical grouping, an error is
674signaled. If it reaches the beginning or end between groupings but
675before count is used up, @code{nil} is returned.
676@end defun
677
678@defun forward-comment count
679This function moves point forward across @var{count} complete comments
680 (that is, including the starting delimiter and the terminating
681delimiter if any), plus any whitespace encountered on the way. It
682moves backward if @var{count} is negative. If it encounters anything
683other than a comment or whitespace, it stops, leaving point at the
684place where it stopped. This includes (for instance) finding the end
685of a comment when moving forward and expecting the beginning of one.
686The function also stops immediately after moving over the specified
687number of complete comments. If @var{count} comments are found as
688expected, with nothing except whitespace between them, it returns
689@code{t}; otherwise it returns @code{nil}.
690
691This function cannot tell whether the ``comments'' it traverses are
692embedded within a string. If they look like comments, it treats them
693as comments.
694@end defun
695
696To move forward over all comments and whitespace following point, use
697@code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a good
698argument to use, because the number of comments in the buffer cannot
699exceed that many.
700
701@node Position Parse
702@subsection Finding the Parse State for a Position
703
704 For syntactic analysis, such as in indentation, often the useful
705thing is to compute the syntactic state corresponding to a given buffer
706position. This function does that conveniently.
707
708@defun syntax-ppss &optional pos
709This function returns the parser state (see next section) that the
710parser would reach at position @var{pos} starting from the beginning
711of the buffer. This is equivalent to @code{(parse-partial-sexp
712(point-min) @var{pos})}, except that @code{syntax-ppss} uses a cache
713to speed up the computation. Due to this optimization, the 2nd value
714(previous complete subexpression) and 6th value (minimum parenthesis
715depth) of the returned parser state are not meaningful.
716@end defun
717
718 @code{syntax-ppss} automatically hooks itself to
719@code{before-change-functions} to keep its cache consistent. But
720updating can fail if @code{syntax-ppss} is called while
721@code{before-change-functions} is temporarily let-bound, or if the
722buffer is modified without obeying the hook, such as when using
723@code{inhibit-modification-hooks}. For this reason, it is sometimes
724necessary to flush the cache manually.
725
726@defun syntax-ppss-flush-cache beg
727This function flushes the cache used by @code{syntax-ppss}, starting at
728position @var{beg}.
729@end defun
730
731 Major modes can make @code{syntax-ppss} run faster by specifying
732where it needs to start parsing.
661 733
662@cindex parse state 734@defvar syntax-begin-function
663The fifth argument @var{state} is a ten-element list of the same form 735If this is non-@code{nil}, it should be a function that moves to an
664as the value of this function, described below. The return value of 736earlier buffer position where the parser state is equivalent to
665one call may be used to initialize the state of the parse on another 737@code{nil}---in other words, a position outside of any comment,
666call to @code{parse-partial-sexp}. 738string, or parenthesis. @code{syntax-ppss} uses it to further
739optimize its computations, when the cache gives no help.
740@end defvar
741
742@node Parser State
743@subsection Parser State
744@cindex parser state
667 745
668The result is a list of ten elements describing the final state of 746 A @dfn{parser state} is a list of ten elements describing the final
669the parse: 747state of parsing text syntactically as part of an expression. The
748parsing functions in the following sections return a parser state as
749the value, and in some cases accept one as an argument also, so that
750you can resume parsing after it stops. Here are the meanings of the
751elements of the parser state:
670 752
671@enumerate 0 753@enumerate 0
672@item 754@item
@@ -721,82 +803,66 @@ data is subject to change; it is used if you pass this list
721as the @var{state} argument to another call. 803as the @var{state} argument to another call.
722@end enumerate 804@end enumerate
723 805
724Elements 1, 2, and 6 are ignored in the argument @var{state}. Element 806 Elements 1, 2, and 6 are ignored in a state which you pass as an
7258 is used only to set the corresponding element of the return value, 807argument to continue parsing, and elements 8 and 9 are used only in
726in certain simple cases. Element 9 is used only to set element 1 of 808trivial cases. Those elements serve primarily to convey information
727the return value, in trivial cases where parsing starts and stops 809to the Lisp program which does the parsing.
728within the same pair of parentheses.
729 810
730@cindex indenting with parentheses 811 One additional piece of useful information is available from a
731This function is most often used to compute indentation for languages 812parser state using this function:
732that have nested parentheses.
733@end defun
734 813
735@defun syntax-ppss &optional pos 814@defun syntax-ppss-toplevel-pos state
736This function returns the state that the parser would have at position 815This function extracts, from parser state @var{state}, the last
737@var{pos}, if it were started with a default start state at the 816position scanned in the parse which was at top level in grammatical
738beginning of the buffer. Thus, it is equivalent to 817structure. ``At top level'' means outside of any parentheses,
739@code{(parse-partial-sexp (point-min) @var{pos})}, except that 818comments, or strings.
740@code{syntax-ppss} uses a cache to speed up the computation. Also,
741the 2nd value (previous complete subexpression) and 6th value (minimum
742parenthesis depth) of the returned state are not meaningful.
743@end defun
744 819
745@defun syntax-ppss-flush-cache beg 820The value is @code{nil} if @var{state} represents a parse which has
746This function flushes the cache used by @code{syntax-ppss}, starting at 821arrived at a top level position.
747position @var{beg}.
748
749When @code{syntax-ppss} is called, it automatically hooks itself
750to @code{before-change-functions} to keep its cache consistent.
751But this can fail if @code{syntax-ppss} is called while
752@code{before-change-functions} is temporarily let-bound, or if the
753buffer is modified without obeying the hook, such as when using
754@code{inhibit-modification-hooks}. For this reason, it is sometimes
755necessary to flush the cache manually.
756@end defun 822@end defun
757 823
758@defvar syntax-begin-function 824 We have provided this access function rather than document how the
759If this is non-@code{nil}, it should be a function that moves to an 825data is represented in the state, because we plan to change the
760earlier buffer position where the parser state is equivalent to 826representation in the future.
761@code{nil}---in other words, a position outside of any comment,
762string, or parenthesis. @code{syntax-ppss} uses it to supplement its
763cache.
764@end defvar
765
766@defun scan-lists from count depth
767This function scans forward @var{count} balanced parenthetical groupings
768from position @var{from}. It returns the position where the scan stops.
769If @var{count} is negative, the scan moves backwards.
770 827
771If @var{depth} is nonzero, parenthesis depth counting begins from that 828@node Low-Level Parsing
772value. The only candidates for stopping are places where the depth in 829@subsection Low-Level Parsing
773parentheses becomes zero; @code{scan-lists} counts @var{count} such
774places and then stops. Thus, a positive value for @var{depth} means go
775out @var{depth} levels of parenthesis.
776 830
777Scanning ignores comments if @code{parse-sexp-ignore-comments} is 831 The most basic way to use the expression parser is to tell it
778non-@code{nil}. 832to start at a given position with a certain state, and parse up to
833a specified end position.
779 834
780If the scan reaches the beginning or end of the buffer (or its 835@defun parse-partial-sexp start limit &optional target-depth stop-before state stop-comment
781accessible portion), and the depth is not zero, an error is signaled. 836This function parses a sexp in the current buffer starting at
782If the depth is zero but the count is not used up, @code{nil} is 837@var{start}, not scanning past @var{limit}. It stops at position
783returned. 838@var{limit} or when certain criteria described below are met, and sets
784@end defun 839point to the location where parsing stops. It returns a parser state
840describing the status of the parse at the point where it stops.
785 841
786@defun scan-sexps from count 842@cindex parenthesis depth
787This function scans forward @var{count} sexps from position @var{from}. 843If the third argument @var{target-depth} is non-@code{nil}, parsing
788It returns the position where the scan stops. If @var{count} is 844stops if the depth in parentheses becomes equal to @var{target-depth}.
789negative, the scan moves backwards. 845The depth starts at 0, or at whatever is given in @var{state}.
790 846
791Scanning ignores comments if @code{parse-sexp-ignore-comments} is 847If the fourth argument @var{stop-before} is non-@code{nil}, parsing
792non-@code{nil}. 848stops when it comes to any character that starts a sexp. If
849@var{stop-comment} is non-@code{nil}, parsing stops when it comes to the
850start of a comment. If @var{stop-comment} is the symbol
851@code{syntax-table}, parsing stops after the start of a comment or a
852string, or the end of a comment or a string, whichever comes first.
793 853
794If the scan reaches the beginning or end of (the accessible part of) the 854If @var{state} is @code{nil}, @var{start} is assumed to be at the top
795buffer while in the middle of a parenthetical grouping, an error is 855level of parenthesis structure, such as the beginning of a function
796signaled. If it reaches the beginning or end between groupings but 856definition. Alternatively, you might wish to resume parsing in the
797before count is used up, @code{nil} is returned. 857middle of the structure. To do this, you must provide a @var{state}
858argument that describes the initial status of parsing. The value
859returned by a previous call to @code{parse-partial-sexp} will do
860nicely.
798@end defun 861@end defun
799 862
863@node Control Parsing
864@subsection Parameters to Control Parsing
865
800@defvar multibyte-syntax-as-symbol 866@defvar multibyte-syntax-as-symbol
801If this variable is non-@code{nil}, @code{scan-sexps} treats all 867If this variable is non-@code{nil}, @code{scan-sexps} treats all
802non-@acronym{ASCII} characters as symbol constituents regardless 868non-@acronym{ASCII} characters as symbol constituents regardless
@@ -817,29 +883,6 @@ The behavior of @code{parse-partial-sexp} is also affected by
817You can use @code{forward-comment} to move forward or backward over 883You can use @code{forward-comment} to move forward or backward over
818one comment or several comments. 884one comment or several comments.
819 885
820@defun forward-comment count
821This function moves point forward across @var{count} complete comments
822(that is, including the starting delimiter and the terminating
823delimiter if any), plus any whitespace encountered on the way. It
824moves backward if @var{count} is negative. If it encounters anything
825other than a comment or whitespace, it stops, leaving point at the
826place where it stopped. This includes (for instance) finding the end
827of a comment when moving forward and expecting the beginning of one.
828The function also stops immediately after moving over the specified
829number of complete comments. If @var{count} comments are found as
830expected, with nothing except whitespace between them, it returns
831@code{t}; otherwise it returns @code{nil}.
832
833This function cannot tell whether the ``comments'' it traverses are
834embedded within a string. If they look like comments, it treats them
835as comments.
836@end defun
837
838To move forward over all comments and whitespace following point, use
839@code{(forward-comment (buffer-size))}. @code{(buffer-size)} is a good
840argument to use, because the number of comments in the buffer cannot
841exceed that many.
842
843@node Standard Syntax Tables 886@node Standard Syntax Tables
844@section Some Standard Syntax Tables 887@section Some Standard Syntax Tables
845 888