From 56f51a9a20c9393decb79060dfebd53f3694c90d Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 26 Feb 2019 13:56:42 -0500 Subject: [PATCH] Implement hy.lex.parse_one_thing --- hy/lex/__init__.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/hy/lex/__init__.py b/hy/lex/__init__.py index eb3ac41..d133f5f 100644 --- a/hy/lex/__init__.py +++ b/hy/lex/__init__.py @@ -74,6 +74,33 @@ def tokenize(source, filename=None): raise e +def parse_one_thing(src_string): + """Parse the first form from the string. Return it and the + remainder of the string.""" + import re + from hy.lex.lexer import lexer + from hy.lex.parser import parser + from rply.errors import LexingError + tokens = [] + err = None + for token in lexer.lex(src_string): + tokens.append(token) + try: + model, = parser.parse( + iter(tokens), + state=ParserState(src_string, filename=None)) + except (LexingError, LexException) as e: + err = e + else: + return model, src_string[re.match( + r'.+\n' * (model.end_line - 1) + + '.' * model.end_column, + src_string).end():] + if err: + raise err + raise ValueError("No form found") + + mangle_delim = 'X'