0.2.2 • Published 10 years ago

cljs-parser v0.2.2

Weekly downloads
-
License
-
Repository
-
Last release
10 years ago

cljs-parser Build Status devDependency Status

Clojurescript parser.

If it doesn't work, it's probably because I have no idea what I'm doing. I've never even coded in clojurescript. Help needed and appreciated.

Usage

var parser = require("cljs-parser")
  , fs = require("fs")

fs.readFile("/path/to/src.cljs", {encoding: "utf8"}, function (er, input) {
  if (er) throw er
  var tree = parser.parse(input)
  // Do something with the tree
})

It creates a tree structure of nodes that have type, left and right properties. The left and right properties point to other nodes unless the node type is "leaf", in which case it's left property points to a token, which has a type and val property.

For the input file hello.cljs:

(ns hello.core)

; Hello World in clojurescript
(defn -main []
  (println "Hello World"))

(set! *main-cli-fn* -main)

output:

{
  "type": "list_list",
  "left": {
    "type": "list_list",
    "left": {
      "type": "list",
      "left": {
        "type": "s_exp_list",
        "left": {
          "type": "leaf",
          "left": {
            "type": "symbol",
            "val": "ns"
          },
          "right": null
        },
        "right": {
          "type": "s_exp_list",
          "left": {
            "type": "leaf",
            "left": {
              "type": "symbol",
              "val": "hello.core"
            },
            "right": null
          }
        }
      }
    },
    "right": {
      "type": "list",
      "left": {
        "type": "s_exp_list",
        "left": {
          "type": "leaf",
          "left": {
            "type": "symbol",
            "val": "defn"
          },
          "right": null
        },
        "right": {
          "type": "s_exp_list",
          "left": {
            "type": "leaf",
            "left": {
              "type": "symbol",
              "val": "-main"
            },
            "right": null
          },
          "right": {
            "type": "s_exp_list",
            "left": {
              "type": "vector"
            },
            "right": {
              "type": "s_exp_list",
              "left": {
                "type": "list",
                "left": {
                  "type": "s_exp_list",
                  "left": {
                    "type": "leaf",
                    "left": {
                      "type": "symbol",
                      "val": "println"
                    },
                    "right": null
                  },
                  "right": {
                    "type": "s_exp_list",
                    "left": {
                      "type": "leaf",
                      "left": {
                        "type": "string",
                        "val": "Hello World"
                      },
                      "right": null
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "right": {
    "type": "list",
    "left": {
      "type": "s_exp_list",
      "left": {
        "type": "leaf",
        "left": {
          "type": "symbol",
          "val": "set!"
        },
        "right": null
      },
      "right": {
        "type": "s_exp_list",
        "left": {
          "type": "leaf",
          "left": {
            "type": "symbol",
            "val": "*main-cli-fn*"
          },
          "right": null
        },
        "right": {
          "type": "s_exp_list",
          "left": {
            "type": "leaf",
            "left": {
              "type": "symbol",
              "val": "-main"
            },
            "right": null
          }
        }
      }
    }
  }
}
0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.3

10 years ago

0.0.2

10 years ago

0.0.1

10 years ago

0.0.0

10 years ago