X-Git-Url: https://code.kerkeslager.com/?p=fur;a=blobdiff_plain;f=parsing.py;h=6b117457ee6808980d2b34c73381415e47463c2a;hp=62ac92f4c715974595e397631705ecf17b11ce6e;hb=c40954b865b48ffa4993f735d56213fb91c3e90e;hpb=2b8b0fd7071b4a829253e7564289aa179b9a47d9 diff --git a/parsing.py b/parsing.py index 62ac92f..6b11745 100644 --- a/parsing.py +++ b/parsing.py @@ -1,5 +1,12 @@ import collections +IntegerLiteral = collections.namedtuple( + 'IntegerLiteral', + [ + 'value', + ], +) + StringLiteral = collections.namedtuple( 'StringLiteral', [ @@ -7,6 +14,16 @@ StringLiteral = collections.namedtuple( ], ) +def _integer_literal_parser(index, tokens): + failure = (False, index, None) + + if tokens[index].type != 'integer_literal': + return failure + value = int(tokens[index].match) + index += 1 + + return True, index, IntegerLiteral(value=value) + def _string_literal_parser(index, tokens): failure = (False, index, None) @@ -17,6 +34,17 @@ def _string_literal_parser(index, tokens): return True, index, StringLiteral(value=value) +def _argument_parser(index, tokens): + failure = (False, index, None) + + for parser in [_integer_literal_parser, _string_literal_parser]: + success, index, value = parser(index, tokens) + + if success: + return (success, index, value) + + return failure + FunctionCall = collections.namedtuple( 'FunctionCall', @@ -38,7 +66,7 @@ def _function_call_parser(index, tokens): return failure index += 1 - success, index, argument = _string_literal_parser(index, tokens) + success, index, argument = _argument_parser(index, tokens) if not success: return failure