def visit_type_var(self, t): if t.id > 0 or self.func_tvars: if t.repr is not None: # Give a representation for the dynamic type. tok = Token('any') tok.pre = t.repr.name.pre return Any(t.line, AnyRepr(tok)) else: return Any() else: return t
def parseCommand(src: bytes, cmd_start: int, allstringliterals: bool) -> Tuple[List[Node], int]: assert (type(src) == bytes) nodes: List[Node] = [] token = lex.scanSkipInlineWhitespace(src, cmd_start) if not token: return nodes, cmd_start if verify_one_match: lex.verifyNoMatches(src, cmd_start, token.pattern_kind) # TODO: make while True: if token.pattern_kind == TokenKind.COMMENT or token.pattern_kind == TokenKind.NEWLINE: return nodes, token.end if token.pattern_kind == TokenKind.CLOSE_PAREN: return nodes, token.pos node, next_token = parseNode(src, token, allstringliterals) nodes.append(node) if not next_token: return nodes, len(src) token = next_token if token.pattern_kind == TokenKind.INLINE_WHITESPACE: scan_result = lex.scan(src, token.end) if not scan_result: return nodes, token.pos assert (scan_result.pattern_kind != TokenKind.INLINE_WHITESPACE) if verify_one_match: lex.verifyNoMatches(src, token.end, scan_result.pattern_kind) token = Token(token.end, scan_result) else: assert (token.pattern_kind == TokenKind.COMMENT or token.pattern_kind == TokenKind.NEWLINE or token.pattern_kind == TokenKind.CLOSE_PAREN)
def parseNode(src: bytes, token: Token, allstringliterals: bool) -> Tuple[Node, Optional[Token]]: assert (token.pos < len(src)) assert (token.pattern_kind != TokenKind.INLINE_WHITESPACE) assert (token.pattern_kind != TokenKind.COMMENT) assert (token.pattern_kind != TokenKind.NEWLINE) assert (token.pattern_kind != TokenKind.CLOSE_PAREN) node: Optional[Node] = None while True: next_node = parseOneNode(src, token, allstringliterals) assert (next_node.end > next_node.pos) if not node: node = next_node else: if isinstance(node, NodeMultiple): assert (node.nodes[-1].end == next_node.pos) node.nodes.append(next_node) node.end = next_node.end else: assert (node.end == next_node.pos) if isinstance(node, NodeBinaryOp): raise SyntaxError( node.pos, "'{}' requires space separation".format( binaryOpUserString(node.kind))) node = NodeMultiple(node.pos, next_node.end, [node, next_node]) if isinstance(next_node, NodeBinaryOp): raise SyntaxError( next_node.pos, "'{}' requires space separation".format( binaryOpUserString(next_node.kind))) scan_result = lex.scan(src, node.end) if not scan_result: return node, None if verify_one_match: lex.verifyNoMatches(src, node.end, scan_result.pattern_kind) token = Token(node.end, scan_result) if (token.pattern_kind == TokenKind.INLINE_WHITESPACE or token.pattern_kind == TokenKind.COMMENT or token.pattern_kind == TokenKind.NEWLINE or token.pattern_kind == TokenKind.CLOSE_PAREN): return node, token
class ReplaceTypeVarsVisitor(TypeTranslator): # Only override type variable handling; otherwise perform an indentity # transformation. bool func_tvars void __init__(self, bool func_tvars): self.func_tvars = func_tvars Typ visit_type_var(self, TypeVar t): if t.id > 0 or self.func_tvars: if t.repr is not None: # Give a representation for the dynamic type. tok = Token('any') tok.pre = t.repr.name.pre return Any(t.line, AnyRepr(tok)) else: return Any() else: return t Typ replace_func_type_vars(Typ typ): """Replace type variables in a type with the None (empty) type.""" return typ.accept(ReplaceFuncTypeVarsVisitor()) class ReplaceFuncTypeVarsVisitor(TypeTranslator): Typ visit_type_var(self, TypeVar t):
from generator import Parser from lex import Token p = Parser() result = p.parse([ Token(Token.Q_AND_A), Token(Token.LITERAL, "What is the capital of England?"), Token(Token.LITERAL, "London"), Token(Token.END) ]) print(result) f = open("C:\\Users\\44778\\Desktop\\revis.html", "w") f.write(result) f.close()