PUMA Library Reference Manual
Puma::Unit Class Reference

#include <Puma/Unit.h>

+ Inheritance diagram for Puma::Unit:

Description

Token chain abstraction of a source file or string.

A unit is the result of scanning a file or string containing recognizable source code. The unit consists of a list of tokens that directly map to the characters and words of the source code.

A unit can have different states, such as being modified. After changing something on a unit, its state should be set to being modified.

All source code manipulations resp. transformations provided by PUMA are based on manipulating the token chain. Tokens can be added, moved, removed, and so on.

Translating the token chain back to the source code text is done by printing the unit using method Puma::Unit::print() or as shown in the following example.

...
std::ofstream file("code.cc");
file << unit;
file.close();

Public Member Functions

 Unit ()
 Constructor. More...
 
 Unit (const List &list)
 Copy-constructor. More...
 
virtual ~Unit ()
 Destructor. More...
 
virtual void name (const char *name)
 Set the name of the unit (usually the file name). More...
 
char * name () const
 Get the name of the unit (usually the file name). More...
 
virtual void print (std::ostream &os) const
 Print the tokens of the unit on the given stream. More...
 
virtual bool isFile () const
 Check if this is a unit for a file. More...
 
virtual bool isMacroExp () const
 Check if this is a unit for a macro expansion. More...
 
virtual bool isTemplateInstance () const
 Check if this is a unit for a template instance. More...
 
UnitStatestate ()
 Get the state of the unit. More...
 
char * toString () const
 Serialize the tokens of the unit. More...
 
Tokenfirst () const
 Get the first token in the unit. More...
 
Tokenlast () const
 Get the last token in the unit. More...
 
Tokennext (const Token *token) const
 Get the next token of the given token. More...
 
Tokenprev (const Token *token) const
 Get the previous token of the given token. More...
 
- Public Member Functions inherited from Puma::List
 List ()
 Constructor. More...
 
 List (const List &copy)
 Copy-constructor. More...
 
 ~List ()
 Destructor. More...
 
Listoperator= (const List &copy)
 Assignment operator. More...
 
Listoperator+= (const List &list)
 Append the given list. More...
 
List operator+ (const List &list)
 Create a new list containing the elements of this list followed by the elements of the given list. More...
 
void clear ()
 Destroy the list elements. More...
 
void append (ListElement &element)
 Append an element to the list. More...
 
void prepend (ListElement &element)
 Prepend an element to the list. More...
 
void insert (ListElement *at, ListElement &element)
 Insert an element at the given list position. More...
 
void remove (ListElement *element)
 Remove the given element from the list. More...
 
void kill (ListElement *from, ListElement *to=(ListElement *) 0)
 Remove and destroy all elements between the given list elements. More...
 
void cut (List &out, ListElement *from, ListElement *to=(ListElement *) 0)
 Cut all elements between the given list elements. More...
 
Listcopy (ListElement *from=(ListElement *) 0, ListElement *to=(ListElement *) 0)
 Copy all elements between the given list elements. More...
 
void paste (ListElement *at, const List &l)
 Insert copies of the elements of the given list at the given list position. More...
 
void paste_before (ListElement *at, const List &l)
 Insert copies of the elements of the given list before the given list position. More...
 
void move (ListElement *at, List &l)
 Insert the elements of the given list at the given list position. More...
 
void move_before (ListElement *at, List &l)
 Insert the elements of the given list before the given list position. More...
 
bool empty () const
 Check if the list is empty. More...
 
const ListElementfirst () const
 Get the first element in the list. More...
 
const ListElementlast () const
 Get the last element in the list. More...
 
const ListElementnext (const ListElement *element) const
 Get the next element of the given element. More...
 
const ListElementprev (const ListElement *element) const
 Get the previous element of the given element. More...
 
- Public Member Functions inherited from Puma::Printable
virtual ~Printable ()
 Destructor. More...
 

Constructor & Destructor Documentation

Puma::Unit::Unit ( )
inline

Constructor.

Puma::Unit::Unit ( const List list)
inline

Copy-constructor.

Parameters
listThe token chain to copy.
virtual Puma::Unit::~Unit ( )
virtual

Destructor.

Destroys the tokens.

Member Function Documentation

Token* Puma::Unit::first ( ) const
inline

Get the first token in the unit.

Returns
The first token, or NULL if unit is empty.
virtual bool Puma::Unit::isFile ( ) const
inlinevirtual

Check if this is a unit for a file.

Returns
True if the tokens belong to a file.

Reimplemented in Puma::FileUnit.

virtual bool Puma::Unit::isMacroExp ( ) const
inlinevirtual

Check if this is a unit for a macro expansion.

Returns
True if the tokens belong to a macro expansion.

Reimplemented in Puma::MacroUnit.

virtual bool Puma::Unit::isTemplateInstance ( ) const
inlinevirtual

Check if this is a unit for a template instance.

Returns
True if the tokens belong to a template instance.
Token* Puma::Unit::last ( ) const
inline

Get the last token in the unit.

Returns
The last token or NULL if unit is empty.
virtual void Puma::Unit::name ( const char *  name)
virtual

Set the name of the unit (usually the file name).

Parameters
nameThe name.

Reimplemented in Puma::FileUnit.

char* Puma::Unit::name ( ) const
inline

Get the name of the unit (usually the file name).

Returns
The name of the unit.
Token* Puma::Unit::next ( const Token token) const
inline

Get the next token of the given token.

Returns
The next token, or NULL if the given token is NULL.
Token* Puma::Unit::prev ( const Token token) const
inline

Get the previous token of the given token.

Returns
The previous token, or NULL if the given token is NULL.
virtual void Puma::Unit::print ( std::ostream &  os) const
virtual

Print the tokens of the unit on the given stream.

Parameters
osThe output stream.

Implements Puma::Printable.

UnitState& Puma::Unit::state ( )
inline

Get the state of the unit.

Returns
A reference to the unit state.
char* Puma::Unit::toString ( ) const

Serialize the tokens of the unit.

Returns
A new string containing the tokens of the unit.