Python token.NAME Examples

The following are 30 code examples of token.NAME(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module token , or try the search function .
Example #1
Source File: reference.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def value(self, value):
    value_parts = value.split('.')

    # If we have too many children, cut the list down to size.
    # pylint: disable=attribute-defined-outside-init
    self._children = self._children[:len(value_parts)]

    # Update child nodes.
    for child, value_part in itertools.izip_longest(
        self._children, value_parts):
      if child:
        # Modify existing children. This helps preserve comments and spaces.
        child.children[-1].value = value_part
      else:
        # Add children as needed.
        token_snippets = [
            snippet.TokenSnippet.Create(token.DOT, '.'),
            snippet.TokenSnippet.Create(token.NAME, value_part),
        ]
        self._children.append(snippet.Symbol(symbol.trailer, token_snippets)) 
Example #2
Source File: pyclbr.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _getname(g):
    # Helper to get a dotted name, return a pair (name, token) where
    # name is the dotted name, or None if there was no dotted name,
    # and token is the next input token.
    parts = []
    tokentype, token = g.next()[0:2]
    if tokentype != NAME and token != '*':
        return (None, token)
    parts.append(token)
    while True:
        tokentype, token = g.next()[0:2]
        if token != '.':
            break
        tokentype, token = g.next()[0:2]
        if tokentype != NAME:
            break
        parts.append(token)
    return (".".join(parts), token) 
Example #3
Source File: pkg_resources.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def evaluate(cls, nodelist):
        while len(nodelist)==2: nodelist = nodelist[1]
        kind = nodelist[0]
        name = nodelist[1]
        if kind==token.NAME:
            try:
                op = cls.values[name]
            except KeyError:
                raise SyntaxError("Unknown name %r" % name)
            return op()
        if kind==token.STRING:
            s = nodelist[1]
            if not cls._safe_string(s):
                raise SyntaxError(
                    "Only plain strings allowed in environment markers")
            return s[1:-1]
        msg = "Language feature not supported in environment markers"
        raise SyntaxError(msg) 
Example #4
Source File: pyclbr.py    From meddle with MIT License 6 votes vote down vote up
def _getname(g):
    # Helper to get a dotted name, return a pair (name, token) where
    # name is the dotted name, or None if there was no dotted name,
    # and token is the next input token.
    parts = []
    tokentype, token = g.next()[0:2]
    if tokentype != NAME and token != '*':
        return (None, token)
    parts.append(token)
    while True:
        tokentype, token = g.next()[0:2]
        if token != '.':
            break
        tokentype, token = g.next()[0:2]
        if tokentype != NAME:
            break
        parts.append(token)
    return (".".join(parts), token) 
Example #5
Source File: transformer.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def classdef(self, nodelist):
        # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite

        name = nodelist[1][1]
        doc = self.get_docstring(nodelist[-1])
        if nodelist[2][0] == token.COLON:
            bases = []
        elif nodelist[3][0] == token.RPAR:
            bases = []
        else:
            bases = self.com_bases(nodelist[3])

        # code for class
        code = self.com_node(nodelist[-1])

        if doc is not None:
            assert isinstance(code, Stmt)
            assert isinstance(code.nodes[0], Discard)
            del code.nodes[0]

        return Class(name, bases, doc, code, lineno=nodelist[1][2]) 
Example #6
Source File: pkg_resources.py    From pledgeservice with Apache License 2.0 6 votes vote down vote up
def comparison(cls, nodelist):
        if len(nodelist) > 4:
            msg = "Chained comparison not allowed in environment markers"
            raise SyntaxError(msg)
        comp = nodelist[2][1]
        cop = comp[1]
        if comp[0] == token.NAME:
            if len(nodelist[2]) == 3:
                if cop == 'not':
                    cop = 'not in'
                else:
                    cop = 'is not'
        try:
            cop = cls.get_op(cop)
        except KeyError:
            msg = repr(cop) + " operator not allowed in environment markers"
            raise SyntaxError(msg)
        return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3])) 
Example #7
Source File: __init__.py    From jbox with MIT License 6 votes vote down vote up
def comparison(cls, nodelist):
        if len(nodelist) > 4:
            msg = "Chained comparison not allowed in environment markers"
            raise SyntaxError(msg)
        comp = nodelist[2][1]
        cop = comp[1]
        if comp[0] == token.NAME:
            if len(nodelist[2]) == 3:
                if cop == 'not':
                    cop = 'not in'
                else:
                    cop = 'is not'
        try:
            cop = cls.get_op(cop)
        except KeyError:
            msg = repr(cop) + " operator not allowed in environment markers"
            raise SyntaxError(msg)
        return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3])) 
Example #8
Source File: transformer.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #9
Source File: __init__.py    From lambda-chef-node-cleanup with Apache License 2.0 6 votes vote down vote up
def comparison(cls, nodelist):
        if len(nodelist) > 4:
            msg = "Chained comparison not allowed in environment markers"
            raise SyntaxError(msg)
        comp = nodelist[2][1]
        cop = comp[1]
        if comp[0] == token.NAME:
            if len(nodelist[2]) == 3:
                if cop == 'not':
                    cop = 'not in'
                else:
                    cop = 'is not'
        try:
            cop = cls.get_op(cop)
        except KeyError:
            msg = repr(cop) + " operator not allowed in environment markers"
            raise SyntaxError(msg)
        return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3])) 
Example #10
Source File: __init__.py    From lambda-chef-node-cleanup with Apache License 2.0 6 votes vote down vote up
def evaluate(cls, nodelist):
        while len(nodelist)==2: nodelist = nodelist[1]
        kind = nodelist[0]
        name = nodelist[1]
        if kind==token.NAME:
            try:
                op = cls.values[name]
            except KeyError:
                raise SyntaxError("Unknown name %r" % name)
            return op()
        if kind==token.STRING:
            s = nodelist[1]
            if not cls._safe_string(s):
                raise SyntaxError(
                    "Only plain strings allowed in environment markers")
            return s[1:-1]
        msg = "Language feature not supported in environment markers"
        raise SyntaxError(msg) 
Example #11
Source File: pyclbr.py    From BinderFilter with MIT License 6 votes vote down vote up
def _getname(g):
    # Helper to get a dotted name, return a pair (name, token) where
    # name is the dotted name, or None if there was no dotted name,
    # and token is the next input token.
    parts = []
    tokentype, token = g.next()[0:2]
    if tokentype != NAME and token != '*':
        return (None, token)
    parts.append(token)
    while True:
        tokentype, token = g.next()[0:2]
        if token != '.':
            break
        tokentype, token = g.next()[0:2]
        if tokentype != NAME:
            break
        parts.append(token)
    return (".".join(parts), token) 
Example #12
Source File: transformer.py    From BinderFilter with MIT License 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #13
Source File: pyclbr.py    From Computable with MIT License 6 votes vote down vote up
def _getname(g):
    # Helper to get a dotted name, return a pair (name, token) where
    # name is the dotted name, or None if there was no dotted name,
    # and token is the next input token.
    parts = []
    tokentype, token = g.next()[0:2]
    if tokentype != NAME and token != '*':
        return (None, token)
    parts.append(token)
    while True:
        tokentype, token = g.next()[0:2]
        if token != '.':
            break
        tokentype, token = g.next()[0:2]
        if tokentype != NAME:
            break
        parts.append(token)
    return (".".join(parts), token) 
Example #14
Source File: pkg_resources.py    From oss-ftp with MIT License 6 votes vote down vote up
def comparison(cls, nodelist):
        if len(nodelist)>4:
            raise SyntaxError("Chained comparison not allowed in environment markers")
        comp = nodelist[2][1]
        cop = comp[1]
        if comp[0] == token.NAME:
            if len(nodelist[2]) == 3:
                if cop == 'not':
                    cop = 'not in'
                else:
                    cop = 'is not'
        try:
            cop = cls.get_op(cop)
        except KeyError:
            raise SyntaxError(repr(cop)+" operator not allowed in environment markers")
        return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3])) 
Example #15
Source File: pkg_resources.py    From oss-ftp with MIT License 6 votes vote down vote up
def evaluate(cls, nodelist):
        while len(nodelist)==2: nodelist = nodelist[1]
        kind = nodelist[0]
        name = nodelist[1]
        if kind==token.NAME:
            try:
                op = cls.values[name]
            except KeyError:
                raise SyntaxError("Unknown name %r" % name)
            return op()
        if kind==token.STRING:
            s = nodelist[1]
            if s[:1] not in "'\"" or s.startswith('"""') or s.startswith("'''") \
                    or '\\' in s:
                raise SyntaxError(
                    "Only plain strings allowed in environment markers")
            return s[1:-1]
        raise SyntaxError("Language feature not supported in environment markers") 
Example #16
Source File: pkg_resources.py    From oss-ftp with MIT License 6 votes vote down vote up
def comparison(cls, nodelist):
        if len(nodelist) > 4:
            msg = "Chained comparison not allowed in environment markers"
            raise SyntaxError(msg)
        comp = nodelist[2][1]
        cop = comp[1]
        if comp[0] == token.NAME:
            if len(nodelist[2]) == 3:
                if cop == 'not':
                    cop = 'not in'
                else:
                    cop = 'is not'
        try:
            cop = cls.get_op(cop)
        except KeyError:
            msg = repr(cop) + " operator not allowed in environment markers"
            raise SyntaxError(msg)
        return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3])) 
Example #17
Source File: pkg_resources.py    From oss-ftp with MIT License 6 votes vote down vote up
def evaluate(cls, nodelist):
        while len(nodelist)==2: nodelist = nodelist[1]
        kind = nodelist[0]
        name = nodelist[1]
        if kind==token.NAME:
            try:
                op = cls.values[name]
            except KeyError:
                raise SyntaxError("Unknown name %r" % name)
            return op()
        if kind==token.STRING:
            s = nodelist[1]
            if not cls._safe_string(s):
                raise SyntaxError(
                    "Only plain strings allowed in environment markers")
            return s[1:-1]
        msg = "Language feature not supported in environment markers"
        raise SyntaxError(msg) 
Example #18
Source File: transformer.py    From oss-ftp with MIT License 6 votes vote down vote up
def classdef(self, nodelist):
        # classdef: 'class' NAME ['(' [testlist] ')'] ':' suite

        name = nodelist[1][1]
        doc = self.get_docstring(nodelist[-1])
        if nodelist[2][0] == token.COLON:
            bases = []
        elif nodelist[3][0] == token.RPAR:
            bases = []
        else:
            bases = self.com_bases(nodelist[3])

        # code for class
        code = self.com_node(nodelist[-1])

        if doc is not None:
            assert isinstance(code, Stmt)
            assert isinstance(code.nodes[0], Discard)
            del code.nodes[0]

        return Class(name, bases, doc, code, lineno=nodelist[1][2]) 
Example #19
Source File: transformer.py    From oss-ftp with MIT License 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #20
Source File: reference.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def Annotate(cls, nodes):
    if not nodes:
      return None
    if nodes[0].type != symbol.atom:
      return None
    if not nodes[0].children or nodes[0].children[0].type != token.NAME:
      return None

    for i in xrange(1, len(nodes)):
      if not nodes:
        break
      if nodes[i].type != symbol.trailer:
        break
      if len(nodes[i].children) != 2:
        break
      if (nodes[i].children[0].type != token.DOT or
          nodes[i].children[1].type != token.NAME):
        break
    else:
      i = len(nodes)

    return [cls(nodes[:i])] + nodes[i:] 
Example #21
Source File: reference.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def value(self, value):
    value_parts = value.split('.')

    # If we have too many children, cut the list down to size.
    # pylint: disable=attribute-defined-outside-init
    self._children = self._children[:len(value_parts)]

    # Update child nodes.
    for child, value_part in itertools.izip_longest(
        self._children, value_parts):
      if child:
        # Modify existing children. This helps preserve comments and spaces.
        child.children[-1].value = value_part
      else:
        # Add children as needed.
        token_snippets = [
            snippet.TokenSnippet.Create(token.DOT, '.'),
            snippet.TokenSnippet.Create(token.NAME, value_part),
        ]
        self._children.append(snippet.Symbol(symbol.trailer, token_snippets)) 
Example #22
Source File: import_statement.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def value(self, value):
    value_parts = value.split('.')
    for value_part in value_parts:
      if keyword.iskeyword(value_part):
        raise ValueError('%s is a reserved keyword.' % value_part)

    # If we have too many children, cut the list down to size.
    # pylint: disable=attribute-defined-outside-init
    self._children = self._children[:len(value_parts)*2-1]

    # Update child nodes.
    for child, value_part in itertools.izip_longest(
        self._children[::2], value_parts):
      if child:
        # Modify existing children. This helps preserve comments and spaces.
        child.value = value_part
      else:
        # Add children as needed.
        self._children.append(snippet.TokenSnippet.Create(token.DOT, '.'))
        self._children.append(
            snippet.TokenSnippet.Create(token.NAME, value_part)) 
Example #23
Source File: import_statement.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def alias(self, value):
    if keyword.iskeyword(value):
      raise ValueError('%s is a reserved keyword.' % value)

    if value:
       # pylint: disable=access-member-before-definition
      if len(self.children) < 3:
        # If we currently have no alias, add one.
         # pylint: disable=access-member-before-definition
        self.children.append(
            snippet.TokenSnippet.Create(token.NAME, 'as', (0, 1)))
         # pylint: disable=access-member-before-definition
        self.children.append(
            snippet.TokenSnippet.Create(token.NAME, value, (0, 1)))
      else:
        # We already have an alias. Just update the value.
        # pylint: disable=access-member-before-definition
        self.children[2].value = value
    else:
      # Removing the alias. Strip the "as foo".
      self.children = [self.children[0]] # pylint: disable=line-too-long, attribute-defined-outside-init 
Example #24
Source File: reference.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def Annotate(cls, nodes):
    if not nodes:
      return None
    if nodes[0].type != symbol.atom:
      return None
    if not nodes[0].children or nodes[0].children[0].type != token.NAME:
      return None

    for i in xrange(1, len(nodes)):
      if not nodes:
        break
      if nodes[i].type != symbol.trailer:
        break
      if len(nodes[i].children) != 2:
        break
      if (nodes[i].children[0].type != token.DOT or
          nodes[i].children[1].type != token.NAME):
        break
    else:
      i = len(nodes)

    return [cls(nodes[:i])] + nodes[i:] 
Example #25
Source File: import_statement.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def value(self, value):
    value_parts = value.split('.')
    for value_part in value_parts:
      if keyword.iskeyword(value_part):
        raise ValueError('%s is a reserved keyword.' % value_part)

    # If we have too many children, cut the list down to size.
    # pylint: disable=attribute-defined-outside-init
    self._children = self._children[:len(value_parts)*2-1]

    # Update child nodes.
    for child, value_part in itertools.izip_longest(
        self._children[::2], value_parts):
      if child:
        # Modify existing children. This helps preserve comments and spaces.
        child.value = value_part
      else:
        # Add children as needed.
        self._children.append(snippet.TokenSnippet.Create(token.DOT, '.'))
        self._children.append(
            snippet.TokenSnippet.Create(token.NAME, value_part)) 
Example #26
Source File: import_statement.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def alias(self, value):
    if keyword.iskeyword(value):
      raise ValueError('%s is a reserved keyword.' % value)

    if value:
       # pylint: disable=access-member-before-definition
      if len(self.children) < 3:
        # If we currently have no alias, add one.
         # pylint: disable=access-member-before-definition
        self.children.append(
            snippet.TokenSnippet.Create(token.NAME, 'as', (0, 1)))
         # pylint: disable=access-member-before-definition
        self.children.append(
            snippet.TokenSnippet.Create(token.NAME, value, (0, 1)))
      else:
        # We already have an alias. Just update the value.
        # pylint: disable=access-member-before-definition
        self.children[2].value = value
    else:
      # Removing the alias. Strip the "as foo".
      self.children = [self.children[0]] # pylint: disable=line-too-long, attribute-defined-outside-init 
Example #27
Source File: reference.py    From Jandroid with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def Annotate(cls, nodes):
    if not nodes:
      return None
    if nodes[0].type != symbol.atom:
      return None
    if not nodes[0].children or nodes[0].children[0].type != token.NAME:
      return None

    for i in xrange(1, len(nodes)):
      if not nodes:
        break
      if nodes[i].type != symbol.trailer:
        break
      if len(nodes[i].children) != 2:
        break
      if (nodes[i].children[0].type != token.DOT or
          nodes[i].children[1].type != token.NAME):
        break
    else:
      i = len(nodes)

    return [cls(nodes[:i])] + nodes[i:] 
Example #28
Source File: __init__.py    From jbox with MIT License 6 votes vote down vote up
def evaluate(cls, nodelist):
        while len(nodelist)==2: nodelist = nodelist[1]
        kind = nodelist[0]
        name = nodelist[1]
        if kind==token.NAME:
            try:
                op = cls.values[name]
            except KeyError:
                raise SyntaxError("Unknown name %r" % name)
            return op()
        if kind==token.STRING:
            s = nodelist[1]
            if not cls._safe_string(s):
                raise SyntaxError(
                    "Only plain strings allowed in environment markers")
            return s[1:-1]
        msg = "Language feature not supported in environment markers"
        raise SyntaxError(msg) 
Example #29
Source File: transformer.py    From oss-ftp with MIT License 5 votes vote down vote up
def decorator_name(self, nodelist):
        listlen = len(nodelist)
        assert listlen >= 1 and listlen % 2 == 1

        item = self.atom_name(nodelist)
        i = 1
        while i < listlen:
            assert nodelist[i][0] == token.DOT
            assert nodelist[i + 1][0] == token.NAME
            item = Getattr(item, nodelist[i + 1][1])
            i += 2

        return item 
Example #30
Source File: PySourceColor.py    From mishkal with GNU General Public License v3.0 5 votes vote down vote up
def _doCSSFooter(self):
        # Optional
        if self.footer != '':
            self.out.write('%s\n'%self.footer)
        else:
            self.out.write('<hr><div class="%s"># %s <br> \
# %s</div>\n'%(MARKUPDICT.get(NAME),self.title, time.ctime()))