zhong-j-yu / rekex

PEG parser generator for Java 17 - grammar as algebraic datatypes
Apache License 2.0
59 stars 6 forks source link

Semantic predicate failures as declared Exceptions #2

Closed zhong-j-yu closed 3 years ago

zhong-j-yu commented 3 years ago

If the invocation of a ctor throws an Exception, the type of which is declared in the throws clause, it is considered a semantic predicate failure; the corresponding production rule is considered to fail to match the input. But this is not fatal, the parser will try alternatives if there's any.

If a ctor throws any other Exception, it is a Fatal error that stops the parser immediately.

If a ctor throws an Error, we do not catch it or handle it, it will propagate out of our parser. It is more fatal than Fatal.

zhong-j-yu commented 3 years ago

Previously, whenever a ctor throws an IllegalArgumentException, it's considered a semantic predicate failure.

The new design requires that predicate failures be declared in method signature, thus the parser knows whether a ctor may contain semantic predicates. If a ctor contains semantic predicates, the ctor must be invoked to see if it satisfies the predicates; if not, move on to succeeding alternatives. But if a ctor does not contain predicates, it does not need to be invoked immediately; it could be invoked much later, e.g. at the end of the parsing process.