/collects/syntax/scribblings/parse/parsing.scrbl

http://github.com/gmarceau/PLT · Racket · 162 lines · 132 code · 30 blank · 0 comment · 4 complexity · ffd51a7ca57203a777b81471db815fa4 MD5 · raw file

  1. #lang scribble/doc
  2. @(require scribble/manual
  3. scribble/struct
  4. scribble/decode
  5. scribble/eval
  6. "parse-common.rkt")
  7. @title{Parsing syntax}
  8. This section describes @racket[syntax-parse], the
  9. @racketmodname[syntax/parse] library's facility for parsing
  10. syntax. Both @racket[syntax-parse] and the specification facility,
  11. @seclink["stxparse-specifying"]{syntax classes}, use a common language
  12. of @tech{syntax patterns}, which is described in detail in
  13. @secref{stxparse-patterns}.
  14. @declare-exporting[syntax/parse]
  15. Two parsing forms are provided: @racket[syntax-parse] and
  16. @racket[syntax-parser].
  17. @defform/subs[(syntax-parse stx-expr parse-option ... clause ...+)
  18. ([parse-option (code:line #:context context-expr)
  19. (code:line #:literals (literal ...))
  20. (code:line #:literal-sets (literal-set ...))
  21. (code:line #:conventions (convention-id ...))
  22. (code:line #:local-conventions (convention-rule ...))
  23. (code:line #:disable-colon-notation)]
  24. [literal literal-id
  25. (pattern-id literal-id)
  26. (pattern-id literal-id #:phase phase-expr)]
  27. [literal-set literal-set-id
  28. (literal-set-id literal-set-option ...)]
  29. [literal-set-option (code:line #:at context-id)
  30. (code:line #:phase phase-expr)]
  31. [clause (syntax-pattern pattern-directive ... expr ...+)])
  32. #:contracts ([stx-expr syntax?]
  33. [context-expr syntax?]
  34. [phase-expr (or/c exact-integer? #f)])]{
  35. Evaluates @racket[stx-expr], which should produce a syntax object, and
  36. matches it against the @racket[clause]s in order. If some clause's
  37. pattern matches, its attributes are bound to the corresponding
  38. subterms of the syntax object and that clause's side conditions and
  39. @racket[expr] is evaluated. The result is the result of @racket[expr].
  40. Each clause consists of a @tech{syntax pattern}, an optional sequence
  41. of @tech{pattern directives}, and a non-empty sequence of body
  42. expressions.
  43. If the syntax object fails to match any of the patterns (or all
  44. matches fail the corresponding clauses' side conditions), a syntax
  45. error is raised.
  46. The following options are supported:
  47. @specsubform[(code:line #:context context-expr)
  48. #:contracts ([context-expr syntax?])]{
  49. When present, @racket[context-expr] is used in reporting parse
  50. failures; otherwise @racket[stx-expr] is used.
  51. @(myexamples
  52. (syntax-parse #'(a b 3)
  53. [(x:id ...) 'ok])
  54. (syntax-parse #'(a b 3)
  55. #:context #'(lambda (a b 3) (+ a b))
  56. [(x:id ...) 'ok]))
  57. }
  58. @specsubform/subs[(code:line #:literals (literal ...))
  59. ([literal literal-id
  60. (pattern-id literal-id)
  61. (pattern-id literal-id #:phase phase-expr)])
  62. #:contracts ([phase-expr (or/c exact-integer? #f)])]{
  63. @margin-note*{
  64. Unlike @racket[syntax-case], @racket[syntax-parse] requires all
  65. literals to have a binding. To match identifiers by their symbolic
  66. names, use the @racket[~datum] pattern form instead.
  67. }
  68. @;
  69. The @racket[#:literals] option specifies identifiers that should be
  70. treated as @tech{literals} rather than @tech{pattern variables}. An
  71. entry in the literals list has two components: the identifier used
  72. within the pattern to signify the positions to be matched
  73. (@racket[pattern-id]), and the identifier expected to occur in those
  74. positions (@racket[literal-id]). If the entry is a single identifier,
  75. that identifier is used for both purposes.
  76. If the @racket[#:phase] option is given, then the literal is compared
  77. at phase @racket[phase-expr]. Specifically, the binding of the
  78. @racket[literal-id] at phase @racket[phase-expr] must match the
  79. input's binding at phase @racket[phase-expr].
  80. }
  81. @specsubform/subs[(code:line #:literal-sets (literal-set ...))
  82. ([literal-set literal-set-id
  83. (literal-set-id literal-set-option ...)]
  84. [literal-set-option (code:line #:at lctx)
  85. (code:line #:phase phase-expr)])
  86. #:contracts ([phase-expr (or/c exact-integer? #f)])]{
  87. Many literals can be declared at once via one or more @tech{literal
  88. sets}, imported with the @racket[#:literal-sets] option. See
  89. @tech{literal sets} for more information.
  90. If the @racket[#:at] keyword is given, the lexical context of the
  91. @racket[lctx] term is used to determine which identifiers in the
  92. patterns are treated as literals; this option is useful primarily for
  93. macros that generate @racket[syntax-parse] expressions.
  94. }
  95. @specsubform[(code:line #:conventions (conventions-id ...))]{
  96. Imports @tech{convention}s that give default syntax classes to pattern
  97. variables that do not explicitly specify a syntax class.
  98. }
  99. @specsubform[(code:line #:local-conventions (convention-rule ...))]{
  100. Uses the @tech{conventions} specified. The advantage of
  101. @racket[#:local-conventions] over @racket[#:conventions] is that local
  102. conventions can be in the scope of syntax-class parameter
  103. bindings. See the section on @tech{conventions} for examples.
  104. }
  105. @specsubform[(code:line #:disable-colon-notation)]{
  106. Suppresses the ``colon notation'' for annotated pattern variables.
  107. @myexamples[
  108. (syntax-parse #'(a b c)
  109. [(x:y ...) 'ok])
  110. (syntax-parse #'(a b c) #:disable-colon-notation
  111. [(x:y ...) 'ok])
  112. ]
  113. }
  114. }
  115. @defform[(syntax-parser parse-option ... clause ...+)]{
  116. Like @racket[syntax-parse], but produces a matching procedure. The
  117. procedure accepts a single argument, which should be a syntax object.
  118. }
  119. @defform[(define/syntax-parse syntax-pattern pattern-directive ... stx-expr)
  120. #:contracts ([stx-expr syntax?])]{
  121. Definition form of @racket[syntax-parse]. That is, it matches the
  122. syntax object result of @racket[stx-expr] against
  123. @racket[syntax-pattern] and creates pattern variable definitions for
  124. the attributes of @racket[syntax-pattern].
  125. @myexamples[
  126. (define/syntax-parse ((~seq kw:keyword arg:expr) ...)
  127. #'(#:a 1 #:b 2 #:c 3))
  128. #'(kw ...)
  129. ]
  130. Compare with @racket[define/with-syntax], a similar definition form
  131. that uses the simpler @racket[syntax-case] patterns.
  132. }