Re: [racket-users] Affecting the errors generated by syntax-parse

2015-07-25 Thread Stephen Chang
I haven't played with the example in detail, but do you want the cut
operator here?

http://docs.racket-lang.org/syntax/stxparse-patterns.html?q=syntax-parse#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7e!%29%29

On Sat, Jul 25, 2015 at 9:55 AM, Jens Axel Søgaard
jensa...@soegaard.net wrote:
 Hi All,

 The syntax-parse form can often generate sensible errors based on the
 progress made
 during the attempt to match a pattern against a syntax-object. Given
 multiple choices
 the choice where the maximum progress was made is used to generate the
 error message.

 Is there a way to tell syntax-parse, that a given choice is to be used as
 the source
 of the error, if the match fails?

 A concrete example: In the toy language below a top-level form can be a
 definition
 or an expression. The malformed definition (define (foo 3) x) is not a legal
 definition -- it is however clear that the user intended to write a
 definition,
 since the form begins with (define ...). During matching syntax-parse
 makes more progress under the assumption that the definition is a sequence
 expression, so the error message becomes: begin expected.

 How can I stop the matching prematurely when the (define ...) is seen?

 /Jens Axel

 #lang racket
 ;;;
 ;;; URLANG
 ;;;

 ; In the grammar below x stands for a non-keyword identifier

 ; program::= top-level-form ...
 ; top-level-form ::= definition | expr

 ; definition ::= (define (x x ...) body)
 ; body   ::= expr

 ; expr   ::= datum | reference | application | sequence
 ; reference  ::= x
 ; application::= (x0 x ...)
 ; sequence   ::= (begin expr ...)

 ; keyword::= define | begin

 ; datum  ::= fixnum

 ; identifier an identifier that is not a keyword
 ; fixnum an integer between -2^53 and 2^53

 (require syntax/parse)

 (define min-fixnum (- (expt 2 53)))
 (define max-fixnum(expt 2 53))

 (define (Fixnum? r)
   (and (number? r) (integer? r)
(= min-fixnum r max-fixnum)))

 (define-syntax-class Fixnum
   #:opaque
   (pattern d
#:fail-unless (Fixnum? (syntax-e #'d)) #f))

 (define-syntax-class Datum
   (pattern (~or d:Fixnum)))

 (define-syntax-class Keyword
   #:literals (begin define)
   (pattern (~or begin define)))

 (define-syntax-class Id
   (pattern (~and x:id
  (~not y:Keyword

 (define-syntax-class Reference
   (pattern x:Id))

 (define-syntax-class Application
   #:literals (define)
   (pattern (e:Expr ...)))

 (define-syntax-class Sequence
   #:literals (begin)
   (pattern (begin e0:Expr e:Expr ...)))

 (define-syntax-class Definition
   #:literals (define)
   (pattern (define (name:Id a:Id ...) body:Body)))


 (define-syntax-class Body
   (pattern b:Expr))

 (define-syntax-class Expr
   (pattern (~or e:Datum
 e:Application
 e:Reference
 e:Sequence)))

 (define-syntax-class TopLevelForm
   (pattern (~or t:Definition
 t:Expr)))

 (define-syntax-class Program
   (pattern (p:TopLevelForm ...)))

 ;;; The following expressions show some legal
 ;;; constructs that are matched correctly.

 (syntax-parse #'3
   [d:Datum 'datum])

 (syntax-parse #'3
   [e:Expr 'expr])

 (syntax-parse #'(define (a x) x)
   [t:TopLevelForm 'toplevelform])

 (syntax-parse #'((define (foo x y) (+ x 1)) (foo 3))
   [p:Program 'program])

 ;;; The malformed definition (define (a 4 x) x) is
 ;;; correctly rejected as a toplevel form, but the
 ;;; automatically generated error is not as intended.

 (syntax-parse #'(define (a 4 x) x)
   [t:TopLevelForm 'toplevelform])

 --
 You received this message because you are subscribed to the Google Groups
 Racket Users group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to racket-users+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Affecting the errors generated by syntax-parse

2015-07-25 Thread Jens Axel Søgaard
Hi All,

The syntax-parse form can often generate sensible errors based on the
progress made
during the attempt to match a pattern against a syntax-object. Given
multiple choices
the choice where the maximum progress was made is used to generate the
error message.

Is there a way to tell syntax-parse, that a given choice is to be used as
the source
of the error, if the match fails?

A concrete example: In the toy language below a top-level form can be a
definition
or an expression. The malformed definition (define (foo 3) x) is not a legal
definition -- it is however clear that the user intended to write a
definition,
since the form begins with (define ...). During matching syntax-parse
makes more progress under the assumption that the definition is a sequence
expression, so the error message becomes: begin expected.

How can I stop the matching prematurely when the (define ...) is seen?

/Jens Axel

#lang racket
;;;
;;; URLANG
;;;

; In the grammar below x stands for a non-keyword identifier

; program::= top-level-form ...
; top-level-form ::= definition | expr

; definition ::= (define (x x ...) body)
; body   ::= expr

; expr   ::= datum | reference | application | sequence
; reference  ::= x
; application::= (x0 x ...)
; sequence   ::= (begin expr ...)

; keyword::= define | begin

; datum  ::= fixnum

; identifier an identifier that is not a keyword
; fixnum an integer between -2^53 and 2^53

(require syntax/parse)

(define min-fixnum (- (expt 2 53)))
(define max-fixnum(expt 2 53))

(define (Fixnum? r)
  (and (number? r) (integer? r)
   (= min-fixnum r max-fixnum)))

(define-syntax-class Fixnum
  #:opaque
  (pattern d
   #:fail-unless (Fixnum? (syntax-e #'d)) #f))

(define-syntax-class Datum
  (pattern (~or d:Fixnum)))

(define-syntax-class Keyword
  #:literals (begin define)
  (pattern (~or begin define)))

(define-syntax-class Id
  (pattern (~and x:id
 (~not y:Keyword

(define-syntax-class Reference
  (pattern x:Id))

(define-syntax-class Application
  #:literals (define)
  (pattern (e:Expr ...)))

(define-syntax-class Sequence
  #:literals (begin)
  (pattern (begin e0:Expr e:Expr ...)))

(define-syntax-class Definition
  #:literals (define)
  (pattern (define (name:Id a:Id ...) body:Body)))


(define-syntax-class Body
  (pattern b:Expr))

(define-syntax-class Expr
  (pattern (~or e:Datum
e:Application
e:Reference
e:Sequence)))

(define-syntax-class TopLevelForm
  (pattern (~or t:Definition
t:Expr)))

(define-syntax-class Program
  (pattern (p:TopLevelForm ...)))

;;; The following expressions show some legal
;;; constructs that are matched correctly.

(syntax-parse #'3
  [d:Datum 'datum])

(syntax-parse #'3
  [e:Expr 'expr])

(syntax-parse #'(define (a x) x)
  [t:TopLevelForm 'toplevelform])

(syntax-parse #'((define (foo x y) (+ x 1)) (foo 3))
  [p:Program 'program])

;;; The malformed definition (define (a 4 x) x) is
;;; correctly rejected as a toplevel form, but the
;;; automatically generated error is not as intended.

(syntax-parse #'(define (a 4 x) x)
  [t:TopLevelForm 'toplevelform])

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Affecting the errors generated by syntax-parse

2015-07-25 Thread Jens Axel Søgaard
Hi Stephen,

Thanks for testing this. Turns out I had removed a #:opaque in Definition
while cleaning up
the example before sending the mail. Now I get the same error as you.

With

(define-syntax-class Definition
  #:opaque
  #:literals (define)
  (pattern (define (name:Id a:Id ...) body:Body)))

I get the wrong error (because #:opaque shortens the progress).

I had experimented with ~! as in:

(define-syntax-class Definition
  #:opaque
  #:literals (define)
  (pattern (define ~! (name:Id a:Id ...) body:Body)))

but the #:opaque ruins the effect of ~! here (not sure why).


Sorry for the noise,
Jens Axel



2015-07-25 16:55 GMT+02:00 Stephen Chang stch...@ccs.neu.edu:

 Actually, I dont get the begin expected error that you mention. I'm
 getting:

 define: expected identifier
   parsing context:
while parsing Id
while parsing Definition
while parsing TopLevelForm in: 4

 with the 4 highlighted, which seems correct since it comes from using
 the Definition class?

 On Sat, Jul 25, 2015 at 10:51 AM, Stephen Chang stch...@ccs.neu.edu
 wrote:
  I haven't played with the example in detail, but do you want the cut
  operator here?
 
 
 http://docs.racket-lang.org/syntax/stxparse-patterns.html?q=syntax-parse#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7e!%29%29
 
  On Sat, Jul 25, 2015 at 9:55 AM, Jens Axel Søgaard
  jensa...@soegaard.net wrote:
  Hi All,
 
  The syntax-parse form can often generate sensible errors based on the
  progress made
  during the attempt to match a pattern against a syntax-object. Given
  multiple choices
  the choice where the maximum progress was made is used to generate the
  error message.
 
  Is there a way to tell syntax-parse, that a given choice is to be used
 as
  the source
  of the error, if the match fails?
 
  A concrete example: In the toy language below a top-level form can be a
  definition
  or an expression. The malformed definition (define (foo 3) x) is not a
 legal
  definition -- it is however clear that the user intended to write a
  definition,
  since the form begins with (define ...). During matching syntax-parse
  makes more progress under the assumption that the definition is a
 sequence
  expression, so the error message becomes: begin expected.
 
  How can I stop the matching prematurely when the (define ...) is seen?
 
  /Jens Axel
 
  #lang racket
  ;;;
  ;;; URLANG
  ;;;
 
  ; In the grammar below x stands for a non-keyword identifier
 
  ; program::= top-level-form ...
  ; top-level-form ::= definition | expr
 
  ; definition ::= (define (x x ...) body)
  ; body   ::= expr
 
  ; expr   ::= datum | reference | application |
 sequence
  ; reference  ::= x
  ; application::= (x0 x ...)
  ; sequence   ::= (begin expr ...)
 
  ; keyword::= define | begin
 
  ; datum  ::= fixnum
 
  ; identifier an identifier that is not a keyword
  ; fixnum an integer between -2^53 and 2^53
 
  (require syntax/parse)
 
  (define min-fixnum (- (expt 2 53)))
  (define max-fixnum(expt 2 53))
 
  (define (Fixnum? r)
(and (number? r) (integer? r)
 (= min-fixnum r max-fixnum)))
 
  (define-syntax-class Fixnum
#:opaque
(pattern d
 #:fail-unless (Fixnum? (syntax-e #'d)) #f))
 
  (define-syntax-class Datum
(pattern (~or d:Fixnum)))
 
  (define-syntax-class Keyword
#:literals (begin define)
(pattern (~or begin define)))
 
  (define-syntax-class Id
(pattern (~and x:id
   (~not y:Keyword
 
  (define-syntax-class Reference
(pattern x:Id))
 
  (define-syntax-class Application
#:literals (define)
(pattern (e:Expr ...)))
 
  (define-syntax-class Sequence
#:literals (begin)
(pattern (begin e0:Expr e:Expr ...)))
 
  (define-syntax-class Definition
#:literals (define)
(pattern (define (name:Id a:Id ...) body:Body)))
 
 
  (define-syntax-class Body
(pattern b:Expr))
 
  (define-syntax-class Expr
(pattern (~or e:Datum
  e:Application
  e:Reference
  e:Sequence)))
 
  (define-syntax-class TopLevelForm
(pattern (~or t:Definition
  t:Expr)))
 
  (define-syntax-class Program
(pattern (p:TopLevelForm ...)))
 
  ;;; The following expressions show some legal
  ;;; constructs that are matched correctly.
 
  (syntax-parse #'3
[d:Datum 'datum])
 
  (syntax-parse #'3
[e:Expr 'expr])
 
  (syntax-parse #'(define (a x) x)
[t:TopLevelForm 'toplevelform])
 
  (syntax-parse #'((define (foo x y) (+ x 1)) (foo 3))
[p:Program 'program])
 
  ;;; The malformed definition (define (a 4 x) x) is
  ;;; correctly rejected as a toplevel form, but the
  ;;; automatically generated error is not as intended.
 
  (syntax-parse #'(define (a 4 x) x)
[t:TopLevelForm 'toplevelform])
 
  --
  You received this message because you are subscribed to the Google
 Groups
  Racket Users group.
  To unsubscribe from this group and stop receiving emails from it, 

Re: [racket-users] Affecting the errors generated by syntax-parse

2015-07-25 Thread Stephen Chang
Actually, I dont get the begin expected error that you mention. I'm getting:

define: expected identifier
  parsing context:
   while parsing Id
   while parsing Definition
   while parsing TopLevelForm in: 4

with the 4 highlighted, which seems correct since it comes from using
the Definition class?

On Sat, Jul 25, 2015 at 10:51 AM, Stephen Chang stch...@ccs.neu.edu wrote:
 I haven't played with the example in detail, but do you want the cut
 operator here?

 http://docs.racket-lang.org/syntax/stxparse-patterns.html?q=syntax-parse#%28form._%28%28lib._syntax%2Fparse..rkt%29._~7e!%29%29

 On Sat, Jul 25, 2015 at 9:55 AM, Jens Axel Søgaard
 jensa...@soegaard.net wrote:
 Hi All,

 The syntax-parse form can often generate sensible errors based on the
 progress made
 during the attempt to match a pattern against a syntax-object. Given
 multiple choices
 the choice where the maximum progress was made is used to generate the
 error message.

 Is there a way to tell syntax-parse, that a given choice is to be used as
 the source
 of the error, if the match fails?

 A concrete example: In the toy language below a top-level form can be a
 definition
 or an expression. The malformed definition (define (foo 3) x) is not a legal
 definition -- it is however clear that the user intended to write a
 definition,
 since the form begins with (define ...). During matching syntax-parse
 makes more progress under the assumption that the definition is a sequence
 expression, so the error message becomes: begin expected.

 How can I stop the matching prematurely when the (define ...) is seen?

 /Jens Axel

 #lang racket
 ;;;
 ;;; URLANG
 ;;;

 ; In the grammar below x stands for a non-keyword identifier

 ; program::= top-level-form ...
 ; top-level-form ::= definition | expr

 ; definition ::= (define (x x ...) body)
 ; body   ::= expr

 ; expr   ::= datum | reference | application | sequence
 ; reference  ::= x
 ; application::= (x0 x ...)
 ; sequence   ::= (begin expr ...)

 ; keyword::= define | begin

 ; datum  ::= fixnum

 ; identifier an identifier that is not a keyword
 ; fixnum an integer between -2^53 and 2^53

 (require syntax/parse)

 (define min-fixnum (- (expt 2 53)))
 (define max-fixnum(expt 2 53))

 (define (Fixnum? r)
   (and (number? r) (integer? r)
(= min-fixnum r max-fixnum)))

 (define-syntax-class Fixnum
   #:opaque
   (pattern d
#:fail-unless (Fixnum? (syntax-e #'d)) #f))

 (define-syntax-class Datum
   (pattern (~or d:Fixnum)))

 (define-syntax-class Keyword
   #:literals (begin define)
   (pattern (~or begin define)))

 (define-syntax-class Id
   (pattern (~and x:id
  (~not y:Keyword

 (define-syntax-class Reference
   (pattern x:Id))

 (define-syntax-class Application
   #:literals (define)
   (pattern (e:Expr ...)))

 (define-syntax-class Sequence
   #:literals (begin)
   (pattern (begin e0:Expr e:Expr ...)))

 (define-syntax-class Definition
   #:literals (define)
   (pattern (define (name:Id a:Id ...) body:Body)))


 (define-syntax-class Body
   (pattern b:Expr))

 (define-syntax-class Expr
   (pattern (~or e:Datum
 e:Application
 e:Reference
 e:Sequence)))

 (define-syntax-class TopLevelForm
   (pattern (~or t:Definition
 t:Expr)))

 (define-syntax-class Program
   (pattern (p:TopLevelForm ...)))

 ;;; The following expressions show some legal
 ;;; constructs that are matched correctly.

 (syntax-parse #'3
   [d:Datum 'datum])

 (syntax-parse #'3
   [e:Expr 'expr])

 (syntax-parse #'(define (a x) x)
   [t:TopLevelForm 'toplevelform])

 (syntax-parse #'((define (foo x y) (+ x 1)) (foo 3))
   [p:Program 'program])

 ;;; The malformed definition (define (a 4 x) x) is
 ;;; correctly rejected as a toplevel form, but the
 ;;; automatically generated error is not as intended.

 (syntax-parse #'(define (a 4 x) x)
   [t:TopLevelForm 'toplevelform])

 --
 You received this message because you are subscribed to the Google Groups
 Racket Users group.
 To unsubscribe from this group and stop receiving emails from it, send an
 email to racket-users+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
Racket Users group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.