f9ccd034fe6e9c4ac998e5f0e1ae5b7c4bb5407f
[bpt/emacs.git] / doc / misc / semantic.texi
1 \input texinfo
2 @setfilename ../../info/semantic
3 @set TITLE Semantic Manual
4 @set AUTHOR Eric M. Ludlam, David Ponce, and Richard Y. Kim
5 @settitle @value{TITLE}
6 @documentencoding UTF-8
7
8 @c *************************************************************************
9 @c @ Header
10 @c *************************************************************************
11
12 @c Merge all indexes into a single index for now.
13 @c We can always separate them later into two or more as needed.
14 @syncodeindex vr cp
15 @syncodeindex fn cp
16 @syncodeindex ky cp
17 @syncodeindex pg cp
18 @syncodeindex tp cp
19
20 @c @footnotestyle separate
21 @c @paragraphindent 2
22 @c @@smallbook
23 @c %**end of header
24
25 @copying
26 This manual documents the Semantic library and utilities.
27
28 Copyright @copyright{} 1999--2005, 2007, 2009--2014 Free Software
29 Foundation, Inc.
30
31 @quotation
32 Permission is granted to copy, distribute and/or modify this document
33 under the terms of the GNU Free Documentation License, Version 1.3 or
34 any later version published by the Free Software Foundation; with no
35 Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
36 and with the Back-Cover Texts as in (a) below. A copy of the license
37 is included in the section entitled ``GNU Free Documentation License.''
38
39 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
40 modify this GNU manual.''
41 @end quotation
42 @end copying
43
44 @dircategory Emacs misc features
45 @direntry
46 * Semantic: (semantic). Source code parser library and utilities.
47 @end direntry
48
49 @titlepage
50 @center @titlefont{Semantic}
51 @sp 4
52 @center by @value{AUTHOR}
53 @page
54 @vskip 0pt plus 1filll
55 @insertcopying
56 @end titlepage
57 @page
58
59 @macro semantic{}
60 @i{Semantic}
61 @end macro
62
63 @macro keyword{kw}
64 @anchor{\kw\}
65 @b{\kw\}
66 @end macro
67
68 @macro obsolete{old,new}
69 @sp 1
70 @strong{Compatibility}:
71 @code{\new\} introduced in @semantic{} version 2.0 supersedes
72 @code{\old\} which is now obsolete.
73 @end macro
74
75 @c *************************************************************************
76 @c @ Document
77 @c *************************************************************************
78 @contents
79
80 @node top
81 @top @value{TITLE}
82
83 @semantic{} is a suite of Emacs libraries and utilities for parsing
84 source code. At its core is a lexical analyzer and two parser
85 generators (@code{bovinator} and @code{wisent}) written in Emacs Lisp.
86 @semantic{} provides a variety of tools for making use of the parser
87 output, including user commands for code navigation and completion, as
88 well as enhancements for imenu, speedbar, whichfunc, eldoc,
89 hippie-expand, and several other parts of Emacs.
90
91 To send bug reports, or participate in discussions about semantic,
92 use the mailing list cedet-semantic@@sourceforge.net via the URL:
93 @url{http://lists.sourceforge.net/lists/listinfo/cedet-semantic}
94
95 @ifnottex
96 @insertcopying
97 @end ifnottex
98
99 @menu
100 * Introduction::
101 * Using Semantic::
102 * Semantic Internals::
103 * Glossary::
104 * GNU Free Documentation License::
105 * Index::
106 @end menu
107
108 @node Introduction
109 @chapter Introduction
110
111 This chapter gives an overview of @semantic{} and its goals.
112
113 Ordinarily, Emacs uses regular expressions (and syntax tables) to
114 analyze source code for purposes such as syntax highlighting. This
115 approach, though simple and efficient, has its limitations: roughly
116 speaking, it only ``guesses'' the meaning of each piece of source code
117 in the context of the programming language, instead of rigorously
118 ``understanding'' it.
119
120 @semantic{} provides a new infrastructure to analyze source code using
121 @dfn{parsers} instead of regular expressions. It contains two
122 built-in parser generators (an @acronym{LL} generator named
123 @code{Bovine} and an @acronym{LALR} generator named @code{Wisent},
124 both written in Emacs Lisp), and parsers for several common
125 programming languages. It can also make use of @dfn{external
126 parsers}---programs such as GNU Global and GNU IDUtils.
127
128 @semantic{} provides a uniform, language-independent @acronym{API} for
129 accessing the parser output. This output can be used by other Emacs
130 Lisp programs to implement ``syntax-aware'' behavior. @semantic{}
131 itself includes several such utilities, including user-level Emacs
132 commands for navigating, searching, and completing source code.
133
134 The following diagram illustrates the structure of the @semantic{}
135 package:
136
137 @table @strong
138 @item Please Note:
139 The words in all-capital are those that @semantic{} itself provides.
140 Others are current or future languages or applications that are not
141 distributed along with @semantic{}.
142 @end table
143
144 @example
145 Applications
146 and
147 Utilities
148 -------
149 / \
150 +---------------+ +--------+ +--------+
151 C --->| C PARSER |--->| | | |
152 +---------------+ | | | |
153 +---------------+ | COMMON | | COMMON |<--- SPEEDBAR
154 Java --->| JAVA PARSER |--->| PARSE | | |
155 +---------------+ | TREE | | PARSE |<--- SEMANTICDB
156 +---------------+ | FORMAT | | API |
157 Scheme --->| SCHEME PARSER |--->| | | |<--- ecb
158 +---------------+ | | | |
159 +---------------+ | | | |
160 Texinfo --->| TEXI. PARSER |--->| | | |
161 +---------------+ | | | |
162
163 ... ... ... ...
164
165 +---------------+ | | | |
166 Lang. Y --->| Y Parser |--->| | | |<--- app. ?
167 +---------------+ | | | |
168 +---------------+ | | | |<--- app. ?
169 Lang. Z --->| Z Parser |--->| | | |
170 +---------------+ +--------+ +--------+
171 @end example
172
173 @menu
174 * Semantic Components::
175 @end menu
176
177 @node Semantic Components
178 @section Semantic Components
179
180 In this section, we provide a more detailed description of the major
181 components of @semantic{}, and how they interact with one another.
182
183 The first step in parsing a source code file is to break it up into
184 its fundamental components. This step is called lexical analysis:
185
186 @example
187 syntax table, keywords list, and options
188 |
189 |
190 v
191 input file ----> Lexer ----> token stream
192 @end example
193
194 @noindent
195 The output of the lexical analyzer is a list of tokens that make up
196 the file. The next step is the actual parsing, shown below:
197
198 @example
199 parser tables
200 |
201 v
202 token stream ---> Parser ----> parse tree
203 @end example
204
205 @noindent
206 The end result, the parse tree, is @semantic{}'s internal
207 representation of the language grammar. @semantic{} provides an
208 @acronym{API} for Emacs Lisp programs to access the parse tree.
209
210 Parsing large files can take several seconds or more. By default,
211 @semantic{} automatically caches parse trees by saving them in your
212 @file{.emacs.d} directory. When you revisit a previously-parsed file,
213 the parse tree is automatically reloaded from this cache, to save
214 time. @xref{SemanticDB}.
215
216 @node Using Semantic
217 @chapter Using Semantic
218
219 @include sem-user.texi
220
221 @node Semantic Internals
222 @chapter Semantic Internals
223
224 This chapter provides an overview of the internals of @semantic{}.
225 This information is usually not needed by application developers or
226 grammar developers; it is useful mostly for the hackers who would like
227 to learn more about how @semantic{} works.
228
229 @menu
230 * Parser code:: Code used for the parsers
231 * Tag handling:: Code used for manipulating tags
232 * Semanticdb Internals:: Code used in the semantic database
233 * Analyzer Internals:: Code used in the code analyzer
234 * Tools:: Code used in user tools
235 * Tests:: Code used for testing
236 @end menu
237
238 @node Parser code
239 @section Parser code
240
241 @semantic{} parsing code is spread across a range of files.
242
243 @table @file
244 @item semantic.el
245 The core infrastructure sets up buffers for parsing, and has all the
246 core parsing routines. Most parsing routines are overloadable, so the
247 actual implementation may be somewhere else.
248
249 @item semantic-edit.el
250 Incremental reparse based on user edits.
251
252 @item semantic-grammar.el
253 @itemx semantic-grammar.wy
254 Parser for the different grammar languages, and a major mode for
255 editing grammars in Emacs.
256
257 @item semantic-lex.el
258 Infrastructure for implementing lexical analyzers. Provides macros
259 for creating individual analyzers for specific features, and a way to
260 combine them together.
261
262 @item semantic-lex-spp.el
263 Infrastructure for a lexical symbolic preprocessor. This was written
264 to implement the C preprocessor, but could be used for other lexical
265 preprocessors.
266
267 @item bovine/bovine-grammar.el
268 @itemx bovine/bovine-grammar-macros.el
269 @itemx bovine/semantic-bovine.el
270 The ``bovine'' grammar. This is the first grammar mode written for
271 @semantic{} and is useful for simple creating simple parsers.
272
273 @item wisent/wisent.el
274 @itemx wisent/bison-wisent.el
275 @itemx wisent/semantic-wisent.el
276 @itemx wisent/semantic-debug-grammar.el
277 A port of bison to Emacs. This infrastructure lets you create LALR
278 based parsers for @semantic{}.
279
280 @item semantic-ast.el
281 Manage Abstract Syntax Trees for parsers.
282
283 @item semantic-debug.el
284 Infrastructure for debugging grammars.
285
286 @item semantic-util.el
287 Various utilities for manipulating tags, such as describing the tag
288 under point, adding labels, and the all important
289 @code{semantic-something-to-tag-table}.
290
291 @end table
292
293 @node Tag handling
294 @section Tag handling
295
296 A tag represents an individual item found in a buffer, such as a
297 function or variable. Tag handling is handled in several source
298 files.
299
300 @table @file
301 @item semantic-tag.el
302 Basic tag creation, queries, cloning, binding, and unbinding.
303
304 @item semantic-tag-write.el
305 Write a tag or tag list to a stream. These routines are used by
306 @file{semanticdb-file.el} when saving a list of tags.
307
308 @item semantic-tag-file.el
309 Files associated with tags. Goto-tag, file for include, and file for
310 a prototype.
311
312 @item semantic-tag-ls.el
313 Language dependent features of a tag, such as parent calculation, slot
314 protection, and other states like abstract, virtual, static, and leaf.
315
316 @item semantic-dep.el
317 Include file handling. Contains the include path concepts, and
318 routines for looking up file names in the include path.
319
320 @item semantic-format.el
321 Convert a tag into a nicely formatted and colored string. Use
322 @code{semantic-test-all-format-tag-functions} to test different output
323 options.
324
325 @item semantic-find.el
326 Find tags matching different conditions in a tag table.
327 These routines are used by @file{semanticdb-find.el} once the database
328 has been converted into a simpler tag table.
329
330 @item semantic-sort.el
331 Sorting lists of tags in different ways. Includes sorting a plain
332 list of tags forward or backward. Includes binning tags based on
333 attributes (bucketize), and tag adoption for multiple references to
334 the same thing.
335
336 @item semantic-doc.el
337 Capture documentation comments from near a tag.
338
339 @end table
340
341 @node Semanticdb Internals
342 @section Semanticdb Internals
343
344 @acronym{Semanticdb} complexity is certainly an issue. It is a rather
345 hairy problem to try and solve.
346
347 @table @file
348 @item semanticdb.el
349 Defines a @dfn{database} and a @dfn{table} base class. You can
350 instantiate these classes, and use them, but they are not persistent.
351
352 This file also provides support for @code{semanticdb-minor-mode},
353 which automatically associates files with tables in databases so that
354 tags are @emph{saved} while a buffer is not in memory.
355
356 The database and tables both also provide applicable cache information,
357 and cache flushing system. The semanticdb search routines use caches
358 to save data structures that are complex to calculate.
359
360 Lastly, it provides the concept of @dfn{project root}. It is a system
361 by which a file can be associated with the root of a project, so if
362 you have a tree of directories and source files, it can find the root,
363 and allow a tag-search to span all available databases in that
364 directory hierarchy.
365
366 @item semanticdb-file.el
367 Provides a subclass of the basic table so that it can be saved to
368 disk. Implements all the code needed to unbind/rebind tags to a
369 buffer and writing them to a file.
370
371 @item semanticdb-el.el
372 Implements a special kind of @dfn{system} database that uses Emacs
373 internals to perform queries.
374
375 @item semanticdb-ebrowse.el
376 Implements a system database that uses Ebrowse to parse files into a
377 table that can be queried for tag names. Successful tag hits during a
378 find causes @semantic{} to pick up and parse the reference files to
379 get the full details.
380
381 @item semanticdb-find.el
382 Infrastructure for searching groups @semantic{} databases, and dealing
383 with the search results format.
384
385 @item semanticdb-ref.el
386 Tracks crossreferences. Cross references are needed when buffer is
387 reparsed, and must alert other tables that any dependent caches may
388 need to be flushed. References are in the form of include files.
389
390 @end table
391
392 @node Analyzer Internals
393 @section Analyzer Internals
394
395 The @semantic{} analyzer is a complex engine which has been broken
396 down across several modules. When the @semantic{} analyzer fails,
397 start with @code{semantic-analyze-debug-assist}, then dive into some
398 of these files.
399
400 @table @file
401 @item semantic-analyze.el
402 The core analyzer for defining the @dfn{current context}. The
403 current context is an object that contains references to aspects of
404 the local context including the current prefix, and a tag list
405 defining what the prefix means.
406
407 @item semantic-analyze-complete.el
408 Provides @code{semantic-analyze-possible-completions}.
409
410 @item semantic-analyze-debug.el
411 The analyzer debugger. Useful when attempting to get everything
412 configured.
413
414 @item semantic-analyze-fcn.el
415 Various support functions needed by the analyzer.
416
417 @item semantic-ctxt.el
418 Local context parser. Contains overloadable functions used to move
419 around through different scopes, get local variables, and collect the
420 current prefix used when doing completion.
421
422 @item semantic-scope.el
423 Calculate @dfn{scope} for a location in a buffer. The scope includes
424 local variables, and tag lists in scope for various reasons, such as
425 C++ using statements.
426
427 @item semanticdb-typecache.el
428 The typecache is part of @code{semanticdb}, but is used primarily by
429 the analyzer to look up datatypes and complex names. The typecache is
430 bound across source files and builds a master lookup table for data
431 type names.
432
433 @item semantic-ia.el
434 Interactive Analyzer functions. Simple routines that do completion or
435 lookups based on the results from the Analyzer. These routines are
436 meant as examples for application writers, but are quite useful as
437 they are.
438
439 @item semantic-ia-sb.el
440 Speedbar support for the analyzer, displaying context info, and
441 completion lists.
442
443 @end table
444
445 @node Tools
446 @section Tools
447
448 These files contain various tools a user can use.
449
450 @table @file
451 @item semantic-idle.el
452 Idle scheduler for @semantic{}. Manages reparsing buffers after
453 edits, and large work tasks in idle time. Includes modes for showing
454 summary help and pop-up completion.
455
456 @item senator.el
457 The @semantic{} navigator. Provides many ways to move through a
458 buffer based on the active tag table.
459
460 @item semantic-decorate.el
461 A minor mode for decorating tags based on details from the parser.
462 Includes overlines for functions, or coloring class fields based on
463 protection.
464
465 @item semantic-decorate-include.el
466 A decoration mode for include files, which assists users in setting up
467 parsing for their includes.
468
469 @item semantic-complete.el
470 Advanced completion prompts for reading tag names in the minibuffer, or
471 inline in a buffer.
472
473 @item semantic-imenu.el
474 Imenu support for using @semantic{} tags in imenu.
475
476 @item semantic-mru-bookmark.el
477 Automatic bookmarking based on tags. Jump to locations you've been
478 before based on tag name.
479
480 @item semantic-sb.el
481 Support for @semantic{} tag usage in Speedbar.
482
483 @item semantic-util-modes.el
484 A bunch of small minor-modes that exposes aspects of the semantic
485 parser state. Includes @code{semantic-stickyfunc-mode}.
486
487 @item document.el
488 @itemx document-vars.el
489 Create an update comments for tags.
490
491 @item semantic-adebug.el
492 Extensions of @file{data-debug.el} for @semantic{}.
493
494 @item semantic-chart.el
495 Draw some charts from stats generated from parsing.
496
497
498 @item semantic-elp.el
499 Profiler for helping to optimize the @semantic{} analyzer.
500
501
502 @end table
503
504 @node Tests
505 @section Tests
506
507 @table @file
508
509 @item semantic-utest.el
510 Basic testing of parsing and incremental parsing for most supported
511 languages.
512
513 @item semantic-ia-utest.el
514 Test the semantic analyzer's ability to provide smart completions.
515
516 @item semantic-utest-c.el
517 Tests for the C parser's lexical pre-processor.
518
519 @item semantic-regtest.el
520 Regression tests from the older Semantic 1.x API.
521
522 @end table
523
524 @node Glossary
525 @appendix Glossary
526
527 @table @asis
528 @item BNF
529 In semantic 1.4, a BNF file represented ``Bovine Normal Form'', the
530 grammar file used for the 1.4 parser generator. This was a play on
531 Backus-Naur Form which proved too confusing.
532
533 @item bovinate
534 A verb representing what happens when a bovine parser parses a file.
535
536 @item bovine lambda
537 In a bovine, or LL parser, the bovine lambda is a function to execute
538 when a specific set of match rules has succeeded in matching text from
539 the buffer.
540
541 @item bovine parser
542 A parser using the bovine parser generator. It is an LL parser
543 suitable for small simple languages.
544
545 @item context
546
547 @item LALR
548
549 @item lexer
550 A program which converts text into a stream of tokens by analyzing
551 them lexically. Lexers will commonly create strings, symbols,
552 keywords and punctuation, and strip whitespaces and comments.
553
554 @item LL
555
556 @item nonterminal
557 A nonterminal symbol or simply a nonterminal stands for a class of
558 syntactically equivalent groupings. A nonterminal symbol name is used
559 in writing grammar rules.
560
561 @item overloadable
562 Some functions are defined via @code{define-overload}.
563 These can be overloaded via ....
564
565 @item parser
566 A program that converts @b{tokens} to @b{tags}.
567
568 @item tag
569 A tag is a representation of some entity in a language file, such as a
570 function, variable, or include statement. In semantic, the word tag is
571 used the same way it is used for the etags or ctags tools.
572
573 A tag is usually bound to a buffer region via overlay, or it just
574 specifies character locations in a file.
575
576 @item token
577 A single atomic item returned from a lexer. It represents some set
578 of characters found in a buffer.
579
580 @item token stream
581 The output of the lexer as well as the input to the parser.
582
583 @item wisent parser
584 A parser using the wisent parser generator. It is a port of bison to
585 Emacs Lisp. It is an LALR parser suitable for complex languages.
586 @end table
587
588
589 @node GNU Free Documentation License
590 @appendix GNU Free Documentation License
591 @include doclicense.texi
592
593 @node Index
594 @unnumbered Index
595 @printindex cp
596
597 @iftex
598 @contents
599 @summarycontents
600 @end iftex
601
602 @bye
603
604 @c Following comments are for the benefit of ispell.
605
606 @c LocalWords: alist API APIs arg argc args argv asis assoc autoload Wisent
607 @c LocalWords: backquote bnf bovinate bovinates LALR
608 @c LocalWords: bovinating bovination bovinator bucketize
609 @c LocalWords: cb cdr charquote checkcache cindex CLOS
610 @c LocalWords: concat concocting const ctxt Decl defcustom
611 @c LocalWords: deffn deffnx defun defvar destructor's dfn diff dir
612 @c LocalWords: doc docstring EDE EIEIO elisp emacsman emph enum
613 @c LocalWords: eq Exp EXPANDFULL expression fn foo func funcall
614 @c LocalWords: ia ids iff ifinfo imenu imenus init int isearch itemx java kbd
615 @c LocalWords: keymap keywordtable lang languagemode lexer lexing Ludlam
616 @c LocalWords: menubar metaparent metaparents min minibuffer Misc mode's
617 @c LocalWords: multitable NAvigaTOR noindent nomedian nonterm noselect
618 @c LocalWords: nosnarf obarray OLE OO outputfile paren parsetable POINT's
619 @c LocalWords: popup positionalonly positiononly positionormarker pre
620 @c LocalWords: printf printindex Programmatically pt quotemode
621 @c LocalWords: ref regex regexp Regexps reparse resetfile samp sb
622 @c LocalWords: scopestart SEmantic semanticdb setfilename setq
623 @c LocalWords: settitle setupfunction sexp sp SPC speedbar speedbar's
624 @c LocalWords: streamorbuffer struct subalist submenu submenus
625 @c LocalWords: subsubsection sw sym texi texinfo titlefont titlepage
626 @c LocalWords: tok TOKEN's toplevel typemodifiers uml unset untar
627 @c LocalWords: uref usedb var vskip xref yak