X-Git-Url: https://code.kerkeslager.com/?a=blobdiff_plain;f=parsing.py;h=f16af14deff75305662685b5393fddeb950d4766;hb=02d64def49065ad614fe0ee2a85060666552192e;hp=6734af7c4ddb226e729162a06a6378910f83ff07;hpb=d6af7d074bf65e782e42055623a197863b5f8000;p=fur diff --git a/parsing.py b/parsing.py index 6734af7..f16af14 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,22 @@ FurInfixExpression = collections.namedtuple( ], ) +FurListLiteralExpression = collections.namedtuple( + 'FurListLiteralExpression', + [ + 'item_expression_list', + ], +) + +FurIfExpression = collections.namedtuple( + 'FurIfExpression', + [ + 'condition_expression', + 'if_statement_list', + 'else_statement_list', + ], +) + def _integer_literal_expression_parser(index, tokens): failure = (False, index, None) @@ -90,41 +98,69 @@ 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): - failure = (False, index, None) + return _parenthese_wrapped_parser(_expression_parser)(index, tokens) - if tokens[index].type == 'open_parenthese': - index += 1 - else: - return failure +def _list_literal_expression_parser(index, tokens): + failure = (False, index, None) - success, index, internal = _expression_parser(index, tokens) - if not success: - return failure + success, index, item_expression_list = _bracket_wrapped_parser(_comma_separated_expression_list_parser)(index, tokens) - if tokens[index].type == 'close_parenthese': - index += 1 + 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 True, index, FurParenthesizedExpression(internal=internal) + return failure def _negation_expression_parser(index, tokens): failure = (False, index, None) @@ -142,10 +178,12 @@ def _negation_expression_parser(index, tokens): def _literal_level_expression_parser(index, tokens): return _or_parser( _negation_expression_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, )(index, tokens) @@ -218,6 +256,8 @@ def _comma_separated_list_parser(subparser): items = [] + _, index, _ = consume_newlines(index, tokens) + success, index, item = subparser(index, tokens) if success: @@ -226,10 +266,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 +285,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 +332,129 @@ 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, + ) + + 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, + ) + + 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, + ), + ) - return True, index, FurFunctionCallExpression(function=function, arguments=arguments) -_expression_parser = _or_level_expression_parser + + +_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 +466,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 +538,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 +554,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 +601,7 @@ if __name__ == '__main__': ( True, 1, - FurStringLiteralExpression(value='Hello, world'), + FurStringLiteralExpression(string='Hello, world'), ), ) @@ -458,7 +614,7 @@ if __name__ == '__main__': 4, FurFunctionCallExpression( name='print', - arguments=(FurStringLiteralExpression(value='Hello, world'),), + arguments=(FurStringLiteralExpression(string='Hello, world'),), ), ), )