julianmendez / jsexp

Parser in Java for Lisp S-expressions.
https://julianmendez.github.io/jsexp/
10 stars 3 forks source link

Grammar of S-expressions #1

Open ahmet-celik opened 5 years ago

ahmet-celik commented 5 years ago

I am using this library in one of my projects. It is very convenient, and simple. However, I am not sure why vertical bar (|) is treated in a special way. Could you provide the complete grammar of the s-expression you recognize?

Thanks, Ahmet

lvijay commented 3 years ago

The | is a secondary escape mechanism. With just quotes, there's no way to include an atom that contains a " (double quote). The below code helps illustrate:

import static de.tudresden.inf.lat.jsexp.SexpFactory.parse;

import de.tudresden.inf.lat.jsexp.SexpParserException;

public class Foo {
    public static void main(String[] args) throws SexpParserException {
        String s1 = "abcd";      // normal
        String s2 = "a\"bcd";    // error, missing quotation mark
        String s3 = "a|bcd";     // error (unhelpfully throws NPE)
        String s4 = "|ab\"cd|";  // how to include a "
        String s5 = "\"ab|cd\""; // how to include a |

        System.out.println(parse(s1));
        try {
            parse(s2);
        } catch (SexpParserException e) {
            System.out.println("expected");
        }
        try {
            System.out.println(parse(s3));
        } catch (SexpParserException e) {
            System.out.println("expected but not encountered");
        } catch (NullPointerException e) {
            System.out.println("it is what it is");
        }
        System.out.println(parse(s4));
        System.out.println(parse(s5));
    }
}