def _parse_trend_cond(token, lexer, symbol_table, engine, is_negated): is_inc = False symbol_id = None access_method = None if token.token_value != 'inc' and token.token_value != 'desc': raise ValueError('Either of [inc, desc] expected, none found, Sir') if token.token_value == 'inc': is_inc = True token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'currency': utils.expect_access_operator(lexer) symbol_id = symbol_table.get_currency(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) utils.expect_keyword(lexer, 'rate') access_method = engine.world.get_currency_rate elif token.token_type == TokenType.keyword and token.token_value == 'stock': utils.expect_access_operator(lexer) symbol_id = symbol_table.get_stock(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) utils.expect_keyword(lexer, 'value') access_method = engine.world.get_stock_price else: raise ValueError('Either stock or currency keywords expected, found: ' + str(token.token_value) + " ,Sir.") utils.expect_keyword(lexer, 'by') percent_growth = utils.expect_number(lexer) utils.expect_keyword(lexer, 'in') days_num = utils.expect_number(lexer) trendCond = TrendCondition(engine.world) trendCond.accessor_method = access_method trendCond.growth_percent = percent_growth trendCond.number_of_days = days_num trendCond.is_inc = is_inc trendCond.is_negated = is_negated return trendCond
def _get_numeric_comparison_argument(token, lexer, symbol_table, engine): if token.token_type == TokenType.number: return None, None, None, token.token_value if token.token_value == 'currency': utils.expect_access_operator(lexer) symbol_id = symbol_table.get_currency(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) token = lexer.get_token() if token.token_type == TokenType.keyword and token.token_value == 'rate': access_method = engine.world.get_currency_rate date_str = _get_date_arg(lexer, engine) return access_method, symbol_id, date_str, None elif token.token_type == TokenType.keyword and token.token_value == 'have': utils.expect_access_operator(lexer) token = lexer.get_token() if token.token_type == TokenType.keyword and token.token_value == 'amount': access_method = engine.investor.has_currency return access_method, symbol_id, None, None else: raise ValueError("Forbidden token found, expected keyword amount " + str(token.token_value) + " ,Sir.") else: raise ValueError( "Forbidden token found, expected keywords rate or have found: " + str(token.token_value) + " ,Sir.") else: utils.expect_access_operator(lexer) symbol_id = symbol_table.get_stock(utils.expect_given_name(lexer)) utils.expect_access_operator(lexer) token = lexer.get_token() if token.token_type == TokenType.keyword and token.token_value == 'value': access_method = engine.world.get_stock_price date_str = _get_date_arg(lexer, engine) return access_method, symbol_id, date_str, None elif token.token_type == TokenType.keyword and token.token_value == 'have': utils.expect_access_operator(lexer) token = lexer.get_token() if token.token_type == TokenType.keyword and token.token_value == 'amount': access_method = engine.investor.has_stock return access_method, symbol_id, None, None else: raise ValueError( "Forbidden token found, expected keyword amount not " + str(token.token_value) + " ,Sir.") else: raise ValueError("Forbidden token found, expected keyword value or have keywords not " + str( token.token_value) + " ,Sir.")
def _parse_rule_cond(token, lexer, symbol_table, engine, is_negated): if token.token_type != TokenType.keyword and token.token_value != 'rule': raise ValueError('Rule keyword expected, found: ' + str(token.token_value) + " ,Sir.") utils.expect_access_operator(lexer) rule_id = int(utils.expect_given_name(lexer)) if not symbol_table.is_rule_id_busy(rule_id): raise ValueError("Trying to check execution or non existant rule: " + str(rule_id) + " ,Sir.") rule = engine.rules.get(rule_id) utils.expect_access_operator(lexer) token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword or token.token_value != 'executed': raise ValueError('Expected executed keyword, found: ' + str(token.token_value) + " ,Sir.") return RuleExecCondition(rule, is_negated)
def parse(self): state = 0 # 0 - config, 1 - events, 2 - start, 3 - rules, 4 - done while True: token = ut.get_token_skipping_whitespace(self.lexer) state = self.change_state_on_token(token, state) if state == 0: file_name = ut.expect_given_name(self.lexer) parse_results = config_parser.parse_file( file_name, self.symbol_table) currencies = parse_results['currencies'] stocks = parse_results['stocks'] for curr in currencies: self.engine.add_currency(curr) for st in stocks: self.engine.add_stock(st) elif state == 1: file_name = ut.expect_given_name(self.lexer) parse_results = events_parser.parse_file( file_name, self.symbol_table) for event in parse_results: self.engine.add_event(event) elif state == 2: file_name = ut.expect_given_name(self.lexer) parse_results = start_parser.parse_file( file_name, self.symbol_table) currencies = parse_results['currencies'] stocks = parse_results['stocks'] for curr in currencies: self.engine.add_start_cond(curr) for st in stocks: self.engine.add_start_cond(st) elif state == 3: rule_parser.parse_from_lexer(self.lexer, self.symbol_table, self.engine, token) elif state == 4: return else: raise ValueError("Something went blood wrong, Sir")
def parse_action(lexer, symbol_table, engine): is_buy = False is_stock = False token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword and token.token_value not in ['buy', 'sell']: raise ValueError('Buy or sell expected to start action, found something else, Sir') if token.token_value == 'buy': is_buy = True token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword and token.token_value not in ['stock', 'currency']: raise ValueError('An action requires you to choose either stock or currency, nothing else, Sir') if token.token_value == 'stock': is_stock = True utils.expect_access_operator(lexer) symbol_name = utils.expect_given_name(lexer) symbol_id = -1 if is_stock: symbol_id = symbol_table.get_stock(symbol_name) else: symbol_id = symbol_table.get_currency(symbol_name) if not is_buy: token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'amount': token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.number: if token.token_value < 0: raise ValueError("You can only sell a positive amount, Sir") return build_action(engine, is_buy, is_stock, symbol_id, amount=token.token_value) elif token.token_type == TokenType.keyword and token.token_value == 'ALL': return build_action(engine, is_buy, is_stock, symbol_id, amount='ALL') else: raise ValueError("Expecting a number or ALL keyword, found neither of them, Sir") elif token.token_type == TokenType.keyword and token.token_value == 'part': amount = utils.expect_number(lexer) if not 1 <= amount <= 100: raise ValueError("Part must be 1 to 100 no more no less, Sir") return build_action(engine, is_buy, is_stock, symbol_id, part=amount) elif token.token_type == TokenType.keyword and token.token_value == 'for': curr_amount = utils.expect_number(lexer) if curr_amount < 0: raise ValueError("You can only sell for a positive price, Sir") return build_action(engine, is_buy, is_stock, symbol_id, curr_amount=curr_amount) else: raise ValueError("Expecting either amount, part, for, none of those were found, Sir") else: token = utils.get_token_skipping_whitespace(lexer) if token.token_type != TokenType.keyword or token.token_value != 'amount': raise ValueError("Expecting keyword amount, Sir") token = utils.get_token_skipping_whitespace(lexer) buy_amount = 0 if token.token_type == TokenType.keyword or token.token_value == 'MAX': buy_amount = 'MAX' elif token.token_type == TokenType.number: buy_amount = token.token_value if buy_amount < 0: raise ValueError("You can only buy a positive amount, Sir") else: raise ValueError("Unexpected Token, Sir") utils.expect_keyword(lexer, 'for') currency_used_id = None token = utils.get_token_skipping_whitespace(lexer) if token.token_type == TokenType.keyword and token.token_value == 'OWN': currency_used_id = 'OWN' elif token.token_type == TokenType.keyword and token.token_value == 'ANY': currency_used_id = 'ANY' elif token.token_type == TokenType.keyword and token.token_value == 'currency': utils.expect_access_operator(lexer) currency_name = utils.expect_given_name(lexer) currency_used_id = symbol_table.get_currency(currency_name) else: raise ValueError("Unexpected token, Sir") return build_action(engine, is_buy, is_stock, symbol_id, buy_amount=buy_amount, curr_used=currency_used_id)