-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""A basic S-expression parser. | ||
Example usage: | ||
$ python sexp.py '123' | ||
123 | ||
$ python sexp.py 'hello' | ||
'hello' | ||
$ python sexp.py '(hello world)' | ||
['hello', 'world'] | ||
$ python sexp.py '(foo (bar) thing 9 hello 9)' | ||
['foo', ['bar'], 'thing', 9, 'hello', 9] | ||
$ python sexp.py '(hello world' | ||
UnexpectedEOF: Expected brace ')' but reached end of file | ||
""" | ||
|
||
|
||
import sys | ||
from typing import Any | ||
|
||
from tokenstream import InvalidSyntax, TokenStream | ||
|
||
|
||
def parse_sexp(stream: TokenStream) -> Any: | ||
with stream.syntax(brace=r"\(|\)", number=r"\d+", name=r"\w+"): | ||
brace, number, name = stream.expect(("brace", "("), "number", "name") | ||
if brace: | ||
return [parse_sexp(stream) for _ in stream.peek_until(("brace", ")"))] | ||
elif number: | ||
return int(number.value) | ||
elif name: | ||
return name.value | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
stream = TokenStream(" ".join(sys.argv[1:])) | ||
result = parse_sexp(stream) | ||
stream.expect_eof() | ||
print(repr(result)) | ||
except InvalidSyntax as exc: | ||
print(f"{exc.__class__.__name__}: {exc}") |