_B='suite'
_A='file_input'
from parso.python import tree
from parso.python.token import PythonTokenTypes
from parso.parser import BaseParser
NAME=PythonTokenTypes.NAME
INDENT=PythonTokenTypes.INDENT
DEDENT=PythonTokenTypes.DEDENT
class Parser(BaseParser):
	'\n    This class is used to parse a Python file, it then divides them into a\n    class structure of different scopes.\n\n    :param pgen_grammar: The grammar object of pgen2. Loaded by load_grammar.\n    ';node_map={'expr_stmt':tree.ExprStmt,'classdef':tree.Class,'funcdef':tree.Function,_A:tree.Module,'import_name':tree.ImportName,'import_from':tree.ImportFrom,'break_stmt':tree.KeywordStatement,'continue_stmt':tree.KeywordStatement,'return_stmt':tree.ReturnStmt,'raise_stmt':tree.KeywordStatement,'yield_expr':tree.YieldExpr,'del_stmt':tree.KeywordStatement,'pass_stmt':tree.KeywordStatement,'global_stmt':tree.GlobalStmt,'nonlocal_stmt':tree.KeywordStatement,'print_stmt':tree.KeywordStatement,'assert_stmt':tree.AssertStmt,'if_stmt':tree.IfStmt,'with_stmt':tree.WithStmt,'for_stmt':tree.ForStmt,'while_stmt':tree.WhileStmt,'try_stmt':tree.TryStmt,'sync_comp_for':tree.SyncCompFor,'decorator':tree.Decorator,'lambdef':tree.Lambda,'lambdef_nocond':tree.Lambda,'namedexpr_test':tree.NamedExpr};default_node=tree.PythonNode;_leaf_map={PythonTokenTypes.STRING:tree.String,PythonTokenTypes.NUMBER:tree.Number,PythonTokenTypes.NEWLINE:tree.Newline,PythonTokenTypes.ENDMARKER:tree.EndMarker,PythonTokenTypes.FSTRING_STRING:tree.FStringString,PythonTokenTypes.FSTRING_START:tree.FStringStart,PythonTokenTypes.FSTRING_END:tree.FStringEnd}
	def __init__(A,pgen_grammar,error_recovery=True,start_nonterminal=_A):super().__init__(pgen_grammar,start_nonterminal,error_recovery=error_recovery);A.syntax_errors=[];A._omit_dedent_list=[];A._indent_counter=0
	def parse(A,tokens):
		B=tokens
		if A._error_recovery:
			if A._start_nonterminal!=_A:raise NotImplementedError
			B=A._recovery_tokenize(B)
		return super().parse(B)
	def convert_node(C,nonterminal,children):
		'\n        Convert raw node information to a PythonBaseNode instance.\n\n        This is passed to the parser driver which calls it whenever a reduction of a\n        grammar rule produces a new complete node, so that the tree is build\n        strictly bottom-up.\n        ';B=nonterminal;A=children
		try:D=C.node_map[B](A)
		except KeyError:
			if B==_B:A=[A[0]]+A[2:-1]
			D=C.default_node(B,A)
		return D
	def convert_leaf(D,type,value,prefix,start_pos):
		B=start_pos;C=prefix;A=value
		if type==NAME:
			if A in D._pgen_grammar.reserved_syntax_strings:return tree.Keyword(A,B,C)
			else:return tree.Name(A,B,C)
		return D._leaf_map.get(type,tree.Operator)(A,B,C)
	def error_recovery(A,token):
		B=token;F=A.stack[-1].nodes
		if F:C=F[-1].get_last_leaf()
		else:C=None
		if A._start_nonterminal==_A and(B.type==PythonTokenTypes.ENDMARKER or B.type==DEDENT and not C.value.endswith('\n')and not C.value.endswith('\r')):
			if A.stack[-1].dfa.from_rule=='simple_stmt':
				try:D=A.stack[-1].dfa.transitions[PythonTokenTypes.NEWLINE]
				except KeyError:pass
				else:
					if D.next_dfa.is_final and not D.dfa_pushes:A.stack[-1].dfa=D.next_dfa;A._add_token(B);return
		if not A._error_recovery:return super().error_recovery(B)
		def H(stack):
			for(B,A)in reversed(list(enumerate(stack))):
				if A.nonterminal==_A:break
				elif A.nonterminal==_B:
					if len(A.nodes)!=1:break
			return B
		I=H(A.stack)
		if A._stack_removal(I+1):A._add_token(B)
		else:
			G,J,K,L=B
			if G==INDENT:A._omit_dedent_list.append(A._indent_counter)
			M=tree.PythonErrorLeaf(G.name,J,K,L);A.stack[-1].nodes.append(M)
		E=A.stack[-1]
		if E.nonterminal==_B:
			try:E.dfa=E.dfa.arcs['stmt']
			except KeyError:pass
	def _stack_removal(A,start_index):
		B=start_index;C=[B for A in A.stack[B:]for B in A.nodes]
		if C:D=tree.PythonErrorNode(C);A.stack[B-1].nodes.append(D)
		A.stack[B:]=[];return bool(C)
	def _recovery_tokenize(A,tokens):
		for C in tokens:
			D=C[0]
			if D==DEDENT:
				B=A._omit_dedent_list
				if B and B[-1]==A._indent_counter:B.pop();A._indent_counter-=1;continue
				A._indent_counter-=1
			elif D==INDENT:A._indent_counter+=1
			yield C