OpenASIP  2.0
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
TokenizerData::TokenTreeNode Class Reference

#include <OperationDAGLanguageParser.hh>

Collaboration diagram for TokenizerData::TokenTreeNode:
Collaboration graph

Public Member Functions

 TokenTreeNode ()
 
 TokenTreeNode (Token &token)
 
bool isFunctionCall () const
 
bool isAssignment () const
 
bool isInteger () const
 
long intValue () const
 
void addToTokenTree (Token &token)
 
const Tokentoken () const
 
int leafCount () const
 
TokenTreeNodeleaf (int index) const
 
std::string toStr () const
 

Static Private Member Functions

static void addToTokenTree (TokenTreeNode *currNode, TokenTreeNode *newToken)
 

Private Attributes

Tokendata_
 Token of token tree node. More...
 
std::vector< TokenTreeNode * > leafs_
 Leafs of this node. More...
 

Detailed Description

One node of tree of tokens.

Each node contains reference to it's token and it's sub-token-nodes..

root \ function / \ name postfix / | name name

Definition at line 397 of file OperationDAGLanguageParser.hh.

Constructor & Destructor Documentation

◆ TokenTreeNode() [1/2]

TokenizerData::TokenTreeNode::TokenTreeNode ( )
inline

Definition at line 399 of file OperationDAGLanguageParser.hh.

399 : data_(NULL) {}

◆ TokenTreeNode() [2/2]

TokenizerData::TokenTreeNode::TokenTreeNode ( Token token)
inline

Definition at line 400 of file OperationDAGLanguageParser.hh.

400 : data_(&token) {}

Member Function Documentation

◆ addToTokenTree() [1/2]

void TokenizerData::TokenTreeNode::addToTokenTree ( Token token)
inline

Adds newly parsed token to tree under this token tree node.

Parameters
tokenToken to add token tree.

Definition at line 485 of file OperationDAGLanguageParser.hh.

485  {
486  addToTokenTree(this, new TokenTreeNode(token));
487  }

◆ addToTokenTree() [2/2]

static void TokenizerData::TokenTreeNode::addToTokenTree ( TokenTreeNode currNode,
TokenTreeNode newToken 
)
inlinestaticprivate

Adds token tree node to under other token tree node.

Parameters
currNodeNode where to add new node.
newTokenToken tree node to add to currNode.

Definition at line 561 of file OperationDAGLanguageParser.hh.

562  {
563 
564  // check if token should be part of one of the leafs
565  for (std::vector<TokenTreeNode*>::iterator i =
566  currNode->leafs_.begin() ;
567  i != currNode->leafs_.end();i++) {
568  TokenTreeNode *currLeaf = *i;
569 
570  // if this token should be part of current leaf
571  if (newToken->data_->start_ >= currLeaf->data_->start_ &&
572  newToken->data_->end_ <= currLeaf->data_->end_) {
573 
574  addToTokenTree(currLeaf, newToken);
575  return;
576  }
577 
578  // move current leafs to inside this token
579  // if necessary
580  if (newToken->data_->start_ <= currLeaf->data_->start_ &&
581  newToken->data_->end_ >= currLeaf->data_->end_) {
582 
583  // remove from original
584  std::vector<TokenTreeNode*>::iterator temp = i;
585  i--;
586  currNode->leafs_.erase(temp);
587 
588  // add to new...
589  newToken->leafs_.push_back(currLeaf);
590  }
591 
592  // if start of current leaf is after end of this..
593  // add token to this point.
594  if (newToken->data_->end_ <= currLeaf->data_->start_) {
595  currNode->leafs_.insert(i, newToken);
596  return;
597  }
598  }
599 
600  // if there wasn't any sub nodes where value could go, add it to
601  // end of current node
602  currNode->leafs_.push_back(newToken);
603  return;
604  }

References data_, TokenizerData::Token::end_, leafs_, and TokenizerData::Token::start_.

◆ intValue()

long TokenizerData::TokenTreeNode::intValue ( ) const
inline

Definition at line 460 of file OperationDAGLanguageParser.hh.

460  {
461  if (!isInteger()) {
462  // TODO: throw exception
463  assert(false && "Node is not integer.");
464  }
465 
466  long retVal = 0;
467 
468  if (leafCount() == 2) {
469  retVal = leaf(1).token().intValue();
470  if (leaf(0).token().isMinusOperator()) {
471  retVal = -retVal;
472  }
473  } else {
474  retVal = token().intValue();
475  }
476 
477  return retVal;
478  }

References assert.

Referenced by OperationDAGBuilder::getBinding().

◆ isAssignment()

bool TokenizerData::TokenTreeNode::isAssignment ( ) const
inline

Returns true if node contain tokens of assignment

leafs must be: ANY, ASSIGNMENT_OPERATOR, ANY

Returns
true if node is assginment.

Definition at line 427 of file OperationDAGLanguageParser.hh.

427  {
428  if (leafCount() == 3 &&
429  leaf(1).token().type_ == ASSIGNMENT_OPERATOR) {
430  return true;
431  }
432  return false;
433  }

Referenced by OperationDAGBuilder::parseNode().

◆ isFunctionCall()

bool TokenizerData::TokenTreeNode::isFunctionCall ( ) const
inline

Returns true if node contain tokens of function call.

leafs must be: PRIMARY_EXPRESSION, POSTFIX_EXPRESSION

Returns
true if node is function call.

Definition at line 410 of file OperationDAGLanguageParser.hh.

410  {
411  if (leafCount() == 2 &&
412  leaf(0).token().type_ == PRIMARY_EXPRESSION &&
413  leaf(1).token().type_ == POSTFIX_EXPRESSION) {
414  return true;
415  }
416  return false;
417  }

Referenced by OperationDAGBuilder::getBinding(), OperationDAGBuilder::getVariableName(), and OperationDAGBuilder::parseNode().

◆ isInteger()

bool TokenizerData::TokenTreeNode::isInteger ( ) const
inline

Returns true if node contain integer value.

leafs must be: none, or (+|-, isIntegerLiteral)

Returns
true if node is integer value.

Definition at line 442 of file OperationDAGLanguageParser.hh.

442  {
443  if (
444  // if two part integer e.g. -1 or +1
445  (leafCount() == 2 &&
446  leaf(1).token().isIntegerLiteral() &&
447  (leaf(0).token().isPlusOperator() ||
448  leaf(0).token().isMinusOperator())) ||
449 
450  // or if just number without prefix
451  (leafCount() == 0 && token().isIntegerLiteral())
452  ) {
453 
454  return true;
455  }
456 
457  return false;
458  }

Referenced by OperationDAGBuilder::getBinding().

◆ leaf()

TokenTreeNode& TokenizerData::TokenTreeNode::leaf ( int  index) const
inline

Returns leaf of requested index.

Parameters
indexIndex of leaf to return.
Returns
leaf of requested index.

Definition at line 513 of file OperationDAGLanguageParser.hh.

513  {
514  assert(index < leafCount());
515  return *leafs_[index];
516  }

References assert.

Referenced by OperationDAGBuilder::getIOOperand(), OperationDAGBuilder::parse(), and OperationDAGBuilder::parseNode().

◆ leafCount()

int TokenizerData::TokenTreeNode::leafCount ( ) const
inline

Returns number of leafs that this token tree node has.

Returns
Number of leafs that this token tree node has.

Definition at line 503 of file OperationDAGLanguageParser.hh.

503  {
504  return leafs_.size();
505  }

Referenced by OperationDAGBuilder::getIOOperand(), OperationDAGBuilder::parse(), and OperationDAGBuilder::parseNode().

◆ token()

const Token& TokenizerData::TokenTreeNode::token ( ) const
inline

Returns referred token of token tree node.

Returns
Referred token of token tree node.

Definition at line 494 of file OperationDAGLanguageParser.hh.

494  {
495  return *data_;
496  }

Referenced by OperationDAGBuilder::assignVariable(), OperationDAGBuilder::declareVariable(), OperationDAGBuilder::getBinding(), OperationDAGBuilder::getIOOperand(), OperationDAGBuilder::getVariableName(), and OperationDAGBuilder::parseNode().

◆ toStr()

std::string TokenizerData::TokenTreeNode::toStr ( ) const
inline

Returns node and its sub nodes as a string.

Returns
Node and its sub nodes as a string.

Definition at line 524 of file OperationDAGLanguageParser.hh.

524  {
525  static int recursioLevel = 0;
526 
527  recursioLevel++;
528  std::stringstream retVal;
529 
530  if (data_ == NULL) {
531  retVal << "Root node" << std::endl;
532  } else {
533  retVal << setw(5) << data_->start_ << ":"
534  << setw(5) << std::left << data_->end_ << std::right
535  << setw(30) << TokenizerData::idString(data_->type_)
536  << " ";
537 
538  for (int fill = 0; fill < (recursioLevel-2)*2; fill++) {
539  retVal << "*";
540  }
541 
542  retVal << " " << data_->strValue_ << std::endl;
543  }
544 
545  for (int i = 0; i < leafCount(); i++) {
546  retVal << " " << leaf(i).toStr();
547  }
548 
549  recursioLevel--;
550  return retVal.str();
551  }

References TokenizerData::idString().

Referenced by OperationDAGBuilder::parseNode().

Here is the call graph for this function:

Member Data Documentation

◆ data_

Token* TokenizerData::TokenTreeNode::data_
private

Token of token tree node.

Definition at line 607 of file OperationDAGLanguageParser.hh.

Referenced by addToTokenTree().

◆ leafs_

std::vector<TokenTreeNode*> TokenizerData::TokenTreeNode::leafs_
private

Leafs of this node.

Definition at line 610 of file OperationDAGLanguageParser.hh.

Referenced by addToTokenTree().


The documentation for this class was generated from the following file:
TokenizerData::TokenTreeNode::isInteger
bool isInteger() const
Definition: OperationDAGLanguageParser.hh:442
TokenizerData::Token::end_
unsigned long end_
Definition: OperationDAGLanguageParser.hh:367
TokenizerData::PRIMARY_EXPRESSION
@ PRIMARY_EXPRESSION
Definition: OperationDAGLanguageParser.hh:159
TokenizerData::Token::strValue_
std::string strValue_
Definition: OperationDAGLanguageParser.hh:368
TokenizerData::TokenTreeNode::TokenTreeNode
TokenTreeNode()
Definition: OperationDAGLanguageParser.hh:399
TokenizerData::TokenTreeNode::addToTokenTree
void addToTokenTree(Token &token)
Definition: OperationDAGLanguageParser.hh:485
TokenizerData::TokenTreeNode::data_
Token * data_
Token of token tree node.
Definition: OperationDAGLanguageParser.hh:607
TokenizerData::idString
static std::string idString(OperationID id)
Definition: OperationDAGLanguageParser.hh:229
assert
#define assert(condition)
Definition: Application.hh:86
TokenizerData::TokenTreeNode::leaf
TokenTreeNode & leaf(int index) const
Definition: OperationDAGLanguageParser.hh:513
TokenizerData::TokenTreeNode::leafCount
int leafCount() const
Definition: OperationDAGLanguageParser.hh:503
TokenizerData::POSTFIX_EXPRESSION
@ POSTFIX_EXPRESSION
Definition: OperationDAGLanguageParser.hh:167
TokenizerData::TokenTreeNode::toStr
std::string toStr() const
Definition: OperationDAGLanguageParser.hh:524
TokenizerData::Token::start_
unsigned long start_
Definition: OperationDAGLanguageParser.hh:366
TokenizerData::Token::intValue
long intValue() const
Definition: OperationDAGLanguageParser.hh:333
TokenizerData::Token::type_
OperationID type_
Definition: OperationDAGLanguageParser.hh:369
TokenizerData::ASSIGNMENT_OPERATOR
@ ASSIGNMENT_OPERATOR
Definition: OperationDAGLanguageParser.hh:181
TokenizerData::TokenTreeNode::token
const Token & token() const
Definition: OperationDAGLanguageParser.hh:494
TokenizerData::TokenTreeNode::leafs_
std::vector< TokenTreeNode * > leafs_
Leafs of this node.
Definition: OperationDAGLanguageParser.hh:610