File: basic.t

package info (click to toggle)
ppxlib 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,804 kB
  • sloc: ml: 66,587; sh: 103; makefile: 40; python: 36
file content (78 lines) | stat: -rw-r--r-- 1,736 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
ppxlib-pp-ast is a simple utility to pretty-print the AST corresponding
to a given piece of source code or a marshalled AST.

It can be used on regular .ml files:

  $ cat > test.ml << EOF
  > let x = x + 2
  > EOF
  $ ppxlib-pp-ast test.ml
  [ Pstr_value
      ( Nonrecursive
      , [ { pvb_pat = Ppat_var "x"
          ; pvb_expr =
              Pexp_apply
                ( Pexp_ident (Lident "+")
                , [ ( Nolabel, Pexp_ident (Lident "x"))
                  ; ( Nolabel, Pexp_constant (Pconst_integer ( "2", None)))
                  ]
                )
          ; pvb_constraint = None
          ; pvb_attributes = __attrs
          ; pvb_loc = __loc
          }
        ]
      )
  ]

on .mli files:

  $ cat > test.mli << EOF
  > val x : int
  > EOF
  $ ppxlib-pp-ast test.mli
  [ Psig_value
      { pval_name = "x"
      ; pval_type = Ptyp_constr ( Lident "int", [])
      ; pval_prim = []
      ; pval_attributes = __attrs
      ; pval_loc = __loc
      }
  ]

But it can also be used to pretty a single expression:

  $ ppxlib-pp-ast --exp - << EOF
  > x + 2
  > EOF
  Pexp_apply
    ( Pexp_ident (Lident "+")
    , [ ( Nolabel, Pexp_ident (Lident "x"))
      ; ( Nolabel, Pexp_constant (Pconst_integer ( "2", None)))
      ]
    )

on a single pattern:

  $ ppxlib-pp-ast --pat - << EOF
  > (x, _::tl)
  > EOF
  Ppat_tuple
    [ Ppat_var "x"
    ; Ppat_construct
        ( Lident "::", Some ( [], Ppat_tuple [ Ppat_any; Ppat_var "tl"]))
    ]

or on a single core_type:

  $ ppxlib-pp-ast --typ - << EOF
  > (int * string) result
  > EOF
  Ptyp_constr
    ( Lident "result"
    , [ Ptyp_tuple
          [ Ptyp_constr ( Lident "int", [])
          ; Ptyp_constr ( Lident "string", [])
          ]
      ]
    )