1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
# -*- coding: utf-8 -*-
# pylint: disable=W0613
from __future__ import print_function
from __future__ import unicode_literals
import logging
from cmakelang import lex
from cmakelang.common import UserError
from cmakelang.parse.util import COMMENT_TOKENS, WHITESPACE_TOKENS
from cmakelang.parse.common import NodeType, ParenBreaker, TreeNode
from cmakelang.parse.printer import tree_string
from cmakelang.parse.argument_nodes import StandardParser, StandardParser2
from cmakelang.parse.simple_nodes import CommentNode
logger = logging.getLogger(__name__)
class FunctionNameNode(TreeNode):
def __init__(self):
super(FunctionNameNode, self).__init__(NodeType.FUNNAME)
self.token = None
@classmethod
def parse(cls, ctx, tokens):
node = cls()
node.token = tokens.pop(0)
node.children.append(node.token)
return node
class StatementNode(TreeNode):
"""Parent node of a statement subtree."""
def __init__(self):
super(StatementNode, self).__init__(NodeType.STATEMENT)
self.funnode = None
self.argtree = None
self.cmdspec = None
def get_funname(self):
return self.funnode.token.spelling.lower()
@classmethod
def consume(cls, ctx, tokens):
"""
Consume a complete statement, removing tokens from the input list and
returning a STATEMENT node.
"""
node = cls()
# Consume the function name
fnname = tokens[0].spelling.lower()
node.funnode = funnode = FunctionNameNode.parse(ctx, tokens)
node.children.append(funnode)
# Consume whitespace up to the parenthesis
while tokens and tokens[0].type in WHITESPACE_TOKENS:
node.children.append(tokens.pop(0))
# TODO(josh): should the parens belong to the statement node or the
# group node?
if tokens[0].type != lex.TokenType.LEFT_PAREN:
raise ValueError(
"Unexpected {} token at {}, expecting l-paren, got {}"
.format(tokens[0].type.name, tokens[0].get_location(),
repr(tokens[0].content)))
lparen = TreeNode(NodeType.LPAREN)
lparen.children.append(tokens.pop(0))
node.children.append(lparen)
while tokens and tokens[0].type in WHITESPACE_TOKENS:
node.children.append(tokens.pop(0))
continue
breakstack = [ParenBreaker()]
parse_fun = ctx.parse_db.get(fnname, None)
if parse_fun is None:
# If the parse_db provides a "_default" then use that. Otherwise use the
# standard parser with no kwargs or flags.
parse_fun = ctx.parse_db.get("_default", StandardParser())
if isinstance(parse_fun, StandardParser2):
node.cmdspec = parse_fun.cmdspec
node.argtree = subtree = parse_fun(ctx, tokens, breakstack)
node.children.append(subtree)
# NOTE(josh): technically we may have a statement specification with
# an exact number of arguments. At this point we have broken out of that
# statement but we might have some comments or whitespace to consume
while tokens and tokens[0].type != lex.TokenType.RIGHT_PAREN:
if tokens[0].type in WHITESPACE_TOKENS:
node.children.append(tokens.pop(0))
continue
if tokens[0].type in COMMENT_TOKENS:
cnode = CommentNode.consume(ctx, tokens)
node.children.append(cnode)
continue
raise UserError(
"Unexpected {} token at {}, expecting r-paren, got {}"
.format(tokens[0].type.name, tokens[0].get_location(),
repr(tokens[0].content)))
if not tokens:
raise UserError(
"Unexpected end of token stream while parsing statement:\n {}"
.format(tree_string([node])))
if tokens[0].type != lex.TokenType.RIGHT_PAREN:
raise UserError(
"Unexpected {} token at {}, expecting r-paren, got {}"
.format(tokens[0].type.name, tokens[0].get_location(),
repr(tokens[0].content)))
rparen = TreeNode(NodeType.RPAREN)
rparen.children.append(tokens.pop(0))
node.children.append(rparen)
CommentNode.consume_trailing(ctx, tokens, node)
return node
class AtWordNode(TreeNode):
def __init__(self):
super(AtWordNode, self).__init__(NodeType.ATWORD)
self.token = None
@classmethod
def parse(cls, ctx, tokens):
node = cls()
node.token = tokens.pop(0)
node.children.append(node.token)
return node
class AtWordStatementNode(TreeNode):
"""Parent node of a statement subtree."""
def __init__(self):
super(AtWordStatementNode, self).__init__(NodeType.ATWORDSTATEMENT)
self.atword_node = None
@classmethod
def consume(cls, ctx, tokens):
"""
Consume an at-word substitution for a complete statement, removing tokens
from the input list and returning an AtWordStatementNode.
"""
node = cls()
# Consume the replacement tokens (at-word)
node.atword_node = AtWordNode.parse(ctx, tokens)
node.children.append(node.atword_node)
# Consume any trailing comments
CommentNode.consume_trailing(ctx, tokens, node)
return node
|