コード例 #1
0
ファイル: power_parser.py プロジェクト: pannous/angle
def must_contain_before_old(before, *args):  # ,before():None
	raiseEnd()
	good = False
	if before and is_string(before): before = [before]
	if before: before = flatten(before) + [';']
	args = flatten(args)
	for x in flatten(args):
		if re.search(r'^\s*\w+\s*$', x):
			good = good or re.search(r'[^\w]%s[^\w]' % x, the.string)
			if (type(good).__name__ == "SRE_Match"):
				good = good.start()
			if good and before and good.pre_match in before and before.index(good.pre_match):
				good = None
		else:  # token
			good = good or re.search(escape_token(x), the.string)
			if (type(good).__name__ == "SRE_Match"):
				good = good.start()
			sub = the.string[0:good]
			if good and before and sub in before and before.index(sub):
				good = None
		if good: break

	if not good: raise NotMatching
	for nl in english_tokens.newline_tokens:
		if nl in str(good): raise NotMatching  # ;while
		# if nl in str(good.pre_match): raise (NotMatching(x))  # ;while
	return OK
コード例 #2
0
ファイル: power_parser.py プロジェクト: pannous/angle
def init(strings):
	# global is ok within one file but do not use it across different files
	global no_rollback_depth, rollback_depths, line_number, original_string, root, lines, depth, left, right, comp
	if not the.moduleMethods:
		load_module_methods()
	the.no_rollback_depth = -1
	the.rollback_depths = []
	the.line_number = 0
	if isinstance(strings, list):
		the.lines = strings
		if (strings[0].endswith("\n")):
			parse_tokens("".join(strings))
		else:
			parse_tokens("\n".join(strings))
	if is_string(strings):
		the.lines = strings.split("\n")
		parse_tokens(strings)
	drop_comments()

	the.tokens_len = len(the.tokenstream)
	the.token_number = -1
	next_token()
	the.string = the.lines[0].strip()  # Postpone angel.problem
	the.original_string = the.string
	the.root = None
	the.nodes = []
	the.depth = 0
	left = right = comp = None
	for nr in english_tokens.numbers:
		the.token_map[nr] = number
コード例 #3
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def must_contain_before_old(before, *args):  # ,before():None
	raiseEnd()
	good = False
	if before and is_string(before): before = [before]
	if before: before = flatten(before) + [';']
	args = flatten(args)
	for x in flatten(args):
		if re.search(r'^\s*\w+\s*$', x):
			good = good or re.search(r'[^\w]%s[^\w]' % x, the.string)
			if (type(good).__name__ == "SRE_Match"):
				good = good.start()
			if good and before and good.pre_match in before and before.index(good.pre_match):
				good = None
		else:  # token
			good = good or re.search(escape_token(x), the.string)
			if (type(good).__name__ == "SRE_Match"):
				good = good.start()
			sub = the.string[0:good]
			if good and before and sub in before and before.index(sub):
				good = None
		if good: break

	if not good: raise NotMatching
	for nl in english_tokens.newline_tokens:
		if nl in str(good): raise NotMatching  # ;while
		# if nl in str(good.pre_match): raise (NotMatching(x))  # ;while
	return OK
コード例 #4
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def init(strings):
	# global is ok within one file but do not use it across different files
	global no_rollback_depth, rollback_depths, line_number, original_string, root, lines, depth, left, right, comp
	if not the.moduleMethods:
		load_module_methods()
	the.no_rollback_depth = -1
	the.rollback_depths = []
	the.line_number = 0
	if isinstance(strings, list):
		the.lines = strings
		if (strings[0].endswith("\n")):
			parse_tokens("".join(strings))
		else:
			parse_tokens("\n".join(strings))
	if is_string(strings):
		the.lines = strings.split("\n")
		parse_tokens(strings)
	drop_comments()

	the.tokens_len = len(the.tokenstream)
	the.token_number = -1
	next_token()
	the.string = the.lines[0].strip()  # Postpone angel.problem
	the.original_string = the.string
	the.root = None
	the.nodes = []
	the.depth = 0
	left = right = comp = None
	for nr in english_tokens.numbers:
		the.token_map[nr] = number
コード例 #5
0
ファイル: power_parser.py プロジェクト: pannous/angle
def starts_with(tokenz):
	if checkEndOfLine(): return False
	if is_string(tokenz):
		return tokenz == the.token
	if the.token in tokenz:
		return the.token
	# for t in tokenz:
	#     if t == the.current_word:
	#         return t
	return False
コード例 #6
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def starts_with(tokenz):
	if checkEndOfLine(): return False
	if is_string(tokenz):
		return tokenz == the.token
	if the.token in tokenz:
		return the.token
	# for t in tokenz:
	#     if t == the.current_word:
	#         return t
	return False
コード例 #7
0
ファイル: power_parser.py プロジェクト: pannous/angle
def error(e, force=False):
	if isinstance(e, GivingUp): raise e  # hand through!
	if isinstance(e, NotMatching): raise e
	if is_string(e): print(e)
	if isinstance(e, Exception):
		# print(e.str(clazz )+" "+e.str(message))
		# print(clean_backtrace e.backtrace)
		# print(e.str( class )+" "+e.str(message))
		print_pointer()
		# if context.use_tree:
		#     import TreeBuilder
		#     TreeBuilder.show_tree()
		if not context._verbose:
			raise e
コード例 #8
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def error(e, force=False):
	if isinstance(e, GivingUp): raise e  # hand through!
	if isinstance(e, NotMatching): raise e
	if is_string(e): print(e)
	if isinstance(e, Exception):
		# print(e.str(clazz )+" "+e.str(message))
		# print(clean_backtrace e.backtrace)
		# print(e.str( class )+" "+e.str(message))
		print_pointer()
		# if context.use_tree:
		#     import TreeBuilder
		#     TreeBuilder.show_tree()
		if not context._verbose:
			raise e
コード例 #9
0
ファイル: power_parser.py プロジェクト: pannous/angle
def must_contain(args, do_raise=True):  # before ;\n
	if isinstance(args[-1], dict):
		return must_contain_before(args[0:-2], args[-1]['before']) # BAD style!!
	if is_string(args): args = [args]
	old = current_token
	pre = the.previous_word
	while not (checkEndOfLine()):
		for x in args:
			if current_word == x:
				set_token(old)
				return x
		next_token()
		if do_raise and (current_word == ';' or current_word == '\n'):
			break
	set_token(old)
	the.previous_word = pre
	if do_raise:
		raise NotMatching("must_contain " + str(args))
	return False
コード例 #10
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def must_contain(args, do_raise=True):  # before ;\n
	if isinstance(args[-1], dict):
		return must_contain_before(args[0:-2], args[-1]['before']) # BAD style!!
	if is_string(args): args = [args]
	old = current_token
	pre = the.previous_word
	while not (checkEndOfLine()):
		for x in args:
			if current_word == x:
				set_token(old)
				return x
		next_token()
		if do_raise and (current_word == ';' or current_word == '\n'):
			break
	set_token(old)
	the.previous_word = pre
	if do_raise:
		raise NotMatching("must_contain " + str(args))
	return False
コード例 #11
0
ファイル: power_parser.py プロジェクト: pannous/angle
def parse(s, target_file=None):
	global last_result, result
	if not s: return
	# verbose("PARSING " + s)
	if (isinstance(s, file_types)):
		source_file = s.name
		s = s.readlines()
	elif s.endswith(".e") or s.endswith(".an"):
		target_file = target_file or s + ".pyc"
		source_file = s
		s = open(s).readlines()
	else:
		source_file = 'out/inline'
		try:open(source_file, 'wt').write(s)
		except:debug("no out directory")
	if context._debug:
		print("  File \"%s\", line 1" % source_file)
	if (len(s) < 1000):
		verbose("--------PARSING:---------")
		verbose(s)
		verbose("-------------------------")
	try:
		import english_parser
		if isinstance(s, file_types):
			source_file = str(s)
			target_file = source_file + ".pyc"
			s = s.readlines()
		if not is_string(s) and not isinstance(s, list):
			the.result = s
			return english_parser.interpretation()  # result, hack
		allow_rollback()
		init(s)
		the.result = english_parser.rooty()
		if isinstance(the.result, FunctionCall):
			the.result = english_parser.do_execute_block(the.result)
		if the.result in ['True', 'true']: the.result = True
		if the.result in ['False', 'false']: the.result = False
		if isinstance(the.result, Variable): the.result = the.result.value
		import ast
		got_ast = isinstance(the.result, ast.AST)
		if isinstance(the.result, list) and len(the.result) > 0:
			got_ast = isinstance(the.result[0], ast.AST)
		if context.use_tree and got_ast:
			import pyc_emitter
			the.result = pyc_emitter.eval_ast(the.result, {}, source_file, target_file, run=True)
		else:
			if isinstance(the.result, ast.Num): the.result = the.result.n
			if isinstance(the.result, ast.Str): the.result = the.result.s
		the.last_result = the.result
	except Exception as e:
		error(target_file)
		print_pointer(True)
		raise  # blank reraises e with stacktrace
	# except NotMatching as e:
	#     import traceback
	#     traceback.print_stack() # backtrace
	#     the.last_result = the.result = None
	#     e=filter_backtrace(e)
	#     error(e)
	#     print_pointer(True)
	except IgnoreException as e:
		pass

	verbose("PARSED SUCCESSFULLY!")
	if context._debug:
		print("  File \"%s\", line 1" % source_file)
	# show_tree()
	# puts svg
	return english_parser.interpretation()  # # result
コード例 #12
0
ファイル: power_parser.py プロジェクト: cppxaxa/angle
def parse(s, target_file=None):
	global last_result, result
	if not s: return
	verbose("PARSING " + s)
	if (isinstance(s, file_types)):
		source_file = s.name
		s = s.readlines()
	elif s.endswith(".e") or s.endswith(".an"):
		target_file = target_file or s + ".pyc"
		source_file = s
		with open(s) as f: s=f.readlines()
	else:
		source_file = 'out/inline'
		try:
			with open(source_file, 'wt') as f:f.write(s)
		except:debug("no out directory")
	if context._debug:
		print("  File \"%s\", line 1" % source_file)
	if (len(s) < 1000):
		verbose("--------PARSING:---------")
		verbose(s)
		verbose("-------------------------")
	try:
		import english_parser
		if isinstance(s, file_types):
			source_file = str(s)
			target_file = source_file + ".pyc"
			s = s.readlines()
		if not is_string(s) and not isinstance(s, list):
			the.result = s
			return english_parser.interpretation()  # result, hack
		allow_rollback()
		init(s)
		the.result = english_parser.rooty()
		if isinstance(the.result, FunctionCall):
			the.result = english_parser.do_execute_block(the.result)
		if the.result in ['True', 'true']: the.result = True
		if the.result in ['False', 'false']: the.result = False
		if isinstance(the.result, Variable): the.result = the.result.value
		import ast
		got_ast = isinstance(the.result, ast.AST)
		if isinstance(the.result, list) and len(the.result) > 0:
			got_ast = isinstance(the.result[0], ast.AST)
		if context.use_tree and got_ast:
			import pyc_emitter
			the.result = pyc_emitter.eval_ast(the.result, {}, source_file, target_file, run=True)
		else:
			if isinstance(the.result, ast.Num): the.result = the.result.n
			if isinstance(the.result, ast.Str): the.result = the.result.s
		the.last_result = the.result
	except Exception as e:
		error(target_file)
		print_pointer(True)
		raise  # blank reraises e with stacktrace
	# except NotMatching as e:
	#     import traceback
	#     traceback.print_stack() # backtrace
	#     the.last_result = the.result = None
	#     e=filter_backtrace(e)
	#     error(e)
	#     print_pointer(True)
	except IgnoreException as e:
		pass

	verbose("PARSED SUCCESSFULLY!")
	if context._debug:
		print("  File \"%s\", line 1" % source_file)
	# show_tree()
	# puts svg
	return english_parser.interpretation()  # # result