[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. Programming

Once a source file has been parsed, the following APIs can be used to write programs that use the token stream most effectively.

9.1 Token Queries  Getting info about a parsed token (nonterminal).
9.2 Nonterminal streams  Working with streams of nonterminals.
9.3 Nonterminals at point  Finding nonterminals at point.
9.4 Nonterminal sorting  Reorganizing streams.
9.5 Nonterminal completion  Completing read functions.
9.6 Override Methods  Language dependent functions covering conversion to text strings, language dependent queries and local context information
9.7 Parser Hooks  How to know when tags change.
9.8 Programming Examples  Simple programming examples.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Token Queries

When writing programs that use the bovinator, the following functions are needed to find get details out of a nonterminal.

Function: semantic-equivalent-tokens-p token1 token2
Compare TOKEN1 and TOKEN2 and return non-nil if they are equivalent. Use eq to test of two tokens are the same. Use this function if tokens are being copied and regrouped to test for if two tokens represent the same thing, but may be constructed of different cons cells.

Function: semantic-token-token token
Retrieve from TOKEN the token identifier. i.e., the symbol 'variable, 'function, 'type, or other.

Function: semantic-token-name token
Retrieve the name of TOKEN.

Function: semantic-token-docstring token &optional buffer
Retrieve the documentation of TOKEN. Optional argument BUFFER indicates where to get the text from. If not provided, then only the POSITION can be provided.

Function: semantic-token-overlay token
Retrieve the OVERLAY part of TOKEN. The returned item may be an overlay or an unloaded buffer representation.

Function: semantic-token-extent token
Retrieve the extent (START END) of TOKEN.

Function: semantic-token-start token
Retrieve the start location of TOKEN.

Function: semantic-token-end token
Retrieve the end location of TOKEN.

Function: semantic-token-type token
Retrieve the type of TOKEN.

Function: semantic-token-put token property value
On token, set property to value.

Function: semantic-token-get token property
For token get the value of property.

Function: semantic-token-extra-spec token spec
Retrieve a specifier for the variable TOKEN. SPC is the symbol whose modifier value to get. This function can get specifiers from any type of TOKEN. Do not use this function if you know what type of token you are dereferencing. Instead, use the function specific to that token type. It will be faster.

Function: semantic-token-type-parts token
Retrieve the parts of the type TOKEN.

Function: semantic-token-type-parent token
Retrieve the parent of the type TOKEN. The return value is a list. A value of nil means no parents. The car of the list is either the parent class, or a list of parent classes. The cdr of the list is the list of interfaces, or abstract classes which are parents of TOKEN.

Function: semantic-token-type-parent-superclass token
Retrieve the parent super classes of type type TOKEN.

Function: semantic-token-type-parent-implement token
Retrieve the parent interfaces of type type TOKEN.

Function: semantic-token-type-modifiers token
Retrieve the type modifiers for the type TOKEN.

Function: semantic-token-type-extra-specs token
Retrieve the extra specifiers for the type TOKEN.

Function: semantic-token-type-extra-spec token spec
Retrieve a extra specifier for the type TOKEN. SPEC is the symbol whose modifier value to get.

Function: semantic-token-function-args token
Retrieve the arguments of the function TOKEN.

Function: semantic-token-function-modifiers token
Retrieve the type modifiers of the function TOKEN.

Function: semantic-token-function-destructor token
Non-nil if TOKEN is a destructor function.

Function: semantic-token-function-extra-specs token
Retrieve the extra specifiers of the function TOKEN.

Function: semantic-token-function-extra-spec token spec
Retrieve a specifier for the function TOKEN. SPEC is a symbol whose specifier value to get.

Function: semantic-token-function-throws token
Retrieve the throws signal of the function TOKEN. This is an optional field, and returns nil if it doesn't exist.

Function: semantic-token-function-parent token
The parent of the function TOKEN. A function has a parent if it is a method of a class, and if the function does not appear in body of its parent class.

Function: semantic-token-variable-const token
Retrieve the status of constantness from the variable TOKEN.

Function: semantic-token-variable-default token
Retrieve the default value of the variable TOKEN.

Function: semantic-token-variable-modifiers token
Retrieve type modifiers for the variable TOKEN.

Function: semantic-token-variable-extra-specs token
Retrieve extra specifiers for the variable TOKEN.

Function: semantic-token-variable-extra-spec token spec
Retrieve a specifier value for the variable TOKEN. SPEC is the symbol whose specifier value to get.

Function: semantic-token-include-system token
Retrieve the flag indicating if the include TOKEN is a system include.

For override methods that query a token, see See section 9.6.2 Token Details.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 Nonterminal streams

These functions take some key, and returns information found inside the nonterminal stream. Some will return one token (the first matching item found.) Others will return a list of all items matching a given criterion. All these functions work regardless of a buffer being in memory or not.

Function: semantic-find-nonterminal-by-name name streamorbuffer &optional search-parts search-include
Find a nonterminal NAME within STREAMORBUFFER. NAME is a string. If SEARCH-PARTS is non-nil, search children of tokens. If SEARCH-INCLUDE is non-nil, search include files.

Function: semantic-find-nonterminal-by-property property value streamorbuffer &optional search-parts search-includes
Find all nonterminals with PROPERTY equal to VALUE in STREAMORBUFFER. Properties can be added with semantic-token-put. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-by-extra-spec spec streamorbuffer &optional search-parts search-includes
Find all nonterminals with a given SPEC in STREAMORBUFFER. SPEC is a symbol key into the modifiers association list. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-by-extra-spec-value spec value streamorbuffer &optional search-parts search-includes
Find all nonterminals with a given SPEC equal to VALUE in STREAMORBUFFER. SPEC is a symbol key into the modifiers association list. VALUE is the value that SPEC should match. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-by-position position streamorbuffer &optional nomedian
Find a nonterminal covering POSITION within STREAMORBUFFER. POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do the median calculation, and return nil.

Function: semantic-find-innermost-nonterminal-by-position position streamorbuffer &optional nomedian
Find a list of nonterminals covering POSITION within STREAMORBUFFER. POSITION is a number, or marker. If NOMEDIAN is non-nil, don't do the median calculation, and return nil. This function will find the topmost item, and recurse until no more details are available of findable.

Function: semantic-find-nonterminal-by-token token streamorbuffer &optional search-parts search-includes
Find all nonterminals with a token TOKEN within STREAMORBUFFER. TOKEN is a symbol representing the type of the tokens to find. Optional argument SEARCH-PARTS and SEARCH-INCLUDE are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-standard streamorbuffer &optional search-parts search-includes
Find all nonterminals in STREAMORBUFFER which define simple token types. Optional argument SEARCH-PARTS and SEARCH-INCLUDE are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-by-type type streamorbuffer &optional search-parts search-includes
Find all nonterminals with type TYPE within STREAMORBUFFER. TYPE is a string which is the name of the type of the token returned. Optional argument SEARCH-PARTS and SEARCH-INCLUDES are passed to semantic-find-nonterminal-by-function.

Function: semantic-find-nonterminal-by-function function streamorbuffer &optional search-parts search-includes
Find all nonterminals in which FUNCTION match within STREAMORBUFFER. FUNCTION must return non-nil if an element of STREAM will be included in the new list.

If optional argument SEARCH-PARTS is non-nil, all sub-parts of tokens are searched. The over-loadable function semantic-nonterminal-children is used for the searching child lists. If SEARCH-PARTS is the symbol 'positiononly, then only children that have positional information are searched.

If SEARCH-INCLUDES is non-nil, then all include files are also searched for matches.

Function: semantic-find-nonterminal-by-function-first-match function streamorbuffer &optional search-parts search-includes
Find the first nonterminal which FUNCTION match within STREAMORBUFFER. FUNCTION must return non-nil if an element of STREAM will be included in the new list. If optional argument SEARCH-PARTS, all sub-parts of tokens are searched. The over-loadable function semantic-nonterminal-children is used for searching. If SEARCH-INCLUDES is non-nil, then all include files are also searched for matches.

Function: semantic-recursive-find-nonterminal-by-name name buffer
Recursively find the first occurrence of NAME. Start search with BUFFER. Recurse through all dependencies till found. The return item is of the form (BUFFER TOKEN) where BUFFER is the buffer in which TOKEN (the token found to match NAME) was found.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 Nonterminals at point

When you just want to get at a nonterminal the cursor is on, there is a more efficient mechanism than using semantic-find-nonterminal-by-position. This mechanism directly queries the overlays the parsing step leaves in the buffer. This provides for very rapid retrieval of what function or variable the cursor is currently in.

These functions query the current buffer's overlay system for tokens.

Function: semantic-find-nonterminal-by-overlay &optional positionormarker buffer
Find all nonterminals covering POSITIONORMARKER by using overlays. If POSITIONORMARKER is nil, use the current point. Optional BUFFER is used if POSITIONORMARKER is a number, otherwise the current buffer is used. This finds all tokens covering the specified position by checking for all overlays covering the current spot. They are then sorted from largest to smallest via the start location.

Function: semantic-find-nonterminal-by-overlay-in-region start end &optional buffer
Find all nonterminals which exist in whole or in part between START and END. Uses overlays to determine position. Optional BUFFER argument specifies the buffer to use.

Function: semantic-current-nonterminal
Return the current nonterminal in the current buffer. If there are more than one in the same location, return the smallest token.

Function: semantic-current-nonterminal-parent
Return the current nonterminals parent in the current buffer. A token's parent would be a containing structure, such as a type containing a field. Return nil if there is no parent.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.4 Nonterminal sorting

Sometimes it is important to reorganize a token stream into a form that is better for display to a user. It is important to not use functions with side effects when doing this, and that could effect the token cache.

There are some existing utility functions which will reorganize the token list for you.

Function: semantic-bucketize tokens &optional parent filter
Sort TOKENS into a group of buckets based on token type. Unknown types are placed in a Misc bucket. Type bucket names are defined by either `semantic-symbol->name-assoc-list'. If PARENT is specified, then TOKENS belong to this PARENT in some way. This will use `semantic-symbol->name-assoc-list-for-type-parts' to generate bucket names. Optional argument FILTER is a filter function to be applied to each bucket. The filter function will take one argument, which is a list of tokens, and may re-organize the list with side-effects.

Variable: semantic-bucketize-token-token
Function used to get a symbol describing the class of a token. This function must take one argument of a semantic token. It should return a symbol found in `semantic-symbol->name-assoc-list' which semantic-bucketize uses to bin up tokens. To create new bins for an application augment `semantic-symbol->name-assoc-list', and `semantic-symbol->name-assoc-list-for-type-parts' in addition to setting this variable (locally in your function).

Function: semantic-adopt-external-members tokens
Rebuild TOKENS so that externally defined members are regrouped. Some languages such as C++ and CLOS permit the declaration of member functions outside the definition of the class. It is easier to study the structure of a program when such methods are grouped together more logically.

This function uses semantic-nonterminal-external-member-p to determine when a potential child is an externally defined member.

Note: Applications which use this function must account for token types which do not have a position, but have children which *do* have positions.

Applications should use semantic-mark-external-member-function to modify all tokens which are found as externally defined to some type. For example, changing the token type for generating extra buckets with the bucket function.

Variable: semantic-orphaned-member-metaparent-type
In semantic-adopt-external-members, the type of 'type for metaparents. A metaparent is a made-up type semantic token used to hold the child list of orphaned members of a named type.

Variable: semantic-mark-external-member-function
Function called when an externally defined orphan is found. Be default, the token is always marked with the adopted property. This function should be locally bound by a program that needs to add additional behaviors into the token list. This function is called with one argument which is a shallow copy of the token to be modified. This function should return the token (or a copy of it) which is then integrated into the revised token list.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.5 Nonterminal completion

These functions provide ways reading the names of items in a buffer with completion.

Function: semantic-read-symbol prompt &optional default stream filter
Read a symbol name from the user for the current buffer. PROMPT is the prompt to use. Optional arguments: DEFAULT is the default choice. If no default is given, one is read from under point. STREAM is the list of tokens to complete from. FILTER is provides a filter on the types of things to complete. FILTER must be a function to call on each element. (See !!!

Function: semantic-read-variable prompt &optional default stream
Read a variable name from the user for the current buffer. PROMPT is the prompt to use. Optional arguments: DEFAULT is the default choice. If no default is given, one is read from under point. STREAM is the list of tokens to complete from.

Function: semantic-read-function prompt &optional default stream
Read a function name from the user for the current buffer. PROMPT is the prompt to use. Optional arguments: DEFAULT is the default choice. If no default is given, one is read from under point. STREAM is the list of tokens to complete from.

Function: semantic-read-type prompt &optional default stream
Read a type name from the user for the current buffer. PROMPT is the prompt to use. Optional arguments: DEFAULT is the default choice. If no default is given, one is read from under point. STREAM is the list of tokens to complete from.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6 Override Methods

These functions are called `override methods' because they provide generic behaviors, which a given language can override. For example, finding a dependency file in Emacs lisp can be done with the `locate-library' command (which overrides the default behavior.) In C, a dependency can be found by searching a generic search path which can be passed in via a variable.

9.6.1 Token->Text  Converting Tokens into text strings
9.6.2 Token Details  Arbitrary token detail fetching
9.6.3 Local Context  Deriving information about a language specific local context.
9.6.4 Making New Methods  How to add your own methods for a tool


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6.1 Token->Text

Any given token consists of Meta information which is best viewed in some textual form. This could be as simple as the token's name, or as a prototype to be added to header file in C. Not only are there several default converters from a Token into text, but there is also some convenient variables that can be used with them. Use these variables to allow options on output forms when displaying tokens in your programs.

Variable: semantic-token->text-functions
List of functions which convert a token to text. Each function must take the parameters TOKEN &optional PARENT COLOR. TOKEN is the token to convert. PARENT is a parent token or name which refers to the structure or class which contains TOKEN. PARENT is NOT a class which a TOKEN would claim as a parent. COLOR indicates that the generated text should be colored using font-lock.

Variable: semantic-token->text-custom-list
A List used by customizable variables to choose a token to text function. Use this variable in the :type field of a customizable variable.

Every token to text conversion function must take the same parameters, which are TOKEN, the token to be converted, PARENT, the containing parent (like a structure which contains a variable), and COLOR, which is a flag specifying that color should be applied to the returned string.

When creating, or using these strings, particularly with color, use concat to build up larger strings instead of format. This will preserve text properties.

Function: semantic-name-nonterminal token &optional parent color
Return the name string describing TOKEN. The name is the shortest possible representation. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.

Function: semantic-summarize-nonterminal token &optional parent color
Summarize TOKEN in a reasonable way. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.

Function: semantic-prototype-nonterminal token &optional parent color
Return a prototype for TOKEN. This function should be overloaded, though it need not be used. This is because it can be used to create code by language independent tools. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.

Function: semantic-prototype-file buffer
Return a file in which prototypes belonging to BUFFER should be placed. Default behavior (if not overridden) looks for a token specifying the prototype file, or the existence of an EDE variable indicating which file prototypes belong in.

Function: semantic-abbreviate-nonterminal token &optional parent color
Return an abbreviated string describing TOKEN. The abbreviation is to be short, with possible symbols indicating the type of token, or other information. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.

Function: semantic-concise-prototype-nonterminal token &optional parent color
Return a concise prototype for TOKEN. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.

Function: semantic-uml-abbreviate-nonterminal token &optional parent color
Return a UML style abbreviation for TOKEN. Optional argument PARENT is the parent type if TOKEN is a detail. Optional argument COLOR means highlight the prototype with font-lock colors.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6.2 Token Details

These functions help derive information about tokens that may not be obvious for non-traditional languages with their own token types.

Function: semantic-nonterminal-children token &optional positionalonly
Return the list of top level children belonging to TOKEN. Children are any sub-tokens which may contain overlays. The default behavior (if not overridden with nonterminal-children is to return type parts for a type, and arguments for a function.

If optional argument POSITIONALONLY is non-nil, then only return valid children if they contain positions. Some languages may choose to create lists of children without position/overlay information.

If this function is overridden, use semantic-nonterminal-children-default to also include the default behavior, and merely extend your own.

Note for language authors: If a mode defines a language that has tokens in it with overlays that should not be considered children, you should still return them with this function. If you do not, then token re-parsing, and database saving will fail.

Function: semantic-nonterminal-external-member-parent token
Return a parent for TOKEN when TOKEN is an external member. TOKEN is an external member if it is defined at a toplevel and has some sort of label defining a parent. The parent return will be a string.

The default behavior, if not overridden with nonterminal-external-member-parent is get the 'parent extra specifier of TOKEN.

If this function is overridden, use semantic-nonterminal-external-member-parent-default to also include the default behavior, and merely extend your own.

Function: semantic-nonterminal-external-member-p parent token
Return non-nil if PARENT is the parent of TOKEN. TOKEN is an external member of PARENT when it is somehow tagged as having PARENT as it's parent.

The default behavior, if not overridden with nonterminal-external-member-p is to match 'parent extra specifier in the name of TOKEN.

If this function is overridden, use semantic-nonterminal-external-member-children-p-default to also include the default behavior, and merely extend your own.

Function: semantic-nonterminal-external-member-children token &optional usedb
Return the list of children which are not *in* TOKEN. If optional argument USEDB is non-nil, then also search files in the Semantic Database. If USEDB is a list of databases, search those databases.

Children in this case are functions or types which are members of TOKEN, such as the parts of a type, but which are not defined inside the class. C++ and CLOS both permit methods of a class to be defined outside the bounds of the class' definition.

The default behavior, if not overridden with nonterminal-external-member-children is to search using semantic-nonterminal-external-member-p in all top level definitions with a parent of TOKEN.

If this function is overridden, use semantic-nonterminal-external-member-children-default to also include the default behavior, and merely extend your own.

Function: semantic-nonterminal-protection token &optional parent
Return protection information about TOKEN with optional PARENT. This function returns on of the following symbols: nil - No special protection. Language dependent. 'public - Anyone can access this TOKEN. 'private - Only methods in the local scope can access TOKEN. 'friend - Like private, except some outer scopes are allowed access to token. Some languages may choose to provide additional return symbols specific to themselves. Use of this function should allow for this.

The default behavior (if not overridden with nonterminal-protection is to return a symbol based on type modifiers.

Function: semantic-nonterminal-abstract token &optional parent
Return non nil if TOKEN is abstract. Optional PARENT is the parent token of TOKEN. In UML, abstract methods and classes have special meaning and behavior in how methods are overridden. In UML, abstract methods are italicized.

The default behavior (if not overridden with nonterminal-abstract is to return true if abstract is in the type modifiers.

Function: semantic-nonterminal-leaf token &optional parent
Return non nil if TOKEN is leaf. Optional PARENT is the parent token of TOKEN. In UML, leaf methods and classes have special meaning and behavior.

The default behavior (if not overridden with nonterminal-leaf is to return true if leaf is in the type modifiers.

Function: semantic-nonterminal-static token &optional parent
Return non nil if TOKEN is static. Optional PARENT is the parent token of TOKEN. In UML, static methods and attributes mean that they are allocated in the parent class, and are not instance specific. UML notation specifies that STATIC entries are underlined.

The default behavior (if not overridden with nonterminal-static is to return true if static is in the type modifiers.

Function: semantic-find-dependency token
Find the filename represented from TOKEN. TOKEN may be a stripped element, in which case PARENT specifies a parent token that has positional information. Depends on semantic-dependency-include-path for searching. Always searches `.' first, then searches additional paths.

Variable: semantic-dependency-include-path
Defines the include path used when searching for files. This should be a list of directories to search which is specific to the file being included. This variable can also be set to a single function. If it is a function, it will be called with one arguments, the file to find as a string, and it should return the full path to that file, or nil.

Function: semantic-find-nonterminal token &optional parent
Find the location of TOKEN. TOKEN may be a stripped element, in which case PARENT specifies a parent token that has position information. Different behaviors are provided depending on the type of token. For example, dependencies (includes) will seek out the file that is depended on, and functions will move to the specified definition.

Function: semantic-find-documentation token
Find documentation from TOKEN and return it as a clean string. TOKEN might have DOCUMENTATION set in it already. If not, there may be some documentation in a comment preceding TOKEN's definition which we can look for. When appropriate, this can be overridden by a language specific enhancement.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6.3 Local Context

Function: semantic-up-context &optional point
Move point up one context from POINT. Return non-nil if there are no more context levels. Overloaded functions using up-context take no parameters.

Function: semantic-beginning-of-context &optional point
Move POINT to the beginning of the current context. Return non-nil if there is no upper context. The default behavior uses semantic-up-context. It can be overridden with beginning-of-context.

Function: semantic-end-of-context &optional point
Move POINT to the end of the current context. Return non-nil if there is no upper context. Be default, this uses semantic-up-context, and assumes parenthetical block delimiters. This can be overridden with end-of-context.

Function: semantic-get-local-variables &optional point
Get the local variables based on POINT's context. Local variables are returned in Semantic token format. Be default, this calculates the current bounds using context blocks navigation, then uses the parser with bovine-inner-scope to parse tokens at the beginning of the context. This can be overridden with get-local-variables.

Function: semantic-get-local-arguments &optional point
Get arguments (variables) from the current context at POINT. Parameters are available if the point is in a function or method. This function returns a list of tokens. If the local token returns just a list of strings, then this function will convert them to tokens. Part of this behavior can be overridden with get-local-arguments.

Function: semantic-get-all-local-variables &optional point
Get all local variables for this context, and parent contexts. Local variables are returned in Semantic token format. Be default, this gets local variables, and local arguments. This can be overridden with get-all-local-variables. Optional argument POINT is the location to start getting the variables from.

These next set of functions handle local context parsing. This means looking at the code (locally) and navigating, and fetching information such as a the type of the parameter the cursor may be typing in.

Function: semantic-end-of-command
Move to the end of the current command. Be default, uses semantic-command-separation-character. Override with end-of-command.

Function: semantic-beginning-of-command
Move to the beginning of the current command. Be default, users semantic-command-separation-character. Override with beginning-of-command.

Function: semantic-ctxt-current-symbol &optional point
Return the current symbol the cursor is on at POINT in a list. This will include a list of type/field names when applicable. This can be overridden using ctxt-current-symbol.

Function: semantic-ctxt-current-assignment &optional point
Return the current assignment near the cursor at POINT. Return a list as per semantic-ctxt-current-symbol. Return nil if there is nothing relevant. Override with ctxt-current-assignment.

Function: semantic-ctxt-current-function &optional point
Return the current function the cursor is in at POINT. The function returned is the one accepting the arguments that the cursor is currently in. This can be overridden with ctxt-current-function.

Function: semantic-ctxt-current-argument &optional point
Return the current symbol the cursor is on at POINT. Override with ctxt-current-argument.

Function: semantic-ctxt-scoped-types &optional point
Return a list of type names currently in scope at POINT. Override with ctxt-scoped-types.

For details on using these functions to get more detailed information about the current context: See section 10.4 Analysis of the current context.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.6.4 Making New Methods


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.7 Parser Hooks

If you write a program that uses the stream of tokens in a persistent display or database, it is necessary to know when tokens change so that your displays can be updated. This is especially important as tokens can be replaced, changed, or deleted, and the associated overlays will then throw errors when you try to use them. Complete integration with token changes can be achieved via several very important hooks.

One interesting way to interact with the parser is to let it know that changes you are going to make will not require re-parsing.

Variable: semantic-edits-are-safe
When non-nil, modifications do not require a reparse. This prevents tokens from being marked dirty, and it prevents top level edits from causing a cache check. Use this when writing programs that could cause a full reparse, but will not change the tag structure, such as adding or updating top-level comments.

Next, it is sometimes useful to know what the current parsing state is. These function can let you know what level of re-parsing may be needed. Careful choices on when to reparse can make your program much faster.

Function: semantic-bovine-toplevel-full-reparse-needed-p &optional checkcache
Return non-nil if the current buffer needs a full reparse. Optional argument CHECKCACHE indicates if the cache check should be made.

Function: semantic-bovine-toplevel-partial-reparse-needed-p &optional checkcache
Return non-nil if the current buffer needs a partial reparse. This only returns non-nil if semantic-bovine-toplevel-full-reparse-needed-p returns nil. Optional argument CHECKCACHE indicates if the cache check should be made when checking semantic-bovine-toplevel-full-reparse-needed-p.

If you need very close interaction with the user's editing, then these two hooks can be used to find out when a given tag is being changed. These hooks could even be used to cut down on re-parsing if used correctly.

For all hooks, make sure you are careful to add it as a local hook if you only want to effect a single buffer. Setting it globally can cause unwanted effects if your program is concerned with a single buffer.

Variable: semantic-dirty-token-hooks
Hooks run after when a token is marked as dirty (edited by the user). The functions must take TOKEN, START, and END as a parameters. This hook will only be called once when a token is first made dirty, subsequent edits will not cause this to run a second time unless that token is first cleaned. Any token marked as dirty will also be called with semantic-clean-token-hooks, unless a full reparse is done instead.

Variable: semantic-clean-token-hooks
Hooks run after a token is marked as clean (re-parsed after user edits.) The functions must take a TOKEN as a parameter. Any token sent to this hook will have first been called with semantic-dirty-token-hooks. This hook is not called for tokens marked dirty if the buffer is completely re-parsed. In that case, use semantic-after-toplevel-cache-change-hook.

Variable: semantic-change-hooks
Hooks run when semantic detects a change in a buffer. Each hook function must take three arguments, identical to the common hook after-change-function.

Lastly, if you just want to know when a buffer changes, use this hook.

Variable: semantic-after-toplevel-bovinate-hook
Hooks run after a toplevel token parse. It is not run if the toplevel parse command is called, and buffer does not need to be fully re-parsed. This function is also called when the toplevel cache is flushed, and the cache is emptied. For language specific hooks, make sure you define this as a local hook.

This hook should not be used any more. Use semantic-after-toplevel-cache-change-hook instead.

Variable: semantic-after-toplevel-cache-change-hook
Hooks run after the buffer token list has changed. This list will change when a buffer is re-parsed, or when the token list in a buffer is cleared. It is *NOT* called if the current token list partially re-parsed.

Hook functions must take one argument, which is the new list of tokens associated with this buffer.

For language specific hooks, make sure you define this as a local hook.

Variable: semantic-after-partial-cache-change-hook
Hooks run after the buffer token list has been updated. This list will change when the current token list has been partially re-parsed.

Hook functions must take one argument, which is the list of tokens updated among the ones associated with this buffer.

For language specific hooks, make sure you define this as a local hook.

Variable: semantic-before-toplevel-cache-flush-hook
Hooks run before the toplevel nonterminal cache is flushed. For language specific hooks, make sure you define this as a local hook. This hook is called before a corresponding semantic-after-toplevel-cache-change-hook which is also called during a flush when the cache is given a new value of nil.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8 Programming Examples

Here are some simple examples that use different aspects of the semantic library APIs. For fully functional example programs with lots of comments, see the file `semantic-examples.el'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8.1 Interactively querying for a token name

If you need a command that asks the user for a token name, you can get full range completion using the query functions 9.5 Nonterminal completion.

 
(interactive (list (semantic-read-symbol "Symbol: ")))


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8.2 Finding a symbol in a buffer

If you have the name of a function or variable, and need to find its location in a buffer, you need a search function. There is a wide range of searches you can perform 9.2 Nonterminal streams.

 
(semantic-find-nonterminal-by-name
 "some-name"
 (current-buffer)
 t    ;; look inside structures and classes for these symbols
 nil) ;; do not look inside header files.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8.3 Finding a symbol in a project

If you have the name of a function or variable, and need to find its location somewhere in a project, you need to use the Semantic Database 11.3 Semantic Database. There are many search functions similar to the ones found in 9.2 Nonterminal streams.

The Semantic Database is interesting in that the return structure is not


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8.4 Locating a token in a buffer

If you have a nonterminal token, or a list of them, you may want to find their position in a buffer.

 
(semanticdb-find-nonterminal-by-name
 "symbol"
 nil   ;; Defaults to the current project's database list.
 t     ;; Search inside types
 nil   ;; Do not search include files
 nil   ;; Only search files in the same mode (all C files)
 t     ;; When a token is found, make sure it is loaded in a buffer.
 )

Of interesting note above, semanticdb can find symbols in files that are not loaded into an Emacs buffer. These tokens do not have an associated overlay, and the function semantic-token-buffer will fail.

The last parameter's tells the search function to find-file-noselect any file in which a matching token was found. This will allow you to merge all the tokens into a completion list, or other flat list needed by most functions that use association lists.

If you do not ask semanticdb to load those files, you will need to explicitly request the database object (found in the car of each sublist) get the file loaded. It is useful to not auto find all files if you don't need to jump to that token.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.8.5 Converting a token into a human readable string.

A nonterminal token is a rather unpleasant Lisp structure when trying to decipher what is going on. As such, there is a wide range of functions available that can convert a token into a human readable, and colorful string 9.6.1 Token->Text.

If you program interfaces with lots of users, you will probably want to have your program define a configurable variable that will let users change the visible portion of your program.

 
(defcustom my-summary-function 'semantic-uml-prototype-nonterminal
  "*Function to use when showing info about my token."
  :group 'my-program
  :type semantic-token->text-custom-list)

Note the special type provided by semantic.

Next, you can call this function to create a string.

 
(funcall my-summary-function token
                             token-parent
                             t ; use color
                             )

In this case, token-parent is an optional argument. In many cases, parent is not used by the outputting function. The parent may be a struct or class that contains token, or nil for top-level definitions. In particular, C++ needs the parent to correctly calculate the protection of each method.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on October, 2 2007 using texi2html