Пример #1
0
def makeNumber(parent: ParserRuleContext, n: int):
    """This node represents a virtual ParseTree node that does not come from
    the parser but rather is constructed via a rewriting rule. For example,
    the expression "a-b" is parsed as "Plus[a, Times[-1, b]]" in exitPlusOp(),
    so a node for "-1" needs to be created even though "-1" does not appear as
    a token in the token stream.

    Note that makeNumber sets the NumberContext's parent but does not add
    anything to parent's children."""

    # The hierarchy is:
    #   CommonToken->TerminalNodeImpl->NumberLiteralContext
    #       ->NumberBaseTenContext->NumberContext->parent

    digits_token = CommonToken(type=FoxySheepParser.DIGITS)
    digits_token._text = str(n)

    number = FoxySheepParser.NumberContext(
        None, FoxySheepParser.ExprContext(None, parent=parent))

    number_literal = FoxySheepParser.NumberBaseTenContext(
        None, FoxySheepParser.NumberLiteralContext(None, parent=number))

    number_literal.addTokenNode(digits_token)
    addChild(number, number_literal)

    return number