3 StringLiteral = collections.namedtuple(
10 def _string_literal_parser(index, tokens):
11 failure = (False, index, None)
13 if tokens[index].type != 'single_quoted_string_literal':
15 value = tokens[index].match[1:-1]
18 return True, index, StringLiteral(value=value)
21 FunctionCall = collections.namedtuple(
29 def _function_call_parser(index, tokens):
30 failure = (False, index, None)
32 if tokens[index].type != 'symbol':
34 name = tokens[index].match
37 if tokens[index].type != 'open_parenthese':
41 success, index, argument = _string_literal_parser(index, tokens)
46 if tokens[index].type != 'close_parenthese':
50 return True, index, FunctionCall(name=name, arguments=(argument,))
52 def _parse(parser, tokens):
53 success, index, result = parser(0, tokens)
58 raise Exception('Unable to parse')
62 return _parse(_function_call_parser, tokens)
64 if __name__ == '__main__':
69 class StringLiteralParserTests(unittest.TestCase):
70 def test_parses_single_quoted_string_literal(self):
72 _string_literal_parser(0, tokenization.tokenize("'Hello, world'")),
76 StringLiteral(value='Hello, world'),
80 class FunctionCallParserTests(unittest.TestCase):
81 def test_parses_function_with_string_literal_argument(self):
83 _function_call_parser(0, tokenization.tokenize("print('Hello, world')")),
89 arguments=(StringLiteral(value='Hello, world'),),