import collections
+def consume_newlines(index, tokens):
+ while index < len(tokens) and tokens[index].type == 'newline':
+ index += 1
+
+ return True, index, None
+
def _or_parser(*parsers):
def result_parser(index, tokens):
failure = (False, index, None)
],
)
+# TODO We don't need to wrap this type
FurParenthesizedExpression = collections.namedtuple(
'FurParenthesizedExpression',
[
'addition_level',
)(index, tokens)
-def _equality_level_expression_parser(index, tokens):
+def _comparison_level_expression_parser(index, tokens):
return _left_recursive_infix_operator_parser(
- lambda token: token.type == 'equality_level_operator',
+ lambda token: token.type == 'comparison_level_operator',
_addition_level_expression_parser,
- 'equality_level',
+ 'comparison_level',
)(index, tokens)
def _and_level_expression_parser(index, tokens):
return _left_recursive_infix_operator_parser(
lambda token: token.type == 'symbol' and token.match == 'and',
- _equality_level_expression_parser,
+ _comparison_level_expression_parser,
'and_level',
)(index, tokens)
)(index, tokens)
def _comma_separated_list_parser(index, tokens):
- failure = (False, index, None)
+ start_index = index
expressions = []
if success:
expressions.append(expression)
else:
- return failure
+ return (True, start_index, ())
while success and index < len(tokens) and tokens[index].type == 'comma':
success = False
],
)
+FurExpressionStatement = collections.namedtuple(
+ 'FurExpressionStatement',
+ [
+ 'expression',
+ ],
+)
+
FurAssignmentStatement = collections.namedtuple(
'FurAssignmentStatement',
[
],
)
+FurFunctionDefinitionStatement = collections.namedtuple(
+ 'FurFunctionDefinitionStatement',
+ [
+ 'name',
+ 'statement_list',
+ ],
+)
+
FurProgram = collections.namedtuple(
'FurProgram',
[
_expression_parser = _or_level_expression_parser
+def _expression_statement_parser(index, tokens):
+ failure = (False, index, None)
+
+ success, index, expression = _expression_parser(index, tokens)
+
+ if not success:
+ return failure
+
+ return (True, index, FurExpressionStatement(expression=expression))
+
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)
return True, index, FurAssignmentStatement(target=target, expression=expression)
+def _function_definition_statement_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'def':
+ index += 1
+ else:
+ return failure
+
+ if tokens[index].type == 'symbol':
+ name = tokens[index].match
+ index += 1
+ else:
+ raise Exception('Expected function name, found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].type == 'open_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected "(", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].type == 'close_parenthese':
+ index += 1
+ else:
+ raise Exception('Expected ")", found "{}" on line {}'.format(
+ tokens[index].match,
+ tokens[index].line,
+ ))
+
+ if tokens[index].type == 'symbol' and tokens[index].match == 'do':
+ index += 1
+ else:
+ return failure
+
+ success, index, statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+
+ _, index, _ = consume_newlines(index, tokens)
+
+ if tokens[index].type == 'keyword' and tokens[index].match == 'end':
+ index += 1
+ else:
+ return failure
+
+ return True, index, FurFunctionDefinitionStatement(name=name, statement_list=statement_list)
+
def _statement_parser(index, tokens):
- # TODO It would be good to include newlines in the parsing of this because it removes the ambiguity between "function(argument)" (one statement) and "function\n(argument)" (two statements)
+ _, index, _ = consume_newlines(index, tokens)
+
+ if index == len(tokens):
+ return (False, index, None)
+
return _or_parser(
_assignment_statement_parser,
- _expression_parser,
+ _expression_statement_parser,
+ _function_definition_statement_parser,
)(index, tokens)
def _program_formatter(statement_list):