X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=parsing.py;h=20768371b20f039e2e193a6d07f57151d210f19f;hb=62bbcc8f9242c7f404db4756460a927b1ce90aec;hp=6734af7c4ddb226e729162a06a6378910f83ff07;hpb=d6af7d074bf65e782e42055623a197863b5f8000;p=fur diff --git a/parsing.py b/parsing.py index 6734af7..2076837 100644 --- a/parsing.py +++ b/parsing.py @@ -39,21 +39,21 @@ def _zero_or_more_parser(formatter, parser): FurIntegerLiteralExpression = collections.namedtuple( 'FurIntegerLiteralExpression', [ - 'value', + 'integer', ], ) FurStringLiteralExpression = collections.namedtuple( 'FurStringLiteralExpression', [ - 'value', + 'string', ], ) FurSymbolExpression = collections.namedtuple( 'FurSymbolExpression', [ - 'value', + 'symbol', ], ) @@ -64,14 +64,6 @@ FurNegationExpression = collections.namedtuple( ], ) -# TODO We don't need to wrap this type -FurParenthesizedExpression = collections.namedtuple( - 'FurParenthesizedExpression', - [ - 'internal', - ], -) - FurInfixExpression = collections.namedtuple( 'FurInfixExpression', [ @@ -82,6 +74,37 @@ FurInfixExpression = collections.namedtuple( ], ) +FurListLiteralExpression = collections.namedtuple( + 'FurListLiteralExpression', + [ + 'item_expression_list', + ], +) + +FurIfExpression = collections.namedtuple( + 'FurIfExpression', + [ + 'condition_expression', + 'if_statement_list', + 'else_statement_list', + ], +) + +FurSymbolExpressionPair = collections.namedtuple( + 'FurSymbolExpressionPair', + [ + 'symbol', + 'expression', + ], +) + +FurStructureLiteralExpression = collections.namedtuple( + 'FurStructureLiteralExpression', + [ + 'fields', + ], +) + def _integer_literal_expression_parser(index, tokens): failure = (False, index, None) @@ -90,41 +113,126 @@ def _integer_literal_expression_parser(index, tokens): value = int(tokens[index].match) index += 1 - return True, index, FurIntegerLiteralExpression(value=value) + return True, index, FurIntegerLiteralExpression(integer=value) def _string_literal_expression_parser(index, tokens): + if tokens[index].type == 'double_quoted_string_literal': + return (True, index + 1, FurStringLiteralExpression(string=tokens[index].match[1:-1])) + if tokens[index].type == 'single_quoted_string_literal': - return (True, index + 1, FurStringLiteralExpression(value=tokens[index].match[1:-1])) + return (True, index + 1, FurStringLiteralExpression(string=tokens[index].match[1:-1])) return (False, index, None) def _symbol_expression_parser(index, tokens): if tokens[index].type == 'symbol': - return (True, index + 1, FurSymbolExpression(value=tokens[index].match)) + return (True, index + 1, FurSymbolExpression(symbol=tokens[index].match)) return (False, index, None) +def _wrapped_parser(open_token, close_token, internal_parser): + def result_parser(index, tokens): + failure = (False, index, None) + + if tokens[index].type == open_token: + index += 1 + else: + return failure + + success, index, internal = internal_parser(index, tokens) + if not success: + return failure + + if tokens[index].type == close_token: + index += 1 + else: + # TODO Put the actual expected character in the error message + raise Exception('Expected closing token on line {}, found "{}"'.format( + tokens[index].line, + tokens[index].match, + )) + + return True, index, internal + + return result_parser + +def _bracket_wrapped_parser(internal_parser): + return _wrapped_parser('open_bracket', 'close_bracket', internal_parser) + +def _parenthese_wrapped_parser(internal_parser): + return _wrapped_parser('open_parenthese', 'close_parenthese', internal_parser) + def _parenthesized_expression_parser(index, tokens): + return _parenthese_wrapped_parser(_expression_parser)(index, tokens) + +def symbol_expression_pair_parser(index, tokens): failure = (False, index, None) - if tokens[index].type == 'open_parenthese': + if tokens[index].type == 'symbol': + symbol = tokens[index].match index += 1 else: return failure - success, index, internal = _expression_parser(index, tokens) - if not success: + if tokens[index].type == 'colon': + index += 1 + else: return failure - if tokens[index].type == 'close_parenthese': - index += 1 + success, index, expression = _expression_parser(index, tokens) + + if not success: + raise Exception() + + return ( + True, + index, + FurSymbolExpressionPair( + symbol=symbol, + expression=expression, + ), + ) + +def _structure_literal_parser(index, tokens): + success, index, result = _parenthese_wrapped_parser(_comma_separated_list_parser(symbol_expression_pair_parser))(index, tokens) + return ( + success, + index, + FurStructureLiteralExpression( + fields=result, + ), + ) + +def _list_literal_expression_parser(index, tokens): + failure = (False, index, None) + + success, index, item_expression_list = _bracket_wrapped_parser(_comma_separated_expression_list_parser)(index, tokens) + + if success: + return success, index, FurListLiteralExpression( + item_expression_list=item_expression_list, + ) else: - raise Exception('Expected ")" on line {}, found "{}"'.format( - tokens[index].line, - tokens[index].match, - )) + return failure + +def _literal_level_expression_parser(index, tokens): + return _or_parser( + _list_item_expression_parser, + _function_call_expression_parser, + _parenthesized_expression_parser, + _integer_literal_expression_parser, + _string_literal_expression_parser, + _list_literal_expression_parser, + _symbol_expression_parser, + _structure_literal_parser, + )(index, tokens) - return True, index, FurParenthesizedExpression(internal=internal) +def _dot_expression_parser(index, tokens): + return _left_recursive_infix_operator_parser( + lambda token: token.type == 'period', + _literal_level_expression_parser, + 'dot_level', + )(index, tokens) def _negation_expression_parser(index, tokens): failure = (False, index, None) @@ -132,21 +240,17 @@ def _negation_expression_parser(index, tokens): if tokens[index].match != '-': return failure - success, index, value = _literal_level_expression_parser(index + 1, tokens) + success, index, value = _dot_expression_parser(index + 1, tokens) if not success: return failure return (True, index, FurNegationExpression(value=value)) -def _literal_level_expression_parser(index, tokens): +def _negation_level_expression_parser(index, tokens): return _or_parser( + _dot_expression_parser, _negation_expression_parser, - _function_call_expression_parser, - _parenthesized_expression_parser, - _integer_literal_expression_parser, - _string_literal_expression_parser, - _symbol_expression_parser, )(index, tokens) def _left_recursive_infix_operator_parser(operator_token_matcher, operand_parser, order): @@ -180,7 +284,7 @@ def _left_recursive_infix_operator_parser(operator_token_matcher, operand_parser def _multiplication_level_expression_parser(index, tokens): return _left_recursive_infix_operator_parser( lambda token: token.type == 'multiplication_level_operator', - _literal_level_expression_parser, + _negation_level_expression_parser, 'multiplication_level', )(index, tokens) @@ -218,6 +322,8 @@ def _comma_separated_list_parser(subparser): items = [] + _, index, _ = consume_newlines(index, tokens) + success, index, item = subparser(index, tokens) if success: @@ -226,10 +332,13 @@ def _comma_separated_list_parser(subparser): return (True, start_index, ()) while success and index < len(tokens) and tokens[index].type == 'comma': + index += 1 success = False - if index + 1 < len(tokens): - success, try_index, item = subparser(index + 1, tokens) + _, index, _ = consume_newlines(index, tokens) + + if index < len(tokens): + success, try_index, item = subparser(index, tokens) if success: items.append(item) @@ -242,6 +351,14 @@ def _comma_separated_list_parser(subparser): def _comma_separated_expression_list_parser(index, tokens): return _comma_separated_list_parser(_expression_parser)(index, tokens) +FurListItemExpression = collections.namedtuple( + 'FurListItemExpression', + [ + 'list_expression', + 'index_expression', + ], +) + FurFunctionCallExpression = collections.namedtuple( 'FurFunctionCallExpression', [ @@ -281,34 +398,126 @@ FurProgram = collections.namedtuple( ], ) -def _function_call_expression_parser(index, tokens): - # TODO Use a FurSymbolExpression for the name +def _list_item_expression_parser(index, tokens): failure = (False, index, None) - success, index, function = _symbol_expression_parser(index, tokens) + # We have to be careful what expressions we add here. Otherwise expressions + # like "a + b[0]" become ambiguous to the parser. + success, index, list_expression = _or_parser( + _symbol_expression_parser, + _parenthesized_expression_parser, + )(index, tokens) if not success: return failure - if tokens[index].type != 'open_parenthese': + success, index, index_expression = _bracket_wrapped_parser(_expression_parser)( + index, + tokens, + ) + + if not success: return failure - index += 1 - success, index, arguments = _comma_separated_expression_list_parser(index, tokens) + while success and index < len(tokens): + # "list_expression" is actually the full list item expression if the next parse attempt doesn't succeed + # We can't give this a better name without a bunch of checks, however. + list_expression = FurListItemExpression( + list_expression=list_expression, + index_expression=index_expression, + ) + + success, index, index_expression = _bracket_wrapped_parser(_expression_parser)( + index, + tokens, + ) + + return True, index, list_expression + +def _function_call_expression_parser(index, tokens): + failure = (False, index, None) + + # We have to be careful what expressions we add here. Otherwise expressions + # like "a + b()" become ambiguous to the parser. + success, index, function = _or_parser( + _symbol_expression_parser, + _parenthesized_expression_parser, + )(index, tokens) if not success: return failure - if tokens[index].type != 'close_parenthese': - raise Exception('Expected ")", found "{}" on line {}'.format( - tokens[index].match, - tokens[index].line, - )) - index += 1 + success, index, arguments = _parenthese_wrapped_parser(_comma_separated_expression_list_parser)( + index, + tokens, + ) - return True, index, FurFunctionCallExpression(function=function, arguments=arguments) + if not success: + return failure + + while success and index < len(tokens): + # "function" is actually the full function call if the next parse attempt doesn't succeed + # We can't give this a better name without a bunch of checks, however. + function = FurFunctionCallExpression( + function=function, + arguments=arguments, + ) + + success, index, arguments = _parenthese_wrapped_parser(_comma_separated_expression_list_parser)( + index, + tokens, + ) -_expression_parser = _or_level_expression_parser + return True, index, function + +def _if_expression_parser(index, tokens): + failure = (False, index, None) + + if tokens[index].match == 'if': + index += 1 + else: + return failure + + success, index, condition_expression = _or_level_expression_parser(index, tokens) + + if not success: + raise Exception('Expected condition after "if" on line {}'.format(tokens[index].line)) + + if tokens[index].match == 'do': + index += 1 + else: + raise Exception('Expected "do" after "if" on line {}'.format(tokens[index].line)) + + + success, index, if_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens) + _, index, _ = consume_newlines(index, tokens) + + if tokens[index].match == 'else': + index += 1 + success, index, else_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens) + _, index, _ = consume_newlines(index, tokens) + else: + else_statement_list = () + + if tokens[index].match == 'end': + index += 1 + else: + raise Exception('Expected "end" after "if" on line {}'.format(tokens[index].line)) + + return ( + True, + index, + FurIfExpression( + condition_expression=condition_expression, + if_statement_list=if_statement_list, + else_statement_list=else_statement_list, + ), + ) + +_expression_parser = _or_parser( + _or_level_expression_parser, + _if_expression_parser, # This should always be at the top level +) def _expression_statement_parser(index, tokens): failure = (False, index, None) @@ -320,18 +529,28 @@ def _expression_statement_parser(index, tokens): return (True, index, FurExpressionStatement(expression=expression)) +BUILTINS = {'print', 'pow'} + def _assignment_statement_parser(index, tokens): - # TODO Use a FurSymbolExpression for the target? Maybe this is actually not a good idea failure = (False, index, None) - if tokens[index].type != 'symbol': + if tokens[index].type == 'symbol': + target = tokens[index].match + target_assignment_line = tokens[index].line + + index += 1 + else: return failure - target = tokens[index].match - index += 1 - if tokens[index].type != 'assignment_operator': + + if tokens[index].type == 'assignment_operator': + if target in BUILTINS: + raise Exception( + 'Trying to assign to builtin "{}" on line {}'.format(target, target_assignment_line), + ) + assignment_operator_index = index + else: return failure - assignment_operator_index = index success, index, expression = _expression_parser(index + 1, tokens) @@ -382,7 +601,7 @@ def _function_definition_statement_parser(index, tokens): tokens[index].line, )) - if tokens[index].type == 'symbol' and tokens[index].match == 'do': + if tokens[index].match == 'do': index += 1 else: return failure @@ -398,7 +617,7 @@ def _function_definition_statement_parser(index, tokens): return True, index, FurFunctionDefinitionStatement( name=name, - argument_name_list=tuple(an.value for an in argument_name_list), + argument_name_list=tuple(an.symbol for an in argument_name_list), statement_list=statement_list, ) @@ -445,7 +664,7 @@ if __name__ == '__main__': ( True, 1, - FurStringLiteralExpression(value='Hello, world'), + FurStringLiteralExpression(string='Hello, world'), ), ) @@ -458,7 +677,7 @@ if __name__ == '__main__': 4, FurFunctionCallExpression( name='print', - arguments=(FurStringLiteralExpression(value='Hello, world'),), + arguments=(FurStringLiteralExpression(string='Hello, world'),), ), ), )