File: json.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 (118 lines) | stat: -rw-r--r-- 2,909 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
ppxlib-pp-ast as a --json flag that pretty prints the AST in JSON format.

Consider the following .ml file:

  $ cat > test.ml << EOF
  > let x = 2
  > let y = true
  > let z =
  >  fun x ->
  >  x
  > EOF

This is how it's printed without the flag:

  $ ppxlib-pp-ast test.ml
  [ Pstr_value
      ( Nonrecursive
      , [ { pvb_pat = Ppat_var "x"
          ; pvb_expr = Pexp_constant (Pconst_integer ( "2", None))
          ; pvb_constraint = None
          ; pvb_attributes = __attrs
          ; pvb_loc = __loc
          }
        ]
      )
  ; Pstr_value
      ( Nonrecursive
      , [ { pvb_pat = Ppat_var "y"
          ; pvb_expr = Pexp_construct ( Lident "true", None)
          ; pvb_constraint = None
          ; pvb_attributes = __attrs
          ; pvb_loc = __loc
          }
        ]
      )
  ; Pstr_value
      ( Nonrecursive
      , [ { pvb_pat = Ppat_var "z"
          ; pvb_expr =
              Pexp_function
                ( [ { pparam_loc = __loc
                    ; pparam_desc = Pparam_val ( Nolabel, None, Ppat_var "x")
                    }
                  ]
                , None
                , Pfunction_body (Pexp_ident (Lident "x"))
                )
          ; pvb_constraint = None
          ; pvb_attributes = __attrs
          ; pvb_loc = __loc
          }
        ]
      )
  ]
Now how it's printed with the flag:

  $ ppxlib-pp-ast --json test.ml
  [
    {
      "Pstr_value": [
        "Nonrecursive",
        [
          {
            "pvb_pat": { "Ppat_var": "x" },
            "pvb_expr": {
              "Pexp_constant": { "Pconst_integer": [ "2", "None" ] }
            },
            "pvb_constraint": "None",
            "pvb_attributes": "__attrs",
            "pvb_loc": "__loc"
          }
        ]
      ]
    },
    {
      "Pstr_value": [
        "Nonrecursive",
        [
          {
            "pvb_pat": { "Ppat_var": "y" },
            "pvb_expr": { "Pexp_construct": [ { "Lident": "true" }, "None" ] },
            "pvb_constraint": "None",
            "pvb_attributes": "__attrs",
            "pvb_loc": "__loc"
          }
        ]
      ]
    },
    {
      "Pstr_value": [
        "Nonrecursive",
        [
          {
            "pvb_pat": { "Ppat_var": "z" },
            "pvb_expr": {
              "Pexp_function": [
                [
                  {
                    "pparam_loc": "__loc",
                    "pparam_desc": {
                      "Pparam_val": [ "Nolabel", "None", { "Ppat_var": "x" } ]
                    }
                  }
                ],
                "None",
                { "Pfunction_body": { "Pexp_ident": { "Lident": "x" } } }
              ]
            },
            "pvb_constraint": "None",
            "pvb_attributes": "__attrs",
            "pvb_loc": "__loc"
          }
        ]
      ]
    }
  ]

You can compase with other flags, for example --show-locs to display location: