js_parser

Package Version Hex Docs

A lexer and parser for JavaScript, written in Gleam.

This project is inspired by the js-tokens and glexer packages, and aims to provide a tool to build ECMA-262 syntax trees for gleam projects.

Installation

gleam add js_parser

Usage

someFile.js

export function someFunction() {
    let someVar;
}

The lexer can take any string and returns a list of tokens, including whitespace characters.

import js_parser

pub fn main() {
  let contents = js_parser.read_file("path/to/someFile.js")
  |> js_parser.parse
  |> io.debug
}

Should output:

[
  KeywordExport, CharWhitespace(" "), KeywordFunction, CharWhitespace(" "), IdentifierName("someFunction"),CharOpenParen, CharCloseParen, CharWhitespace(" "), CharOpenBrace,
  LineTerminatorSequence("\n"), CharWhitespace("    "),
  IdentifierName("let"), CharWhitespace(" "), IdentifierName("someVar"), CharSemicolon,LineTerminatorSequence("\n"),
  CharCloseBrace, LineTerminatorSequence("\n")
]
gleam run -- --f samples/js/index.js
gleam test
Search Document