Extended indexing operators#1064
Conversation
|
I like it! (In general I also like the idea of having operator names mirror the operator syntax, similar to Agda's mixfixes, which has first been given to me, in an OCaml contet, by Nicolas Pouillard in 2010. But this was already part of your previous proposal.) |
|
If I understand correctly, the testsuite currently checks the parsed AST. Would it be possible to also test that these programs are pretty-printed back correctly (that is, in a way that can be parsed back to the same thing)? |
|
The |
| mkexp @@ Pexp_apply(id , [Nolabel, $1; Nolabel, $4; Nolabel, $7]) } | ||
| | simple_expr DOTOP LBRACE expr RBRACE | ||
| { let id = mkexp @@ Pexp_ident( ghloc @@ Lident ("." ^ $2 ^ "{}")) in | ||
| mkexp @@ Pexp_apply(id, [Nolabel, $1; Nolabel, $4]) } |
There was a problem hiding this comment.
This is orthogonal, but it might be beneficial to add rules of the form simple_expr DOTOP LBRACE expr error to give better error messages.
|
The patch looks good to me (and I like the proposed solution). I wonder if maybe it would be time to change the representation of (non module) identifier to a sum type. That would make the printing implementation slightly less ridiculous. |
|
Extending on @Drup comment, I have added a comment to clarify the cryptic string manipulation in |
|
Looks good but I don't much like the restrictions on characters:
|
|
With the current scheme (i.e. using a new DOTOP token in the lexer) allowing either
There is potentially two ways to get around this limitation: restrict the syntax to merge The first solution will slightly break the symmetry between classical indexing operators and the extended ones: |
|
OK, those are good reasons. |
b0dd657 to
03f8294
Compare
This commit adds extended indexing operators to the parser.
For instance,
```
let (.%()) p (x,y) = p.( x ).( y )
;; p.%(0,0)
```
Extended indexing operators name starts with a leading
dot "." followed by an operator symbol, except "." or "<"
and a sequence of any operators and must be closes with a
couple of enclosing parentheses ( i.e. "()", "[]", "{}" )
and then an optional assignment operator "<-":
* '.' dotsymbolchar symbolchar* '(' ')' ['<-']
* '.' dotsymbolchar symbolchar* '[' ']' ['<-']
* '.' dotsymbolchar symbolchar* '{' '}' ['<-']
Similarly, expressions of the form
* expr_1 '.' dotsymbolchar symbolchar* '(' expr_2 ')'
* expr_1 '.' dotsymbolchar symbolchar* '[' expr_2 ']'
* expr_1 '.' dotsymbolchar symbolchar* '{' expr_2 '}'
and
* expr_1 '.' dotsymbolchar symbolchar* '(' expr_2 ')' '<-' expr_3
* expr_1 '.' dotsymbolchar symbolchar* '[' expr_2 ']' '<-' expr_3
* expr_1 '.' dotsymbolchar symbolchar* '{' expr_2 '}' '<-' expr_3
are desugared to
* ('.' dotsymbolchar symbolchar* '(' ')' ) expr_1 expr_2
* ('.' dotsymbolchar symbolchar* '[' ']' ) expr_1 expr_2
* ('.' dotsymbolchar symbolchar* '{' '}' ) expr_1 expr_2
and
* ('.' dotsymbolchar symbolchar* '(' ')' '<-' ) expr_1 expr_2 expr_3
* ('.' dotsymbolchar symbolchar* '[' ']' ) expr_1 expr_2 expr_3
* ('.' dotsymbolchar symbolchar* '{' '}' '<-' ) expr_1 expr_2 expr_3
03f8294 to
4aec17f
Compare
|
What is the status of this PR for 4.0{6,7}? If the restriction on the operators character is deemed too complicated, a coarser and simpler restriction may be to forbid the characters |
|
I think the restriction is OK. It will always be possible to write unreadable programs so we shouldn't spend too much time on this "issue". Please rebase and I'll merge. |
|
I just noticed that this doesn't support things like |
|
I took the liberty of rebasing this myself. Will be ready to merge once Travis etc finishes. |
Depending on the complexity of the fix and the proximity of the release, maybe. |
|
@lpw25 , I will send a PR adding back qualified operators tomorrow morning. |
| user-defined indexing operators, obtained by adding at least | ||
| one operator character after the dot symbol to the standard indexing | ||
| operators: e,g ".%()", ".?[]", ".@{}<-" | ||
| (Florian Angeletti, review by Damien Doligez and Gabriel Radanne) |
There was a problem hiding this comment.
@Octachron I think this PR breaks backwards compatibility in some edge cases: see https://caml.inria.fr/mantis/view.php?id=7637.
|
Not necessarily a problem, but worth noting here: this change uses up the syntactic space that was considered for the succinct version of overriding local open, i.e. M.!(e)as a shorthand for let open! M in eWith this PR, |
|
Another issue with this change (which I should have spotted earlier): it reserves However, it appears that none of the code available via OPAM is yet using |
) I've also regenerated the test to be sure that printing now works (in the one very simple example). In the long run: * The primitives should probably all be made lowercase for simplicity and for consistency with the IR (currently the "constructor-like" ones get initial caps). * We should be testing fexpr printing as well as parsing (roundtrip test).
Co-authored-by: cuihtlauac <cuihtlauac@users.noreply.github.com>
After #69, #622 and the last developper meeting, this PR proposes to add a new family of user-definable indexing operators, that does not interfere nor can be confused with any existing indexing operators.
Extended indexing operators names start with a leading dot
.followed by an operator character, except.(to avoid confusion with..which has a different meaning ) or<(used as an opening bracket at the type level) , and a possibly empty sequence of operator characters. It must be closed with a couple of enclosing brackets ( i.e.(),[],{}) and optionaly an assignment operator<-:Expressions of the form
expr_1.∘∘∘(expr_2)are then desugared to( .∘∘∘( )) expr_1 expr_2.Similarly for assignment,
expr_1.∘∘∘(expr_2) <- expr_3is desugared to( .∘∘∘( )<-) expr_1 expr_2 expr_3. The same syntactic sugar is applied to all bracket variants (i.e.{,(,[).The compulsory operator character should help to mark these operators as user-defined, and decrease the confusion potential of these operators. Moreover, the use of a qualified dot respects the use of unqualified
.as a projection primitive.