Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add s-expression parser to lib/util #381

Merged
merged 2 commits into from
Mar 30, 2025
Merged

Add s-expression parser to lib/util #381

merged 2 commits into from
Mar 30, 2025

Conversation

mkhan45
Copy link
Contributor

@mkhan45 mkhan45 commented Mar 18, 2025

I don't remember if you thought this should be added to util, but:

    def parser = SexpParser.new(filename, file_bytes);
    def res = parser.readSexp();
    match (res) {
        Success(res) => {
            ...
        }
        err => {
            ...
        }
    }

case EmptySexp;
}

class SexpParser extends TextReader {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is a parser that might be fed very large files, having position information for errors is really useful. Given that I think it makes sense that this parser take either an error generator object or has a mutable field for a callback on errors, that includes location information.

Then the parser can return a default value for the ADT (atom of an empty string) if it fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to just exploit the underlying TextReader's error fields?

    def readAtom() -> SExpr {
        skipWhitespace();
        def atom_end = star_rel(1, atomChar);
        if (atom_end == -1) {
	    fail("Expected atom");
	    return SExpr.Atom("");
	}

        def tok = readToken(atom_end - pos);
        skipWhitespace();
        return SExpr.Atom(tok.image);
    }

@@ -0,0 +1,75 @@
type Sexp {
case Atom(data: string);
case List(elts: Range<Sexp>); // usually Cons
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a list for the elements? Also, I think I'd prefer elems or elements as the field name.

@@ -0,0 +1,75 @@
type Sexp {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe SExpr as a name?

@titzer titzer merged commit 36fa9c5 into titzer:master Mar 30, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants