proxygen
cpp.ast.TypeConverter Class Reference
Inheritance diagram for cpp.ast.TypeConverter:

Public Member Functions

def __init__ (self, namespace_stack)
 
def ToType (self, tokens)
 
def DeclarationToParts (self, parts, needs_name_removed)
 
def ToParameters (self, tokens)
 
def CreateReturnType (self, return_type_seq)
 
def GetTemplateIndices (self, names)
 
def __init__ (self, namespace_stack)
 
def ToType (self, tokens)
 
def DeclarationToParts (self, parts, needs_name_removed)
 
def ToParameters (self, tokens)
 
def CreateReturnType (self, return_type_seq)
 
def GetTemplateIndices (self, names)
 
def __init__ (self, namespace_stack)
 
def ToType (self, tokens)
 
def DeclarationToParts (self, parts, needs_name_removed)
 
def ToParameters (self, tokens)
 
def CreateReturnType (self, return_type_seq)
 
def GetTemplateIndices (self, names)
 

Public Attributes

 namespace_stack
 

Private Member Functions

def _GetTemplateEnd (self, tokens, start)
 
def _GetTemplateEnd (self, tokens, start)
 
def _GetTemplateEnd (self, tokens, start)
 

Detailed Description

Definition at line 458 of file ast.py.

Constructor & Destructor Documentation

def cpp.ast.TypeConverter.__init__ (   self,
  namespace_stack 
)

Definition at line 460 of file ast.py.

Referenced by cpp.ast.TypeConverter.__init__(), and cpp.ast.TypeConverter.GetTemplateIndices().

460  def __init__(self, namespace_stack):
461  self.namespace_stack = namespace_stack
462 
def __init__(self, namespace_stack)
Definition: ast.py:460
def cpp.ast.TypeConverter.__init__ (   self,
  namespace_stack 
)

Definition at line 460 of file ast.py.

References cpp.ast.TypeConverter.__init__(), and cpp.ast.TypeConverter.namespace_stack.

460  def __init__(self, namespace_stack):
461  self.namespace_stack = namespace_stack
462 
def __init__(self, namespace_stack)
Definition: ast.py:460
def cpp.ast.TypeConverter.__init__ (   self,
  namespace_stack 
)

Definition at line 460 of file ast.py.

References cpp.ast.TypeConverter.__init__(), and cpp.ast.TypeConverter.namespace_stack.

460  def __init__(self, namespace_stack):
461  self.namespace_stack = namespace_stack
462 
def __init__(self, namespace_stack)
Definition: ast.py:460

Member Function Documentation

def cpp.ast.TypeConverter._GetTemplateEnd (   self,
  tokens,
  start 
)
private

Definition at line 463 of file ast.py.

Referenced by cpp.ast.TypeConverter._GetTemplateEnd(), cpp.ast.TypeConverter.DeclarationToParts(), and cpp.ast.TypeConverter.ToType().

463  def _GetTemplateEnd(self, tokens, start):
464  count = 1
465  end = start
466  while 1:
467  token = tokens[end]
468  end += 1
469  if token.name == '<':
470  count += 1
471  elif token.name == '>':
472  count -= 1
473  if count == 0:
474  break
475  return tokens[start:end-1], end
476 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def cpp.ast.TypeConverter._GetTemplateEnd (   self,
  tokens,
  start 
)
private

Definition at line 463 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd().

463  def _GetTemplateEnd(self, tokens, start):
464  count = 1
465  end = start
466  while 1:
467  token = tokens[end]
468  end += 1
469  if token.name == '<':
470  count += 1
471  elif token.name == '>':
472  count -= 1
473  if count == 0:
474  break
475  return tokens[start:end-1], end
476 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def cpp.ast.TypeConverter._GetTemplateEnd (   self,
  tokens,
  start 
)
private

Definition at line 463 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd().

463  def _GetTemplateEnd(self, tokens, start):
464  count = 1
465  end = start
466  while 1:
467  token = tokens[end]
468  end += 1
469  if token.name == '<':
470  count += 1
471  elif token.name == '>':
472  count -= 1
473  if count == 0:
474  break
475  return tokens[start:end-1], end
476 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def cpp.ast.TypeConverter.CreateReturnType (   self,
  return_type_seq 
)

Definition at line 652 of file ast.py.

References cpp.ast.TypeConverter.DeclarationToParts().

Referenced by cpp.ast.TypeConverter.CreateReturnType().

652  def CreateReturnType(self, return_type_seq):
653  if not return_type_seq:
654  return None
655  start = return_type_seq[0].start
656  end = return_type_seq[-1].end
657  _, name, templated_types, modifiers, default, other_tokens = \
658  self.DeclarationToParts(return_type_seq, False)
659  names = [n.name for n in other_tokens]
660  reference = '&' in names
661  pointer = '*' in names
662  array = '[' in names
663  return Type(start, end, name, templated_types, modifiers,
664  reference, pointer, array)
665 
def CreateReturnType(self, return_type_seq)
Definition: ast.py:652
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.CreateReturnType (   self,
  return_type_seq 
)

Definition at line 652 of file ast.py.

References cpp.ast.TypeConverter.CreateReturnType(), and cpp.ast.TypeConverter.DeclarationToParts().

652  def CreateReturnType(self, return_type_seq):
653  if not return_type_seq:
654  return None
655  start = return_type_seq[0].start
656  end = return_type_seq[-1].end
657  _, name, templated_types, modifiers, default, other_tokens = \
658  self.DeclarationToParts(return_type_seq, False)
659  names = [n.name for n in other_tokens]
660  reference = '&' in names
661  pointer = '*' in names
662  array = '[' in names
663  return Type(start, end, name, templated_types, modifiers,
664  reference, pointer, array)
665 
def CreateReturnType(self, return_type_seq)
Definition: ast.py:652
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.CreateReturnType (   self,
  return_type_seq 
)

Definition at line 652 of file ast.py.

References cpp.ast.TypeConverter.CreateReturnType(), and cpp.ast.TypeConverter.DeclarationToParts().

652  def CreateReturnType(self, return_type_seq):
653  if not return_type_seq:
654  return None
655  start = return_type_seq[0].start
656  end = return_type_seq[-1].end
657  _, name, templated_types, modifiers, default, other_tokens = \
658  self.DeclarationToParts(return_type_seq, False)
659  names = [n.name for n in other_tokens]
660  reference = '&' in names
661  pointer = '*' in names
662  array = '[' in names
663  return Type(start, end, name, templated_types, modifiers,
664  reference, pointer, array)
665 
def CreateReturnType(self, return_type_seq)
Definition: ast.py:652
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.DeclarationToParts (   self,
  parts,
  needs_name_removed 
)

Definition at line 536 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), cpp.ast.TypeConverter.DeclarationToParts(), folly.enumerate(), join, and cpp.ast.TypeConverter.ToType().

536  def DeclarationToParts(self, parts, needs_name_removed):
537  name = None
538  default = []
539  if needs_name_removed:
540  # Handle default (initial) values properly.
541  for i, t in enumerate(parts):
542  if t.name == '=':
543  default = parts[i+1:]
544  name = parts[i-1].name
545  if name == ']' and parts[i-2].name == '[':
546  name = parts[i-3].name
547  i -= 1
548  parts = parts[:i-1]
549  break
550  else:
551  if parts[-1].token_type == tokenize.NAME:
552  name = parts.pop().name
553  else:
554  # TODO(nnorwitz): this is a hack that happens for code like
555  # Register(Foo<T>); where it thinks this is a function call
556  # but it's actually a declaration.
557  name = '???'
558  modifiers = []
559  type_name = []
560  other_tokens = []
561  templated_types = []
562  i = 0
563  end = len(parts)
564  while i < end:
565  p = parts[i]
566  if keywords.IsKeyword(p.name):
567  modifiers.append(p.name)
568  elif p.name == '<':
569  templated_tokens, new_end = self._GetTemplateEnd(parts, i+1)
570  templated_types = self.ToType(templated_tokens)
571  i = new_end - 1
572  # Don't add a spurious :: to data members being initialized.
573  next_index = i + 1
574  if next_index < end and parts[next_index].name == '::':
575  i += 1
576  elif p.name in ('[', ']', '='):
577  # These are handled elsewhere.
578  other_tokens.append(p)
579  elif p.name not in ('*', '&', '>'):
580  # Ensure that names have a space between them.
581  if (type_name and type_name[-1].token_type == tokenize.NAME and
582  p.token_type == tokenize.NAME):
583  type_name.append(tokenize.Token(tokenize.SYNTAX, ' ', 0, 0))
584  type_name.append(p)
585  else:
586  other_tokens.append(p)
587  i += 1
588  type_name = ''.join([t.name for t in type_name])
589  return name, type_name, templated_types, modifiers, default, other_tokens
590 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
detail::RangeEnumerator< Range > enumerate(Range &&r)
Definition: Enumerate.h:167
#define join
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.DeclarationToParts (   self,
  parts,
  needs_name_removed 
)

Definition at line 536 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), folly.enumerate(), join, and cpp.ast.TypeConverter.ToType().

Referenced by cpp.ast.TypeConverter.CreateReturnType(), cpp.ast.TypeConverter.DeclarationToParts(), and cpp.ast.TypeConverter.ToParameters().

536  def DeclarationToParts(self, parts, needs_name_removed):
537  name = None
538  default = []
539  if needs_name_removed:
540  # Handle default (initial) values properly.
541  for i, t in enumerate(parts):
542  if t.name == '=':
543  default = parts[i+1:]
544  name = parts[i-1].name
545  if name == ']' and parts[i-2].name == '[':
546  name = parts[i-3].name
547  i -= 1
548  parts = parts[:i-1]
549  break
550  else:
551  if parts[-1].token_type == tokenize.NAME:
552  name = parts.pop().name
553  else:
554  # TODO(nnorwitz): this is a hack that happens for code like
555  # Register(Foo<T>); where it thinks this is a function call
556  # but it's actually a declaration.
557  name = '???'
558  modifiers = []
559  type_name = []
560  other_tokens = []
561  templated_types = []
562  i = 0
563  end = len(parts)
564  while i < end:
565  p = parts[i]
566  if keywords.IsKeyword(p.name):
567  modifiers.append(p.name)
568  elif p.name == '<':
569  templated_tokens, new_end = self._GetTemplateEnd(parts, i+1)
570  templated_types = self.ToType(templated_tokens)
571  i = new_end - 1
572  # Don't add a spurious :: to data members being initialized.
573  next_index = i + 1
574  if next_index < end and parts[next_index].name == '::':
575  i += 1
576  elif p.name in ('[', ']', '='):
577  # These are handled elsewhere.
578  other_tokens.append(p)
579  elif p.name not in ('*', '&', '>'):
580  # Ensure that names have a space between them.
581  if (type_name and type_name[-1].token_type == tokenize.NAME and
582  p.token_type == tokenize.NAME):
583  type_name.append(tokenize.Token(tokenize.SYNTAX, ' ', 0, 0))
584  type_name.append(p)
585  else:
586  other_tokens.append(p)
587  i += 1
588  type_name = ''.join([t.name for t in type_name])
589  return name, type_name, templated_types, modifiers, default, other_tokens
590 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
detail::RangeEnumerator< Range > enumerate(Range &&r)
Definition: Enumerate.h:167
#define join
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.DeclarationToParts (   self,
  parts,
  needs_name_removed 
)

Definition at line 536 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), cpp.ast.TypeConverter.DeclarationToParts(), folly.enumerate(), join, and cpp.ast.TypeConverter.ToType().

536  def DeclarationToParts(self, parts, needs_name_removed):
537  name = None
538  default = []
539  if needs_name_removed:
540  # Handle default (initial) values properly.
541  for i, t in enumerate(parts):
542  if t.name == '=':
543  default = parts[i+1:]
544  name = parts[i-1].name
545  if name == ']' and parts[i-2].name == '[':
546  name = parts[i-3].name
547  i -= 1
548  parts = parts[:i-1]
549  break
550  else:
551  if parts[-1].token_type == tokenize.NAME:
552  name = parts.pop().name
553  else:
554  # TODO(nnorwitz): this is a hack that happens for code like
555  # Register(Foo<T>); where it thinks this is a function call
556  # but it's actually a declaration.
557  name = '???'
558  modifiers = []
559  type_name = []
560  other_tokens = []
561  templated_types = []
562  i = 0
563  end = len(parts)
564  while i < end:
565  p = parts[i]
566  if keywords.IsKeyword(p.name):
567  modifiers.append(p.name)
568  elif p.name == '<':
569  templated_tokens, new_end = self._GetTemplateEnd(parts, i+1)
570  templated_types = self.ToType(templated_tokens)
571  i = new_end - 1
572  # Don't add a spurious :: to data members being initialized.
573  next_index = i + 1
574  if next_index < end and parts[next_index].name == '::':
575  i += 1
576  elif p.name in ('[', ']', '='):
577  # These are handled elsewhere.
578  other_tokens.append(p)
579  elif p.name not in ('*', '&', '>'):
580  # Ensure that names have a space between them.
581  if (type_name and type_name[-1].token_type == tokenize.NAME and
582  p.token_type == tokenize.NAME):
583  type_name.append(tokenize.Token(tokenize.SYNTAX, ' ', 0, 0))
584  type_name.append(p)
585  else:
586  other_tokens.append(p)
587  i += 1
588  type_name = ''.join([t.name for t in type_name])
589  return name, type_name, templated_types, modifiers, default, other_tokens
590 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
detail::RangeEnumerator< Range > enumerate(Range &&r)
Definition: Enumerate.h:167
#define join
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.GetTemplateIndices (   self,
  names 
)

Definition at line 666 of file ast.py.

Referenced by cpp.ast.TypeConverter.GetTemplateIndices().

666  def GetTemplateIndices(self, names):
667  # names is a list of strings.
668  start = names.index('<')
669  end = len(names) - 1
670  while end > 0:
671  if names[end] == '>':
672  break
673  end -= 1
674  return start, end+1
675 
def GetTemplateIndices(self, names)
Definition: ast.py:666
def cpp.ast.TypeConverter.GetTemplateIndices (   self,
  names 
)

Definition at line 666 of file ast.py.

References cpp.ast.TypeConverter.__init__(), cpp.ast.TypeConverter.GetTemplateIndices(), and object.

666  def GetTemplateIndices(self, names):
667  # names is a list of strings.
668  start = names.index('<')
669  end = len(names) - 1
670  while end > 0:
671  if names[end] == '>':
672  break
673  end -= 1
674  return start, end+1
675 
def GetTemplateIndices(self, names)
Definition: ast.py:666
def cpp.ast.TypeConverter.GetTemplateIndices (   self,
  names 
)

Definition at line 666 of file ast.py.

References cpp.ast.TypeConverter.__init__(), cpp.ast.TypeConverter.GetTemplateIndices(), and object.

666  def GetTemplateIndices(self, names):
667  # names is a list of strings.
668  start = names.index('<')
669  end = len(names) - 1
670  while end > 0:
671  if names[end] == '>':
672  break
673  end -= 1
674  return start, end+1
675 
def GetTemplateIndices(self, names)
Definition: ast.py:666
def cpp.ast.TypeConverter.ToParameters (   self,
  tokens 
)

Definition at line 591 of file ast.py.

References cpp.ast.TypeConverter.DeclarationToParts(), and cpp.ast.TypeConverter.ToParameters().

591  def ToParameters(self, tokens):
592  if not tokens:
593  return []
594 
595  result = []
596  name = type_name = ''
597  type_modifiers = []
598  pointer = reference = array = False
599  first_token = None
600  default = []
601 
602  def AddParameter(end):
603  if default:
604  del default[0] # Remove flag.
605  parts = self.DeclarationToParts(type_modifiers, True)
606  (name, type_name, templated_types, modifiers,
607  unused_default, unused_other_tokens) = parts
608  parameter_type = Type(first_token.start, first_token.end,
609  type_name, templated_types, modifiers,
610  reference, pointer, array)
611  p = Parameter(first_token.start, end, name,
612  parameter_type, default)
613  result.append(p)
614 
615  template_count = 0
616  for s in tokens:
617  if not first_token:
618  first_token = s
619  if s.name == '<':
620  template_count += 1
621  elif s.name == '>':
622  template_count -= 1
623  if template_count > 0:
624  type_modifiers.append(s)
625  continue
626 
627  if s.name == ',':
628  AddParameter(s.start)
629  name = type_name = ''
630  type_modifiers = []
631  pointer = reference = array = False
632  first_token = None
633  default = []
634  elif s.name == '*':
635  pointer = True
636  elif s.name == '&':
637  reference = True
638  elif s.name == '[':
639  array = True
640  elif s.name == ']':
641  pass # Just don't add to type_modifiers.
642  elif s.name == '=':
643  # Got a default value. Add any value (None) as a flag.
644  default.append(None)
645  elif default:
646  default.append(s)
647  else:
648  type_modifiers.append(s)
649  AddParameter(tokens[-1].end)
650  return result
651 
def ToParameters(self, tokens)
Definition: ast.py:591
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.ToParameters (   self,
  tokens 
)

Definition at line 591 of file ast.py.

References cpp.ast.TypeConverter.DeclarationToParts(), and cpp.ast.TypeConverter.ToParameters().

591  def ToParameters(self, tokens):
592  if not tokens:
593  return []
594 
595  result = []
596  name = type_name = ''
597  type_modifiers = []
598  pointer = reference = array = False
599  first_token = None
600  default = []
601 
602  def AddParameter(end):
603  if default:
604  del default[0] # Remove flag.
605  parts = self.DeclarationToParts(type_modifiers, True)
606  (name, type_name, templated_types, modifiers,
607  unused_default, unused_other_tokens) = parts
608  parameter_type = Type(first_token.start, first_token.end,
609  type_name, templated_types, modifiers,
610  reference, pointer, array)
611  p = Parameter(first_token.start, end, name,
612  parameter_type, default)
613  result.append(p)
614 
615  template_count = 0
616  for s in tokens:
617  if not first_token:
618  first_token = s
619  if s.name == '<':
620  template_count += 1
621  elif s.name == '>':
622  template_count -= 1
623  if template_count > 0:
624  type_modifiers.append(s)
625  continue
626 
627  if s.name == ',':
628  AddParameter(s.start)
629  name = type_name = ''
630  type_modifiers = []
631  pointer = reference = array = False
632  first_token = None
633  default = []
634  elif s.name == '*':
635  pointer = True
636  elif s.name == '&':
637  reference = True
638  elif s.name == '[':
639  array = True
640  elif s.name == ']':
641  pass # Just don't add to type_modifiers.
642  elif s.name == '=':
643  # Got a default value. Add any value (None) as a flag.
644  default.append(None)
645  elif default:
646  default.append(s)
647  else:
648  type_modifiers.append(s)
649  AddParameter(tokens[-1].end)
650  return result
651 
def ToParameters(self, tokens)
Definition: ast.py:591
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.ToParameters (   self,
  tokens 
)

Definition at line 591 of file ast.py.

References cpp.ast.TypeConverter.DeclarationToParts().

Referenced by cpp.ast.TypeConverter.ToParameters().

591  def ToParameters(self, tokens):
592  if not tokens:
593  return []
594 
595  result = []
596  name = type_name = ''
597  type_modifiers = []
598  pointer = reference = array = False
599  first_token = None
600  default = []
601 
602  def AddParameter(end):
603  if default:
604  del default[0] # Remove flag.
605  parts = self.DeclarationToParts(type_modifiers, True)
606  (name, type_name, templated_types, modifiers,
607  unused_default, unused_other_tokens) = parts
608  parameter_type = Type(first_token.start, first_token.end,
609  type_name, templated_types, modifiers,
610  reference, pointer, array)
611  p = Parameter(first_token.start, end, name,
612  parameter_type, default)
613  result.append(p)
614 
615  template_count = 0
616  for s in tokens:
617  if not first_token:
618  first_token = s
619  if s.name == '<':
620  template_count += 1
621  elif s.name == '>':
622  template_count -= 1
623  if template_count > 0:
624  type_modifiers.append(s)
625  continue
626 
627  if s.name == ',':
628  AddParameter(s.start)
629  name = type_name = ''
630  type_modifiers = []
631  pointer = reference = array = False
632  first_token = None
633  default = []
634  elif s.name == '*':
635  pointer = True
636  elif s.name == '&':
637  reference = True
638  elif s.name == '[':
639  array = True
640  elif s.name == ']':
641  pass # Just don't add to type_modifiers.
642  elif s.name == '=':
643  # Got a default value. Add any value (None) as a flag.
644  default.append(None)
645  elif default:
646  default.append(s)
647  else:
648  type_modifiers.append(s)
649  AddParameter(tokens[-1].end)
650  return result
651 
def ToParameters(self, tokens)
Definition: ast.py:591
def DeclarationToParts(self, parts, needs_name_removed)
Definition: ast.py:536
def cpp.ast.TypeConverter.ToType (   self,
  tokens 
)
Convert [Token,...] to [Class(...), ] useful for base classes.
For example, code like class Foo : public Bar<x, y> { ... };
the "Bar<x, y>" portion gets converted to an AST.

Returns:
  [Class(...), ...]

Definition at line 477 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), join, and cpp.ast.TypeConverter.ToType().

477  def ToType(self, tokens):
478  """Convert [Token,...] to [Class(...), ] useful for base classes.
479  For example, code like class Foo : public Bar<x, y> { ... };
480  the "Bar<x, y>" portion gets converted to an AST.
481 
482  Returns:
483  [Class(...), ...]
484  """
485  result = []
486  name_tokens = []
487  reference = pointer = array = False
488 
489  def AddType(templated_types):
490  # Partition tokens into name and modifier tokens.
491  names = []
492  modifiers = []
493  for t in name_tokens:
494  if keywords.IsKeyword(t.name):
495  modifiers.append(t.name)
496  else:
497  names.append(t.name)
498  name = ''.join(names)
499  if name_tokens:
500  result.append(Type(name_tokens[0].start, name_tokens[-1].end,
501  name, templated_types, modifiers,
502  reference, pointer, array))
503  del name_tokens[:]
504 
505  i = 0
506  end = len(tokens)
507  while i < end:
508  token = tokens[i]
509  if token.name == '<':
510  new_tokens, new_end = self._GetTemplateEnd(tokens, i+1)
511  AddType(self.ToType(new_tokens))
512  # If there is a comma after the template, we need to consume
513  # that here otherwise it becomes part of the name.
514  i = new_end
515  reference = pointer = array = False
516  elif token.name == ',':
517  AddType([])
518  reference = pointer = array = False
519  elif token.name == '*':
520  pointer = True
521  elif token.name == '&':
522  reference = True
523  elif token.name == '[':
524  pointer = True
525  elif token.name == ']':
526  pass
527  else:
528  name_tokens.append(token)
529  i += 1
530 
531  if name_tokens:
532  # No '<' in the tokens, just a simple name and no template.
533  AddType([])
534  return result
535 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
#define join
def cpp.ast.TypeConverter.ToType (   self,
  tokens 
)
Convert [Token,...] to [Class(...), ] useful for base classes.
For example, code like class Foo : public Bar<x, y> { ... };
the "Bar<x, y>" portion gets converted to an AST.

Returns:
  [Class(...), ...]

Definition at line 477 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), join, and cpp.ast.TypeConverter.ToType().

Referenced by cpp.ast.TypeConverter.DeclarationToParts(), and cpp.ast.TypeConverter.ToType().

477  def ToType(self, tokens):
478  """Convert [Token,...] to [Class(...), ] useful for base classes.
479  For example, code like class Foo : public Bar<x, y> { ... };
480  the "Bar<x, y>" portion gets converted to an AST.
481 
482  Returns:
483  [Class(...), ...]
484  """
485  result = []
486  name_tokens = []
487  reference = pointer = array = False
488 
489  def AddType(templated_types):
490  # Partition tokens into name and modifier tokens.
491  names = []
492  modifiers = []
493  for t in name_tokens:
494  if keywords.IsKeyword(t.name):
495  modifiers.append(t.name)
496  else:
497  names.append(t.name)
498  name = ''.join(names)
499  if name_tokens:
500  result.append(Type(name_tokens[0].start, name_tokens[-1].end,
501  name, templated_types, modifiers,
502  reference, pointer, array))
503  del name_tokens[:]
504 
505  i = 0
506  end = len(tokens)
507  while i < end:
508  token = tokens[i]
509  if token.name == '<':
510  new_tokens, new_end = self._GetTemplateEnd(tokens, i+1)
511  AddType(self.ToType(new_tokens))
512  # If there is a comma after the template, we need to consume
513  # that here otherwise it becomes part of the name.
514  i = new_end
515  reference = pointer = array = False
516  elif token.name == ',':
517  AddType([])
518  reference = pointer = array = False
519  elif token.name == '*':
520  pointer = True
521  elif token.name == '&':
522  reference = True
523  elif token.name == '[':
524  pointer = True
525  elif token.name == ']':
526  pass
527  else:
528  name_tokens.append(token)
529  i += 1
530 
531  if name_tokens:
532  # No '<' in the tokens, just a simple name and no template.
533  AddType([])
534  return result
535 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
#define join
def cpp.ast.TypeConverter.ToType (   self,
  tokens 
)
Convert [Token,...] to [Class(...), ] useful for base classes.
For example, code like class Foo : public Bar<x, y> { ... };
the "Bar<x, y>" portion gets converted to an AST.

Returns:
  [Class(...), ...]

Definition at line 477 of file ast.py.

References cpp.ast.TypeConverter._GetTemplateEnd(), join, and cpp.ast.TypeConverter.ToType().

477  def ToType(self, tokens):
478  """Convert [Token,...] to [Class(...), ] useful for base classes.
479  For example, code like class Foo : public Bar<x, y> { ... };
480  the "Bar<x, y>" portion gets converted to an AST.
481 
482  Returns:
483  [Class(...), ...]
484  """
485  result = []
486  name_tokens = []
487  reference = pointer = array = False
488 
489  def AddType(templated_types):
490  # Partition tokens into name and modifier tokens.
491  names = []
492  modifiers = []
493  for t in name_tokens:
494  if keywords.IsKeyword(t.name):
495  modifiers.append(t.name)
496  else:
497  names.append(t.name)
498  name = ''.join(names)
499  if name_tokens:
500  result.append(Type(name_tokens[0].start, name_tokens[-1].end,
501  name, templated_types, modifiers,
502  reference, pointer, array))
503  del name_tokens[:]
504 
505  i = 0
506  end = len(tokens)
507  while i < end:
508  token = tokens[i]
509  if token.name == '<':
510  new_tokens, new_end = self._GetTemplateEnd(tokens, i+1)
511  AddType(self.ToType(new_tokens))
512  # If there is a comma after the template, we need to consume
513  # that here otherwise it becomes part of the name.
514  i = new_end
515  reference = pointer = array = False
516  elif token.name == ',':
517  AddType([])
518  reference = pointer = array = False
519  elif token.name == '*':
520  pointer = True
521  elif token.name == '&':
522  reference = True
523  elif token.name == '[':
524  pointer = True
525  elif token.name == ']':
526  pass
527  else:
528  name_tokens.append(token)
529  i += 1
530 
531  if name_tokens:
532  # No '<' in the tokens, just a simple name and no template.
533  AddType([])
534  return result
535 
def _GetTemplateEnd(self, tokens, start)
Definition: ast.py:463
def ToType(self, tokens)
Definition: ast.py:477
#define join

Member Data Documentation


The documentation for this class was generated from the following file: